99 lines
2.5 KiB
JavaScript
99 lines
2.5 KiB
JavaScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
var _a;
|
|
function groupByMap(data, groupFn) {
|
|
const result = new Map();
|
|
for (const element of data) {
|
|
const key = groupFn(element);
|
|
let target = result.get(key);
|
|
if (!target) {
|
|
target = [];
|
|
result.set(key, target);
|
|
}
|
|
target.push(element);
|
|
}
|
|
return result;
|
|
}
|
|
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 };
|
|
}
|
|
/**
|
|
* 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`.
|
|
*/
|
|
function intersection(setA, setB) {
|
|
const result = new Set();
|
|
for (const elem of setB) {
|
|
if (setA.has(elem)) {
|
|
result.add(elem);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
class SetWithKey {
|
|
static { _a = Symbol.toStringTag; }
|
|
constructor(values, toKey) {
|
|
this.toKey = toKey;
|
|
this._map = new Map();
|
|
this[_a] = 'SetWithKey';
|
|
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();
|
|
}
|
|
}
|
|
|
|
export { SetWithKey, diffSets, groupByMap, intersection };
|