Skip to content

Commit

Permalink
perf: replace split props
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Mar 28, 2024
1 parent 9ed4684 commit d2fe829
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions packages/utils/src/split-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@ export interface SplitPropsFn {
): [Dict, Dict]
}

export const splitProps: SplitPropsFn = (props: any, keys: any) => {
const picked = {} as any
const omitted = { ...props }

if (typeof keys === "function") {
for (const key in props) {
if (keys(key)) {
picked[key] = props[key]
delete omitted[key]
export const splitProps: SplitPropsFn = (props: any, keys: any[]) => {
const descriptors = Object.getOwnPropertyDescriptors(props)
const dKeys = Object.keys(descriptors)
const split = (k: string[]) => {
const clone = {} as Dict
for (let i = 0; i < k.length; i++) {
const key = k[i]
if (descriptors[key]) {
Object.defineProperty(clone, key, descriptors[key])
delete descriptors[key]
}
}
} else {
for (const key of keys) {
picked[key] = props[key]
delete omitted[key]
}
return clone
}

return [picked, omitted] as any
const fn = (key: any) => split(Array.isArray(key) ? key : dKeys.filter(key))

return fn(keys).concat(split(dKeys))
}

export const createSplitProps = <T>(keys: (keyof T)[]) => {
Expand Down

0 comments on commit d2fe829

Please sign in to comment.