@rc-component/util 1.4.0 → 1.5.0

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/es/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { default as useMergedState } from './hooks/useMergedState';
3
3
  export { default as useControlledState } from './hooks/useControlledState';
4
4
  export { supportNodeRef, supportRef, useComposeRef } from './ref';
5
5
  export { default as get } from './utils/get';
6
- export { default as set, merge } from './utils/set';
6
+ export { default as set, merge, mergeWith } from './utils/set';
7
7
  export { default as warning, noteOnce } from './warning';
8
8
  export { default as omit } from './omit';
9
9
  export { default as toArray } from './Children/toArray';
package/es/index.js CHANGED
@@ -3,7 +3,7 @@ export { default as useMergedState } from "./hooks/useMergedState";
3
3
  export { default as useControlledState } from "./hooks/useControlledState";
4
4
  export { supportNodeRef, supportRef, useComposeRef } from "./ref";
5
5
  export { default as get } from "./utils/get";
6
- export { default as set, merge } from "./utils/set";
6
+ export { default as set, merge, mergeWith } from "./utils/set";
7
7
  export { default as warning, noteOnce } from "./warning";
8
8
  export { default as omit } from "./omit";
9
9
  export { default as toArray } from "./Children/toArray";
package/es/utils/set.d.ts CHANGED
@@ -1,6 +1,18 @@
1
1
  export type Path = (string | number | symbol)[];
2
2
  export default function set<Entity = any, Output = Entity, Value = any>(entity: Entity, paths: Path, value: Value, removeIfUndefined?: boolean): Output;
3
+ export type MergeFn = (current: any, next: any) => any;
3
4
  /**
4
- * Merge objects which will create
5
+ * Merge multiple objects. Support custom merge logic.
6
+ * @param sources object sources
7
+ * @param config.prepareArray Customize array prepare function.
8
+ * It will return empty [] by default.
9
+ * So when match array, it will auto be override with next array in sources.
10
+ */
11
+ export declare function mergeWith<T extends object>(sources: T[], config?: {
12
+ prepareArray?: MergeFn;
13
+ }): T;
14
+ /**
15
+ * Merge multiple objects into a new single object.
16
+ * Arrays will be replaced by default.
5
17
  */
6
18
  export declare function merge<T extends object>(...sources: T[]): T;
package/es/utils/set.js CHANGED
@@ -38,10 +38,20 @@ function createEmpty(source) {
38
38
  }
39
39
  const keys = typeof Reflect === 'undefined' ? Object.keys : Reflect.ownKeys;
40
40
 
41
+ // ================================ Merge ================================
42
+
41
43
  /**
42
- * Merge objects which will create
44
+ * Merge multiple objects. Support custom merge logic.
45
+ * @param sources object sources
46
+ * @param config.prepareArray Customize array prepare function.
47
+ * It will return empty [] by default.
48
+ * So when match array, it will auto be override with next array in sources.
43
49
  */
44
- export function merge(...sources) {
50
+ export function mergeWith(sources, config = {}) {
51
+ const {
52
+ prepareArray
53
+ } = config;
54
+ const finalPrepareArray = prepareArray || (() => []);
45
55
  let clone = createEmpty(sources[0]);
46
56
  sources.forEach(src => {
47
57
  function internalMerge(path, parentLoopSet) {
@@ -55,7 +65,7 @@ export function merge(...sources) {
55
65
  const originValue = get(clone, path);
56
66
  if (isArr) {
57
67
  // Array will always be override
58
- clone = set(clone, path, []);
68
+ clone = set(clone, path, finalPrepareArray(originValue, value));
59
69
  } else if (!originValue || typeof originValue !== 'object') {
60
70
  // Init container if not exist
61
71
  clone = set(clone, path, createEmpty(value));
@@ -71,4 +81,12 @@ export function merge(...sources) {
71
81
  internalMerge([]);
72
82
  });
73
83
  return clone;
84
+ }
85
+
86
+ /**
87
+ * Merge multiple objects into a new single object.
88
+ * Arrays will be replaced by default.
89
+ */
90
+ export function merge(...sources) {
91
+ return mergeWith(sources);
74
92
  }
package/lib/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { default as useMergedState } from './hooks/useMergedState';
3
3
  export { default as useControlledState } from './hooks/useControlledState';
4
4
  export { supportNodeRef, supportRef, useComposeRef } from './ref';
5
5
  export { default as get } from './utils/get';
6
- export { default as set, merge } from './utils/set';
6
+ export { default as set, merge, mergeWith } from './utils/set';
7
7
  export { default as warning, noteOnce } from './warning';
8
8
  export { default as omit } from './omit';
9
9
  export { default as toArray } from './Children/toArray';
package/lib/index.js CHANGED
@@ -15,6 +15,12 @@ Object.defineProperty(exports, "merge", {
15
15
  return _set.merge;
16
16
  }
17
17
  });
18
+ Object.defineProperty(exports, "mergeWith", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _set.mergeWith;
22
+ }
23
+ });
18
24
  Object.defineProperty(exports, "noteOnce", {
19
25
  enumerable: true,
20
26
  get: function () {
@@ -1,6 +1,18 @@
1
1
  export type Path = (string | number | symbol)[];
2
2
  export default function set<Entity = any, Output = Entity, Value = any>(entity: Entity, paths: Path, value: Value, removeIfUndefined?: boolean): Output;
3
+ export type MergeFn = (current: any, next: any) => any;
3
4
  /**
4
- * Merge objects which will create
5
+ * Merge multiple objects. Support custom merge logic.
6
+ * @param sources object sources
7
+ * @param config.prepareArray Customize array prepare function.
8
+ * It will return empty [] by default.
9
+ * So when match array, it will auto be override with next array in sources.
10
+ */
11
+ export declare function mergeWith<T extends object>(sources: T[], config?: {
12
+ prepareArray?: MergeFn;
13
+ }): T;
14
+ /**
15
+ * Merge multiple objects into a new single object.
16
+ * Arrays will be replaced by default.
5
17
  */
6
18
  export declare function merge<T extends object>(...sources: T[]): T;
package/lib/utils/set.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = set;
7
7
  exports.merge = merge;
8
+ exports.mergeWith = mergeWith;
8
9
  var _get = _interopRequireDefault(require("./get"));
9
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
11
  function internalSet(entity, paths, value, removeIfUndefined) {
@@ -46,10 +47,20 @@ function createEmpty(source) {
46
47
  }
47
48
  const keys = typeof Reflect === 'undefined' ? Object.keys : Reflect.ownKeys;
48
49
 
50
+ // ================================ Merge ================================
51
+
49
52
  /**
50
- * Merge objects which will create
53
+ * Merge multiple objects. Support custom merge logic.
54
+ * @param sources object sources
55
+ * @param config.prepareArray Customize array prepare function.
56
+ * It will return empty [] by default.
57
+ * So when match array, it will auto be override with next array in sources.
51
58
  */
52
- function merge(...sources) {
59
+ function mergeWith(sources, config = {}) {
60
+ const {
61
+ prepareArray
62
+ } = config;
63
+ const finalPrepareArray = prepareArray || (() => []);
53
64
  let clone = createEmpty(sources[0]);
54
65
  sources.forEach(src => {
55
66
  function internalMerge(path, parentLoopSet) {
@@ -63,7 +74,7 @@ function merge(...sources) {
63
74
  const originValue = (0, _get.default)(clone, path);
64
75
  if (isArr) {
65
76
  // Array will always be override
66
- clone = set(clone, path, []);
77
+ clone = set(clone, path, finalPrepareArray(originValue, value));
67
78
  } else if (!originValue || typeof originValue !== 'object') {
68
79
  // Init container if not exist
69
80
  clone = set(clone, path, createEmpty(value));
@@ -79,4 +90,12 @@ function merge(...sources) {
79
90
  internalMerge([]);
80
91
  });
81
92
  return clone;
93
+ }
94
+
95
+ /**
96
+ * Merge multiple objects into a new single object.
97
+ * Arrays will be replaced by default.
98
+ */
99
+ function merge(...sources) {
100
+ return mergeWith(sources);
82
101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rc-component/util",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Common Utils For React Component",
5
5
  "keywords": [
6
6
  "react",