devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
132 lines (122 loc) • 4.09 kB
JavaScript
/**
* DevExtreme (cjs/__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/
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getIntersection = getIntersection;
exports.getUniqueValues = getUniqueValues;
exports.groupBy = groupBy;
exports.normalizeIndexes = normalizeIndexes;
exports.removeDuplicates = removeDuplicates;
exports.wrapToArray = wrapToArray;
var _config = _interopRequireDefault(require("../../../core/config"));
var _object = require("../../../core/utils/object");
var _type = require("../../../core/utils/type");
function _interopRequireDefault(e) {
return e && e.__esModule ? e : {
default: e
}
}
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
}
function getUniqueValues(values) {
return [...new Set(values)]
}
function getIntersection(firstArray, secondArray) {
const occurrences = createOccurrenceMap(secondArray);
return firstArray.filter(value => takeOccurrence(occurrences, value))
}
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))
}
function normalizeIndexes(items, indexPropName, currentItem, needIndexCallback) {
const indexedItems = {};
const {
useLegacyVisibleIndex: useLegacyVisibleIndex
} = (0, _config.default)();
let currentIndex = 0;
const shouldUpdateIndex = item => !(0, _type.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;
(0, _object.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
}
})
}
}
function groupBy(array, getGroupName) {
const groupedResult = {};
array.forEach(item => {
const groupName = getGroupName(item);
const key = (0, _type.isDefined)(groupName) ? groupName : String(groupName);
const group = groupedResult[key] ?? [];
group.push(item);
groupedResult[key] = group
});
return groupedResult
}
function wrapToArray(item) {
return Array.isArray(item) ? item : [item]
}