es-toolkit 1.51.0-dev.2077 → 1.51.0-dev.2078

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.
@@ -1,3 +1,5 @@
1
+ import { Merge } from "../types/Merge.mjs";
2
+
1
3
  //#region src/object/merge.d.ts
2
4
  /**
3
5
  * Merges the properties of the source object into the target object.
@@ -39,6 +41,6 @@
39
41
  * console.log(result);
40
42
  * // Output: { a: [1, 2, 3] }
41
43
  */
42
- declare function merge<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S): T & S;
44
+ declare function merge<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S): Merge<T, S>;
43
45
  //#endregion
44
46
  export { merge };
@@ -1,3 +1,5 @@
1
+ import { Merge } from "../types/Merge.js";
2
+
1
3
  //#region src/object/merge.d.ts
2
4
  /**
3
5
  * Merges the properties of the source object into the target object.
@@ -39,6 +41,6 @@
39
41
  * console.log(result);
40
42
  * // Output: { a: [1, 2, 3] }
41
43
  */
42
- declare function merge<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S): T & S;
44
+ declare function merge<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S): Merge<T, S>;
43
45
  //#endregion
44
46
  export { merge };
@@ -2,46 +2,6 @@ const require_isPlainObject = require("../predicate/isPlainObject.js");
2
2
  const require_isMergeableValue = require("../_internal/isMergeableValue.js");
3
3
  const require_isUnsafeProperty = require("../_internal/isUnsafeProperty.js");
4
4
  //#region src/object/merge.ts
5
- /**
6
- * Merges the properties of the source object into the target object.
7
- *
8
- * This function performs a deep merge, meaning nested objects and arrays are merged recursively.
9
- * If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
10
- * If a property in the source object is undefined, it will not overwrite a defined property in the target object.
11
- *
12
- * Note that this function mutates the target object.
13
- *
14
- * @param target - The target object into which the source object properties will be merged. This object is modified in place.
15
- * @param source - The source object whose properties will be merged into the target object.
16
- * @returns The updated target object with properties from the source object merged in.
17
- *
18
- * @template T - Type of the target object.
19
- * @template S - Type of the source object.
20
- *
21
- * @example
22
- * const target = { a: 1, b: { x: 1, y: 2 } };
23
- * const source = { b: { y: 3, z: 4 }, c: 5 };
24
- *
25
- * const result = merge(target, source);
26
- * console.log(result);
27
- * // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
28
- *
29
- * @example
30
- * const target = { a: [1, 2], b: { x: 1 } };
31
- * const source = { a: [3], b: { y: 2 } };
32
- *
33
- * const result = merge(target, source);
34
- * console.log(result);
35
- * // Output: { a: [3, 2], b: { x: 1, y: 2 } }
36
- *
37
- * @example
38
- * const target = { a: null };
39
- * const source = { a: [1, 2, 3] };
40
- *
41
- * const result = merge(target, source);
42
- * console.log(result);
43
- * // Output: { a: [1, 2, 3] }
44
- */
45
5
  function merge(target, source) {
46
6
  const sourceKeys = Object.keys(source);
47
7
  for (let i = 0; i < sourceKeys.length; i++) {
@@ -2,46 +2,6 @@ import { isPlainObject } from "../predicate/isPlainObject.mjs";
2
2
  import { isMergeableValue } from "../_internal/isMergeableValue.mjs";
3
3
  import { isUnsafeProperty } from "../_internal/isUnsafeProperty.mjs";
4
4
  //#region src/object/merge.ts
5
- /**
6
- * Merges the properties of the source object into the target object.
7
- *
8
- * This function performs a deep merge, meaning nested objects and arrays are merged recursively.
9
- * If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
10
- * If a property in the source object is undefined, it will not overwrite a defined property in the target object.
11
- *
12
- * Note that this function mutates the target object.
13
- *
14
- * @param target - The target object into which the source object properties will be merged. This object is modified in place.
15
- * @param source - The source object whose properties will be merged into the target object.
16
- * @returns The updated target object with properties from the source object merged in.
17
- *
18
- * @template T - Type of the target object.
19
- * @template S - Type of the source object.
20
- *
21
- * @example
22
- * const target = { a: 1, b: { x: 1, y: 2 } };
23
- * const source = { b: { y: 3, z: 4 }, c: 5 };
24
- *
25
- * const result = merge(target, source);
26
- * console.log(result);
27
- * // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
28
- *
29
- * @example
30
- * const target = { a: [1, 2], b: { x: 1 } };
31
- * const source = { a: [3], b: { y: 2 } };
32
- *
33
- * const result = merge(target, source);
34
- * console.log(result);
35
- * // Output: { a: [3, 2], b: { x: 1, y: 2 } }
36
- *
37
- * @example
38
- * const target = { a: null };
39
- * const source = { a: [1, 2, 3] };
40
- *
41
- * const result = merge(target, source);
42
- * console.log(result);
43
- * // Output: { a: [1, 2, 3] }
44
- */
45
5
  function merge(target, source) {
46
6
  const sourceKeys = Object.keys(source);
47
7
  for (let i = 0; i < sourceKeys.length; i++) {
@@ -0,0 +1,37 @@
1
+ import { Simplify } from "./Simplify.mjs";
2
+
3
+ //#region src/types/Merge.d.ts
4
+ /**
5
+ * The result of deeply merging the source type `S` into the target type `T`,
6
+ * matching what `merge(target, source)` returns at runtime.
7
+ *
8
+ * Keys present on only one side are kept as-is. When both sides have a plain
9
+ * object at the same key, the merge recurses; tuples merge index by index, and
10
+ * other arrays merge into an array of both element types. If the source value
11
+ * can be `undefined`, the target type is kept because `merge` skips
12
+ * `undefined` source values. Values that are not plain objects or arrays
13
+ * (functions, `Date`, `RegExp`, `Map`, `Set`, etc.) are not merged into;
14
+ * the source value replaces the target value. When an array meets a plain
15
+ * object, `merge` keeps the target and assigns the source's properties onto
16
+ * it, so both property sets are kept (`T & S`).
17
+ *
18
+ * @template T - Type of the target object.
19
+ * @template S - Type of the source object.
20
+ *
21
+ * @example
22
+ * type Target = { a: number; b: { x: number; y: number } };
23
+ * type Source = { b: { y: string; z: boolean }; c: string };
24
+ * type Result = Merge<Target, Source>;
25
+ * // => { a: number; b: { x: number; y: string; z: boolean }; c: string }
26
+ */
27
+ type Merge<T, S> = undefined extends S ? T | MergeDefined<T, Exclude<S, undefined>> : MergeDefined<T, S>;
28
+ /**
29
+ * Values `merge` does not merge into: everything that is not a plain object
30
+ * or an array. The source value replaces the target value instead.
31
+ */
32
+ type NonMergeable = ((...args: any[]) => unknown) | Date | RegExp | Error | Promise<unknown> | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | WeakMap<object, unknown> | WeakSet<object>;
33
+ type MergeDefined<T, S> = S extends readonly unknown[] ? (T extends readonly unknown[] ? MergeArrays<T, S> : T extends NonMergeable ? S : T extends object ? T & S : S) : S extends NonMergeable ? S : S extends object ? (T extends readonly unknown[] ? T & S : T extends NonMergeable ? S : T extends object ? MergeRecords<T, S> : S) : S;
34
+ type MergeArrays<T extends readonly unknown[], S extends readonly unknown[]> = T extends readonly [] ? S : S extends readonly [] ? T : T extends readonly [infer TH, ...infer TR] ? S extends readonly [infer SH, ...infer SR] ? [Merge<TH, SH>, ...MergeArrays<TR, SR>] : Array<T[number] | S[number]> : Array<T[number] | S[number]>;
35
+ type MergeRecords<T extends object, S extends object> = Simplify<Omit<T, keyof S> & Omit<S, keyof T> & { [K in keyof T & keyof S]: Merge<T[K], S[K]> }>;
36
+ //#endregion
37
+ export { Merge };
@@ -0,0 +1,37 @@
1
+ import { Simplify } from "./Simplify.js";
2
+
3
+ //#region src/types/Merge.d.ts
4
+ /**
5
+ * The result of deeply merging the source type `S` into the target type `T`,
6
+ * matching what `merge(target, source)` returns at runtime.
7
+ *
8
+ * Keys present on only one side are kept as-is. When both sides have a plain
9
+ * object at the same key, the merge recurses; tuples merge index by index, and
10
+ * other arrays merge into an array of both element types. If the source value
11
+ * can be `undefined`, the target type is kept because `merge` skips
12
+ * `undefined` source values. Values that are not plain objects or arrays
13
+ * (functions, `Date`, `RegExp`, `Map`, `Set`, etc.) are not merged into;
14
+ * the source value replaces the target value. When an array meets a plain
15
+ * object, `merge` keeps the target and assigns the source's properties onto
16
+ * it, so both property sets are kept (`T & S`).
17
+ *
18
+ * @template T - Type of the target object.
19
+ * @template S - Type of the source object.
20
+ *
21
+ * @example
22
+ * type Target = { a: number; b: { x: number; y: number } };
23
+ * type Source = { b: { y: string; z: boolean }; c: string };
24
+ * type Result = Merge<Target, Source>;
25
+ * // => { a: number; b: { x: number; y: string; z: boolean }; c: string }
26
+ */
27
+ type Merge<T, S> = undefined extends S ? T | MergeDefined<T, Exclude<S, undefined>> : MergeDefined<T, S>;
28
+ /**
29
+ * Values `merge` does not merge into: everything that is not a plain object
30
+ * or an array. The source value replaces the target value instead.
31
+ */
32
+ type NonMergeable = ((...args: any[]) => unknown) | Date | RegExp | Error | Promise<unknown> | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | WeakMap<object, unknown> | WeakSet<object>;
33
+ type MergeDefined<T, S> = S extends readonly unknown[] ? (T extends readonly unknown[] ? MergeArrays<T, S> : T extends NonMergeable ? S : T extends object ? T & S : S) : S extends NonMergeable ? S : S extends object ? (T extends readonly unknown[] ? T & S : T extends NonMergeable ? S : T extends object ? MergeRecords<T, S> : S) : S;
34
+ type MergeArrays<T extends readonly unknown[], S extends readonly unknown[]> = T extends readonly [] ? S : S extends readonly [] ? T : T extends readonly [infer TH, ...infer TR] ? S extends readonly [infer SH, ...infer SR] ? [Merge<TH, SH>, ...MergeArrays<TR, SR>] : Array<T[number] | S[number]> : Array<T[number] | S[number]>;
35
+ type MergeRecords<T extends object, S extends object> = Simplify<Omit<T, keyof S> & Omit<S, keyof T> & { [K in keyof T & keyof S]: Merge<T[K], S[K]> }>;
36
+ //#endregion
37
+ export { Merge };
@@ -1,3 +1,5 @@
1
+ import { Simplify } from "./Simplify.mjs";
2
+ import { Merge } from "./Merge.mjs";
1
3
  import { ObjectKeys } from "./ObjectKeys.mjs";
2
4
  import { ToCamelCaseKeys } from "./ToCamelCaseKeys.mjs";
3
5
  import { ToConstantCaseKeys } from "./ToConstantCaseKeys.mjs";
@@ -7,7 +9,6 @@ import { ToSnakeCaseKeys } from "./ToSnakeCaseKeys.mjs";
7
9
  import { DeepPartial } from "./DeepPartial.mjs";
8
10
  import { DeepReadonly } from "./DeepReadonly.mjs";
9
11
  import { NonEmptyArray } from "./NonEmptyArray.mjs";
10
- import { Simplify } from "./Simplify.mjs";
11
12
  import { ValueOf } from "./ValueOf.mjs";
12
13
  import { Writable } from "./Writable.mjs";
13
- export { type DeepPartial, type DeepReadonly, type NonEmptyArray, type ObjectKeys, type Simplify, type ToCamelCaseKeys, type ToConstantCaseKeys, type ToKebabCaseKeys, type ToPascalCaseKeys, type ToSnakeCaseKeys, type ValueOf, type Writable };
14
+ export { type DeepPartial, type DeepReadonly, type Merge, type NonEmptyArray, type ObjectKeys, type Simplify, type ToCamelCaseKeys, type ToConstantCaseKeys, type ToKebabCaseKeys, type ToPascalCaseKeys, type ToSnakeCaseKeys, type ValueOf, type Writable };
@@ -1,3 +1,5 @@
1
+ import { Simplify } from "./Simplify.js";
2
+ import { Merge } from "./Merge.js";
1
3
  import { ObjectKeys } from "./ObjectKeys.js";
2
4
  import { ToCamelCaseKeys } from "./ToCamelCaseKeys.js";
3
5
  import { ToConstantCaseKeys } from "./ToConstantCaseKeys.js";
@@ -7,7 +9,6 @@ import { ToSnakeCaseKeys } from "./ToSnakeCaseKeys.js";
7
9
  import { DeepPartial } from "./DeepPartial.js";
8
10
  import { DeepReadonly } from "./DeepReadonly.js";
9
11
  import { NonEmptyArray } from "./NonEmptyArray.js";
10
- import { Simplify } from "./Simplify.js";
11
12
  import { ValueOf } from "./ValueOf.js";
12
13
  import { Writable } from "./Writable.js";
13
- export { type DeepPartial, type DeepReadonly, type NonEmptyArray, type ObjectKeys, type Simplify, type ToCamelCaseKeys, type ToConstantCaseKeys, type ToKebabCaseKeys, type ToPascalCaseKeys, type ToSnakeCaseKeys, type ValueOf, type Writable };
14
+ export { type DeepPartial, type DeepReadonly, type Merge, type NonEmptyArray, type ObjectKeys, type Simplify, type ToCamelCaseKeys, type ToConstantCaseKeys, type ToKebabCaseKeys, type ToPascalCaseKeys, type ToSnakeCaseKeys, type ValueOf, type Writable };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
- "version": "1.51.0-dev.2077+61705608",
3
+ "version": "1.51.0-dev.2078+a1918b04",
4
4
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
5
5
  "homepage": "https://es-toolkit.dev",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",