UNPKG

@widergy/web-utils

Version:
77 lines (76 loc) 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.arrayBufferToJson = exports.intersect = exports.autocompleteWithNumbers = exports.isEmpty = exports.reducerAccumulator = exports.validateArray = exports.updateOrAddObjectInArray = exports.updateObjectInArray = exports.replaceObjectInArray = exports.flatten = exports.range = void 0; const range = (n) => [...Array(n).keys()]; exports.range = range; const flatten = (array) => [].concat(...array); exports.flatten = flatten; const replaceObjectInArray = (list, entity, index, valuesToReplace) => [...list.slice(0, index), entity.merge(valuesToReplace), ...list.slice(index + 1)]; exports.replaceObjectInArray = replaceObjectInArray; const updateObjectInArray = (array, object, isEqual = (item) => item.id === object.id) => { const index = array.findIndex(isEqual); return index >= 0 ? array .slice(0, index) .concat(object) .concat(array.slice(index + 1)) : array; }; exports.updateObjectInArray = updateObjectInArray; const updateOrAddObjectInArray = (array, object, isEqual = (item) => item.id === object.id) => { const index = array.findIndex(isEqual); return index >= 0 ? array .slice(0, index) .concat(object) .concat(array.slice(index + 1)) : array.concat(object); }; exports.updateOrAddObjectInArray = updateOrAddObjectInArray; const validateArray = (minValue) => (val) => val && val.length >= minValue && minValue > 0 ? undefined : 'ERROR'; exports.validateArray = validateArray; const reducerAccumulator = (accumulator, currentValue) => accumulator + currentValue; exports.reducerAccumulator = reducerAccumulator; const isEmpty = (array) => !array || array.length === 0; exports.isEmpty = isEmpty; const autocompleteWithNumbers = (x, y) => x > y ? [] : [x, ...(0, exports.autocompleteWithNumbers)(x + 1, y)]; exports.autocompleteWithNumbers = autocompleteWithNumbers; const intersect = (...args) => { const xArr = args[0]; const yArr = args[1]; const rest = args.slice(2); if (!yArr) return xArr; const zArr = xArr.filter((x) => yArr.includes(x)); return (0, exports.intersect)(zArr, ...rest); }; exports.intersect = intersect; const arrayBufferToJson = (arrayBuffer) => { if ('TextDecoder' in window) { const dataView = new DataView(arrayBuffer); const decoder = new TextDecoder('utf8'); const response = JSON.parse(decoder.decode(dataView)); return response; } let decodedString = ''; new Uint8Array(arrayBuffer).forEach((byte) => { decodedString += String.fromCharCode(byte); }); const response = JSON.parse(decodedString); return response; }; exports.arrayBufferToJson = arrayBufferToJson; const ARRAY_UTILS = { range: exports.range, flatten: exports.flatten, replaceObjectInArray: exports.replaceObjectInArray, updateObjectInArray: exports.updateObjectInArray, updateOrAddObjectInArray: exports.updateOrAddObjectInArray, validateArray: exports.validateArray, reducerAccumulator: exports.reducerAccumulator, isEmpty: exports.isEmpty, autocompleteWithNumbers: exports.autocompleteWithNumbers, intersect: exports.intersect, arrayBufferToJson: exports.arrayBufferToJson, }; exports.default = ARRAY_UTILS;