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

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
+ {"version":3,"sources":["../src/compat/_internal/isIndex.ts","../src/compat/_internal/toPath.ts","../src/compat/object/set.ts","../src/compat/array/zipObjectDeep.ts"],"sourcesContent":["const IS_UNSIGNED_INTEGER = /^(?:0|[1-9]\\d*)$/\n\nexport function isIndex(value: PropertyKey) {\n switch (typeof value) {\n case 'number': {\n return Number.isInteger(value) && value >= 0 && value < Number.MAX_SAFE_INTEGER;\n }\n case 'symbol': {\n return false;\n }\n case 'string': {\n return IS_UNSIGNED_INTEGER.test(value);\n }\n }\n}","const ESCAPE_REGEXP = /\\\\(\\\\)?/g;\nconst PROPERTY_REGEXP = RegExp(\n // Match anything that isn't a dot or bracket.\n \"[^.[\\\\]]+\" +\n \"|\" +\n // Or match property names within brackets.\n \"\\\\[(?:\" +\n // Match a non-string expression.\n \"([^\\\"'][^[]*)\" +\n \"|\" +\n // Or match strings (supports escaping characters).\n \"([\\\"'])((?:(?!\\\\2)[^\\\\\\\\]|\\\\\\\\.)*?)\\\\2\" +\n \")\\\\]\" +\n \"|\" +\n // Or match \"\" as the space between consecutive dots or empty brackets.\n \"(?=(?:\\\\.|\\\\[\\\\])(?:\\\\.|\\\\[\\\\]|$))\",\n \"g\",\n);\n\n/**\n * Converts a deep key string into an array of path segments.\n *\n * This function takes a string representing a deep key (e.g., 'a.b.c' or 'a[b][c]') and breaks it down into an array of strings, each representing a segment of the path.\n *\n * @param deepKey - The deep key string to convert.\n * @returns An array of strings, each representing a segment of the path.\n *\n * Examples:\n *\n * toPath('a.b.c') // Returns ['a', 'b', 'c']\n * toPath('a[b][c]') // Returns ['a', 'b', 'c']\n * toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']\n * toPath('a[\"b.c\"].d') // Returns ['a', 'b.c', 'd']\n * toPath('') // Returns []\n * toPath('.a[b].c.d[e][\"f.g\"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']\n */\nexport function toPath(deepKey: string): string[] {\n const result: string[] = [];\n\n if (deepKey[0] === \".\") {\n result.push(\"\");\n }\n\n let match: RegExpExecArray | null;\n\n while ((match = PROPERTY_REGEXP.exec(deepKey)) !== null) {\n let [key, expr, quote, substr] = match;\n\n if (quote) {\n key = substr.replace(ESCAPE_REGEXP, \"$1\");\n } else if (expr) {\n key = expr.trim();\n }\n\n result.push(key);\n }\n\n return result;\n}\n","import { isIndex } from \"../_internal/isIndex\";\nimport { toPath } from \"../_internal/toPath\";\n\n/**\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: object, path: PropertyKey | readonly PropertyKey[], value: unknown): T;\nexport function set<T extends object>(obj: T, path: PropertyKey | readonly PropertyKey[], value: unknown): T {\n const resolvedPath = Array.isArray(path)\n ? path\n : typeof path === 'string'\n ? toPath(path)\n : [path];\n\n let current: any = obj;\n\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] = isIndex(nextKey) ? [] : {};\n }\n\n current = current[key];\n }\n\n const lastKey = resolvedPath[resolvedPath.length - 1];\n current[lastKey] = value;\n\n return obj;\n}\n","import { zip } from \"../../array/zip.ts\";\nimport { set } from \"../object/set.ts\";\n\n/**\n * Creates a deeply nested object given arrays of paths and values.\n *\n * This function takes two arrays: one containing arrays of property paths, and the other containing corresponding values.\n * It returns a new object where paths from the first array are used as key paths to set values, with corresponding elements from the second array as values.\n * Paths can be dot-separated strings or arrays of property names.\n * \n * If the `keys` array is longer than the `values` array, the remaining keys will have `undefined` as their values.\n *\n * @template V - The type of elements in the array.\n * @param {string[] | string[][]} keys - An array of property paths, each path can be a dot-separated string or an array of property names.\n * @param {V[]} values - An array of values corresponding to the property paths.\n * @returns {object} A new object composed of the given property paths and values.\n *\n * @example\n * const paths = ['a.b.c', 'd.e.f'];\n * const values = [1, 2];\n * const result = zipObjectDeep(paths, values);\n * // result will be { a: { b: { c: 1 } }, d: { e: { f: 2 } } }\n *\n * @example\n * const paths = [['a', 'b', 'c'], ['d', 'e', 'f']];\n * const values = [1, 2];\n * const result = zipObjectDeep(paths, values);\n * // result will be { a: { b: { c: 1 } }, d: { e: { f: 2 } } }\n * \n * @example\n * const paths = ['a.b[0].c', 'a.b[1].d'];\n * const values = [1, 2];\n * const result = zipObjectDeep(paths, values);\n * // result will be { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }\n */\nexport function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V } {\n const result = {} as { [K in P]: V };\n const zipped = zip(keys, values);\n\n for (let i = 0; i < zipped.length; i++) {\n const [key, value] = zipped[i];\n\n if (key != null) {\n set(result, key, value);\n }\n }\n\n return result;\n}\n"],"mappings":";;;;;AAAA,IAAM,sBAAsB;AAErB,SAAS,QAAQ,OAAoB;AAC1C,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK,UAAU;AACb,aAAO,OAAO,UAAU,KAAK,KAAK,SAAS,KAAK,QAAQ,OAAO;AAAA,IACjE;AAAA,IACA,KAAK,UAAU;AACb,aAAO;AAAA,IACT;AAAA,IACA,KAAK,UAAU;AACb,aAAO,oBAAoB,KAAK,KAAK;AAAA,IACv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OAAO,MAAM;AACf,UAAI,QAAQ,KAAK,KAAK;AAAA,IACxB;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -41,6 +41,7 @@ export { xorBy } from '../array/xorBy.mjs';
41
41
  export { xorWith } from '../array/xorWith.mjs';
42
42
  export { zip } from '../array/zip.mjs';
43
43
  export { zipObject } from '../array/zipObject.mjs';
44
+ export { z as zipObjectDeep } from '../zipObjectDeep-eeZxUsAU.mjs';
44
45
  export { zipWith } from '../array/zipWith.mjs';
45
46
  export { without } from '../array/without.mjs';
46
47
  export { head } from '../array/head.mjs';
@@ -108,4 +109,33 @@ export { capitalize } from '../string/capitalize.mjs';
108
109
  */
109
110
  declare function chunk<T>(arr: readonly T[], size?: number): T[][];
110
111
 
111
- export { chunk };
112
+ /**
113
+ * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.
114
+ *
115
+ * @template T - The type of the object.
116
+ * @param {Settable} obj - The object to modify.
117
+ * @param {Path} path - The path of the property to set.
118
+ * @param {any} value - The value to set.
119
+ * @returns {T} - The modified object.
120
+ *
121
+ * @example
122
+ * // Set a value in a nested object
123
+ * const obj = { a: { b: { c: 3 } } };
124
+ * set(obj, 'a.b.c', 4);
125
+ * console.log(obj.a.b.c); // 4
126
+ *
127
+ * @example
128
+ * // Set a value in an array
129
+ * const arr = [1, 2, 3];
130
+ * set(arr, 1, 4);
131
+ * console.log(arr[1]); // 4
132
+ *
133
+ * @example
134
+ * // Create non-existent path and set value
135
+ * const obj = {};
136
+ * set(obj, 'a.b.c', 4);
137
+ * console.log(obj); // { a: { b: { c: 4 } } }
138
+ */
139
+ declare function set<T>(obj: object, path: PropertyKey | readonly PropertyKey[], value: unknown): T;
140
+
141
+ export { chunk, set };
@@ -41,6 +41,7 @@ export { xorBy } from '../array/xorBy.js';
41
41
  export { xorWith } from '../array/xorWith.js';
42
42
  export { zip } from '../array/zip.js';
43
43
  export { zipObject } from '../array/zipObject.js';
44
+ export { z as zipObjectDeep } from '../zipObjectDeep-eeZxUsAU.js';
44
45
  export { zipWith } from '../array/zipWith.js';
45
46
  export { without } from '../array/without.js';
46
47
  export { head } from '../array/head.js';
@@ -108,4 +109,33 @@ export { capitalize } from '../string/capitalize.js';
108
109
  */
109
110
  declare function chunk<T>(arr: readonly T[], size?: number): T[][];
110
111
 
111
- export { chunk };
112
+ /**
113
+ * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.
114
+ *
115
+ * @template T - The type of the object.
116
+ * @param {Settable} obj - The object to modify.
117
+ * @param {Path} path - The path of the property to set.
118
+ * @param {any} value - The value to set.
119
+ * @returns {T} - The modified object.
120
+ *
121
+ * @example
122
+ * // Set a value in a nested object
123
+ * const obj = { a: { b: { c: 3 } } };
124
+ * set(obj, 'a.b.c', 4);
125
+ * console.log(obj.a.b.c); // 4
126
+ *
127
+ * @example
128
+ * // Set a value in an array
129
+ * const arr = [1, 2, 3];
130
+ * set(arr, 1, 4);
131
+ * console.log(arr[1]); // 4
132
+ *
133
+ * @example
134
+ * // Create non-existent path and set value
135
+ * const obj = {};
136
+ * set(obj, 'a.b.c', 4);
137
+ * console.log(obj); // { a: { b: { c: 4 } } }
138
+ */
139
+ declare function set<T>(obj: object, path: PropertyKey | readonly PropertyKey[], value: unknown): T;
140
+
141
+ export { chunk, set };
@@ -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,
@@ -118,6 +119,7 @@ __export(compat_exports, {
118
119
  xorWith: () => xorWith,
119
120
  zip: () => zip,
120
121
  zipObject: () => zipObject,
122
+ zipObjectDeep: () => zipObjectDeep,
121
123
  zipWith: () => zipWith
122
124
  });
123
125
  module.exports = __toCommonJS(compat_exports);
@@ -580,6 +582,77 @@ function zipObject(keys, values) {
580
582
  return result;
581
583
  }
582
584
 
585
+ // src/compat/_internal/isIndex.ts
586
+ var IS_UNSIGNED_INTEGER = /^(?:0|[1-9]\d*)$/;
587
+ function isIndex(value) {
588
+ switch (typeof value) {
589
+ case "number": {
590
+ return Number.isInteger(value) && value >= 0 && value < Number.MAX_SAFE_INTEGER;
591
+ }
592
+ case "symbol": {
593
+ return false;
594
+ }
595
+ case "string": {
596
+ return IS_UNSIGNED_INTEGER.test(value);
597
+ }
598
+ }
599
+ }
600
+
601
+ // src/compat/_internal/toPath.ts
602
+ var ESCAPE_REGEXP = /\\(\\)?/g;
603
+ var PROPERTY_REGEXP = RegExp(
604
+ // Match anything that isn't a dot or bracket.
605
+ `[^.[\\]]+|\\[(?:([^"'][^[]*)|(["'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2)\\]|(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))`,
606
+ "g"
607
+ );
608
+ function toPath(deepKey) {
609
+ const result = [];
610
+ if (deepKey[0] === ".") {
611
+ result.push("");
612
+ }
613
+ let match;
614
+ while ((match = PROPERTY_REGEXP.exec(deepKey)) !== null) {
615
+ let [key, expr, quote, substr] = match;
616
+ if (quote) {
617
+ key = substr.replace(ESCAPE_REGEXP, "$1");
618
+ } else if (expr) {
619
+ key = expr.trim();
620
+ }
621
+ result.push(key);
622
+ }
623
+ return result;
624
+ }
625
+
626
+ // src/compat/object/set.ts
627
+ function set(obj, path, value) {
628
+ const resolvedPath = Array.isArray(path) ? path : typeof path === "string" ? toPath(path) : [path];
629
+ let current = obj;
630
+ for (let i = 0; i < resolvedPath.length - 1; i++) {
631
+ const key = resolvedPath[i];
632
+ const nextKey = resolvedPath[i + 1];
633
+ if (current[key] == null) {
634
+ current[key] = isIndex(nextKey) ? [] : {};
635
+ }
636
+ current = current[key];
637
+ }
638
+ const lastKey = resolvedPath[resolvedPath.length - 1];
639
+ current[lastKey] = value;
640
+ return obj;
641
+ }
642
+
643
+ // src/compat/array/zipObjectDeep.ts
644
+ function zipObjectDeep(keys, values) {
645
+ const result = {};
646
+ const zipped = zip(keys, values);
647
+ for (let i = 0; i < zipped.length; i++) {
648
+ const [key, value] = zipped[i];
649
+ if (key != null) {
650
+ set(result, key, value);
651
+ }
652
+ }
653
+ return result;
654
+ }
655
+
583
656
  // src/array/zipWith.ts
584
657
  function zipWith(arr1, ...rest) {
585
658
  const arrs = [arr1, ...rest.slice(0, -1)];
@@ -1077,6 +1150,7 @@ function chunk2(arr, size = 1) {
1077
1150
  round,
1078
1151
  sample,
1079
1152
  sampleSize,
1153
+ set,
1080
1154
  shuffle,
1081
1155
  snakeCase,
1082
1156
  startCase,
@@ -1103,6 +1177,7 @@ function chunk2(arr, size = 1) {
1103
1177
  xorWith,
1104
1178
  zip,
1105
1179
  zipObject,
1180
+ zipObjectDeep,
1106
1181
  zipWith
1107
1182
  });
1108
1183
  //# sourceMappingURL=index.js.map