UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

114 lines (111 loc) • 3.65 kB
/** * DevExtreme (esm/__internal/core/utils/array.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import config from "../../../core/config"; import { orderEach } from "../../../core/utils/object"; import { isDefined } from "../../../core/utils/type"; function createOccurrenceMap(values) { return values.reduce((map, value) => { map.set(value, (map.get(value) ?? 0) + 1); return map }, new Map) } function takeOccurrence(occurrences, value) { const count = occurrences.get(value); if (!count) { return false } occurrences.set(value, count - 1); return true } export function getUniqueValues(values) { return [...new Set(values)] } export function getIntersection(firstArray, secondArray) { const occurrences = createOccurrenceMap(secondArray); return firstArray.filter(value => takeOccurrence(occurrences, value)) } export function removeDuplicates() { let from = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : []; let toRemove = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : []; const occurrences = createOccurrenceMap(toRemove); return from.filter(value => !takeOccurrence(occurrences, value)) } export function normalizeIndexes(items, indexPropName, currentItem, needIndexCallback) { const indexedItems = {}; const { useLegacyVisibleIndex: useLegacyVisibleIndex } = config(); let currentIndex = 0; const shouldUpdateIndex = item => !isDefined(item[indexPropName]) && (!needIndexCallback || !!needIndexCallback(item)); const setIndex = (item, index) => { const indexedItem = item; indexedItem[indexPropName] = index }; items.forEach(item => { const index = item[indexPropName]; if (Number(index) >= 0) { const key = String(index); const sameIndexItems = indexedItems[key] ?? []; indexedItems[key] = sameIndexItems; if (item === currentItem) { sameIndexItems.unshift(item) } else { sameIndexItems.push(item) } } else { setIndex(item, void 0) } }); if (!useLegacyVisibleIndex) { items.forEach(item => { if (shouldUpdateIndex(item)) { while (indexedItems[String(currentIndex)]) { currentIndex += 1 } indexedItems[String(currentIndex)] = [item]; currentIndex += 1 } }) } currentIndex = 0; orderEach(indexedItems, (index, sameIndexItems) => { sameIndexItems.forEach(item => { if (Number(index) >= 0) { setIndex(item, currentIndex); currentIndex += 1 } }) }); if (useLegacyVisibleIndex) { items.forEach(item => { if (shouldUpdateIndex(item)) { setIndex(item, currentIndex); currentIndex += 1 } }) } } export function groupBy(array, getGroupName) { const groupedResult = {}; array.forEach(item => { const groupName = getGroupName(item); const key = isDefined(groupName) ? groupName : String(groupName); const group = groupedResult[key] ?? []; group.push(item); groupedResult[key] = group }); return groupedResult } export function wrapToArray(item) { return Array.isArray(item) ? item : [item] }