es-toolkit 1.10.1-dev.231 → 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.
package/dist/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  import "./chunk-NKLIE2OI.mjs";
2
- import "./chunk-ARZ5BDR6.mjs";
3
2
  import "./chunk-XT74R35S.mjs";
4
3
  import {
5
4
  camelCase
@@ -36,13 +35,11 @@ import {
36
35
  import {
37
36
  isUndefined
38
37
  } from "./chunk-VH2PV426.mjs";
38
+ import "./chunk-ARZ5BDR6.mjs";
39
39
  import {
40
40
  delay
41
41
  } from "./chunk-O64NE2P4.mjs";
42
- import "./chunk-54TK5BIS.mjs";
43
- import {
44
- set
45
- } from "./chunk-GL7PPRA4.mjs";
42
+ import "./chunk-OSOLEQLZ.mjs";
46
43
  import {
47
44
  clone
48
45
  } from "./chunk-U4JGKZ56.mjs";
@@ -86,7 +83,9 @@ import {
86
83
  import {
87
84
  inRange
88
85
  } from "./chunk-BH6HLGBA.mjs";
89
- import "./chunk-DDH7K4QM.mjs";
86
+ import {
87
+ zipObjectDeep
88
+ } from "./chunk-MKCRW4QT.mjs";
90
89
  import {
91
90
  xor
92
91
  } from "./chunk-L7FQX5MN.mjs";
@@ -326,7 +325,6 @@ export {
326
325
  round,
327
326
  sample,
328
327
  sampleSize,
329
- set,
330
328
  shuffle,
331
329
  snakeCase,
332
330
  startCase,
@@ -353,6 +351,7 @@ export {
353
351
  xorWith,
354
352
  zip,
355
353
  zipObject,
354
+ zipObjectDeep,
356
355
  zipWith
357
356
  };
358
357
  //# sourceMappingURL=index.mjs.map
@@ -4,4 +4,3 @@ export { pick } from './pick.mjs';
4
4
  export { pickBy } from './pickBy.mjs';
5
5
  export { invert } from './invert.mjs';
6
6
  export { clone } from './clone.mjs';
7
- export { set } from './set.mjs';
@@ -4,4 +4,3 @@ export { pick } from './pick.js';
4
4
  export { pickBy } from './pickBy.js';
5
5
  export { invert } from './invert.js';
6
6
  export { clone } from './clone.js';
7
- export { set } from './set.js';
@@ -39,8 +39,7 @@ __export(object_exports, {
39
39
  omit: () => omit,
40
40
  omitBy: () => omitBy,
41
41
  pick: () => pick,
42
- pickBy: () => pickBy,
43
- set: () => set
42
+ pickBy: () => pickBy
44
43
  });
45
44
  module.exports = __toCommonJS(object_exports);
46
45
 
@@ -134,27 +133,6 @@ function clone(obj) {
134
133
  function isPrimitive(value) {
135
134
  return value == null || typeof value !== "object" && typeof value !== "function";
136
135
  }
137
-
138
- // src/object/set.ts
139
- function set(obj, path, value) {
140
- if (obj instanceof Map || obj instanceof Set) {
141
- throw new TypeError("Set or Map is not supported");
142
- }
143
- const keys = Array.isArray(path) ? path : String(path).replace(/\[|\]/g, (match) => {
144
- return match === "[" ? "." : "";
145
- }).split(".");
146
- let pointer = obj;
147
- for (let i = 0; i < keys.length - 1; i++) {
148
- const key = keys[i];
149
- const nextKey = keys[i + 1];
150
- if (pointer[key] == null || typeof pointer[key] !== "object") {
151
- pointer[key] = /^\d+$/.test(nextKey) ? [] : {};
152
- }
153
- pointer = pointer[key];
154
- }
155
- pointer[keys[keys.length - 1]] = value;
156
- return obj;
157
- }
158
136
  // Annotate the CommonJS export names for ESM import in node:
159
137
  0 && (module.exports = {
160
138
  clone,
@@ -162,7 +140,6 @@ function set(obj, path, value) {
162
140
  omit,
163
141
  omitBy,
164
142
  pick,
165
- pickBy,
166
- set
143
+ pickBy
167
144
  });
168
145
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/object/index.ts","../../src/object/omit.ts","../../src/object/omitBy.ts","../../src/object/pick.ts","../../src/object/pickBy.ts","../../src/object/invert.ts","../../src/object/clone.ts","../../src/object/set.ts"],"sourcesContent":["export { omit } from './omit.ts';\nexport { omitBy } from './omitBy.ts';\nexport { pick } from './pick.ts';\nexport { pickBy } from './pickBy.ts';\nexport { invert } from './invert.ts';\nexport { clone } from './clone.ts';\nexport { set } from './set.ts';\n","/**\n * Creates a new object with specified keys omitted.\n *\n * This function takes an object and an array of keys, and returns a new object that\n * excludes the properties corresponding to the specified keys.\n *\n * @template T - The type of object.\n * @template K - The type of keys in object.\n * @param {T} obj - The object to omit keys from.\n * @param {K[]} keys - An array of keys to be omitted from the object.\n * @returns {Omit<T, K>} A new object with the specified keys omitted.\n *\n * @example\n * const obj = { a: 1, b: 2, c: 3 };\n * const result = omit(obj, ['b', 'c']);\n * // result will be { a: 1 }\n */\nexport function omit<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {\n const result = { ...obj };\n\n for (const key of keys) {\n delete result[key];\n }\n\n return result as Omit<T, K>;\n}\n","/**\n * Creates a new object composed of the properties that do not satisfy the predicate function.\n *\n * This function takes an object and a predicate function, and returns a new object that\n * includes only the properties for which the predicate function returns false.\n *\n * @template T - The type of object.\n * @param {T} obj - The object to omit properties from.\n * @param {(value: T[string], key: keyof T) => boolean} shouldOmit - A predicate function that determines\n * whether a property should be omitted. It takes the property's key and value as arguments and returns `true`\n * if the property should be omitted, and `false` otherwise.\n * @returns {Partial<T>} A new object with the properties that do not satisfy the predicate function.\n *\n * @example\n * const obj = { a: 1, b: 'omit', c: 3 };\n * const shouldOmit = (key, value) => typeof value === 'string';\n * const result = omitBy(obj, shouldOmit);\n * // result will be { a: 1, c: 3 }\n */\nexport function omitBy<T extends Record<string, any>>(\n obj: T,\n shouldOmit: (value: T[keyof T], key: keyof T) => boolean\n): Partial<T> {\n const result: Partial<T> = {};\n\n for (const [key, value] of Object.entries(obj)) {\n if (shouldOmit(value, key)) {\n continue;\n }\n\n (result as any)[key] = value;\n }\n\n return result;\n}\n","/**\n * Creates a new object composed of the picked object properties.\n *\n * This function takes an object and an array of keys, and returns a new object that\n * includes only the properties corresponding to the specified keys.\n *\n * @template T - The type of object.\n * @template K - The type of keys in object.\n * @param {T} obj - The object to pick keys from.\n * @param {K[]} keys - An array of keys to be picked from the object.\n * @returns {Pick<T, K>} A new object with the specified keys picked.\n *\n * @example\n * const obj = { a: 1, b: 2, c: 3 };\n * const result = pick(obj, ['a', 'c']);\n * // result will be { a: 1, c: 3 }\n */\nexport function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {\n const result = {} as Pick<T, K>;\n\n for (const key of keys) {\n result[key] = obj[key];\n }\n\n return result;\n}\n","/**\n * Creates a new object composed of the properties that satisfy the predicate function.\n *\n * This function takes an object and a predicate function, and returns a new object that\n * includes only the properties for which the predicate function returns true.\n *\n * @template T - The type of object.\n * @param {T} obj - The object to pick properties from.\n * @param {(value: T[keyof T], key: keyof T) => boolean} shouldPick - A predicate function that determines\n * whether a property should be picked. It takes the property's key and value as arguments and returns `true`\n * if the property should be picked, and `false` otherwise.\n * @returns {Partial<T>} A new object with the properties that satisfy the predicate function.\n *\n * @example\n * const obj = { a: 1, b: 'pick', c: 3 };\n * const shouldPick = (value) => typeof value === 'string';\n * const result = pickBy(obj, shouldPick);\n * // result will be { b: 'pick' }\n */\nexport function pickBy<T extends Record<string, any>>(\n obj: T,\n shouldPick: (value: T[keyof T], key: keyof T) => boolean\n): Partial<T> {\n const result: Partial<T> = {};\n\n for (const [key, value] of Object.entries(obj)) {\n if (!shouldPick(value, key)) {\n continue;\n }\n\n (result as any)[key] = value;\n }\n\n return result;\n}\n","/**\n * Inverts the keys and values of an object. The keys of the input object become the values of the output object and vice versa.\n *\n * This function takes an object and creates a new object by inverting its keys and values. If the input object has duplicate values,\n * the key of the last occurrence will be used as the value for the new key in the output object. It effectively creates a reverse mapping\n * of the input object's key-value pairs.\n *\n * @template K - Type of the keys in the input object (string, number, symbol)\n * @template V - Type of the values in the input object (string, number, symbol)\n * @param {Record<K, V>} obj - The input object whose keys and values are to be inverted\n * @returns {{ [key in V]: K }} - A new object with keys and values inverted\n *\n * @example\n * invert({ a: 1, b: 2, c: 3 }); // { 1: 'a', 2: 'b', 3: 'c' }\n * invert({ 1: 'a', 2: 'b', 3: 'c' }); // { a: '1', b: '2', c: '3' }\n * invert({ a: 1, 2: 'b', c: 3, 4: 'd' }); // { 1: 'a', b: '2', 3: 'c', d: '4' }\n * invert({ a: Symbol('sym1'), b: Symbol('sym2') }); // { [Symbol('sym1')]: 'a', [Symbol('sym2')]: 'b' }\n */\nexport function invert<K extends PropertyKey, V extends PropertyKey>(obj: Record<K, V>): { [key in V]: K } {\n const result = {} as { [key in V]: K };\n\n const keys = Object.keys(obj) as K[];\n \n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n const value = obj[key];\n result[value] = key;\n }\n\n return result;\n}\n","/**\n * Creates a shallow clone of the given object.\n *\n * @template T - The type of the object.\n * @param {T} obj - The object to clone.\n * @returns {T} - A shallow clone of the given object.\n *\n * @example\n * // Clone a primitive values\n * const num = 29;\n * const clonedNum = clone(num);\n * console.log(clonedNum); // 29\n * console.log(clonedNum === num) ; // true\n *\n * @example\n * // Clone an array\n * const arr = [1, 2, 3];\n * const clonedArr = clone(arr);\n * console.log(clonedArr); // [1, 2, 3]\n * console.log(clonedArr === arr); // false\n *\n * @example\n * // Clone an object\n * const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] };\n * const clonedObj = clone(obj);\n * console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] }\n * console.log(clonedObj === obj); // false\n */\nexport function clone<T>(obj: T): T {\n if (isPrimitive(obj)) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.slice() as T;\n }\n\n if (obj instanceof Date) {\n return new Date(obj.getTime()) as T;\n }\n\n if (obj instanceof RegExp) {\n return new RegExp(obj.source, obj.flags) as T;\n }\n\n if (obj instanceof Map) {\n const result = new Map();\n for (const [key, value] of obj) {\n result.set(key, value);\n }\n return result as T;\n }\n\n if (obj instanceof Set) {\n const result = new Set();\n for (const value of obj) {\n result.add(value);\n }\n return result as T;\n }\n\n if (typeof obj === \"object\") {\n return Object.assign({}, obj) as T;\n }\n return obj;\n}\n\ntype Primitive = null | undefined | string | number | boolean | symbol | bigint;\n\nfunction isPrimitive(value: unknown): value is Primitive {\n return value == null ||\n (typeof value !== \"object\" && typeof value !== \"function\");\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: 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":"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","names":[]}
1
+ {"version":3,"sources":["../../src/object/index.ts","../../src/object/omit.ts","../../src/object/omitBy.ts","../../src/object/pick.ts","../../src/object/pickBy.ts","../../src/object/invert.ts","../../src/object/clone.ts"],"sourcesContent":["export { omit } from './omit.ts';\nexport { omitBy } from './omitBy.ts';\nexport { pick } from './pick.ts';\nexport { pickBy } from './pickBy.ts';\nexport { invert } from './invert.ts';\nexport { clone } from './clone.ts';\n","/**\n * Creates a new object with specified keys omitted.\n *\n * This function takes an object and an array of keys, and returns a new object that\n * excludes the properties corresponding to the specified keys.\n *\n * @template T - The type of object.\n * @template K - The type of keys in object.\n * @param {T} obj - The object to omit keys from.\n * @param {K[]} keys - An array of keys to be omitted from the object.\n * @returns {Omit<T, K>} A new object with the specified keys omitted.\n *\n * @example\n * const obj = { a: 1, b: 2, c: 3 };\n * const result = omit(obj, ['b', 'c']);\n * // result will be { a: 1 }\n */\nexport function omit<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {\n const result = { ...obj };\n\n for (const key of keys) {\n delete result[key];\n }\n\n return result as Omit<T, K>;\n}\n","/**\n * Creates a new object composed of the properties that do not satisfy the predicate function.\n *\n * This function takes an object and a predicate function, and returns a new object that\n * includes only the properties for which the predicate function returns false.\n *\n * @template T - The type of object.\n * @param {T} obj - The object to omit properties from.\n * @param {(value: T[string], key: keyof T) => boolean} shouldOmit - A predicate function that determines\n * whether a property should be omitted. It takes the property's key and value as arguments and returns `true`\n * if the property should be omitted, and `false` otherwise.\n * @returns {Partial<T>} A new object with the properties that do not satisfy the predicate function.\n *\n * @example\n * const obj = { a: 1, b: 'omit', c: 3 };\n * const shouldOmit = (key, value) => typeof value === 'string';\n * const result = omitBy(obj, shouldOmit);\n * // result will be { a: 1, c: 3 }\n */\nexport function omitBy<T extends Record<string, any>>(\n obj: T,\n shouldOmit: (value: T[keyof T], key: keyof T) => boolean\n): Partial<T> {\n const result: Partial<T> = {};\n\n for (const [key, value] of Object.entries(obj)) {\n if (shouldOmit(value, key)) {\n continue;\n }\n\n (result as any)[key] = value;\n }\n\n return result;\n}\n","/**\n * Creates a new object composed of the picked object properties.\n *\n * This function takes an object and an array of keys, and returns a new object that\n * includes only the properties corresponding to the specified keys.\n *\n * @template T - The type of object.\n * @template K - The type of keys in object.\n * @param {T} obj - The object to pick keys from.\n * @param {K[]} keys - An array of keys to be picked from the object.\n * @returns {Pick<T, K>} A new object with the specified keys picked.\n *\n * @example\n * const obj = { a: 1, b: 2, c: 3 };\n * const result = pick(obj, ['a', 'c']);\n * // result will be { a: 1, c: 3 }\n */\nexport function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {\n const result = {} as Pick<T, K>;\n\n for (const key of keys) {\n result[key] = obj[key];\n }\n\n return result;\n}\n","/**\n * Creates a new object composed of the properties that satisfy the predicate function.\n *\n * This function takes an object and a predicate function, and returns a new object that\n * includes only the properties for which the predicate function returns true.\n *\n * @template T - The type of object.\n * @param {T} obj - The object to pick properties from.\n * @param {(value: T[keyof T], key: keyof T) => boolean} shouldPick - A predicate function that determines\n * whether a property should be picked. It takes the property's key and value as arguments and returns `true`\n * if the property should be picked, and `false` otherwise.\n * @returns {Partial<T>} A new object with the properties that satisfy the predicate function.\n *\n * @example\n * const obj = { a: 1, b: 'pick', c: 3 };\n * const shouldPick = (value) => typeof value === 'string';\n * const result = pickBy(obj, shouldPick);\n * // result will be { b: 'pick' }\n */\nexport function pickBy<T extends Record<string, any>>(\n obj: T,\n shouldPick: (value: T[keyof T], key: keyof T) => boolean\n): Partial<T> {\n const result: Partial<T> = {};\n\n for (const [key, value] of Object.entries(obj)) {\n if (!shouldPick(value, key)) {\n continue;\n }\n\n (result as any)[key] = value;\n }\n\n return result;\n}\n","/**\n * Inverts the keys and values of an object. The keys of the input object become the values of the output object and vice versa.\n *\n * This function takes an object and creates a new object by inverting its keys and values. If the input object has duplicate values,\n * the key of the last occurrence will be used as the value for the new key in the output object. It effectively creates a reverse mapping\n * of the input object's key-value pairs.\n *\n * @template K - Type of the keys in the input object (string, number, symbol)\n * @template V - Type of the values in the input object (string, number, symbol)\n * @param {Record<K, V>} obj - The input object whose keys and values are to be inverted\n * @returns {{ [key in V]: K }} - A new object with keys and values inverted\n *\n * @example\n * invert({ a: 1, b: 2, c: 3 }); // { 1: 'a', 2: 'b', 3: 'c' }\n * invert({ 1: 'a', 2: 'b', 3: 'c' }); // { a: '1', b: '2', c: '3' }\n * invert({ a: 1, 2: 'b', c: 3, 4: 'd' }); // { 1: 'a', b: '2', 3: 'c', d: '4' }\n * invert({ a: Symbol('sym1'), b: Symbol('sym2') }); // { [Symbol('sym1')]: 'a', [Symbol('sym2')]: 'b' }\n */\nexport function invert<K extends PropertyKey, V extends PropertyKey>(obj: Record<K, V>): { [key in V]: K } {\n const result = {} as { [key in V]: K };\n\n const keys = Object.keys(obj) as K[];\n \n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n const value = obj[key];\n result[value] = key;\n }\n\n return result;\n}\n","/**\n * Creates a shallow clone of the given object.\n *\n * @template T - The type of the object.\n * @param {T} obj - The object to clone.\n * @returns {T} - A shallow clone of the given object.\n *\n * @example\n * // Clone a primitive values\n * const num = 29;\n * const clonedNum = clone(num);\n * console.log(clonedNum); // 29\n * console.log(clonedNum === num) ; // true\n *\n * @example\n * // Clone an array\n * const arr = [1, 2, 3];\n * const clonedArr = clone(arr);\n * console.log(clonedArr); // [1, 2, 3]\n * console.log(clonedArr === arr); // false\n *\n * @example\n * // Clone an object\n * const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] };\n * const clonedObj = clone(obj);\n * console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] }\n * console.log(clonedObj === obj); // false\n */\nexport function clone<T>(obj: T): T {\n if (isPrimitive(obj)) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.slice() as T;\n }\n\n if (obj instanceof Date) {\n return new Date(obj.getTime()) as T;\n }\n\n if (obj instanceof RegExp) {\n return new RegExp(obj.source, obj.flags) as T;\n }\n\n if (obj instanceof Map) {\n const result = new Map();\n for (const [key, value] of obj) {\n result.set(key, value);\n }\n return result as T;\n }\n\n if (obj instanceof Set) {\n const result = new Set();\n for (const value of obj) {\n result.add(value);\n }\n return result as T;\n }\n\n if (typeof obj === \"object\") {\n return Object.assign({}, obj) as T;\n }\n return obj;\n}\n\ntype Primitive = null | undefined | string | number | boolean | symbol | bigint;\n\nfunction isPrimitive(value: unknown): value is Primitive {\n return value == null ||\n (typeof value !== \"object\" && typeof value !== \"function\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACiBO,SAAS,KAAuD,KAAQ,MAAuB;AACpG,QAAM,SAAS,mBAAK;AAEpB,aAAW,OAAO,MAAM;AACtB,WAAO,OAAO,GAAG;AAAA,EACnB;AAEA,SAAO;AACT;;;ACNO,SAAS,OACd,KACA,YACY;AACZ,QAAM,SAAqB,CAAC;AAE5B,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC9C,QAAI,WAAW,OAAO,GAAG,GAAG;AAC1B;AAAA,IACF;AAEA,IAAC,OAAe,GAAG,IAAI;AAAA,EACzB;AAEA,SAAO;AACT;;;ACjBO,SAAS,KAAuD,KAAQ,MAAuB;AACpG,QAAM,SAAS,CAAC;AAEhB,aAAW,OAAO,MAAM;AACtB,WAAO,GAAG,IAAI,IAAI,GAAG;AAAA,EACvB;AAEA,SAAO;AACT;;;ACNO,SAAS,OACd,KACA,YACY;AACZ,QAAM,SAAqB,CAAC;AAE5B,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC9C,QAAI,CAAC,WAAW,OAAO,GAAG,GAAG;AAC3B;AAAA,IACF;AAEA,IAAC,OAAe,GAAG,IAAI;AAAA,EACzB;AAEA,SAAO;AACT;;;AChBO,SAAS,OAAqD,KAAsC;AACzG,QAAM,SAAS,CAAC;AAEhB,QAAM,OAAO,OAAO,KAAK,GAAG;AAE5B,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,UAAM,MAAM,KAAK,CAAC;AAClB,UAAM,QAAQ,IAAI,GAAG;AACrB,WAAO,KAAK,IAAI;AAAA,EAClB;AAEA,SAAO;AACT;;;ACFO,SAAS,MAAS,KAAW;AAClC,MAAI,YAAY,GAAG,GAAG;AACpB,WAAO;AAAA,EACT;AAEA,MAAI,MAAM,QAAQ,GAAG,GAAG;AACtB,WAAO,IAAI,MAAM;AAAA,EACnB;AAEA,MAAI,eAAe,MAAM;AACvB,WAAO,IAAI,KAAK,IAAI,QAAQ,CAAC;AAAA,EAC/B;AAEA,MAAI,eAAe,QAAQ;AACzB,WAAO,IAAI,OAAO,IAAI,QAAQ,IAAI,KAAK;AAAA,EACzC;AAEA,MAAI,eAAe,KAAK;AACtB,UAAM,SAAS,oBAAI,IAAI;AACvB,eAAW,CAAC,KAAK,KAAK,KAAK,KAAK;AAC9B,aAAO,IAAI,KAAK,KAAK;AAAA,IACvB;AACA,WAAO;AAAA,EACT;AAEA,MAAI,eAAe,KAAK;AACtB,UAAM,SAAS,oBAAI,IAAI;AACvB,eAAW,SAAS,KAAK;AACvB,aAAO,IAAI,KAAK;AAAA,IAClB;AACA,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,QAAQ,UAAU;AAC3B,WAAO,OAAO,OAAO,CAAC,GAAG,GAAG;AAAA,EAC9B;AACA,SAAO;AACT;AAIA,SAAS,YAAY,OAAoC;AACvD,SAAO,SAAS,QACb,OAAO,UAAU,YAAY,OAAO,UAAU;AACnD;","names":[]}
@@ -1,7 +1,4 @@
1
- import "../chunk-54TK5BIS.mjs";
2
- import {
3
- set
4
- } from "../chunk-GL7PPRA4.mjs";
1
+ import "../chunk-OSOLEQLZ.mjs";
5
2
  import {
6
3
  clone
7
4
  } from "../chunk-U4JGKZ56.mjs";
@@ -27,7 +24,6 @@ export {
27
24
  omit,
28
25
  omitBy,
29
26
  pick,
30
- pickBy,
31
- set
27
+ pickBy
32
28
  };
33
29
  //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Creates a deeply nested object given arrays of paths and values.
3
+ *
4
+ * This function takes two arrays: one containing arrays of property paths, and the other containing corresponding values.
5
+ * 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.
6
+ * Paths can be dot-separated strings or arrays of property names.
7
+ *
8
+ * If the `keys` array is longer than the `values` array, the remaining keys will have `undefined` as their values.
9
+ *
10
+ * @template V - The type of elements in the array.
11
+ * @param {string[] | string[][]} keys - An array of property paths, each path can be a dot-separated string or an array of property names.
12
+ * @param {V[]} values - An array of values corresponding to the property paths.
13
+ * @returns {object} A new object composed of the given property paths and values.
14
+ *
15
+ * @example
16
+ * const paths = ['a.b.c', 'd.e.f'];
17
+ * const values = [1, 2];
18
+ * const result = zipObjectDeep(paths, values);
19
+ * // result will be { a: { b: { c: 1 } }, d: { e: { f: 2 } } }
20
+ *
21
+ * @example
22
+ * const paths = [['a', 'b', 'c'], ['d', 'e', 'f']];
23
+ * const values = [1, 2];
24
+ * const result = zipObjectDeep(paths, values);
25
+ * // result will be { a: { b: { c: 1 } }, d: { e: { f: 2 } } }
26
+ *
27
+ * @example
28
+ * const paths = ['a.b[0].c', 'a.b[1].d'];
29
+ * const values = [1, 2];
30
+ * const result = zipObjectDeep(paths, values);
31
+ * // result will be { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
32
+ */
33
+ declare function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): {
34
+ [K in P]: V;
35
+ };
36
+
37
+ export { zipObjectDeep as z };
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Creates a deeply nested object given arrays of paths and values.
3
+ *
4
+ * This function takes two arrays: one containing arrays of property paths, and the other containing corresponding values.
5
+ * 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.
6
+ * Paths can be dot-separated strings or arrays of property names.
7
+ *
8
+ * If the `keys` array is longer than the `values` array, the remaining keys will have `undefined` as their values.
9
+ *
10
+ * @template V - The type of elements in the array.
11
+ * @param {string[] | string[][]} keys - An array of property paths, each path can be a dot-separated string or an array of property names.
12
+ * @param {V[]} values - An array of values corresponding to the property paths.
13
+ * @returns {object} A new object composed of the given property paths and values.
14
+ *
15
+ * @example
16
+ * const paths = ['a.b.c', 'd.e.f'];
17
+ * const values = [1, 2];
18
+ * const result = zipObjectDeep(paths, values);
19
+ * // result will be { a: { b: { c: 1 } }, d: { e: { f: 2 } } }
20
+ *
21
+ * @example
22
+ * const paths = [['a', 'b', 'c'], ['d', 'e', 'f']];
23
+ * const values = [1, 2];
24
+ * const result = zipObjectDeep(paths, values);
25
+ * // result will be { a: { b: { c: 1 } }, d: { e: { f: 2 } } }
26
+ *
27
+ * @example
28
+ * const paths = ['a.b[0].c', 'a.b[1].d'];
29
+ * const values = [1, 2];
30
+ * const result = zipObjectDeep(paths, values);
31
+ * // result will be { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
32
+ */
33
+ declare function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): {
34
+ [K in P]: V;
35
+ };
36
+
37
+ export { zipObjectDeep as z };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.10.1-dev.231+b9432e1c",
4
+ "version": "1.10.1-dev.237+ff4b97ab",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-54TK5BIS.mjs.map
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-DDH7K4QM.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,25 +0,0 @@
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
@@ -1 +0,0 @@
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":[]}
@@ -1,32 +0,0 @@
1
- /**
2
- * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.
3
- *
4
- * @template T - The type of the object.
5
- * @param {Settable} obj - The object to modify.
6
- * @param {Path} path - The path of the property to set.
7
- * @param {any} value - The value to set.
8
- * @returns {T} - The modified object.
9
- *
10
- * @example
11
- * // Set a value in a nested object
12
- * const obj = { a: { b: { c: 3 } } };
13
- * set(obj, 'a.b.c', 4);
14
- * console.log(obj.a.b.c); // 4
15
- *
16
- * @example
17
- * // Set a value in an array
18
- * const arr = [1, 2, 3];
19
- * set(arr, 1, 4);
20
- * console.log(arr[1]); // 4
21
- *
22
- * @example
23
- * // Create non-existent path and set value
24
- * const obj = {};
25
- * set(obj, 'a.b.c', 4);
26
- * console.log(obj); // { a: { b: { c: 4 } } }
27
- */
28
- declare function set<T>(obj: Settable, path: Path, value: any): T;
29
- type Settable = object | any[];
30
- type Path = string | number | Array<string | number>;
31
-
32
- export { set };
@@ -1,32 +0,0 @@
1
- /**
2
- * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.
3
- *
4
- * @template T - The type of the object.
5
- * @param {Settable} obj - The object to modify.
6
- * @param {Path} path - The path of the property to set.
7
- * @param {any} value - The value to set.
8
- * @returns {T} - The modified object.
9
- *
10
- * @example
11
- * // Set a value in a nested object
12
- * const obj = { a: { b: { c: 3 } } };
13
- * set(obj, 'a.b.c', 4);
14
- * console.log(obj.a.b.c); // 4
15
- *
16
- * @example
17
- * // Set a value in an array
18
- * const arr = [1, 2, 3];
19
- * set(arr, 1, 4);
20
- * console.log(arr[1]); // 4
21
- *
22
- * @example
23
- * // Create non-existent path and set value
24
- * const obj = {};
25
- * set(obj, 'a.b.c', 4);
26
- * console.log(obj); // { a: { b: { c: 4 } } }
27
- */
28
- declare function set<T>(obj: Settable, path: Path, value: any): T;
29
- type Settable = object | any[];
30
- type Path = string | number | Array<string | number>;
31
-
32
- export { set };
@@ -1,49 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/object/set.ts
21
- var set_exports = {};
22
- __export(set_exports, {
23
- set: () => set
24
- });
25
- module.exports = __toCommonJS(set_exports);
26
- function set(obj, path, value) {
27
- if (obj instanceof Map || obj instanceof Set) {
28
- throw new TypeError("Set or Map is not supported");
29
- }
30
- const keys = Array.isArray(path) ? path : String(path).replace(/\[|\]/g, (match) => {
31
- return match === "[" ? "." : "";
32
- }).split(".");
33
- let pointer = obj;
34
- for (let i = 0; i < keys.length - 1; i++) {
35
- const key = keys[i];
36
- const nextKey = keys[i + 1];
37
- if (pointer[key] == null || typeof pointer[key] !== "object") {
38
- pointer[key] = /^\d+$/.test(nextKey) ? [] : {};
39
- }
40
- pointer = pointer[key];
41
- }
42
- pointer[keys[keys.length - 1]] = value;
43
- return obj;
44
- }
45
- // Annotate the CommonJS export names for ESM import in node:
46
- 0 && (module.exports = {
47
- set
48
- });
49
- //# sourceMappingURL=set.js.map
@@ -1 +0,0 @@
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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;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":[]}
@@ -1,8 +0,0 @@
1
- import {
2
- set
3
- } from "../chunk-GL7PPRA4.mjs";
4
- import "../chunk-2MM5EJJX.mjs";
5
- export {
6
- set
7
- };
8
- //# sourceMappingURL=set.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}