UNPKG

@storm-stack/utilities

Version:

This package includes various base utility class and various functions to assist in the development process.

25 lines (24 loc) 749 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setField = setField; var _types = require("@storm-stack/types"); var _toPath = require("./to-path.cjs"); function setField(object, path, value) { const resolvedPath = Array.isArray(path) ? path : (0, _types.isString)(path) ? (0, _toPath.toPath)(path) : [path]; let current = object; for (let i = 0; i < resolvedPath.length - 1; i++) { const key = resolvedPath[i]; const nextKey = resolvedPath[i + 1]; if (current[key] === null) { current[key] = (0, _types.isObjectIndex)(nextKey) ? [] : {}; } current = current[key]; } const lastKey = resolvedPath.at(-1); if (lastKey) { current[lastKey] = value; } return object; }