forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.ts
More file actions
45 lines (38 loc) · 1.22 KB
/
Copy pathobject.ts
File metadata and controls
45 lines (38 loc) · 1.22 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
38
39
40
41
42
43
44
45
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export function mapObject<T, V>(obj: {[k: string]: T},
mapper: (k: string, v: T) => V): {[k: string]: V} {
return Object.keys(obj).reduce((acc: {[k: string]: V}, k: string) => {
acc[k] = mapper(k, obj[k]);
return acc;
}, {});
}
const copySymbol = Symbol();
// tslint:disable-next-line:no-any
export function deepCopy<T extends any> (value: T): T {
if (Array.isArray(value)) {
return value.map((o: T) => deepCopy(o));
} else if (value && typeof value === 'object') {
if (value[copySymbol]) {
// This is a circular dependency. Just return the cloned value.
return value[copySymbol];
}
if (value['toJSON']) {
return JSON.parse((value['toJSON'] as () => string)());
}
const copy = new (Object.getPrototypeOf(value).constructor)();
value[copySymbol] = copy;
for (const key of Object.getOwnPropertyNames(value)) {
copy[key] = deepCopy(value[key]);
}
value[copySymbol] = undefined;
return copy;
} else {
return value;
}
}