UNPKG

@progress/kendo-angular-grid

Version:

Kendo UI Grid for Angular - high performance data grid with paging, filtering, virtualization, CRUD, and more.

82 lines (81 loc) 2.75 kB
/**----------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the project root for more information *-------------------------------------------------------------------------------------------*/ /** * @hidden * * Quick look-up structure for combinations of keys. * Similar to the native JS Set, however, working with a couple of keys instead of with a single key. * Supports both primitive keys and object keys (compared by reference). */ export class PairSet { /** * Gets the total number of X/Y key pairs. */ get size() { return this.totalKeysCount; } /** * Holds a set of Y keys for each defined X key. * Each X key creates a map which holds a set of Y keys. * * Map { 1 => Set { 1, 2, 3 } } // pairs: [1, 1], [1, 2], [1, 3] */ keysX = new Map(); /** * Count the each added or deleted key manually to avoid iterating over all items when calling `this.size`. */ totalKeysCount = 0; constructor(items, keyXField, keyYField) { if (items && keyXField && keyYField) { items.forEach(item => this.add(item[keyXField], item[keyYField])); } } /** * Adds a couple of items identified as a combination. */ add(keyX, keyY) { if (!this.keysX.has(keyX)) { this.keysX.set(keyX, new Set()); } if (!this.has(keyX, keyY)) { this.keysX.get(keyX).add(keyY); this.totalKeysCount += 1; } } /** * Adds a combination of a couple of items identified together. */ delete(keyX, keyY) { if (this.has(keyX, keyY)) { this.keysX.get(keyX).delete(keyY); this.totalKeysCount -= 1; } } /** * Checks whether the defined combination is stored. */ has(keyX, keyY) { return this.keysX.has(keyX) && this.keysX.get(keyX).has(keyY); } /** * Clears all key combinations. */ clear() { this.keysX.clear(); this.totalKeysCount = 0; } /** * Converts the persisted data structure to an array of objects, * using the provided field names for the object props. */ toArray(keyXField, keyYField) { return Array.from(this.keysX).reduce((pairs, pair) => { // Array.from(mapInstance) returns an array of arrays [[itemKey1, columnKeysSet1], [itemKey2, columnKeysSet2]] const [keyX, keysY] = pair; Array.from(keysY).forEach(keyY => pairs.push({ [keyXField]: keyX, [keyYField]: keyY })); return pairs; }, []); } }