| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* An interface for a JavaScript object that |
| 8 |
* acts a dictionary. The keys are strings. |
| 9 |
*/ |
| 10 |
export type IStringDictionary<V> = Record<string, V>; |
| 11 |
|
| 12 |
/** |
| 13 |
* An interface for a JavaScript object that |
| 14 |
* acts a dictionary. The keys are numbers. |
| 15 |
*/ |
| 16 |
export type INumberDictionary<V> = Record<number, V>; |
| 17 |
|
| 18 |
/** |
| 19 |
* Groups the collection into a dictionary based on the provided |
| 20 |
* group function. |
| 21 |
*/ |
| 22 |
export function groupBy<K extends string | number | symbol, V>(data: V[], groupFn: (element: V) => K): Record<K, V[]> { |
| 23 |
const result: Record<K, V[]> = Object.create(null); |
| 24 |
for (const element of data) { |
| 25 |
const key = groupFn(element); |
| 26 |
let target = result[key]; |
| 27 |
if (!target) { |
| 28 |
target = result[key] = []; |
| 29 |
} |
| 30 |
target.push(element); |
| 31 |
} |
| 32 |
return result; |
| 33 |
} |
| 34 |
|
| 35 |
export function diffSets<T>(before: Set<T>, after: Set<T>): { removed: T[]; added: T[] } { |
| 36 |
const removed: T[] = []; |
| 37 |
const added: T[] = []; |
| 38 |
for (const element of before) { |
| 39 |
if (!after.has(element)) { |
| 40 |
removed.push(element); |
| 41 |
} |
| 42 |
} |
| 43 |
for (const element of after) { |
| 44 |
if (!before.has(element)) { |
| 45 |
added.push(element); |
| 46 |
} |
| 47 |
} |
| 48 |
return { removed, added }; |
| 49 |
} |
| 50 |
|
| 51 |
export function diffMaps<K, V>(before: Map<K, V>, after: Map<K, V>): { removed: V[]; added: V[] } { |
| 52 |
const removed: V[] = []; |
| 53 |
const added: V[] = []; |
| 54 |
for (const [index, value] of before) { |
| 55 |
if (!after.has(index)) { |
| 56 |
removed.push(value); |
| 57 |
} |
| 58 |
} |
| 59 |
for (const [index, value] of after) { |
| 60 |
if (!before.has(index)) { |
| 61 |
added.push(value); |
| 62 |
} |
| 63 |
} |
| 64 |
return { removed, added }; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Computes the intersection of two sets. |
| 69 |
* |
| 70 |
* @param setA - The first set. |
| 71 |
* @param setB - The second iterable. |
| 72 |
* @returns A new set containing the elements that are in both `setA` and `setB`. |
| 73 |
*/ |
| 74 |
export function intersection<T>(setA: Set<T>, setB: Iterable<T>): Set<T> { |
| 75 |
const result = new Set<T>(); |
| 76 |
for (const elem of setB) { |
| 77 |
if (setA.has(elem)) { |
| 78 |
result.add(elem); |
| 79 |
} |
| 80 |
} |
| 81 |
return result; |
| 82 |
} |
| 83 |
|
| 84 |
export class SetWithKey<T> implements Set<T> { |
| 85 |
private _map = new Map<any, T>(); |
| 86 |
|
| 87 |
constructor(values: T[], private toKey: (t: T) => any) { |
| 88 |
for (const value of values) { |
| 89 |
this.add(value); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
get size(): number { |
| 94 |
return this._map.size; |
| 95 |
} |
| 96 |
|
| 97 |
add(value: T): this { |
| 98 |
const key = this.toKey(value); |
| 99 |
this._map.set(key, value); |
| 100 |
return this; |
| 101 |
} |
| 102 |
|
| 103 |
delete(value: T): boolean { |
| 104 |
return this._map.delete(this.toKey(value)); |
| 105 |
} |
| 106 |
|
| 107 |
has(value: T): boolean { |
| 108 |
return this._map.has(this.toKey(value)); |
| 109 |
} |
| 110 |
|
| 111 |
*entries(): IterableIterator<[T, T]> { |
| 112 |
for (const entry of this._map.values()) { |
| 113 |
yield [entry, entry]; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
keys(): IterableIterator<T> { |
| 118 |
return this.values(); |
| 119 |
} |
| 120 |
|
| 121 |
*values(): IterableIterator<T> { |
| 122 |
for (const entry of this._map.values()) { |
| 123 |
yield entry; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
clear(): void { |
| 128 |
this._map.clear(); |
| 129 |
} |
| 130 |
|
| 131 |
forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: any): void { |
| 132 |
this._map.forEach(entry => callbackfn.call(thisArg, entry, entry, this)); |
| 133 |
} |
| 134 |
|
| 135 |
[Symbol.iterator](): IterableIterator<T> { |
| 136 |
return this.values(); |
| 137 |
} |
| 138 |
|
| 139 |
[Symbol.toStringTag]: string = 'SetWithKey'; |
| 140 |
} |