Download react-hook-form/src/utils/set.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 902 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/utils/set.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/utils/set.ts
-
curl -L -o set.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/utils/set.ts
902 Bytes
| import type { FieldPath, FieldValues } from '../types'; | |
| import isKey from './isKey'; | |
| import isObject from './isObject'; | |
| import stringToPath from './stringToPath'; | |
| export default ( | |
| object: FieldValues, | |
| path: FieldPath<FieldValues>, | |
| value?: unknown, | |
| ) => { | |
| let index = -1; | |
| const tempPath = isKey(path) ? [path] : stringToPath(path); | |
| const length = tempPath.length; | |
| const lastIndex = length - 1; | |
| while (++index < length) { | |
| const key = tempPath[index]; | |
| let newValue = value; | |
| if (index !== lastIndex) { | |
| const objValue = object[key]; | |
| newValue = | |
| isObject(objValue) || Array.isArray(objValue) | |
| ? objValue | |
| : !isNaN(+tempPath[index + 1]) | |
| ? [] | |
| : {}; | |
| } | |
| if (key === '__proto__' || key === 'constructor' || key === 'prototype') { | |
| return; | |
| } | |
| object[key] = newValue; | |
| object = object[key]; | |
| } | |
| }; | |