@gx-design-vue/pro-utils 0.2.0-beta.80 → 0.2.0-beta.81

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,43 @@
1
+ /**
2
+ * Deeply clones the given object.
3
+ *
4
+ * You can customize the deep cloning process using the `cloneValue` function.
5
+ * The function takes the current value `value`, the property name `key`, and the entire object `obj` as arguments.
6
+ * If the function returns a value, that value is used;
7
+ * if it returns `undefined`, the default cloning method is used.
8
+ *
9
+ * @template T - The type of the object.
10
+ * @param {T} obj - The object to clone.
11
+ * @param {Function} [cloneValue] - A function to customize the cloning process.
12
+ * @returns {T} - A deep clone of the given object.
13
+ *
14
+ * @example
15
+ * // Clone a primitive value
16
+ * const num = 29;
17
+ * const clonedNum = cloneDeepWith(num);
18
+ * console.log(clonedNum); // 29
19
+ * console.log(clonedNum === num); // true
20
+ *
21
+ * @example
22
+ * // Clone an object with a customizer
23
+ * const obj = { a: 1, b: 2 };
24
+ * const clonedObj = cloneDeepWith(obj, (value) => {
25
+ * if (typeof value === 'number') {
26
+ * return value * 2; // Double the number
27
+ * }
28
+ * });
29
+ * console.log(clonedObj); // { a: 2, b: 4 }
30
+ * console.log(clonedObj === obj); // false
31
+ *
32
+ * @example
33
+ * // Clone an array with a customizer
34
+ * const arr = [1, 2, 3];
35
+ * const clonedArr = cloneDeepWith(arr, (value) => {
36
+ * return value + 1; // Increment each value
37
+ * });
38
+ * console.log(clonedArr); // [2, 3, 4]
39
+ * console.log(clonedArr === arr); // false
40
+ */
41
+ export declare function cloneDeepWith<T>(obj: T, cloneValue: (value: any, key: PropertyKey | undefined, obj: T, stack: Map<any, any>) => any): T;
42
+ export declare function cloneDeepWithImpl<T>(valueToClone: any, keyToClone: PropertyKey | undefined, objectToClone: T, stack?: Map<any, any>, cloneValue?: ((value: any, key: PropertyKey | undefined, obj: T, stack: Map<any, any>) => any) | undefined): T;
43
+ export declare function copyProperties<T>(target: any, source: any, objectToClone?: T, stack?: Map<any, any> | undefined, cloneValue?: ((value: any, key: PropertyKey | undefined, obj: T, stack: Map<any, any>) => any) | undefined): void;
@@ -10,7 +10,7 @@
10
10
  * const num = 29;
11
11
  * const clonedNum = clone(num);
12
12
  * console.log(clonedNum); // 29
13
- * console.log(clonedNum === num) ; // true
13
+ * console.log(clonedNum === num); // true
14
14
  *
15
15
  * @example
16
16
  * // Clone an array
@@ -45,4 +45,3 @@
45
45
  * console.log(clonedObj === obj); // false
46
46
  */
47
47
  export declare function cloneDeep<T>(obj: T): T;
48
- export declare function copyProperties(target: any, source: any, stack?: Map<any, any>): void;
package/dist/index.d.ts CHANGED
@@ -7,12 +7,15 @@ import getScroll from './scroll/getScroll';
7
7
  import scrollTo from './scroll/scrollTo';
8
8
  import throttleByAnimationFrame from './scroll/throttleByAnimationFrame';
9
9
  export * from './cloneDeep';
10
+ export * from './cloneDeep/cloneDeepWith';
10
11
  export * from './isBrowser';
11
12
  export * from './isDeepEqualReact';
12
13
  export * from './isImg';
13
14
  export * from './isNil';
14
15
  export * from './isUrl';
15
16
  export * from './merge';
17
+ export * from './merge/mergeWith';
18
+ export * from './merge/useDeepMege';
16
19
  export * from './nanoid';
17
20
  export * from './omitBoolean';
18
21
  export * from './omitUndefined';
@@ -1,4 +1,3 @@
1
- import type { DeepPartial } from '../typings';
2
1
  /**
3
2
  * 用于合并 n 个对象
4
3
  * @param {any[]} ...rest
@@ -6,18 +5,46 @@ import type { DeepPartial } from '../typings';
6
5
  */
7
6
  declare const merge: <T = any>(...rest: any[]) => T;
8
7
  /**
9
- * @Author gx12358
10
- * @DateTime 2024/12/6
11
- * @lastTime 2024/12/6
12
- * @description 深度合并对象
13
- * @description target 目标值
14
- * @description source 合并值
15
- * @description options 参数配置
16
- * @description options - omitNil 去除空值
17
- * @description options - omitEmpty 去除空数组或对象
8
+ * Merges the properties of the source object into a deep clone of the target object.
9
+ * Unlike `merge`, This function does not modify the original target object.
10
+ *
11
+ * This function performs a deep merge, meaning nested objects and arrays are merged recursively.
12
+ *
13
+ * - If a property in the source object is an array or object and the corresponding property in the target object is also an array or object, they will be merged.
14
+ * - If a property in the source object is undefined, it will not overwrite a defined property in the target object.
15
+ *
16
+ * Note that this function does not mutate the target object.
17
+ *
18
+ * @param {T} target - The target object to be cloned and merged into. This object is not modified directly.
19
+ * @param {S} source - The source object whose properties will be merged into the cloned target object.
20
+ * @returns {T & S} A new object with properties from the source object merged into a deep clone of the target object.
21
+ *
22
+ * @template T - Type of the target object.
23
+ * @template S - Type of the source object.
24
+ *
25
+ * @example
26
+ * const target = { a: 1, b: { x: 1, y: 2 } };
27
+ * const source = { b: { y: 3, z: 4 }, c: 5 };
28
+ *
29
+ * const result = toMerged(target, source);
30
+ * console.log(result);
31
+ * // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
32
+ *
33
+ * @example
34
+ * const target = { a: [1, 2], b: { x: 1 } };
35
+ * const source = { a: [3], b: { y: 2 } };
36
+ *
37
+ * const result = toMerged(target, source);
38
+ * console.log(result);
39
+ * // Output: { a: [3, 2], b: { x: 1, y: 2 } }
40
+ *
41
+ * @example
42
+ * const target = { a: null };
43
+ * const source = { a: [1, 2, 3] };
44
+ *
45
+ * const result = toMerged(target, source);
46
+ * console.log(result);
47
+ * // Output: { a: [1, 2, 3] }
18
48
  */
19
- declare function deepMerge<T extends object>(target: T, source: DeepPartial<T>, options?: {
20
- omitNil?: boolean;
21
- omitEmpty?: boolean;
22
- }): T;
49
+ declare function deepMerge<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S): T & S;
23
50
  export { deepMerge, merge };
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Merges the properties of the source object into the target object.
3
+ *
4
+ * You can provide a custom `merge` function to control how properties are merged. It should return the value to be set in the target object.
5
+ *
6
+ * If it returns `undefined`, a default deep merge will be applied for arrays and objects:
7
+ *
8
+ * - 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.
9
+ * - If a property in the source object is undefined, it will not overwrite a defined property in the target object.
10
+ *
11
+ * Note that this function mutates the target object.
12
+ *
13
+ * @param {T} target - The target object into which the source object properties will be merged. This object is modified in place.
14
+ * @param {S} source - The source object whose properties will be merged into the target object.
15
+ * @param {(targetValue: any, sourceValue: any, key: string, target: T, source: S) => any} merge - A custom merge function that defines how properties should be combined. It receives the following arguments:
16
+ * - `targetValue`: The current value of the property in the target object.
17
+ * - `sourceValue`: The value of the property in the source object.
18
+ * - `key`: The key of the property being merged.
19
+ * - `target`: The target object.
20
+ * - `source`: The source object.
21
+ *
22
+ * @returns {T & S} The updated target object with properties from the source object merged in.
23
+ *
24
+ * @template T - Type of the target object.
25
+ * @template S - Type of the source object.
26
+ *
27
+ * @example
28
+ * const target = { a: 1, b: 2 };
29
+ * const source = { b: 3, c: 4 };
30
+ *
31
+ * mergeWith(target, source, (targetValue, sourceValue) => {
32
+ * if (typeof targetValue === 'number' && typeof sourceValue === 'number') {
33
+ * return targetValue + sourceValue;
34
+ * }
35
+ * });
36
+ * // Returns { a: 1, b: 5, c: 4 }
37
+ * @example
38
+ * const target = { a: [1], b: [2] };
39
+ * const source = { a: [3], b: [4] };
40
+ *
41
+ * const result = mergeWith(target, source, (objValue, srcValue) => {
42
+ * if (Array.isArray(objValue)) {
43
+ * return objValue.concat(srcValue);
44
+ * }
45
+ * });
46
+ *
47
+ * expect(result).toEqual({ a: [1, 3], b: [2, 4] });
48
+ */
49
+ export declare function mergeWith<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S, merge: (targetValue: any, sourceValue: any, key: string, target: T, source: S) => any): T & S;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Merges the properties of the source object into a deep clone of the target object.
3
+ * Unlike `merge`, This function does not modify the original target object.
4
+ *
5
+ * This function performs a deep merge, meaning nested objects and arrays are merged recursively.
6
+ *
7
+ * - If a property in the source object is an array or object and the corresponding property in the target object is also an array or object, they will be merged.
8
+ * - If a property in the source object is undefined, it will not overwrite a defined property in the target object.
9
+ *
10
+ * Note that this function does not mutate the target object.
11
+ *
12
+ * @param {T} target - The target object to be cloned and merged into. This object is not modified directly.
13
+ * @param {S} source - The source object whose properties will be merged into the cloned target object.
14
+ * @returns {T & S} A new object with properties from the source object merged into a deep clone of the target object.
15
+ *
16
+ * @template T - Type of the target object.
17
+ * @template S - Type of the source object.
18
+ *
19
+ * @example
20
+ * const target = { a: 1, b: { x: 1, y: 2 } };
21
+ * const source = { b: { y: 3, z: 4 }, c: 5 };
22
+ *
23
+ * const result = toMerged(target, source);
24
+ * console.log(result);
25
+ * // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
26
+ *
27
+ * @example
28
+ * const target = { a: [1, 2], b: { x: 1 } };
29
+ * const source = { a: [3], b: { y: 2 } };
30
+ *
31
+ * const result = toMerged(target, source);
32
+ * console.log(result);
33
+ * // Output: { a: [3, 2], b: { x: 1, y: 2 } }
34
+ *
35
+ * @example
36
+ * const target = { a: null };
37
+ * const source = { a: [1, 2, 3] };
38
+ *
39
+ * const result = toMerged(target, source);
40
+ * console.log(result);
41
+ * // Output: { a: [1, 2, 3] }
42
+ */
43
+ export declare function useDeepMerge<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S): T & S;