forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapExtensions.ts
More file actions
37 lines (35 loc) · 1.26 KB
/
Copy pathMapExtensions.ts
File metadata and controls
37 lines (35 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
/**
* Helper functions for working with the `Map<K, V>` data type.
*
* @public
*/
export class MapExtensions {
/**
* Adds all the (key, value) pairs from the source map into the target map.
* @remarks
* This function modifies targetMap. Any existing keys will be overwritten.
* @param targetMap - The map that entries will be added to
* @param sourceMap - The map containing the entries to be added
*/
public static mergeFromMap<K, V>(targetMap: Map<K, V>, sourceMap: ReadonlyMap<K, V>): void {
for (const pair of sourceMap.entries()) {
targetMap.set(pair[0], pair[1]);
}
}
/**
* Converts a string-keyed map to an object.
* @remarks
* This function has the same effect as Object.fromEntries(map.entries())
* in supported versions of Node (\>= 12.0.0).
* @param map - The map that the object properties will be sourced from
*/
public static toObject<TValue>(map: Map<string, TValue>): { [key: string]: TValue } {
const object: { [key: string]: TValue } = {};
for (const [key, value] of map.entries()) {
object[key] = value;
}
return object;
}
}