@bemoje/array 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @bemoje/array v0.0.
|
|
2
|
+
* @bemoje/array v0.0.8
|
|
3
3
|
* (c) Benjamin Møller Jensen
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/"use strict";var number=require("@bemoje/number"),sort=require("@bemoje/sort"),validation=require("@bemoje/validation"),string=require("@bemoje/string");function arrSum(array){return array.reduce((acc,cur)=>acc+cur,0)}function arrAverage(array){if(!array.length)throw new Error("Cannot take an average of zero values.");return arrSum(array)/array.length}function arrEachToString(array){return array.map(element=>""+element)}function arrEvery(input,predicate){for(let i=0,len=input.length;i<len;i++)if(!1===predicate(input[i],i,input))return!1;return!0}function arrFindIndicesOf(input,predicate){const result=[];for(let i=0;i<input.length;i++)predicate(input[i])&&result.push(i);return result}function arrFindLast(input,predicate){for(let i=input.length-1;0<=i;i--)if(!0===predicate(input[i]))return input[i]}function arrFindLastIndexOf(input,predicate){for(let i=input.length-1;0<=i;i--)if(predicate(input[i]))return i;return-1}function arrFlatten(input,maxDepth=Number.MAX_SAFE_INTEGER){function recurse(arr,depth){for(let i=0;i<arr.length;i++)Array.isArray(arr[i])&&depth<maxDepth?recurse(arr[i],depth+1):accum.push(arr[i])}const accum=[];return recurse(input,0),accum}function arrIndicesOf(input,element){const result=[];for(let i=0;i<input.length;i++)element===input[i]&&result.push(i);return result}function arrLast(array){if(!array.length)throw new Error("Cannot get last element of empty array.");return array[array.length-1]}function arrMapMutable(input,f){for(let i=0;i<input.length;i++)input[i]=f(input[i],i,input);return input}function arrObjectsUniqueKeys(objects){const keys=new Set;for(const o of objects)for(const key of Object.keys(o))keys.add(key);return Array.from(keys)}function arrObjectsToTable(objects,options={}){var _a;const headers=(null===(_a=null===options||void 0===options?void 0:options.headers)||void 0===_a?void 0:_a.slice())||arrObjectsUniqueKeys(objects),table=[headers];for(const o of objects){const row=headers.map(header=>{const value=o[header];return value===void 0?options.emptyCell:value});table.push(row)}return table}function arrRemoveDuplicates(array){return Array.from(new Set(array))}function arrShallowEquals(input1,input2){return!(input1.length!==input2.length)&&arrEvery(input1,(value,i)=>value===input2[i])}function arrSwap(input,from,to){return from===to?input:([input[from],input[to]]=[input[to],input[from]],input)}function arrShuffle(input){if(1>=input.length)return input;const original=input.slice();for(let equal=!0;equal;){for(let i=0;i<input.length;i++){const newIndex=number.randomIntBetween(0,input.length-1);arrSwap(input,i,newIndex)}equal=arrShallowEquals(input,original)}return input}function arrSome(input,predicate){for(let i=0,len=input.length;i<len;i++)if(!0===predicate(input[i],i,input))return!0;return!1}function arrSortedInsertionIndex(array,value,comparator){let first=0,count=array.length;for(;0<count;){const step=Math.trunc(count/2);let it=first+step;0>=comparator(array[it],value)?(first=++it,count-=step+1):count=step}return first}function arrSortNumeric(input){return input.sort(sort.compareNumeric)}function arrSplit(array,n){validation.assertion(n,validation.isPositiveInteger);const result=[];if(0===array.length)return result;if(1===n)return[array];const chunkSize=Math.ceil(array.length/n);for(let i=0;i<array.length;i+=chunkSize)result.push(array.slice(i,i+chunkSize));return result}function arrTableAssertRowsSameLength(rows,headers){const numHeaders=(headers||rows[0]).length;for(const row of rows)if(row.length!==numHeaders)throw new Error(`Expected ${numHeaders} columns, got ${row.length}`)}function arrTableEachToString(table){return table.map(arrEachToString)}function arrTableToCsv(input,delimiter=";",replaceLinebreakWith="|"){return input.map(row=>row.map(item=>string.strReplaceAll(item+"",delimiter,"").replace(/\r*\n/g,replaceLinebreakWith)).join(delimiter)).join("\n")}function arrTableToObjects(rows,headers,ignoreKeys){if(!headers){if(1>=rows.length)return[];headers=rows[0].map(header=>null===header||void 0===header?"":header+""),rows=rows.slice(1)}else if(!rows.length)return[];const _headers=headers;return rows.map(row=>{const o={};for(let i=0;i<_headers.length;i++){const header=_headers[i];ignoreKeys&&ignoreKeys.has(header)||(o[header]=row[i])}return o})}exports.arrAverage=arrAverage,exports.arrEachToString=arrEachToString,exports.arrEvery=arrEvery,exports.arrFindIndicesOf=arrFindIndicesOf,exports.arrFindLast=arrFindLast,exports.arrFindLastIndexOf=arrFindLastIndexOf,exports.arrFlatten=arrFlatten,exports.arrIndicesOf=arrIndicesOf,exports.arrLast=arrLast,exports.arrMapMutable=arrMapMutable,exports.arrObjectsToTable=arrObjectsToTable,exports.arrObjectsUniqueKeys=arrObjectsUniqueKeys,exports.arrRemoveDuplicates=arrRemoveDuplicates,exports.arrShallowEquals=arrShallowEquals,exports.arrShuffle=arrShuffle,exports.arrSome=arrSome,exports.arrSortNumeric=arrSortNumeric,exports.arrSortedInsertionIndex=arrSortedInsertionIndex,exports.arrSplit=arrSplit,exports.arrSum=arrSum,exports.arrSwap=arrSwap,exports.arrTableAssertRowsSameLength=arrTableAssertRowsSameLength,exports.arrTableEachToString=arrTableEachToString,exports.arrTableToCsv=arrTableToCsv,exports.arrTableToObjects=arrTableToObjects;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @bemoje/array v0.0.
|
|
2
|
+
* @bemoje/array v0.0.8
|
|
3
3
|
* (c) Benjamin Møller Jensen
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/import{randomIntBetween}from"@bemoje/number";import{compareNumeric}from"@bemoje/sort";import{assertion,isPositiveInteger}from"@bemoje/validation";import{strReplaceAll}from"@bemoje/string";function arrSum(array){return array.reduce((acc,cur)=>acc+cur,0)}function arrAverage(array){if(!array.length)throw new Error("Cannot take an average of zero values.");return arrSum(array)/array.length}function arrEachToString(array){return array.map(element=>""+element)}function arrEvery(input,predicate){for(let i=0,len=input.length;i<len;i++)if(!1===predicate(input[i],i,input))return!1;return!0}function arrFindIndicesOf(input,predicate){const result=[];for(let i=0;i<input.length;i++)predicate(input[i])&&result.push(i);return result}function arrFindLast(input,predicate){for(let i=input.length-1;0<=i;i--)if(!0===predicate(input[i]))return input[i]}function arrFindLastIndexOf(input,predicate){for(let i=input.length-1;0<=i;i--)if(predicate(input[i]))return i;return-1}function arrFlatten(input,maxDepth=Number.MAX_SAFE_INTEGER){function recurse(arr,depth){for(let i=0;i<arr.length;i++)Array.isArray(arr[i])&&depth<maxDepth?recurse(arr[i],depth+1):accum.push(arr[i])}const accum=[];return recurse(input,0),accum}function arrIndicesOf(input,element){const result=[];for(let i=0;i<input.length;i++)element===input[i]&&result.push(i);return result}function arrLast(array){if(!array.length)throw new Error("Cannot get last element of empty array.");return array[array.length-1]}function arrMapMutable(input,f){for(let i=0;i<input.length;i++)input[i]=f(input[i],i,input);return input}function arrObjectsUniqueKeys(objects){const keys=new Set;for(const o of objects)for(const key of Object.keys(o))keys.add(key);return Array.from(keys)}function arrObjectsToTable(objects,options={}){var _a;const headers=(null===(_a=null===options||void 0===options?void 0:options.headers)||void 0===_a?void 0:_a.slice())||arrObjectsUniqueKeys(objects),table=[headers];for(const o of objects){const row=headers.map(header=>{const value=o[header];return value===void 0?options.emptyCell:value});table.push(row)}return table}function arrRemoveDuplicates(array){return Array.from(new Set(array))}function arrShallowEquals(input1,input2){return!(input1.length!==input2.length)&&arrEvery(input1,(value,i)=>value===input2[i])}function arrSwap(input,from,to){return from===to?input:([input[from],input[to]]=[input[to],input[from]],input)}function arrShuffle(input){if(1>=input.length)return input;const original=input.slice();for(let equal=!0;equal;){for(let i=0;i<input.length;i++){const newIndex=randomIntBetween(0,input.length-1);arrSwap(input,i,newIndex)}equal=arrShallowEquals(input,original)}return input}function arrSome(input,predicate){for(let i=0,len=input.length;i<len;i++)if(!0===predicate(input[i],i,input))return!0;return!1}function arrSortedInsertionIndex(array,value,comparator){let first=0,count=array.length;for(;0<count;){const step=Math.trunc(count/2);let it=first+step;0>=comparator(array[it],value)?(first=++it,count-=step+1):count=step}return first}function arrSortNumeric(input){return input.sort(compareNumeric)}function arrSplit(array,n){assertion(n,isPositiveInteger);const result=[];if(0===array.length)return result;if(1===n)return[array];const chunkSize=Math.ceil(array.length/n);for(let i=0;i<array.length;i+=chunkSize)result.push(array.slice(i,i+chunkSize));return result}function arrTableAssertRowsSameLength(rows,headers){const numHeaders=(headers||rows[0]).length;for(const row of rows)if(row.length!==numHeaders)throw new Error(`Expected ${numHeaders} columns, got ${row.length}`)}function arrTableEachToString(table){return table.map(arrEachToString)}function arrTableToCsv(input,delimiter=";",replaceLinebreakWith="|"){return input.map(row=>row.map(item=>strReplaceAll(item+"",delimiter,"").replace(/\r*\n/g,replaceLinebreakWith)).join(delimiter)).join("\n")}function arrTableToObjects(rows,headers,ignoreKeys){if(!headers){if(1>=rows.length)return[];headers=rows[0].map(header=>null===header||void 0===header?"":header+""),rows=rows.slice(1)}else if(!rows.length)return[];const _headers=headers;return rows.map(row=>{const o={};for(let i=0;i<_headers.length;i++){const header=_headers[i];ignoreKeys&&ignoreKeys.has(header)||(o[header]=row[i])}return o})}export{arrAverage,arrEachToString,arrEvery,arrFindIndicesOf,arrFindLast,arrFindLastIndexOf,arrFlatten,arrIndicesOf,arrLast,arrMapMutable,arrObjectsToTable,arrObjectsUniqueKeys,arrRemoveDuplicates,arrShallowEquals,arrShuffle,arrSome,arrSortNumeric,arrSortedInsertionIndex,arrSplit,arrSum,arrSwap,arrTableAssertRowsSameLength,arrTableEachToString,arrTableToCsv,arrTableToObjects};
|