Skip to content

Commit 920e727

Browse files
authored
fix(splitProps): support Symbol keys (#2814)
* fix(splitProps): support `Symbol` keys * chore(splitPropsTests): simplify description
1 parent abb8854 commit 920e727

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

.changeset/sunny-peaches-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/utils": patch
3+
---
4+
5+
fix: support `Symbol` keys in `splitProps`

packages/utilities/core/src/object.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ export function pick<T extends Record<string, any>, K extends keyof T>(obj: T, k
2828
return filtered as any
2929
}
3030

31-
type Dict = Record<string, any>
31+
type Dict = Record<string | symbol, any>
3232

3333
export function splitProps<T extends Dict>(props: T, keys: (keyof T)[]) {
3434
const rest: Dict = {}
3535
const result: Dict = {}
3636

3737
const keySet = new Set(keys)
3838

39-
for (const key in props) {
39+
for (const key of Reflect.ownKeys(props)) {
4040
if (keySet.has(key)) {
4141
result[key] = props[key]
4242
} else {

packages/utilities/core/tests/object.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from "vitest"
2-
import { compact } from "../src"
2+
import { compact, splitProps } from "../src"
33

44
describe("compact()", () => {
55
test("should compact object", () => {
@@ -45,3 +45,30 @@ describe("compact()", () => {
4545
expect(compact(obj)).toEqual(expected)
4646
})
4747
})
48+
49+
describe("splitProps()", () => {
50+
test("should split props", () => {
51+
const props = { a: 1, b: 2, c: 3 }
52+
const [result, rest] = splitProps(props, ["a", "c"])
53+
54+
expect(result).toEqual({ a: 1, c: 3 })
55+
expect(rest).toEqual({ b: 2 })
56+
})
57+
58+
test("should handle missing keys gracefully", () => {
59+
const props = { a: 1 }
60+
const [result, rest] = splitProps(props, ["b" as keyof typeof props])
61+
62+
expect(result).toEqual({})
63+
expect(rest).toEqual({ a: 1 })
64+
})
65+
66+
test("should perserve symbol keys", () => {
67+
const symA = Symbol("a")
68+
const symB = Symbol("b")
69+
const props = { [symA]: 1, [symB]: 2, c: 3 }
70+
const [result, rest] = splitProps(props, [symA, "c"])
71+
expect(result).toEqual({ [symA]: 1, c: 3 })
72+
expect(rest).toEqual({ [symB]: 2 })
73+
})
74+
})

0 commit comments

Comments
 (0)