es-toolkit 1.51.0-dev.2076 → 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.
- package/dist/object/merge.d.mts +3 -1
- package/dist/object/merge.d.ts +3 -1
- package/dist/object/merge.js +0 -40
- package/dist/object/merge.mjs +0 -40
- package/dist/object/omitBy.d.mts +4 -2
- package/dist/object/omitBy.d.ts +4 -2
- package/dist/object/omitBy.js +4 -3
- package/dist/object/omitBy.mjs +4 -3
- package/dist/object/pickBy.d.mts +4 -2
- package/dist/object/pickBy.d.ts +4 -2
- package/dist/object/pickBy.js +4 -3
- package/dist/object/pickBy.mjs +4 -3
- package/dist/types/Merge.d.mts +37 -0
- package/dist/types/Merge.d.ts +37 -0
- package/dist/types/ObjectKeys.d.mts +19 -0
- package/dist/types/ObjectKeys.d.ts +19 -0
- package/dist/types/index.d.mts +4 -2
- package/dist/types/index.d.ts +4 -2
- package/package.json +1 -1
package/dist/object/merge.d.mts
CHANGED
|
@@ -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
|
|
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 };
|
package/dist/object/merge.d.ts
CHANGED
|
@@ -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
|
|
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 };
|
package/dist/object/merge.js
CHANGED
|
@@ -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++) {
|
package/dist/object/merge.mjs
CHANGED
|
@@ -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++) {
|
package/dist/object/omitBy.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ObjectKeys } from "../types/ObjectKeys.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/object/omitBy.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Creates a new object composed of the properties that do not satisfy the predicate function.
|
|
@@ -9,7 +11,7 @@
|
|
|
9
11
|
* @param obj - The object to omit properties from.
|
|
10
12
|
* @param shouldOmit - A predicate function that determines
|
|
11
13
|
* whether a property should be omitted. It takes the property's key and value as arguments and returns `true`
|
|
12
|
-
* if the property should be omitted, and `false` otherwise.
|
|
14
|
+
* if the property should be omitted, and `false` otherwise. Numeric keys are passed as strings.
|
|
13
15
|
* @returns A new object with the properties that do not satisfy the predicate function.
|
|
14
16
|
*
|
|
15
17
|
* @example
|
|
@@ -18,6 +20,6 @@
|
|
|
18
20
|
* const result = omitBy(obj, shouldOmit);
|
|
19
21
|
* // result will be { a: 1, c: 3 }
|
|
20
22
|
*/
|
|
21
|
-
declare function omitBy<T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key:
|
|
23
|
+
declare function omitBy<T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key: ObjectKeys<T>) => boolean): Partial<T>;
|
|
22
24
|
//#endregion
|
|
23
25
|
export { omitBy };
|
package/dist/object/omitBy.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ObjectKeys } from "../types/ObjectKeys.js";
|
|
2
|
+
|
|
1
3
|
//#region src/object/omitBy.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Creates a new object composed of the properties that do not satisfy the predicate function.
|
|
@@ -9,7 +11,7 @@
|
|
|
9
11
|
* @param obj - The object to omit properties from.
|
|
10
12
|
* @param shouldOmit - A predicate function that determines
|
|
11
13
|
* whether a property should be omitted. It takes the property's key and value as arguments and returns `true`
|
|
12
|
-
* if the property should be omitted, and `false` otherwise.
|
|
14
|
+
* if the property should be omitted, and `false` otherwise. Numeric keys are passed as strings.
|
|
13
15
|
* @returns A new object with the properties that do not satisfy the predicate function.
|
|
14
16
|
*
|
|
15
17
|
* @example
|
|
@@ -18,6 +20,6 @@
|
|
|
18
20
|
* const result = omitBy(obj, shouldOmit);
|
|
19
21
|
* // result will be { a: 1, c: 3 }
|
|
20
22
|
*/
|
|
21
|
-
declare function omitBy<T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key:
|
|
23
|
+
declare function omitBy<T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key: ObjectKeys<T>) => boolean): Partial<T>;
|
|
22
24
|
//#endregion
|
|
23
25
|
export { omitBy };
|
package/dist/object/omitBy.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param obj - The object to omit properties from.
|
|
10
10
|
* @param shouldOmit - A predicate function that determines
|
|
11
11
|
* whether a property should be omitted. It takes the property's key and value as arguments and returns `true`
|
|
12
|
-
* if the property should be omitted, and `false` otherwise.
|
|
12
|
+
* if the property should be omitted, and `false` otherwise. Numeric keys are passed as strings.
|
|
13
13
|
* @returns A new object with the properties that do not satisfy the predicate function.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
@@ -23,8 +23,9 @@ function omitBy(obj, shouldOmit) {
|
|
|
23
23
|
const keys = Object.keys(obj);
|
|
24
24
|
for (let i = 0; i < keys.length; i++) {
|
|
25
25
|
const key = keys[i];
|
|
26
|
-
const
|
|
27
|
-
|
|
26
|
+
const objectKey = key;
|
|
27
|
+
const value = obj[objectKey];
|
|
28
|
+
if (!shouldOmit(value, key)) result[objectKey] = value;
|
|
28
29
|
}
|
|
29
30
|
return result;
|
|
30
31
|
}
|
package/dist/object/omitBy.mjs
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param obj - The object to omit properties from.
|
|
10
10
|
* @param shouldOmit - A predicate function that determines
|
|
11
11
|
* whether a property should be omitted. It takes the property's key and value as arguments and returns `true`
|
|
12
|
-
* if the property should be omitted, and `false` otherwise.
|
|
12
|
+
* if the property should be omitted, and `false` otherwise. Numeric keys are passed as strings.
|
|
13
13
|
* @returns A new object with the properties that do not satisfy the predicate function.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
@@ -23,8 +23,9 @@ function omitBy(obj, shouldOmit) {
|
|
|
23
23
|
const keys = Object.keys(obj);
|
|
24
24
|
for (let i = 0; i < keys.length; i++) {
|
|
25
25
|
const key = keys[i];
|
|
26
|
-
const
|
|
27
|
-
|
|
26
|
+
const objectKey = key;
|
|
27
|
+
const value = obj[objectKey];
|
|
28
|
+
if (!shouldOmit(value, key)) result[objectKey] = value;
|
|
28
29
|
}
|
|
29
30
|
return result;
|
|
30
31
|
}
|
package/dist/object/pickBy.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ObjectKeys } from "../types/ObjectKeys.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/object/pickBy.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Creates a new object composed of the properties that satisfy the predicate function.
|
|
@@ -9,7 +11,7 @@
|
|
|
9
11
|
* @param obj - The object to pick properties from.
|
|
10
12
|
* @param shouldPick - A predicate function that determines
|
|
11
13
|
* whether a property should be picked. It takes the property's key and value as arguments and returns `true`
|
|
12
|
-
* if the property should be picked, and `false` otherwise.
|
|
14
|
+
* if the property should be picked, and `false` otherwise. Numeric keys are passed as strings.
|
|
13
15
|
* @returns A new object with the properties that satisfy the predicate function.
|
|
14
16
|
*
|
|
15
17
|
* @example
|
|
@@ -18,6 +20,6 @@
|
|
|
18
20
|
* const result = pickBy(obj, shouldPick);
|
|
19
21
|
* // result will be { b: 'pick' }
|
|
20
22
|
*/
|
|
21
|
-
declare function pickBy<T extends Record<string, any>>(obj: T, shouldPick: (value: T[keyof T], key:
|
|
23
|
+
declare function pickBy<T extends Record<string, any>>(obj: T, shouldPick: (value: T[keyof T], key: ObjectKeys<T>) => boolean): Partial<T>;
|
|
22
24
|
//#endregion
|
|
23
25
|
export { pickBy };
|
package/dist/object/pickBy.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ObjectKeys } from "../types/ObjectKeys.js";
|
|
2
|
+
|
|
1
3
|
//#region src/object/pickBy.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Creates a new object composed of the properties that satisfy the predicate function.
|
|
@@ -9,7 +11,7 @@
|
|
|
9
11
|
* @param obj - The object to pick properties from.
|
|
10
12
|
* @param shouldPick - A predicate function that determines
|
|
11
13
|
* whether a property should be picked. It takes the property's key and value as arguments and returns `true`
|
|
12
|
-
* if the property should be picked, and `false` otherwise.
|
|
14
|
+
* if the property should be picked, and `false` otherwise. Numeric keys are passed as strings.
|
|
13
15
|
* @returns A new object with the properties that satisfy the predicate function.
|
|
14
16
|
*
|
|
15
17
|
* @example
|
|
@@ -18,6 +20,6 @@
|
|
|
18
20
|
* const result = pickBy(obj, shouldPick);
|
|
19
21
|
* // result will be { b: 'pick' }
|
|
20
22
|
*/
|
|
21
|
-
declare function pickBy<T extends Record<string, any>>(obj: T, shouldPick: (value: T[keyof T], key:
|
|
23
|
+
declare function pickBy<T extends Record<string, any>>(obj: T, shouldPick: (value: T[keyof T], key: ObjectKeys<T>) => boolean): Partial<T>;
|
|
22
24
|
//#endregion
|
|
23
25
|
export { pickBy };
|
package/dist/object/pickBy.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param obj - The object to pick properties from.
|
|
10
10
|
* @param shouldPick - A predicate function that determines
|
|
11
11
|
* whether a property should be picked. It takes the property's key and value as arguments and returns `true`
|
|
12
|
-
* if the property should be picked, and `false` otherwise.
|
|
12
|
+
* if the property should be picked, and `false` otherwise. Numeric keys are passed as strings.
|
|
13
13
|
* @returns A new object with the properties that satisfy the predicate function.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
@@ -23,8 +23,9 @@ function pickBy(obj, shouldPick) {
|
|
|
23
23
|
const keys = Object.keys(obj);
|
|
24
24
|
for (let i = 0; i < keys.length; i++) {
|
|
25
25
|
const key = keys[i];
|
|
26
|
-
const
|
|
27
|
-
|
|
26
|
+
const objectKey = key;
|
|
27
|
+
const value = obj[objectKey];
|
|
28
|
+
if (shouldPick(value, key)) result[objectKey] = value;
|
|
28
29
|
}
|
|
29
30
|
return result;
|
|
30
31
|
}
|
package/dist/object/pickBy.mjs
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param obj - The object to pick properties from.
|
|
10
10
|
* @param shouldPick - A predicate function that determines
|
|
11
11
|
* whether a property should be picked. It takes the property's key and value as arguments and returns `true`
|
|
12
|
-
* if the property should be picked, and `false` otherwise.
|
|
12
|
+
* if the property should be picked, and `false` otherwise. Numeric keys are passed as strings.
|
|
13
13
|
* @returns A new object with the properties that satisfy the predicate function.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
@@ -23,8 +23,9 @@ function pickBy(obj, shouldPick) {
|
|
|
23
23
|
const keys = Object.keys(obj);
|
|
24
24
|
for (let i = 0; i < keys.length; i++) {
|
|
25
25
|
const key = keys[i];
|
|
26
|
-
const
|
|
27
|
-
|
|
26
|
+
const objectKey = key;
|
|
27
|
+
const value = obj[objectKey];
|
|
28
|
+
if (shouldPick(value, key)) result[objectKey] = value;
|
|
28
29
|
}
|
|
29
30
|
return result;
|
|
30
31
|
}
|
|
@@ -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 };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/types/ObjectKeys.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a union of the keys of `T` as they are returned by `Object.keys`:
|
|
4
|
+
* numeric keys are converted to strings and symbol keys are excluded. The key-side
|
|
5
|
+
* counterpart to `ValueOf`.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The object type to read keys from.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* type Keys = ObjectKeys<{ a: number; 1: string }>;
|
|
11
|
+
* // => 'a' | '1'
|
|
12
|
+
*
|
|
13
|
+
* const obj = { a: 1, b: 2 };
|
|
14
|
+
* const keys = Object.keys(obj) as Array<ObjectKeys<typeof obj>>;
|
|
15
|
+
* // => Array<'a' | 'b'>
|
|
16
|
+
*/
|
|
17
|
+
type ObjectKeys<T> = `${Exclude<keyof T, symbol>}`;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { ObjectKeys };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/types/ObjectKeys.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a union of the keys of `T` as they are returned by `Object.keys`:
|
|
4
|
+
* numeric keys are converted to strings and symbol keys are excluded. The key-side
|
|
5
|
+
* counterpart to `ValueOf`.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The object type to read keys from.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* type Keys = ObjectKeys<{ a: number; 1: string }>;
|
|
11
|
+
* // => 'a' | '1'
|
|
12
|
+
*
|
|
13
|
+
* const obj = { a: 1, b: 2 };
|
|
14
|
+
* const keys = Object.keys(obj) as Array<ObjectKeys<typeof obj>>;
|
|
15
|
+
* // => Array<'a' | 'b'>
|
|
16
|
+
*/
|
|
17
|
+
type ObjectKeys<T> = `${Exclude<keyof T, symbol>}`;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { ObjectKeys };
|
package/dist/types/index.d.mts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { Simplify } from "./Simplify.mjs";
|
|
2
|
+
import { Merge } from "./Merge.mjs";
|
|
3
|
+
import { ObjectKeys } from "./ObjectKeys.mjs";
|
|
1
4
|
import { ToCamelCaseKeys } from "./ToCamelCaseKeys.mjs";
|
|
2
5
|
import { ToConstantCaseKeys } from "./ToConstantCaseKeys.mjs";
|
|
3
6
|
import { ToKebabCaseKeys } from "./ToKebabCaseKeys.mjs";
|
|
@@ -6,7 +9,6 @@ import { ToSnakeCaseKeys } from "./ToSnakeCaseKeys.mjs";
|
|
|
6
9
|
import { DeepPartial } from "./DeepPartial.mjs";
|
|
7
10
|
import { DeepReadonly } from "./DeepReadonly.mjs";
|
|
8
11
|
import { NonEmptyArray } from "./NonEmptyArray.mjs";
|
|
9
|
-
import { Simplify } from "./Simplify.mjs";
|
|
10
12
|
import { ValueOf } from "./ValueOf.mjs";
|
|
11
13
|
import { Writable } from "./Writable.mjs";
|
|
12
|
-
export { type DeepPartial, type DeepReadonly, type NonEmptyArray, 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/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { Simplify } from "./Simplify.js";
|
|
2
|
+
import { Merge } from "./Merge.js";
|
|
3
|
+
import { ObjectKeys } from "./ObjectKeys.js";
|
|
1
4
|
import { ToCamelCaseKeys } from "./ToCamelCaseKeys.js";
|
|
2
5
|
import { ToConstantCaseKeys } from "./ToConstantCaseKeys.js";
|
|
3
6
|
import { ToKebabCaseKeys } from "./ToKebabCaseKeys.js";
|
|
@@ -6,7 +9,6 @@ import { ToSnakeCaseKeys } from "./ToSnakeCaseKeys.js";
|
|
|
6
9
|
import { DeepPartial } from "./DeepPartial.js";
|
|
7
10
|
import { DeepReadonly } from "./DeepReadonly.js";
|
|
8
11
|
import { NonEmptyArray } from "./NonEmptyArray.js";
|
|
9
|
-
import { Simplify } from "./Simplify.js";
|
|
10
12
|
import { ValueOf } from "./ValueOf.js";
|
|
11
13
|
import { Writable } from "./Writable.js";
|
|
12
|
-
export { type DeepPartial, type DeepReadonly, type NonEmptyArray, 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.
|
|
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",
|