114 lines
2.9 KiB
JavaScript
114 lines
2.9 KiB
JavaScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
/**
|
|
* Groups the collection into a dictionary based on the provided
|
|
* group function.
|
|
*/
|
|
export function groupBy(data, groupFn) {
|
|
const result = Object.create(null);
|
|
for (const element of data) {
|
|
const key = groupFn(element);
|
|
let target = result[key];
|
|
if (!target) {
|
|
target = result[key] = [];
|
|
}
|
|
target.push(element);
|
|
}
|
|
return result;
|
|
}
|
|
export function diffSets(before, after) {
|
|
const removed = [];
|
|
const added = [];
|
|
for (const element of before) {
|
|
if (!after.has(element)) {
|
|
removed.push(element);
|
|
}
|
|
}
|
|
for (const element of after) {
|
|
if (!before.has(element)) {
|
|
added.push(element);
|
|
}
|
|
}
|
|
return { removed, added };
|
|
}
|
|
export function diffMaps(before, after) {
|
|
const removed = [];
|
|
const added = [];
|
|
for (const [index, value] of before) {
|
|
if (!after.has(index)) {
|
|
removed.push(value);
|
|
}
|
|
}
|
|
for (const [index, value] of after) {
|
|
if (!before.has(index)) {
|
|
added.push(value);
|
|
}
|
|
}
|
|
return { removed, added };
|
|
}
|
|
/**
|
|
* Computes the intersection of two sets.
|
|
*
|
|
* @param setA - The first set.
|
|
* @param setB - The second iterable.
|
|
* @returns A new set containing the elements that are in both `setA` and `setB`.
|
|
*/
|
|
export function intersection(setA, setB) {
|
|
const result = new Set();
|
|
for (const elem of setB) {
|
|
if (setA.has(elem)) {
|
|
result.add(elem);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
export class SetWithKey {
|
|
toKey;
|
|
_map = new Map();
|
|
constructor(values, toKey) {
|
|
this.toKey = toKey;
|
|
for (const value of values) {
|
|
this.add(value);
|
|
}
|
|
}
|
|
get size() {
|
|
return this._map.size;
|
|
}
|
|
add(value) {
|
|
const key = this.toKey(value);
|
|
this._map.set(key, value);
|
|
return this;
|
|
}
|
|
delete(value) {
|
|
return this._map.delete(this.toKey(value));
|
|
}
|
|
has(value) {
|
|
return this._map.has(this.toKey(value));
|
|
}
|
|
*entries() {
|
|
for (const entry of this._map.values()) {
|
|
yield [entry, entry];
|
|
}
|
|
}
|
|
keys() {
|
|
return this.values();
|
|
}
|
|
*values() {
|
|
for (const entry of this._map.values()) {
|
|
yield entry;
|
|
}
|
|
}
|
|
clear() {
|
|
this._map.clear();
|
|
}
|
|
forEach(callbackfn, thisArg) {
|
|
this._map.forEach(entry => callbackfn.call(thisArg, entry, entry, this));
|
|
}
|
|
[Symbol.iterator]() {
|
|
return this.values();
|
|
}
|
|
[Symbol.toStringTag] = 'SetWithKey';
|
|
}
|
|
//# sourceMappingURL=collections.js.map
|