Skip to content

Commit 69c0860

Browse files
Broccohansl
authored andcommitted
fix(@angular-devkit/schematics): Fix deep copy logic
1 parent a107498 commit 69c0860

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

packages/angular_devkit/core/src/utils/object.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@ export function mapObject<T, V>(obj: {[k: string]: T},
1414
return acc;
1515
}, {});
1616
}
17+
18+
// tslint:disable-next-line:no-any
19+
export function deepCopy<T extends any> (object: T): T {
20+
if (Array.isArray(object)) {
21+
// tslint:disable-next-line:no-any
22+
return object.map((o: any) => deepCopy(o));
23+
} else if (typeof object === 'object') {
24+
if (object['toJSON']) {
25+
return JSON.parse((object['toJSON'] as () => string)());
26+
}
27+
28+
const copy = {} as T;
29+
for (const key of Object.keys(object)) {
30+
copy[key] = deepCopy(object[key]);
31+
}
32+
33+
return copy;
34+
} else {
35+
return object;
36+
}
37+
}

packages/angular_devkit/schematics/tools/schema-option-transform.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import {
99
BaseException,
10+
deepCopy,
1011
schema,
1112
} from '@angular-devkit/core';
1213
import { Observable, of as observableOf } from 'rxjs';
@@ -26,29 +27,11 @@ export class InvalidInputOptions extends BaseException {
2627
}
2728
}
2829

29-
30-
// tslint:disable-next-line:no-any
31-
function _deepCopy<T extends {[key: string]: any}>(object: T): T {
32-
return JSON.parse(JSON.stringify(object));
33-
// const copy = {} as T;
34-
// for (const key of Object.keys(object)) {
35-
// if (typeof object[key] == 'object') {
36-
// copy[key] = _deepCopy(object[key]);
37-
// break;
38-
// } else {
39-
// copy[key] = object[key];
40-
// }
41-
// }
42-
43-
// return copy;
44-
}
45-
46-
4730
// This can only be used in NodeJS.
4831
export function validateOptionsWithSchema(registry: schema.SchemaRegistry) {
4932
return <T extends {}>(schematic: SchematicDesc, options: T): Observable<T> => {
5033
// Prevent a schematic from changing the options object by making a copy of it.
51-
options = _deepCopy(options);
34+
options = deepCopy(options);
5235

5336
if (schematic.schema && schematic.schemaJson) {
5437
// Make a deep copy of options.

0 commit comments

Comments
 (0)