UNPKG

@empathyco/x-components

Version:
188 lines (186 loc) 5.86 kB
/** * Returns if the given array is `null`, `undefined`, or has no elements. * * @param array - The array to check if it is empty. * @returns `true` if the array is `null`, `undefined`, or has no elements. `false` otherwise. * @public */ function isArrayEmpty(array) { return array == null || array.length === 0; } /** * Reduce an array to an object. The type of the object returned depends on the type of the params. * If the 'key' is passed then the function returns an object which properties names are the value * of each object[key] and the value under that property are each object. * If the 'key' is not passed then the function returns an object which properties names are each * array item, and the value is also the array item. * * @param array - Array from which to create an object. * @param key - Key used to access to each object[key] value, used for each property name in the * new object. * * @returns New object which properties object[key] contains each item in the array and the key is * either the item of the array or a property of each item designated by 'key' param. * * @public */ function arrayToObject(array, key) { return array.reduce((accumulator, current) => { if (key) { // eslint-disable-next-line ts/no-unsafe-member-access accumulator[current[key]] = current; } else if (typeof current === 'string') { accumulator[current] = current; } return accumulator; }, {}); } /** * Groups the array items based on the provided `groupBy` function. * * @param array - The array to iterate, grouping its items in different arrays based on the * `groupBy` function. * @param groupBy - A function to determine the group name of a single item. * @returns The items grouped in a dictionary. * * @public */ function groupItemsBy(array, groupBy) { return array.reduce((accumulator, current, index) => { const keyValue = groupBy(current, index); if (!accumulator[keyValue]) { accumulator[keyValue] = []; } accumulator[keyValue].push(current); return accumulator; }, {}); } /** * Filters an array with all elements that pass the test implemented by the given filter * function. If an item has another list of items in the `childrenKey` property it recursively * filters that new list, adding it to the returned one. * * @param array - Array to be filtered. * @param condition - Predicate function to test each element of the array. It should return `true` * to keep the element; or `false` otherwise. * @param childrenKey - Property name within the array used to perform a recursive call. * * @example * Input - Output example * * ``` * const hierarchicalFilters: Filter[] = [ * { * id: 'filter1' * selected: true, * children: [ * { * id: 'filter1-1' * selected: true, * children: [] * }, * { * id: 'filter1-2' * selected: false, * children: [] * } * ] * }, * { * id: 'filter2', * selected: false, * children: [ * { * id: 'filter2-1', * selected: true // not should happen * } * ] * } * ] * * const filteredArray: Filter[] = deepFilterArray( * hierarchicalFilters, * filter => filter.selected, * 'children' * ) * * /* * filteredArray = [ * { * id: 'filter1' * selected: true, * children: [ * { * id: 'filter1-1' * selected: true, * children: [] * }, * { * id: 'filter1-2' * selected: false, * children: [] * } * ] * }, * { * id: 'filter1-1' * selected: true, * children: [] * } * ] * ``` * * @returns A new array with the elements that pass the condition, or an empty array if no one * pass the test. * * @public */ function deepFilter(array, condition, childrenKey) { return array.reduce(function filter(filteredArray, item) { if (condition(item)) { filteredArray.push(item); item[childrenKey]?.reduce(filter, filteredArray); } return filteredArray; }, []); } /** * Flat an `Item[]` deeply using the given `childrenKey` to access to his children. * * @param array - The list of items to flat. Each item may have another list of items of * the same type to flat. * @param childrenKey - Property name where each of the items of the given array may have another * list of items to be flattened. * * @returns A flat list with all the found items. * * @public */ function deepFlat(array, childrenKey) { return array.reduce(function flat(flattenedArray, item) { flattenedArray.push(item); item[childrenKey]?.reduce(flat, flattenedArray); return flattenedArray; }, []); } /** * Creates an Emitter filter function to compare two arrays and filter those that are equal. * * @param comparator - String with the name of the field of the arrays items to compare, or function * to compare items directly. * @returns The comparator function that receives two arrays to compare them. * * @internal */ function createEmitterArrayFilter(comparator) { const comparatorFn = typeof comparator === 'function' ? comparator : (newItem, oldItem) => newItem[comparator] === oldItem[comparator]; return function (newCollection, oldCollection) { return (newCollection.length !== oldCollection.length || newCollection.some(newItem => !oldCollection.find(oldItem => comparatorFn(newItem, oldItem)))); }; } export { arrayToObject, createEmitterArrayFilter, deepFilter, deepFlat, groupItemsBy, isArrayEmpty }; //# sourceMappingURL=array.js.map