@nocobase/utils 0.8.0-alpha.8 → 0.8.1-alpha.3
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/lib/assign.d.ts +10 -0
- package/lib/assign.js +145 -0
- package/lib/date.d.ts +2 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +13 -0
- package/package.json +2 -2
package/lib/assign.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare type MergeStrategyType = 'merge' | 'deepMerge' | 'overwrite' | 'andMerge' | 'orMerge' | 'intersect' | 'union';
|
|
2
|
+
declare type MergeStrategyFunc = (x: any, y: any) => any;
|
|
3
|
+
export declare type MergeStrategy = MergeStrategyType | MergeStrategyFunc;
|
|
4
|
+
export interface MergeStrategies {
|
|
5
|
+
[key: string]: MergeStrategy;
|
|
6
|
+
}
|
|
7
|
+
export default function isPlainObject(value: any): boolean;
|
|
8
|
+
export declare const mergeStrategies: Map<MergeStrategyType, MergeStrategyFunc>;
|
|
9
|
+
export declare function assign(target: any, source: any, strategies?: MergeStrategies): any;
|
|
10
|
+
export {};
|
package/lib/assign.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.assign = assign;
|
|
7
|
+
exports.default = isPlainObject;
|
|
8
|
+
exports.mergeStrategies = void 0;
|
|
9
|
+
|
|
10
|
+
function _deepmerge() {
|
|
11
|
+
const data = _interopRequireDefault(require("deepmerge"));
|
|
12
|
+
|
|
13
|
+
_deepmerge = function _deepmerge() {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
return data;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function _lodash() {
|
|
21
|
+
const data = _interopRequireDefault(require("lodash"));
|
|
22
|
+
|
|
23
|
+
_lodash = function _lodash() {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
31
|
+
|
|
32
|
+
function isPlainObject(value) {
|
|
33
|
+
if (Object.prototype.toString.call(value) !== '[object Object]') {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const prototype = Object.getPrototypeOf(value);
|
|
38
|
+
return prototype === null || prototype === Object.prototype;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getEnumerableOwnPropertySymbols(target) {
|
|
42
|
+
return Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(target).filter(symbol => target.propertyIsEnumerable(symbol)) : [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function getKeys(target) {
|
|
46
|
+
return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const mergeStrategies = new Map();
|
|
50
|
+
exports.mergeStrategies = mergeStrategies;
|
|
51
|
+
mergeStrategies.set('overwrite', (_, y) => {
|
|
52
|
+
if (typeof y === 'string') {
|
|
53
|
+
y = y.split(',');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return y;
|
|
57
|
+
});
|
|
58
|
+
mergeStrategies.set('andMerge', (x, y) => {
|
|
59
|
+
if (!x && !y) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!x) {
|
|
64
|
+
return y;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!y) {
|
|
68
|
+
return x;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
$and: [x, y]
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
mergeStrategies.set('orMerge', (x, y) => {
|
|
76
|
+
if (!x && !y) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!x) {
|
|
81
|
+
return y;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!y) {
|
|
85
|
+
return x;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
$or: [x, y]
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
mergeStrategies.set('deepMerge', (x, y) => {
|
|
93
|
+
return isPlainObject(x) && isPlainObject(y) ? (0, _deepmerge().default)(x, y, {
|
|
94
|
+
arrayMerge: (x, y) => y
|
|
95
|
+
}) : y;
|
|
96
|
+
});
|
|
97
|
+
mergeStrategies.set('merge', (x, y) => {
|
|
98
|
+
return isPlainObject(x) && isPlainObject(y) ? Object.assign(x, y) : y;
|
|
99
|
+
});
|
|
100
|
+
mergeStrategies.set('union', (x, y) => {
|
|
101
|
+
if (typeof x === 'string') {
|
|
102
|
+
x = x.split(',');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (typeof y === 'string') {
|
|
106
|
+
y = y.split(',');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return _lodash().default.uniq((x || []).concat(y || [])).filter(Boolean);
|
|
110
|
+
});
|
|
111
|
+
mergeStrategies.set('intersect', (x, y) => (() => {
|
|
112
|
+
if (typeof x === 'string') {
|
|
113
|
+
x = x.split(',');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (typeof y === 'string') {
|
|
117
|
+
y = y.split(',');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!Array.isArray(x) || x.length === 0) {
|
|
121
|
+
return y || [];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!Array.isArray(y) || y.length === 0) {
|
|
125
|
+
return x || [];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return x.filter(v => y.includes(v));
|
|
129
|
+
})().filter(Boolean));
|
|
130
|
+
|
|
131
|
+
function assign(target, source, strategies = {}) {
|
|
132
|
+
getKeys(source).forEach(sourceKey => {
|
|
133
|
+
const strategy = strategies[sourceKey];
|
|
134
|
+
let func = mergeStrategies.get('deepMerge');
|
|
135
|
+
|
|
136
|
+
if (typeof strategy === 'function') {
|
|
137
|
+
func = strategy;
|
|
138
|
+
} else if (typeof strategy === 'string' && mergeStrategies.has(strategy)) {
|
|
139
|
+
func = mergeStrategies.get(strategy);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
target[sourceKey] = func(target[sourceKey], source[sourceKey]);
|
|
143
|
+
});
|
|
144
|
+
return target;
|
|
145
|
+
}
|
package/lib/date.d.ts
CHANGED
|
@@ -5,6 +5,6 @@ export interface Str2momentOptions {
|
|
|
5
5
|
utcOffset?: any;
|
|
6
6
|
}
|
|
7
7
|
export declare const getDefaultFormat: (props: any) => any;
|
|
8
|
-
export declare const toGmt: (value: moment.Moment | moment.Moment[]) => string | moment.Moment | moment.Moment[]
|
|
9
|
-
export declare const toLocal: (value: moment.Moment | moment.Moment[]) => string | moment.Moment | moment.Moment[]
|
|
8
|
+
export declare const toGmt: (value: moment.Moment | moment.Moment[]) => string | string[] | moment.Moment | moment.Moment[];
|
|
9
|
+
export declare const toLocal: (value: moment.Moment | moment.Moment[]) => string | string[] | moment.Moment | moment.Moment[];
|
|
10
10
|
export declare const str2moment: (value?: string | string[], options?: Str2momentOptions) => any;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -119,4 +119,17 @@ Object.keys(_uid).forEach(function (key) {
|
|
|
119
119
|
return _uid[key];
|
|
120
120
|
}
|
|
121
121
|
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
var _assign = require("./assign");
|
|
125
|
+
|
|
126
|
+
Object.keys(_assign).forEach(function (key) {
|
|
127
|
+
if (key === "default" || key === "__esModule") return;
|
|
128
|
+
if (key in exports && exports[key] === _assign[key]) return;
|
|
129
|
+
Object.defineProperty(exports, key, {
|
|
130
|
+
enumerable: true,
|
|
131
|
+
get: function get() {
|
|
132
|
+
return _assign[key];
|
|
133
|
+
}
|
|
134
|
+
});
|
|
122
135
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/utils",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1-alpha.3",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -19,5 +19,5 @@
|
|
|
19
19
|
"moment": "2.x",
|
|
20
20
|
"rc-input-number": "7.x"
|
|
21
21
|
},
|
|
22
|
-
"gitHead": "
|
|
22
|
+
"gitHead": "e03df3df5962b99d9fbf5b6e33fbe2b23f14f3d3"
|
|
23
23
|
}
|