es-toolkit 1.10.1-dev.230 → 1.10.1-dev.231

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-54TK5BIS.mjs.map
@@ -0,0 +1,25 @@
1
+ // src/object/set.ts
2
+ function set(obj, path, value) {
3
+ if (obj instanceof Map || obj instanceof Set) {
4
+ throw new TypeError("Set or Map is not supported");
5
+ }
6
+ const keys = Array.isArray(path) ? path : String(path).replace(/\[|\]/g, (match) => {
7
+ return match === "[" ? "." : "";
8
+ }).split(".");
9
+ let pointer = obj;
10
+ for (let i = 0; i < keys.length - 1; i++) {
11
+ const key = keys[i];
12
+ const nextKey = keys[i + 1];
13
+ if (pointer[key] == null || typeof pointer[key] !== "object") {
14
+ pointer[key] = /^\d+$/.test(nextKey) ? [] : {};
15
+ }
16
+ pointer = pointer[key];
17
+ }
18
+ pointer[keys[keys.length - 1]] = value;
19
+ return obj;
20
+ }
21
+
22
+ export {
23
+ set
24
+ };
25
+ //# sourceMappingURL=chunk-GL7PPRA4.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/object/set.ts"],"sourcesContent":["/**\n * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.\n *\n * @template T - The type of the object.\n * @param {Settable} obj - The object to modify.\n * @param {Path} path - The path of the property to set.\n * @param {any} value - The value to set.\n * @returns {T} - The modified object.\n *\n * @example\n * // Set a value in a nested object\n * const obj = { a: { b: { c: 3 } } };\n * set(obj, 'a.b.c', 4);\n * console.log(obj.a.b.c); // 4\n *\n * @example\n * // Set a value in an array\n * const arr = [1, 2, 3];\n * set(arr, 1, 4);\n * console.log(arr[1]); // 4\n *\n * @example\n * // Create non-existent path and set value\n * const obj = {};\n * set(obj, 'a.b.c', 4);\n * console.log(obj); // { a: { b: { c: 4 } } }\n */\nexport function set<T>(obj: Settable, path: Path, value: any): T {\n if (obj instanceof Map || obj instanceof Set) {\n throw new TypeError('Set or Map is not supported');\n }\n //TODO: memoize\n const keys = Array.isArray(path)\n ? path\n : String(path as string)\n .replace(/\\[|\\]/g, match => {\n return match === '[' ? '.' : '';\n })\n .split('.');\n\n let pointer: any = obj;\n\n for (let i = 0; i < keys.length - 1; i++) {\n const key = keys[i];\n const nextKey = keys[i + 1];\n if (pointer[key] == null || typeof pointer[key] !== 'object') {\n pointer[key] = /^\\d+$/.test(nextKey as string) ? [] : {};\n }\n pointer = pointer[key];\n }\n\n pointer[keys[keys.length - 1]] = value;\n return obj as T;\n}\n\ntype Settable = object | any[];\ntype Path = string | number | Array<string | number>;\n"],"mappings":";AA2BO,SAAS,IAAO,KAAe,MAAY,OAAe;AAC/D,MAAI,eAAe,OAAO,eAAe,KAAK;AAC5C,UAAM,IAAI,UAAU,6BAA6B;AAAA,EACnD;AAEA,QAAM,OAAO,MAAM,QAAQ,IAAI,IAC3B,OACA,OAAO,IAAc,EAClB,QAAQ,UAAU,WAAS;AAC1B,WAAO,UAAU,MAAM,MAAM;AAAA,EAC/B,CAAC,EACA,MAAM,GAAG;AAEhB,MAAI,UAAe;AAEnB,WAAS,IAAI,GAAG,IAAI,KAAK,SAAS,GAAG,KAAK;AACxC,UAAM,MAAM,KAAK,CAAC;AAClB,UAAM,UAAU,KAAK,IAAI,CAAC;AAC1B,QAAI,QAAQ,GAAG,KAAK,QAAQ,OAAO,QAAQ,GAAG,MAAM,UAAU;AAC5D,cAAQ,GAAG,IAAI,QAAQ,KAAK,OAAiB,IAAI,CAAC,IAAI,CAAC;AAAA,IACzD;AACA,cAAU,QAAQ,GAAG;AAAA,EACvB;AAEA,UAAQ,KAAK,KAAK,SAAS,CAAC,CAAC,IAAI;AACjC,SAAO;AACT;","names":[]}
@@ -70,6 +70,7 @@ export { pick } from '../object/pick.mjs';
70
70
  export { pickBy } from '../object/pickBy.mjs';
71
71
  export { invert } from '../object/invert.mjs';
72
72
  export { clone } from '../object/clone.mjs';
73
+ export { set } from '../object/set.mjs';
73
74
  export { isEqual } from '../predicate/isEqual.mjs';
74
75
  export { isNil } from '../predicate/isNil.mjs';
75
76
  export { isNotNil } from '../predicate/isNotNil.mjs';
@@ -70,6 +70,7 @@ export { pick } from '../object/pick.js';
70
70
  export { pickBy } from '../object/pickBy.js';
71
71
  export { invert } from '../object/invert.js';
72
72
  export { clone } from '../object/clone.js';
73
+ export { set } from '../object/set.js';
73
74
  export { isEqual } from '../predicate/isEqual.js';
74
75
  export { isNil } from '../predicate/isNil.js';
75
76
  export { isNotNil } from '../predicate/isNotNil.js';
@@ -92,6 +92,7 @@ __export(compat_exports, {
92
92
  round: () => round,
93
93
  sample: () => sample,
94
94
  sampleSize: () => sampleSize,
95
+ set: () => set,
95
96
  shuffle: () => shuffle,
96
97
  snakeCase: () => snakeCase,
97
98
  startCase: () => startCase,
@@ -881,6 +882,27 @@ function isPrimitive(value) {
881
882
  return value == null || typeof value !== "object" && typeof value !== "function";
882
883
  }
883
884
 
885
+ // src/object/set.ts
886
+ function set(obj, path, value) {
887
+ if (obj instanceof Map || obj instanceof Set) {
888
+ throw new TypeError("Set or Map is not supported");
889
+ }
890
+ const keys = Array.isArray(path) ? path : String(path).replace(/\[|\]/g, (match) => {
891
+ return match === "[" ? "." : "";
892
+ }).split(".");
893
+ let pointer = obj;
894
+ for (let i = 0; i < keys.length - 1; i++) {
895
+ const key = keys[i];
896
+ const nextKey = keys[i + 1];
897
+ if (pointer[key] == null || typeof pointer[key] !== "object") {
898
+ pointer[key] = /^\d+$/.test(nextKey) ? [] : {};
899
+ }
900
+ pointer = pointer[key];
901
+ }
902
+ pointer[keys[keys.length - 1]] = value;
903
+ return obj;
904
+ }
905
+
884
906
  // src/predicate/isEqual.ts
885
907
  function isEqual(a, b) {
886
908
  if (Object.is(a, b)) {
@@ -1077,6 +1099,7 @@ function chunk2(arr, size = 1) {
1077
1099
  round,
1078
1100
  sample,
1079
1101
  sampleSize,
1102
+ set,
1080
1103
  shuffle,
1081
1104
  snakeCase,
1082
1105
  startCase,