UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

89 lines (88 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createOptionsArray = createOptionsArray; exports.removeDuplicatesFromArray = removeDuplicatesFromArray; exports.getDuplicates = getDuplicates; exports.findMissingElements = findMissingElements; exports.splitArray = splitArray; exports.splitArrayByProperty = splitArrayByProperty; exports.rotateArray = rotateArray; exports.moveArrayElement = moveArrayElement; const non_primitives_1 = require("../guards/non-primitives"); const primitives_1 = require("../guards/primitives"); const index_1 = require("../utils/index"); const helpers_1 = require("./helpers"); function createOptionsArray(data, config) { const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', retainNumberValue = false, } = config || {}; if (data?.length) { return data?.map((datum) => { const firstValue = retainNumberValue && (0, primitives_1.isNumber)(datum[firstFieldKey]) ? datum[firstFieldKey] : String(datum[firstFieldKey] ?? ''); return { [firstFieldName]: firstValue, [secondFieldName]: String(datum[secondFieldKey] ?? ''), }; }); } else { return []; } } function removeDuplicatesFromArray(array) { return array?.filter((item, index, self) => index === self?.findIndex((el) => (0, index_1.isDeepEqual)(el, item))); } function getDuplicates(array) { const seen = []; const duplicates = []; for (const item of array) { const hasSeen = seen?.find((el) => (0, index_1.isDeepEqual)(el, item)); const hasDuplicate = duplicates?.find((el) => (0, index_1.isDeepEqual)(el, item)); if (hasSeen && !hasDuplicate) { duplicates?.push(item); } else if (!hasSeen) { seen?.push(item); } } return duplicates; } function findMissingElements(array1, array2, missingFrom) { const source = (missingFrom === 'from-first' ? array1 : array2) ?? []; const target = (missingFrom === 'from-first' ? array2 : array1) ?? []; return source.filter((s) => !target?.some((t) => (0, index_1.isDeepEqual)(t, s))); } function splitArray(arr, chunkSize) { const result = []; for (let i = 0; i < arr?.length; i += chunkSize) { result.push(arr.slice(i, i + chunkSize)); } return result; } function splitArrayByProperty(source, property) { if (!(0, non_primitives_1.isValidArray)(source)) return []; const grouped = {}; source.forEach((item) => { const rawKey = (0, helpers_1._resolveNestedKey)(item, property); const key = rawKey != null ? String(rawKey) : '__undefined__'; if (!grouped[key]) { grouped[key] = []; } grouped[key].push(item); }); return Object.values(grouped); } function rotateArray(arr, steps) { const length = arr?.length; if (length === 0) return arr; const offset = ((steps % length) + length) % length; return arr.slice(-offset).concat(arr.slice(0, -offset)); } function moveArrayElement(arr, fromIndex, toIndex) { const newArr = [...arr]; const [item] = newArr.splice(fromIndex, 1); newArr.splice(toIndex, 0, item); return newArr; }