{"version":3,"file":"set-D4MA3o6n.cjs","names":["createTypeGuard"],"sources":["../src/functions/_internal/isMaliciousObjectPath.ts","../src/functions/set/set.ts"],"sourcesContent":["import { createTypeGuard } from '../createTypeGuard';\n\n/**\n * Determines whether the given property name is malicious or not (e.g. `__proto__` or `constructor`).\n * @param propertyName The property name to check for maliciousness.\n * @returns true if the property name is malicious, false otherwise.\n * @example\n * ```ts\n * isMaliciousObjectProperty('constructor'); // returns true\n * isMaliciousObjectProperty('nonMaliciousPath'); // returns false\n * isMaliciousObjectProperty('__proto__'); // returns true\n * ```\n */\nexport const isMaliciousObjectProperty = createTypeGuard<string, string>(\n  Object.getOwnPropertyNames(Object.getPrototypeOf({}))\n);\n","import type { Get } from 'type-fest';\n\nimport type { ObjectPath } from '../../types';\nimport type { RequireKeysDeep } from '../../types/_internal';\nimport { isMaliciousObjectProperty } from '../_internal/isMaliciousObjectPath';\nimport { assert } from '../assert';\n\n/**\n * Sets a value at the specified (possibly nested) path in an object.\n * @template TObject The type of the object.\n * @template Path The type of the path.\n * @param object The object to set the value in.\n * @param path The path to set the value at.\n * @param value The value to set.\n * @example\n * ```ts\n * const object: {\n *   foo?: {\n *     bar?: {\n *       anArray: string[];\n *     };\n *   }\n * };\n *\n * set(object, 'foo.bar.anArray[0]', 'value');\n * object.foo.bar.anArray[0]; // 'value'\n */\nexport function set<\n  TObject extends Record<string, unknown>,\n  Path extends ObjectPath<TObject>,\n>(\n  object: TObject,\n  path: Path,\n  value: Get<TObject, Path>\n): asserts object is TObject & RequireKeysDeep<TObject, Path> {\n  const segments = path.match(pathSegmentsRegex);\n  assert(segments !== null, 'Invalid path');\n  assert(\n    segments.every((segment) => isMaliciousObjectProperty(segment) === false),\n    'Potentially malicious path'\n  );\n\n  // biome-ignore lint/suspicious/noExplicitAny: easier to work with, and we're trusting the compiler\n  let currentObject: any = object;\n  for (let index = 0; index < segments.length - 1; index++) {\n    // biome-ignore lint/style/noNonNullAssertion: trust the compiler (and unit tests 😄).\n    const segment = segments[index]!;\n    if (segment in currentObject === false) {\n      currentObject[segment] = Object.create(null) as Record<string, unknown>;\n    }\n    currentObject = currentObject[segment];\n  }\n\n  // biome-ignore lint/style/noNonNullAssertion: this is checked at the type-level\n  currentObject[segments.at(-1)!] = value;\n}\n\nconst pathSegmentsRegex = /[\\w-]+/g;\n"],"mappings":";;;;;;;;;;;;;;;AAaA,MAAa,4BAA4BA,wCACvC,OAAO,oBAAoB,OAAO,eAAe,EAAE,CAAC,CAAC,CACtD;;;;;;;;;;;;;;;;;;;;;;;;ACYD,SAAgB,IAId,QACA,MACA,OAC4D;CAC5D,MAAM,WAAW,KAAK,MAAM,kBAAkB;AAC9C,uBAAO,aAAa,MAAM,eAAe;AACzC,uBACE,SAAS,OAAO,YAAY,0BAA0B,QAAQ,KAAK,MAAM,EACzE,6BACD;CAGD,IAAI,gBAAqB;AACzB,MAAK,IAAI,QAAQ,GAAG,QAAQ,SAAS,SAAS,GAAG,SAAS;EAExD,MAAM,UAAU,SAAS;AACzB,MAAI,WAAW,kBAAkB,MAC/B,eAAc,WAAW,OAAO,OAAO,KAAK;AAE9C,kBAAgB,cAAc;;AAIhC,eAAc,SAAS,GAAG,GAAG,IAAK;;AAGpC,MAAM,oBAAoB"}