@stryke/helpers
Version: 
A package containing miscellaneous helper functions that are used across many different Storm Software projects.
21 lines (20 loc) • 773 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setField = setField;
var _isObjectIndex = require("@stryke/type-checks/is-object-index");
var _isString = require("@stryke/type-checks/is-string");
var _toPath = require("./to-path.cjs");
function setField(i, e, c) {
  const r = Array.isArray(e) ? e : (0, _isString.isString)(e) ? (0, _toPath.toPath)(e) : [e];
  for (const t of r) if (t === "__proto__" || t === "constructor" || t === "prototype") throw new Error(`Invalid key in path: ${t}`);
  let o = i;
  for (let t = 0; t < r.length - 1; t++) {
    const n = r[t],
      y = r[t + 1];
    o[n] === null && (o[n] = (0, _isObjectIndex.isObjectIndex)(y) ? [] : {}), o = o[n];
  }
  const s = r.at(-1);
  return s && (o[s] = c), i;
}