Skip to content

Commit b4897a0

Browse files
Copilotunnoq
andauthored
fix: clone util should clone symbol properties of object (#1258)
The `clone` function only iterated over string-keyed properties via `for...in`, missing symbol properties entirely. ### Changes - Added `Object.getOwnPropertySymbols()` iteration to clone symbol-keyed properties - Added test case for nested symbol property cloning ### Example ```typescript const sym = Symbol('test') const obj = { a: 1, [sym]: { b: 2 } } const cloned = clone(obj) // Before: cloned[sym] was undefined // After: cloned[sym] equals { b: 2 } (deep cloned) ``` <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > clone util should clone symbol properties of object > > if (isObject(value)) { > const result: Record<PropertyKey, unknown> = {} > > for (const key in value) { > result[key] = clone(value[key]) > } > > return result as any > } </details> <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: unnoq <64189902+unnoq@users.noreply.github.com>
1 parent e565e67 commit b4897a0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages/shared/src/object.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ it('clone', () => {
8585
expect(cloned.nested.arr).not.toBe(obj.nested.arr)
8686
})
8787

88+
it('clone with symbol properties', () => {
89+
const sym = Symbol('test')
90+
const nestedSym = Symbol('nested')
91+
const obj = { a: 1, [sym]: { b: 2, [nestedSym]: 3 } }
92+
const cloned = clone(obj)
93+
94+
expect(cloned.a).toBe(1)
95+
expect(cloned[sym]).toEqual({ b: 2, [nestedSym]: 3 })
96+
expect(cloned[sym]).not.toBe(obj[sym])
97+
expect(cloned[sym][nestedSym]).toBe(3)
98+
})
99+
88100
it('get', () => {
89101
expect(get({ a: { b: 1 } }, ['a', 'b'])).toEqual(1)
90102
expect(get({ a: { b: 1 } }, ['a', 'b', 'c'])).toEqual(undefined)

packages/shared/src/object.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export function clone<T>(value: T): T {
7070
result[key] = clone(value[key])
7171
}
7272

73+
for (const sym of Object.getOwnPropertySymbols(value)) {
74+
result[sym] = clone(value[sym])
75+
}
76+
7377
return result as any
7478
}
7579

0 commit comments

Comments
 (0)