/
githubmirror
/
react-hook-form
Обзор
Документация
Войти
/
githubmirror
/
react-hook-form
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/utils/unset.ts
68 строк
2 KB
Bill
🦦 perf: improve perf and save bundle size (#13591)
25 июл 2026, 04:21
Не верифицирован
25 июл 2026, 04:21
c51f3d8
Код
Авторство
О чём код?
import { PROTOTYPE_KEYWORDS } from '../constants'; import isEmptyObject from './isEmptyObject'; import isKey from './isKey'; import isNullOrUndefined from './isNullOrUndefined'; import isObject from './isObject'; import isString from './isString'; import isUndefined from './isUndefined'; import stringToPath from './stringToPath'; function baseGet(object: any, updatePath: (string | number)[]) { const length = updatePath.length - 1; let index = 0; while (index < length) { if (isNullOrUndefined(object)) { object = undefined; break; } object = object[updatePath[index]]; index++; } return object; } function isEmptyArray(obj: unknown[]) { for (const key in obj) { if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) { return false; } } return true; } export default function unset(object: any, path: string | (string | number)[]) { if (isString(path) && Object.prototype.hasOwnProperty.call(object, path)) { delete object[path]; return object; } const paths = Array.isArray(path) ? path : isKey(path) ? [path] : stringToPath(path); if (paths.some((segment) => PROTOTYPE_KEYWORDS.includes(String(segment)))) { return object; } const childObject = paths.length === 1 ? object : baseGet(object, paths); const index = paths.length - 1; const key = paths[index]; if (childObject) { delete childObject[key]; } if ( index !== 0 && ((isObject(childObject) && isEmptyObject(childObject)) || (Array.isArray(childObject) && isEmptyArray(childObject))) ) { unset(object, paths.slice(0, -1)); } return object; }