@layerfig/config
Version:
Layer and runtime-validate type-safe configs for JavaScript apps.
1 lines • 3.31 kB
Source Map (JSON)
{"version":3,"file":"set-Bl59_6Sr.cjs","names":["item: unknown","obj: T","path: string | (string | number)[]","value: unknown","keys: (string | number)[]","current: Record<string, unknown>"],"sources":["../src/utils/is-plain-object.ts","../src/utils/set.ts"],"sourcesContent":["/**\n * Type guard to check if value is a plain object\n */\nexport function isPlainObject(item: unknown): item is Record<string, unknown> {\n\tif (item === null || typeof item !== \"object\") {\n\t\treturn false;\n\t}\n\n\t// Check for built-in object types that should be treated as primitives\n\tif (\n\t\titem instanceof Date ||\n\t\titem instanceof RegExp ||\n\t\titem instanceof Error ||\n\t\titem instanceof Map ||\n\t\titem instanceof Set ||\n\t\titem instanceof WeakMap ||\n\t\titem instanceof WeakSet ||\n\t\tArray.isArray(item)\n\t) {\n\t\treturn false;\n\t}\n\n\t// Check if it's a plain object (created by {} or new Object())\n\tconst proto = Object.getPrototypeOf(item);\n\treturn proto === null || proto === Object.prototype;\n}\n","import { isPlainObject } from \"./is-plain-object\";\n\n/**\n * Set value at path in object (lodash set replacement)\n * @param obj - Target object to modify\n * @param path - Path to set (dot notation string or array of keys)\n * @param value - Value to set at the path\n * @returns The modified object\n */\nexport function set<T extends Record<string, unknown>>(\n\tobj: T,\n\tpath: string | (string | number)[],\n\tvalue: unknown,\n): T {\n\t// Convert string path to array of keys and filter out empty strings\n\tconst keys: (string | number)[] = Array.isArray(path)\n\t\t? path.filter((key) => key !== \"\" && key != null) // Filter out empty/null/undefined\n\t\t: path\n\t\t\t\t.toString()\n\t\t\t\t.split(\".\")\n\t\t\t\t.filter((key) => key !== \"\");\n\n\t// Early return if no valid keys\n\tif (keys.length === 0) {\n\t\treturn obj;\n\t}\n\n\tlet current: Record<string, unknown> = obj;\n\n\t// Navigate to the parent of the target key\n\tfor (let i = 0; i < keys.length - 1; i++) {\n\t\tconst key = keys[i];\n\n\t\t// Type guard: ensure key is defined\n\t\tif (key == null) continue;\n\n\t\t// Create nested object if it doesn't exist or isn't an object\n\t\tif (!(key in current) || !isPlainObject(current[key])) {\n\t\t\tcurrent[key] = {};\n\t\t}\n\n\t\tcurrent = current[key] as Record<string, unknown>;\n\t}\n\n\t// Set the final value - with proper type checking\n\tconst lastKey = keys[keys.length - 1];\n\tif (lastKey != null) {\n\t\tcurrent[lastKey] = value;\n\t}\n\n\treturn obj;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,SAAgB,cAAcA,MAAgD;AAC7E,KAAI,SAAS,eAAe,SAAS,SACpC,QAAO;AAIR,KACC,gBAAgB,QAChB,gBAAgB,UAChB,gBAAgB,SAChB,gBAAgB,OAChB,gBAAgB,OAChB,gBAAgB,WAChB,gBAAgB,WAChB,MAAM,QAAQ,KAAK,CAEnB,QAAO;CAIR,MAAM,QAAQ,OAAO,eAAe,KAAK;AACzC,QAAO,UAAU,QAAQ,UAAU,OAAO;AAC1C;;;;;;;;;;;AChBD,SAAgB,IACfC,KACAC,MACAC,OACI;CAEJ,MAAMC,OAA4B,MAAM,QAAQ,KAAK,GAClD,KAAK,OAAO,CAAC,QAAQ,QAAQ,MAAM,OAAO,KAAK,GAC/C,KACC,UAAU,CACV,MAAM,IAAI,CACV,OAAO,CAAC,QAAQ,QAAQ,GAAG;AAG/B,KAAI,KAAK,WAAW,EACnB,QAAO;CAGR,IAAIC,UAAmC;AAGvC,MAAK,IAAI,IAAI,GAAG,IAAI,KAAK,SAAS,GAAG,KAAK;EACzC,MAAM,MAAM,KAAK;AAGjB,MAAI,OAAO,KAAM;AAGjB,QAAM,OAAO,aAAa,cAAc,QAAQ,KAAK,CACpD,SAAQ,OAAO,CAAE;AAGlB,YAAU,QAAQ;CAClB;CAGD,MAAM,UAAU,KAAK,KAAK,SAAS;AACnC,KAAI,WAAW,KACd,SAAQ,WAAW;AAGpB,QAAO;AACP"}