Download chakra-ui/packages/react/src/utils/split-props.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/utils/split-props.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/utils/split-props.ts
-
curl -L -o split-props.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/chakra-ui/packages/react/src/utils/split-props.ts
1.12 kB
| import { isFunction } from "./is" | |
| import type { Dict } from "./types" | |
| type PredicateFn<T> = (key: T) => boolean | |
| export interface SplitPropsFn { | |
| <T extends Dict, K extends keyof T>( | |
| props: T, | |
| keys: K[], | |
| ): [Pick<T, K>, Omit<T, K>] | |
| <T extends Dict, K extends PredicateFn<keyof T>>( | |
| props: T, | |
| keys: K, | |
| ): [Dict, Dict] | |
| } | |
| const splitPropFn = (props: Dict, predicate: PredicateFn<keyof Dict>) => { | |
| const rest: Dict = {} | |
| const result: Dict = {} | |
| const allKeys = Object.keys(props) | |
| for (const key of allKeys) { | |
| if (predicate(key)) { | |
| result[key] = props[key] | |
| } else { | |
| rest[key] = props[key] | |
| } | |
| } | |
| return [result, rest] as any | |
| } | |
| export const splitProps: SplitPropsFn = (props: any, keys: any[]) => { | |
| const predicate = isFunction(keys) | |
| ? keys | |
| : (key: keyof Dict) => keys.includes(key) | |
| return splitPropFn(props, predicate) | |
| } | |
| export const createSplitProps = <T>(keys: (keyof T)[]) => { | |
| return function split<Props extends Partial<T>>( | |
| props: Props, | |
| ): [T, Omit<Props, keyof T>] { | |
| // @ts-expect-error | |
| return splitProps(props, keys) | |
| } | |
| } | |