Skip to content

Commit 4e597d3

Browse files
author
lukasz-kuzynski-11sigma
authored
feat: added option to bundleTarget to prevent deep clone of document (#99)
* feat: added option to `bundleTarget` to prevent deep clone of document
1 parent 0483fb4 commit 4e597d3

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/__tests__/bundle.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,61 @@ describe('bundleTargetPath()', () => {
6363
});
6464
});
6565

66+
it('should operate on original object if `cloneDocument` set to false', () => {
67+
const document = {
68+
definitions: {
69+
user: {
70+
id: 'foo',
71+
address: {
72+
$ref: '#/definitions/address',
73+
},
74+
},
75+
address: {
76+
street: 'foo',
77+
user: {
78+
$ref: '#/definitions/user',
79+
},
80+
},
81+
card: {
82+
zip: '20815',
83+
},
84+
},
85+
__target__: {
86+
entity: {
87+
$ref: '#/definitions/user',
88+
},
89+
},
90+
};
91+
92+
const result = bundleTarget({
93+
document,
94+
path: '#/__target__',
95+
cloneDocument: false,
96+
});
97+
98+
expect(document.__target__).toStrictEqual(result);
99+
100+
expect(result).toEqual({
101+
entity: {
102+
$ref: `#/${BUNDLE_ROOT}/user`,
103+
},
104+
[BUNDLE_ROOT]: {
105+
user: {
106+
id: 'foo',
107+
address: {
108+
$ref: `#/${BUNDLE_ROOT}/address`,
109+
},
110+
},
111+
address: {
112+
street: 'foo',
113+
user: {
114+
$ref: `#/${BUNDLE_ROOT}/user`,
115+
},
116+
},
117+
},
118+
});
119+
});
120+
66121
it('should include falsy values', () => {
67122
const document = {
68123
definitions: {

src/bundle.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ export const bundleTarget = <T = unknown>(
1717
path,
1818
bundleRoot = BUNDLE_ROOT,
1919
errorsRoot = ERRORS_ROOT,
20-
}: { document: T; path: string; bundleRoot?: string; errorsRoot?: string },
20+
cloneDocument = true,
21+
}: { document: T; path: string; bundleRoot?: string; errorsRoot?: string; cloneDocument?: boolean },
2122
cur?: unknown,
2223
) => {
2324
if (path === bundleRoot || path === errorsRoot) {
2425
throw new Error(`Roots do not make any sense`);
2526
}
2627

27-
return bundle(cloneDeep(document), pointerToPath(bundleRoot), pointerToPath(errorsRoot))(path, { [path]: true }, cur);
28+
const workingDocument = cloneDocument ? cloneDeep(document) : document;
29+
return bundle(workingDocument, pointerToPath(bundleRoot), pointerToPath(errorsRoot))(path, { [path]: true }, cur);
2830
};
2931

3032
const bundle = (document: unknown, bundleRoot: JsonPath, errorsRoot: JsonPath) => {

0 commit comments

Comments
 (0)