UNPKG

@sussudio/base

Version:

Internal APIs for VS Code's utilities and user interface building blocks.

79 lines (78 loc) 1.79 kB
/*--------------------------------------------------------------------------------------------- * 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 }; } export class SetMap { map = new Map(); add(key, value) { let values = this.map.get(key); if (!values) { values = new Set(); this.map.set(key, values); } values.add(value); } delete(key, value) { const values = this.map.get(key); if (!values) { return; } values.delete(value); if (values.size === 0) { this.map.delete(key); } } forEach(key, fn) { const values = this.map.get(key); if (!values) { return; } values.forEach(fn); } }