@ultraq/array-utils
Advanced tools
| /** | ||
| * Flattens an array of arrays of infinite depth into a single-dimension array. | ||
| * | ||
| * > This is now natively in JavaScript as the `flat` method on an Array | ||
| * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat). | ||
| * > If you can't use `flat`, then this method will do the job 🙂 | ||
| * | ||
| * @param {Array<any>} array | ||
| * @return {Array<any>} Flattened array. | ||
| */ | ||
| export function flatten(array: Array<any>): Array<any>; | ||
| /** | ||
| * Creates an array of numbers from the starting value (inclusive) to the end | ||
| * (exclusive), with an optional step (the gap between values). | ||
| * | ||
| * @param {Number} start | ||
| * The value to start at, the first item in the returned array. | ||
| * @param {Number} end | ||
| * The value to end with, the last item in the returned array. | ||
| * @param {Number} [step=1] | ||
| * The increment/gap between values, defaults to 1. | ||
| * @return {number[]} An array encompassing the given range. | ||
| */ | ||
| export function range(start: number, end: number, step?: number): number[]; | ||
| /** | ||
| * A function to execute on each item in an array, returning truthy | ||
| * if the item passes whatever test is required for the use of this | ||
| * predicate. | ||
| * | ||
| * @template T | ||
| * @callback Predicate<T> | ||
| * @param {T} item | ||
| * @return {boolean} | ||
| */ | ||
| /** | ||
| * Remove and return the first item from `array` that matches the predicate | ||
| * function. | ||
| * | ||
| * @template T | ||
| * @param {T[]} array | ||
| * @param {Predicate<T>} predicate | ||
| * Function to test each item of the array with. If it returns a truthy value | ||
| * for the item, then that item is removed and returned. | ||
| * @return {T | undefined} The matching item, or `undefined` if no match was found. | ||
| */ | ||
| export function remove<T>(array: T[], predicate: Predicate<T>): T; | ||
| /** | ||
| * <T> | ||
| */ | ||
| export type Predicate<T> = (item: T) => boolean; |
+29
-18
@@ -9,3 +9,2 @@ "use strict"; | ||
| exports.remove = remove; | ||
| /* | ||
@@ -29,19 +28,20 @@ * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
| * Flattens an array of arrays of infinite depth into a single-dimension array. | ||
| * | ||
| * | ||
| * > This is now natively in JavaScript as the `flat` method on an Array | ||
| * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat). | ||
| * > If you can't use `flat`, then this method will do the job 🙂 | ||
| * | ||
| * @param {Array} array | ||
| * @return {Array} Flattened array. | ||
| * | ||
| * @param {Array<any>} array | ||
| * @return {Array<any>} Flattened array. | ||
| */ | ||
| function flatten(array) { | ||
| return array.reduce(function (accumulator, value) { | ||
| return accumulator.concat(Array.isArray(value) ? flatten(value) : value); | ||
| return array.reduce(function (acc, value) { | ||
| return acc.concat(Array.isArray(value) ? flatten(value) : value); | ||
| }, []); | ||
| } | ||
| /** | ||
| * Creates an array of numbers from the starting value (inclusive) to the end | ||
| * (exclusive), with an optional step (the gap between values). | ||
| * | ||
| * | ||
| * @param {Number} start | ||
@@ -53,6 +53,4 @@ * The value to start at, the first item in the returned array. | ||
| * The increment/gap between values, defaults to 1. | ||
| * @return {Array} An array encompassing the given range. | ||
| * @return {number[]} An array encompassing the given range. | ||
| */ | ||
| function range(start, end) { | ||
@@ -64,13 +62,25 @@ var step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; | ||
| } | ||
| /** | ||
| * A function to execute on each item in an array, returning truthy | ||
| * if the item passes whatever test is required for the use of this | ||
| * predicate. | ||
| * | ||
| * @template T | ||
| * @callback Predicate<T> | ||
| * @param {T} item | ||
| * @return {boolean} | ||
| */ | ||
| /** | ||
| * Remove and return the first item from `array` that matches the predicate | ||
| * function. | ||
| * | ||
| * @param {Array} array | ||
| * @param {Function} predicate | ||
| * Invoked with the array item. | ||
| * @return {Object} The matching item, or `null` if no match was found. | ||
| * | ||
| * @template T | ||
| * @param {T[]} array | ||
| * @param {Predicate<T>} predicate | ||
| * Function to test each item of the array with. If it returns a truthy value | ||
| * for the item, then that item is removed and returned. | ||
| * @return {T | undefined} The matching item, or `undefined` if no match was found. | ||
| */ | ||
| function remove(array, predicate) { | ||
@@ -82,2 +92,3 @@ return array.find(function (item, index) { | ||
| } | ||
| return false; | ||
| }); | ||
@@ -84,0 +95,0 @@ } |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["array-utils.js"],"names":[],"mappings":";;;;;;;;;AAAA;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;;AAUO,SAAS,OAAT,CAAiB,KAAjB,EAAwB;AAE9B,SAAO,KAAK,CAAC,MAAN,CAAa,UAAC,WAAD,EAAc,KAAd,EAAwB;AAC3C,WAAO,WAAW,CAAC,MAAZ,CAAmB,KAAK,CAAC,OAAN,CAAc,KAAd,IAAuB,OAAO,CAAC,KAAD,CAA9B,GAAwC,KAA3D,CAAP;AACA,GAFM,EAEJ,EAFI,CAAP;AAGA;AAED;;;;;;;;;;;;;;AAYO,SAAS,KAAT,CAAe,KAAf,EAAsB,GAAtB,EAAqC;AAAA,MAAV,IAAU,uEAAH,CAAG;AAE3C,SAAO,KAAK,CAAC,KAAN,CAAY,CAAZ,EAAe,KAAK,CAAC,IAAI,CAAC,IAAL,CAAU,CAAC,GAAG,GAAG,KAAP,IAAgB,IAA1B,CAAD,CAApB,EAAuD,GAAvD,CAA2D,UAAC,KAAD,EAAQ,KAAR;AAAA,WAAkB,KAAK,GAAG,IAAR,GAAe,KAAjC;AAAA,GAA3D,CAAP;AACA;AAED;;;;;;;;;;;AASO,SAAS,MAAT,CAAgB,KAAhB,EAAuB,SAAvB,EAAkC;AAExC,SAAO,KAAK,CAAC,IAAN,CAAW,UAAC,IAAD,EAAO,KAAP,EAAiB;AAClC,QAAI,SAAS,CAAC,IAAD,CAAb,EAAqB;AACpB,MAAA,KAAK,CAAC,MAAN,CAAa,KAAb,EAAoB,CAApB;AACA,aAAO,IAAP;AACA;AACD,GALM,CAAP;AAMA","file":"array-utils.cjs.js","sourcesContent":["/* \n * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Flattens an array of arrays of infinite depth into a single-dimension array.\n * \n * > This is now natively in JavaScript as the `flat` method on an Array\n * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat).\n * > If you can't use `flat`, then this method will do the job 🙂\n * \n * @param {Array} array\n * @return {Array} Flattened array.\n */\nexport function flatten(array) {\n\n\treturn array.reduce((accumulator, value) => {\n\t\treturn accumulator.concat(Array.isArray(value) ? flatten(value) : value);\n\t}, []);\n}\n\n/**\n * Creates an array of numbers from the starting value (inclusive) to the end\n * (exclusive), with an optional step (the gap between values).\n * \n * @param {Number} start\n * The value to start at, the first item in the returned array.\n * @param {Number} end\n * The value to end with, the last item in the returned array.\n * @param {Number} [step=1]\n * The increment/gap between values, defaults to 1.\n * @return {Array} An array encompassing the given range.\n */\nexport function range(start, end, step = 1) {\n\n\treturn Array.apply(0, Array(Math.ceil((end - start) / step))).map((empty, index) => index * step + start);\n}\n\n/**\n * Remove and return the first item from `array` that matches the predicate\n * function.\n * \n * @param {Array} array\n * @param {Function} predicate\n * Invoked with the array item.\n * @return {Object} The matching item, or `null` if no match was found.\n */\nexport function remove(array, predicate) {\n\n\treturn array.find((item, index) => {\n\t\tif (predicate(item)) {\n\t\t\tarray.splice(index, 1);\n\t\t\treturn item;\n\t\t}\n\t});\n}\n"]} | ||
| {"version":3,"file":"array-utils.cjs.js","names":["flatten","array","reduce","acc","value","concat","Array","isArray","range","start","end","step","arguments","length","undefined","apply","Math","ceil","map","empty","index","remove","predicate","find","item","splice"],"sources":["array-utils.js"],"sourcesContent":["/* \n * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Flattens an array of arrays of infinite depth into a single-dimension array.\n *\n * > This is now natively in JavaScript as the `flat` method on an Array\n * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat).\n * > If you can't use `flat`, then this method will do the job 🙂\n *\n * @param {Array<any>} array\n * @return {Array<any>} Flattened array.\n */\nexport function flatten(array) {\n\treturn array.reduce((acc, value) => {\n\t\treturn acc.concat(Array.isArray(value) ? flatten(value) : value);\n\t}, []);\n}\n\n/**\n * Creates an array of numbers from the starting value (inclusive) to the end\n * (exclusive), with an optional step (the gap between values).\n *\n * @param {Number} start\n * The value to start at, the first item in the returned array.\n * @param {Number} end\n * The value to end with, the last item in the returned array.\n * @param {Number} [step=1]\n * The increment/gap between values, defaults to 1.\n * @return {number[]} An array encompassing the given range.\n */\nexport function range(start, end, step = 1) {\n\treturn Array.apply(0, Array(Math.ceil((end - start) / step))).map((empty, index) => index * step + start);\n}\n\n/**\n * A function to execute on each item in an array, returning truthy\n * if the item passes whatever test is required for the use of this\n * predicate.\n *\n * @template T\n * @callback Predicate<T>\n * @param {T} item\n * @return {boolean}\n */\n\n/**\n * Remove and return the first item from `array` that matches the predicate\n * function.\n *\n * @template T\n * @param {T[]} array\n * @param {Predicate<T>} predicate\n * Function to test each item of the array with. If it returns a truthy value\n * for the item, then that item is removed and returned.\n * @return {T | undefined} The matching item, or `undefined` if no match was found.\n */\nexport function remove(array, predicate) {\n\treturn array.find((item, index) => {\n\t\tif (predicate(item)) {\n\t\t\tarray.splice(index, 1);\n\t\t\treturn item;\n\t\t}\n\t\treturn false;\n\t});\n}\n"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,OAAOA,CAACC,KAAK,EAAE;EAC9B,OAAOA,KAAK,CAACC,MAAM,CAAC,UAACC,GAAG,EAAEC,KAAK,EAAK;IACnC,OAAOD,GAAG,CAACE,MAAM,CAACC,KAAK,CAACC,OAAO,CAACH,KAAK,CAAC,GAAGJ,OAAO,CAACI,KAAK,CAAC,GAAGA,KAAK,CAAC;EACjE,CAAC,EAAE,EAAE,CAAC;AACP;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASI,KAAKA,CAACC,KAAK,EAAEC,GAAG,EAAY;EAAA,IAAVC,IAAI,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC;EACzC,OAAON,KAAK,CAACS,KAAK,CAAC,CAAC,EAAET,KAAK,CAACU,IAAI,CAACC,IAAI,CAAC,CAACP,GAAG,GAAGD,KAAK,IAAIE,IAAI,CAAC,CAAC,CAAC,CAACO,GAAG,CAAC,UAACC,KAAK,EAAEC,KAAK;IAAA,OAAKA,KAAK,GAAGT,IAAI,GAAGF,KAAK;EAAA,EAAC;AAC1G;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASY,MAAMA,CAACpB,KAAK,EAAEqB,SAAS,EAAE;EACxC,OAAOrB,KAAK,CAACsB,IAAI,CAAC,UAACC,IAAI,EAAEJ,KAAK,EAAK;IAClC,IAAIE,SAAS,CAACE,IAAI,CAAC,EAAE;MACpBvB,KAAK,CAACwB,MAAM,CAACL,KAAK,EAAE,CAAC,CAAC;MACtB,OAAOI,IAAI;IACZ;IACA,OAAO,KAAK;EACb,CAAC,CAAC;AACH"} |
+29
-15
@@ -19,19 +19,20 @@ /* | ||
| * Flattens an array of arrays of infinite depth into a single-dimension array. | ||
| * | ||
| * | ||
| * > This is now natively in JavaScript as the `flat` method on an Array | ||
| * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat). | ||
| * > If you can't use `flat`, then this method will do the job 🙂 | ||
| * | ||
| * @param {Array} array | ||
| * @return {Array} Flattened array. | ||
| * | ||
| * @param {Array<any>} array | ||
| * @return {Array<any>} Flattened array. | ||
| */ | ||
| export function flatten(array) { | ||
| return array.reduce(function (accumulator, value) { | ||
| return accumulator.concat(Array.isArray(value) ? flatten(value) : value); | ||
| return array.reduce(function (acc, value) { | ||
| return acc.concat(Array.isArray(value) ? flatten(value) : value); | ||
| }, []); | ||
| } | ||
| /** | ||
| * Creates an array of numbers from the starting value (inclusive) to the end | ||
| * (exclusive), with an optional step (the gap between values). | ||
| * | ||
| * | ||
| * @param {Number} start | ||
@@ -43,5 +44,4 @@ * The value to start at, the first item in the returned array. | ||
| * The increment/gap between values, defaults to 1. | ||
| * @return {Array} An array encompassing the given range. | ||
| * @return {number[]} An array encompassing the given range. | ||
| */ | ||
| export function range(start, end) { | ||
@@ -53,12 +53,25 @@ var step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; | ||
| } | ||
| /** | ||
| * A function to execute on each item in an array, returning truthy | ||
| * if the item passes whatever test is required for the use of this | ||
| * predicate. | ||
| * | ||
| * @template T | ||
| * @callback Predicate<T> | ||
| * @param {T} item | ||
| * @return {boolean} | ||
| */ | ||
| /** | ||
| * Remove and return the first item from `array` that matches the predicate | ||
| * function. | ||
| * | ||
| * @param {Array} array | ||
| * @param {Function} predicate | ||
| * Invoked with the array item. | ||
| * @return {Object} The matching item, or `null` if no match was found. | ||
| * | ||
| * @template T | ||
| * @param {T[]} array | ||
| * @param {Predicate<T>} predicate | ||
| * Function to test each item of the array with. If it returns a truthy value | ||
| * for the item, then that item is removed and returned. | ||
| * @return {T | undefined} The matching item, or `undefined` if no match was found. | ||
| */ | ||
| export function remove(array, predicate) { | ||
@@ -70,2 +83,3 @@ return array.find(function (item, index) { | ||
| } | ||
| return false; | ||
| }); | ||
@@ -72,0 +86,0 @@ } |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["array-utils.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;;AAUA,OAAO,SAAS,OAAT,CAAiB,KAAjB,EAAwB;AAE9B,SAAO,KAAK,CAAC,MAAN,CAAa,UAAC,WAAD,EAAc,KAAd,EAAwB;AAC3C,WAAO,WAAW,CAAC,MAAZ,CAAmB,KAAK,CAAC,OAAN,CAAc,KAAd,IAAuB,OAAO,CAAC,KAAD,CAA9B,GAAwC,KAA3D,CAAP;AACA,GAFM,EAEJ,EAFI,CAAP;AAGA;AAED;;;;;;;;;;;;;AAYA,OAAO,SAAS,KAAT,CAAe,KAAf,EAAsB,GAAtB,EAAqC;AAAA,MAAV,IAAU,uEAAH,CAAG;AAE3C,SAAO,KAAK,CAAC,KAAN,CAAY,CAAZ,EAAe,KAAK,CAAC,IAAI,CAAC,IAAL,CAAU,CAAC,GAAG,GAAG,KAAP,IAAgB,IAA1B,CAAD,CAApB,EAAuD,GAAvD,CAA2D,UAAC,KAAD,EAAQ,KAAR;AAAA,WAAkB,KAAK,GAAG,IAAR,GAAe,KAAjC;AAAA,GAA3D,CAAP;AACA;AAED;;;;;;;;;;AASA,OAAO,SAAS,MAAT,CAAgB,KAAhB,EAAuB,SAAvB,EAAkC;AAExC,SAAO,KAAK,CAAC,IAAN,CAAW,UAAC,IAAD,EAAO,KAAP,EAAiB;AAClC,QAAI,SAAS,CAAC,IAAD,CAAb,EAAqB;AACpB,MAAA,KAAK,CAAC,MAAN,CAAa,KAAb,EAAoB,CAApB;AACA,aAAO,IAAP;AACA;AACD,GALM,CAAP;AAMA","file":"array-utils.es.js","sourcesContent":["/* \n * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Flattens an array of arrays of infinite depth into a single-dimension array.\n * \n * > This is now natively in JavaScript as the `flat` method on an Array\n * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat).\n * > If you can't use `flat`, then this method will do the job 🙂\n * \n * @param {Array} array\n * @return {Array} Flattened array.\n */\nexport function flatten(array) {\n\n\treturn array.reduce((accumulator, value) => {\n\t\treturn accumulator.concat(Array.isArray(value) ? flatten(value) : value);\n\t}, []);\n}\n\n/**\n * Creates an array of numbers from the starting value (inclusive) to the end\n * (exclusive), with an optional step (the gap between values).\n * \n * @param {Number} start\n * The value to start at, the first item in the returned array.\n * @param {Number} end\n * The value to end with, the last item in the returned array.\n * @param {Number} [step=1]\n * The increment/gap between values, defaults to 1.\n * @return {Array} An array encompassing the given range.\n */\nexport function range(start, end, step = 1) {\n\n\treturn Array.apply(0, Array(Math.ceil((end - start) / step))).map((empty, index) => index * step + start);\n}\n\n/**\n * Remove and return the first item from `array` that matches the predicate\n * function.\n * \n * @param {Array} array\n * @param {Function} predicate\n * Invoked with the array item.\n * @return {Object} The matching item, or `null` if no match was found.\n */\nexport function remove(array, predicate) {\n\n\treturn array.find((item, index) => {\n\t\tif (predicate(item)) {\n\t\t\tarray.splice(index, 1);\n\t\t\treturn item;\n\t\t}\n\t});\n}\n"]} | ||
| {"version":3,"file":"array-utils.es.js","names":["flatten","array","reduce","acc","value","concat","Array","isArray","range","start","end","step","arguments","length","undefined","apply","Math","ceil","map","empty","index","remove","predicate","find","item","splice"],"sources":["array-utils.js"],"sourcesContent":["/* \n * Copyright 2017, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Flattens an array of arrays of infinite depth into a single-dimension array.\n *\n * > This is now natively in JavaScript as the `flat` method on an Array\n * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat).\n * > If you can't use `flat`, then this method will do the job 🙂\n *\n * @param {Array<any>} array\n * @return {Array<any>} Flattened array.\n */\nexport function flatten(array) {\n\treturn array.reduce((acc, value) => {\n\t\treturn acc.concat(Array.isArray(value) ? flatten(value) : value);\n\t}, []);\n}\n\n/**\n * Creates an array of numbers from the starting value (inclusive) to the end\n * (exclusive), with an optional step (the gap between values).\n *\n * @param {Number} start\n * The value to start at, the first item in the returned array.\n * @param {Number} end\n * The value to end with, the last item in the returned array.\n * @param {Number} [step=1]\n * The increment/gap between values, defaults to 1.\n * @return {number[]} An array encompassing the given range.\n */\nexport function range(start, end, step = 1) {\n\treturn Array.apply(0, Array(Math.ceil((end - start) / step))).map((empty, index) => index * step + start);\n}\n\n/**\n * A function to execute on each item in an array, returning truthy\n * if the item passes whatever test is required for the use of this\n * predicate.\n *\n * @template T\n * @callback Predicate<T>\n * @param {T} item\n * @return {boolean}\n */\n\n/**\n * Remove and return the first item from `array` that matches the predicate\n * function.\n *\n * @template T\n * @param {T[]} array\n * @param {Predicate<T>} predicate\n * Function to test each item of the array with. If it returns a truthy value\n * for the item, then that item is removed and returned.\n * @return {T | undefined} The matching item, or `undefined` if no match was found.\n */\nexport function remove(array, predicate) {\n\treturn array.find((item, index) => {\n\t\tif (predicate(item)) {\n\t\t\tarray.splice(index, 1);\n\t\t\treturn item;\n\t\t}\n\t\treturn false;\n\t});\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,OAAOA,CAACC,KAAK,EAAE;EAC9B,OAAOA,KAAK,CAACC,MAAM,CAAC,UAACC,GAAG,EAAEC,KAAK,EAAK;IACnC,OAAOD,GAAG,CAACE,MAAM,CAACC,KAAK,CAACC,OAAO,CAACH,KAAK,CAAC,GAAGJ,OAAO,CAACI,KAAK,CAAC,GAAGA,KAAK,CAAC;EACjE,CAAC,EAAE,EAAE,CAAC;AACP;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASI,KAAKA,CAACC,KAAK,EAAEC,GAAG,EAAY;EAAA,IAAVC,IAAI,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC;EACzC,OAAON,KAAK,CAACS,KAAK,CAAC,CAAC,EAAET,KAAK,CAACU,IAAI,CAACC,IAAI,CAAC,CAACP,GAAG,GAAGD,KAAK,IAAIE,IAAI,CAAC,CAAC,CAAC,CAACO,GAAG,CAAC,UAACC,KAAK,EAAEC,KAAK;IAAA,OAAKA,KAAK,GAAGT,IAAI,GAAGF,KAAK;EAAA,EAAC;AAC1G;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASY,MAAMA,CAACpB,KAAK,EAAEqB,SAAS,EAAE;EACxC,OAAOrB,KAAK,CAACsB,IAAI,CAAC,UAACC,IAAI,EAAEJ,KAAK,EAAK;IAClC,IAAIE,SAAS,CAACE,IAAI,CAAC,EAAE;MACpBvB,KAAK,CAACwB,MAAM,CAACL,KAAK,EAAE,CAAC,CAAC;MACtB,OAAOI,IAAI;IACZ;IACA,OAAO,KAAK;EACb,CAAC,CAAC;AACH"} |
+27
-16
@@ -19,14 +19,13 @@ /* | ||
| * Flattens an array of arrays of infinite depth into a single-dimension array. | ||
| * | ||
| * | ||
| * > This is now natively in JavaScript as the `flat` method on an Array | ||
| * > instance. [Check MDN for which browsers have access to this feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat). | ||
| * > If you can't use `flat`, then this method will do the job 🙂 | ||
| * | ||
| * @param {Array} array | ||
| * @return {Array} Flattened array. | ||
| * | ||
| * @param {Array<any>} array | ||
| * @return {Array<any>} Flattened array. | ||
| */ | ||
| export function flatten(array) { | ||
| return array.reduce((accumulator, value) => { | ||
| return accumulator.concat(Array.isArray(value) ? flatten(value) : value); | ||
| return array.reduce((acc, value) => { | ||
| return acc.concat(Array.isArray(value) ? flatten(value) : value); | ||
| }, []); | ||
@@ -38,3 +37,3 @@ } | ||
| * (exclusive), with an optional step (the gap between values). | ||
| * | ||
| * | ||
| * @param {Number} start | ||
@@ -46,6 +45,5 @@ * The value to start at, the first item in the returned array. | ||
| * The increment/gap between values, defaults to 1. | ||
| * @return {Array} An array encompassing the given range. | ||
| * @return {number[]} An array encompassing the given range. | ||
| */ | ||
| export function range(start, end, step = 1) { | ||
| return Array.apply(0, Array(Math.ceil((end - start) / step))).map((empty, index) => index * step + start); | ||
@@ -55,12 +53,24 @@ } | ||
| /** | ||
| * A function to execute on each item in an array, returning truthy | ||
| * if the item passes whatever test is required for the use of this | ||
| * predicate. | ||
| * | ||
| * @template T | ||
| * @callback Predicate<T> | ||
| * @param {T} item | ||
| * @return {boolean} | ||
| */ | ||
| /** | ||
| * Remove and return the first item from `array` that matches the predicate | ||
| * function. | ||
| * | ||
| * @param {Array} array | ||
| * @param {Function} predicate | ||
| * Invoked with the array item. | ||
| * @return {Object} The matching item, or `null` if no match was found. | ||
| * | ||
| * @template T | ||
| * @param {T[]} array | ||
| * @param {Predicate<T>} predicate | ||
| * Function to test each item of the array with. If it returns a truthy value | ||
| * for the item, then that item is removed and returned. | ||
| * @return {T | undefined} The matching item, or `undefined` if no match was found. | ||
| */ | ||
| export function remove(array, predicate) { | ||
| return array.find((item, index) => { | ||
@@ -71,3 +81,4 @@ if (predicate(item)) { | ||
| } | ||
| return false; | ||
| }); | ||
| } |
@@ -23,3 +23,3 @@ /* | ||
| */ | ||
| describe('ArrayUtils', function() { | ||
| describe('array-utils', function() { | ||
@@ -26,0 +26,0 @@ describe('#flatten', function() { |
+13
-11
| { | ||
| "name": "@ultraq/array-utils", | ||
| "version": "2.1.0", | ||
| "version": "3.0.0", | ||
| "description": "A collection of utilities for JavaScript arrays", | ||
@@ -20,2 +20,3 @@ "author": "Emanuel Rabina <emanuelrabina@gmail.com> (http://www.ultraq.net.nz/)", | ||
| "main": "array-utils.cjs.js", | ||
| "types": "array-utils.d.ts", | ||
| "sideEffects": false, | ||
@@ -25,19 +26,20 @@ "scripts": { | ||
| "test": "jest", | ||
| "coverage": "cat ./coverage/lcov.info | coveralls", | ||
| "build": "npm run build:cjs && npm run build:es", | ||
| "build": "npm run build:cjs && npm run build:es && npm run build:dts", | ||
| "build:cjs": "BABEL_ENV=cjs babel array-utils.js --out-file array-utils.cjs.js --source-maps", | ||
| "build:es": "BABEL_ENV=es babel array-utils.js --out-file array-utils.es.js --source-maps", | ||
| "prepublish": "npm run build" | ||
| "build:dts": "tsc --allowJs --declaration --emitDeclarationOnly array-utils.js", | ||
| "prepublishOnly": "npm run build" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/cli": "^7.10.5", | ||
| "@babel/preset-env": "^7.11.0", | ||
| "coveralls": "^3.1.0", | ||
| "eslint": "^7.6.0", | ||
| "eslint-config-ultraq": "^2.3.3", | ||
| "jest": "^26.2.2" | ||
| "@babel/cli": "^7.22.15", | ||
| "@babel/preset-env": "^7.22.15", | ||
| "@types/jest": "^29.5.4", | ||
| "eslint": "^8.48.0", | ||
| "eslint-config-ultraq": "^2.4.0", | ||
| "jest": "^29.6.4", | ||
| "typescript": "^5.2.2" | ||
| }, | ||
| "engines": { | ||
| "node": ">=10" | ||
| "node": ">=18" | ||
| } | ||
| } |
+2
-2
@@ -5,4 +5,4 @@ | ||
| [](https://travis-ci.com/ultraq/array-utils) | ||
| [](https://coveralls.io/github/ultraq/array-utils?branch=master) | ||
| [](https://github.com/ultraq/array-utils/actions) | ||
| [](https://codecov.io/gh/ultraq/array-utils) | ||
| [](https://www.npmjs.com/package/@ultraq/array-utils) | ||
@@ -9,0 +9,0 @@ [](https://bundlephobia.com/result?p=@ultraq/array-utils) |
35202
16.6%11
10%344
34.9%7
16.67%