{"version":3,"file":"set-field.mjs","names":[],"sources":["../src/set-field.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       🗲 Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isObjectIndex } from \"@stryke/type-checks/is-object-index\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { DeepKey } from \"@stryke/types/object\";\nimport { toPath } from \"./to-path\";\n\n/**\n * Sets a value at a given deep path in an object.\n *\n * @param object - The object to set the value in.\n * @param path - The deep path to set the value at.\n * @param value - The value to set.\n * @returns The object with the value set at the given deep path.\n */\nexport function setField<\n  TObject extends Record<string, any> = Record<string, any>\n>(object: TObject, path: DeepKey<TObject>, value: unknown): TObject {\n  const resolvedPath = Array.isArray(path)\n    ? path\n    : isString(path)\n      ? toPath(path)\n      : [path];\n\n  // Validate resolvedPath to prevent prototype pollution\n  for (const key of resolvedPath) {\n    if (key === \"__proto__\" || key === \"constructor\" || key === \"prototype\") {\n      throw new Error(`Invalid key in path: ${key}`);\n    }\n  }\n\n  let current: any = object;\n  for (let i = 0; i < resolvedPath.length - 1; i++) {\n    const key = resolvedPath[i];\n    const nextKey = resolvedPath[i + 1];\n\n    if (current[key] === null) {\n      current[key] = isObjectIndex(nextKey) ? [] : {};\n    }\n\n    current = current[key];\n  }\n\n  const lastKey = resolvedPath.at(-1);\n  if (lastKey) {\n    current[lastKey] = value;\n  }\n\n  return object;\n}\n"],"mappings":";;;;;;;;;;;;;AA+BA,SAAgB,SAEd,QAAiB,MAAwB,OAAyB;CAClE,MAAM,eAAe,MAAM,QAAQ,KAAK,GACpC,OACA,SAAS,KAAK,GACZ,OAAO,KAAK,GACZ,CAAC,KAAK;AAGZ,MAAK,MAAM,OAAO,aAChB,KAAI,QAAQ,eAAe,QAAQ,iBAAiB,QAAQ,YAC1D,OAAM,IAAI,MAAM,wBAAwB,MAAM;CAIlD,IAAI,UAAe;AACnB,MAAK,IAAI,IAAI,GAAG,IAAI,aAAa,SAAS,GAAG,KAAK;EAChD,MAAM,MAAM,aAAa;EACzB,MAAM,UAAU,aAAa,IAAI;AAEjC,MAAI,QAAQ,SAAS,KACnB,SAAQ,OAAO,cAAc,QAAQ,GAAG,EAAE,GAAG,EAAE;AAGjD,YAAU,QAAQ;;CAGpB,MAAM,UAAU,aAAa,GAAG,GAAG;AACnC,KAAI,QACF,SAAQ,WAAW;AAGrB,QAAO"}