@nocobase/utils 0.9.2-alpha.4 → 0.9.4-alpha.1
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/client.d.ts +3 -0
- package/lib/client.js +33 -0
- package/lib/getValuesByPath.js +22 -5
- package/lib/index.d.ts +1 -0
- package/lib/index.js +11 -0
- package/lib/json-templates.d.ts +1 -0
- package/lib/json-templates.js +147 -0
- package/lib/log.d.ts +1 -0
- package/lib/log.js +10 -0
- package/lib/notification.d.ts +1 -0
- package/lib/notification.js +20 -0
- package/lib/parse-filter.d.ts +1 -0
- package/lib/parse-filter.js +10 -10
- package/package.json +5 -3
package/lib/client.d.ts
CHANGED
|
@@ -3,7 +3,10 @@ export * from './common';
|
|
|
3
3
|
export * from './date';
|
|
4
4
|
export * from './forEach';
|
|
5
5
|
export * from './getValuesByPath';
|
|
6
|
+
export * from './json-templates';
|
|
7
|
+
export * from './log';
|
|
6
8
|
export * from './merge';
|
|
9
|
+
export * from './notification';
|
|
7
10
|
export * from './number';
|
|
8
11
|
export * from './parse-filter';
|
|
9
12
|
export * from './registry';
|
package/lib/client.js
CHANGED
|
@@ -58,6 +58,28 @@ Object.keys(_getValuesByPath).forEach(function (key) {
|
|
|
58
58
|
}
|
|
59
59
|
});
|
|
60
60
|
});
|
|
61
|
+
var _jsonTemplates = require("./json-templates");
|
|
62
|
+
Object.keys(_jsonTemplates).forEach(function (key) {
|
|
63
|
+
if (key === "default" || key === "__esModule") return;
|
|
64
|
+
if (key in exports && exports[key] === _jsonTemplates[key]) return;
|
|
65
|
+
Object.defineProperty(exports, key, {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
get: function get() {
|
|
68
|
+
return _jsonTemplates[key];
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
var _log = require("./log");
|
|
73
|
+
Object.keys(_log).forEach(function (key) {
|
|
74
|
+
if (key === "default" || key === "__esModule") return;
|
|
75
|
+
if (key in exports && exports[key] === _log[key]) return;
|
|
76
|
+
Object.defineProperty(exports, key, {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
get: function get() {
|
|
79
|
+
return _log[key];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
61
83
|
var _merge = require("./merge");
|
|
62
84
|
Object.keys(_merge).forEach(function (key) {
|
|
63
85
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -69,6 +91,17 @@ Object.keys(_merge).forEach(function (key) {
|
|
|
69
91
|
}
|
|
70
92
|
});
|
|
71
93
|
});
|
|
94
|
+
var _notification = require("./notification");
|
|
95
|
+
Object.keys(_notification).forEach(function (key) {
|
|
96
|
+
if (key === "default" || key === "__esModule") return;
|
|
97
|
+
if (key in exports && exports[key] === _notification[key]) return;
|
|
98
|
+
Object.defineProperty(exports, key, {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
get: function get() {
|
|
101
|
+
return _notification[key];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
72
105
|
var _number = require("./number");
|
|
73
106
|
Object.keys(_number).forEach(function (key) {
|
|
74
107
|
if (key === "default" || key === "__esModule") return;
|
package/lib/getValuesByPath.js
CHANGED
|
@@ -4,6 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getValuesByPath = void 0;
|
|
7
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
8
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
9
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
7
10
|
const getValuesByPath = (obj, path, defaultValue) => {
|
|
8
11
|
if (!obj) {
|
|
9
12
|
return defaultValue;
|
|
@@ -12,22 +15,36 @@ const getValuesByPath = (obj, path, defaultValue) => {
|
|
|
12
15
|
let result = [];
|
|
13
16
|
let currentValue = obj;
|
|
14
17
|
for (let i = 0; i < keys.length; i++) {
|
|
18
|
+
var _currentValue;
|
|
15
19
|
const key = keys[i];
|
|
16
20
|
if (Array.isArray(currentValue)) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
var _iterator = _createForOfIteratorHelper(currentValue),
|
|
22
|
+
_step;
|
|
23
|
+
try {
|
|
24
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
25
|
+
const element = _step.value;
|
|
26
|
+
const value = getValuesByPath(element, keys.slice(i).join('.'), defaultValue);
|
|
27
|
+
result = result.concat(value);
|
|
28
|
+
}
|
|
29
|
+
} catch (err) {
|
|
30
|
+
_iterator.e(err);
|
|
31
|
+
} finally {
|
|
32
|
+
_iterator.f();
|
|
20
33
|
}
|
|
21
34
|
break;
|
|
22
35
|
}
|
|
23
|
-
|
|
24
|
-
if (currentValue == null) {
|
|
36
|
+
if (((_currentValue = currentValue) === null || _currentValue === void 0 ? void 0 : _currentValue[key]) === undefined) {
|
|
25
37
|
break;
|
|
26
38
|
}
|
|
39
|
+
currentValue = currentValue[key];
|
|
27
40
|
if (i === keys.length - 1) {
|
|
28
41
|
result.push(currentValue);
|
|
29
42
|
}
|
|
30
43
|
}
|
|
44
|
+
result = result.filter(Boolean);
|
|
45
|
+
if (result.length === 0) {
|
|
46
|
+
return defaultValue;
|
|
47
|
+
}
|
|
31
48
|
return result.length === 1 ? result[0] : result;
|
|
32
49
|
};
|
|
33
50
|
exports.getValuesByPath = getValuesByPath;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -167,4 +167,15 @@ Object.keys(_uid).forEach(function (key) {
|
|
|
167
167
|
return _uid[key];
|
|
168
168
|
}
|
|
169
169
|
});
|
|
170
|
+
});
|
|
171
|
+
var _jsonTemplates = require("./json-templates");
|
|
172
|
+
Object.keys(_jsonTemplates).forEach(function (key) {
|
|
173
|
+
if (key === "default" || key === "__esModule") return;
|
|
174
|
+
if (key in exports && exports[key] === _jsonTemplates[key]) return;
|
|
175
|
+
Object.defineProperty(exports, key, {
|
|
176
|
+
enumerable: true,
|
|
177
|
+
get: function get() {
|
|
178
|
+
return _jsonTemplates[key];
|
|
179
|
+
}
|
|
180
|
+
});
|
|
170
181
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parse(value: any): any;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.parse = parse;
|
|
7
|
+
function _objectPath() {
|
|
8
|
+
const data = _interopRequireDefault(require("object-path"));
|
|
9
|
+
_objectPath = function _objectPath() {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _dedupe() {
|
|
15
|
+
const data = _interopRequireDefault(require("dedupe"));
|
|
16
|
+
_dedupe = function _dedupe() {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
+
// json-templates
|
|
23
|
+
// Simple templating within JSON structures.
|
|
24
|
+
//
|
|
25
|
+
// Created by Curran Kelleher and Chrostophe Serafin.
|
|
26
|
+
// Contributions from Paul Brewer and Javier Blanco Martinez.
|
|
27
|
+
|
|
28
|
+
// An enhanced version of `typeof` that handles arrays and dates as well.
|
|
29
|
+
function type(value) {
|
|
30
|
+
let valueType = typeof value;
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
valueType = 'array';
|
|
33
|
+
} else if (value instanceof Date) {
|
|
34
|
+
valueType = 'date';
|
|
35
|
+
} else if (value === null) {
|
|
36
|
+
valueType = 'null';
|
|
37
|
+
}
|
|
38
|
+
return valueType;
|
|
39
|
+
}
|
|
40
|
+
// Constructs a parameter object from a match result.
|
|
41
|
+
// e.g. "['{{foo}}']" --> { key: "foo" }
|
|
42
|
+
// e.g. "['{{foo:bar}}']" --> { key: "foo", defaultValue: "bar" }
|
|
43
|
+
function Parameter(match) {
|
|
44
|
+
let param;
|
|
45
|
+
const matchValue = match.substr(2, match.length - 4).trim();
|
|
46
|
+
const i = matchValue.indexOf(':');
|
|
47
|
+
if (i !== -1) {
|
|
48
|
+
param = {
|
|
49
|
+
key: matchValue.substr(0, i),
|
|
50
|
+
defaultValue: matchValue.substr(i + 1)
|
|
51
|
+
};
|
|
52
|
+
} else {
|
|
53
|
+
param = {
|
|
54
|
+
key: matchValue
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return param;
|
|
58
|
+
}
|
|
59
|
+
// Constructs a template function with deduped `parameters` property.
|
|
60
|
+
function Template(fn, parameters) {
|
|
61
|
+
// Paul Brewer Dec 2017 add deduplication call, use only key property to eliminate
|
|
62
|
+
Object.assign(fn, {
|
|
63
|
+
parameters: (0, _dedupe().default)(parameters, item => item.key)
|
|
64
|
+
});
|
|
65
|
+
return fn;
|
|
66
|
+
}
|
|
67
|
+
// Parses the given template object.
|
|
68
|
+
//
|
|
69
|
+
// Returns a function `template(context)` that will "fill in" the template
|
|
70
|
+
// with the context object passed to it.
|
|
71
|
+
//
|
|
72
|
+
// The returned function has a `parameters` property,
|
|
73
|
+
// which is an array of parameter descriptor objects,
|
|
74
|
+
// each of which has a `key` property and possibly a `defaultValue` property.
|
|
75
|
+
function parse(value) {
|
|
76
|
+
switch (type(value)) {
|
|
77
|
+
case 'string':
|
|
78
|
+
return parseString(value);
|
|
79
|
+
case 'object':
|
|
80
|
+
return parseObject(value);
|
|
81
|
+
case 'array':
|
|
82
|
+
return parseArray(value);
|
|
83
|
+
default:
|
|
84
|
+
return Template(function () {
|
|
85
|
+
return value;
|
|
86
|
+
}, []);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Parses leaf nodes of the template object that are strings.
|
|
90
|
+
// Also used for parsing keys that contain templates.
|
|
91
|
+
const parseString = (() => {
|
|
92
|
+
// This regular expression detects instances of the
|
|
93
|
+
// template parameter syntax such as {{foo}} or {{foo:someDefault}}.
|
|
94
|
+
const regex = /{{(\w|:|[\s-+.,@///()?=*_$])+}}/g;
|
|
95
|
+
return str => {
|
|
96
|
+
let parameters = [];
|
|
97
|
+
let templateFn = context => str;
|
|
98
|
+
const matches = str.match(regex);
|
|
99
|
+
if (matches) {
|
|
100
|
+
parameters = matches.map(Parameter);
|
|
101
|
+
templateFn = context => {
|
|
102
|
+
context = context || {};
|
|
103
|
+
return matches.reduce((result, match, i) => {
|
|
104
|
+
const parameter = parameters[i];
|
|
105
|
+
let value = _objectPath().default.get(context, parameter.key);
|
|
106
|
+
if (value == null) {
|
|
107
|
+
value = parameter.defaultValue;
|
|
108
|
+
}
|
|
109
|
+
if (typeof value === 'function') {
|
|
110
|
+
value = value();
|
|
111
|
+
}
|
|
112
|
+
if (typeof value === 'object') {
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
// Accommodate numbers as values.
|
|
116
|
+
if (matches.length === 1 && str.startsWith('{{') && str.endsWith('}}')) {
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
return result.replace(match, value == null ? '' : value);
|
|
120
|
+
}, str);
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return Template(templateFn, parameters);
|
|
124
|
+
};
|
|
125
|
+
})();
|
|
126
|
+
// Parses non-leaf-nodes in the template object that are objects.
|
|
127
|
+
function parseObject(object) {
|
|
128
|
+
const children = Object.keys(object).map(key => ({
|
|
129
|
+
keyTemplate: parseString(key),
|
|
130
|
+
valueTemplate: parse(object[key])
|
|
131
|
+
}));
|
|
132
|
+
const templateParameters = children.reduce((parameters, child) => parameters.concat(child.valueTemplate.parameters, child.keyTemplate.parameters), []);
|
|
133
|
+
const templateFn = context => {
|
|
134
|
+
return children.reduce((newObject, child) => {
|
|
135
|
+
newObject[child.keyTemplate(context)] = child.valueTemplate(context);
|
|
136
|
+
return newObject;
|
|
137
|
+
}, {});
|
|
138
|
+
};
|
|
139
|
+
return Template(templateFn, templateParameters);
|
|
140
|
+
}
|
|
141
|
+
// Parses non-leaf-nodes in the template object that are arrays.
|
|
142
|
+
function parseArray(array) {
|
|
143
|
+
const templates = array.map(parse);
|
|
144
|
+
const templateParameters = templates.reduce((parameters, template) => parameters.concat(template.parameters), []);
|
|
145
|
+
const templateFn = context => templates.map(template => template(context));
|
|
146
|
+
return Template(templateFn, templateParameters);
|
|
147
|
+
}
|
package/lib/log.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const error: (message: Error | string, ...args: any[]) => void;
|
package/lib/log.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const showToast: (message: any, type?: string, duration?: number) => void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.showToast = void 0;
|
|
7
|
+
function _antd() {
|
|
8
|
+
const data = require("antd");
|
|
9
|
+
_antd = function _antd() {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
const showToast = (message, type = 'info', duration = 5000) => {
|
|
15
|
+
_antd().notification[type]({
|
|
16
|
+
message,
|
|
17
|
+
duration
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
exports.showToast = showToast;
|
package/lib/parse-filter.d.ts
CHANGED
package/lib/parse-filter.js
CHANGED
|
@@ -7,14 +7,8 @@ exports.flatten = flatten;
|
|
|
7
7
|
exports.getDateVars = getDateVars;
|
|
8
8
|
exports.getDayRange = getDayRange;
|
|
9
9
|
exports.parseFilter = void 0;
|
|
10
|
+
exports.splitPathToTwoParts = splitPathToTwoParts;
|
|
10
11
|
exports.utc2unit = utc2unit;
|
|
11
|
-
function _get() {
|
|
12
|
-
const data = _interopRequireDefault(require("lodash/get"));
|
|
13
|
-
_get = function _get() {
|
|
14
|
-
return data;
|
|
15
|
-
};
|
|
16
|
-
return data;
|
|
17
|
-
}
|
|
18
12
|
function _set() {
|
|
19
13
|
const data = _interopRequireDefault(require("lodash/set"));
|
|
20
14
|
_set = function _set() {
|
|
@@ -29,6 +23,7 @@ function _moment() {
|
|
|
29
23
|
};
|
|
30
24
|
return data;
|
|
31
25
|
}
|
|
26
|
+
var _getValuesByPath = require("./getValuesByPath");
|
|
32
27
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
33
28
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
34
29
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
@@ -76,9 +71,10 @@ function flatten(target, opts) {
|
|
|
76
71
|
function unflatten(obj, opts = {}) {
|
|
77
72
|
const parsed = {};
|
|
78
73
|
const transformValue = opts.transformValue || keyIdentity;
|
|
79
|
-
Object.keys(obj).
|
|
74
|
+
for (var _i = 0, _Object$keys = Object.keys(obj); _i < _Object$keys.length; _i++) {
|
|
75
|
+
const key = _Object$keys[_i];
|
|
80
76
|
(0, _set().default)(parsed, key, transformValue(obj[key], key));
|
|
81
|
-
}
|
|
77
|
+
}
|
|
82
78
|
return parsed;
|
|
83
79
|
}
|
|
84
80
|
const parsePath = path => {
|
|
@@ -163,7 +159,7 @@ const parseFilter = /*#__PURE__*/function () {
|
|
|
163
159
|
const match = re.exec(value);
|
|
164
160
|
if (match) {
|
|
165
161
|
const key = match[1].trim();
|
|
166
|
-
const val = (0,
|
|
162
|
+
const val = (0, _getValuesByPath.getValuesByPath)(vars, key, null);
|
|
167
163
|
const field = getField === null || getField === void 0 ? void 0 : getField(path);
|
|
168
164
|
value = typeof val === 'function' ? val === null || val === void 0 ? void 0 : val({
|
|
169
165
|
field,
|
|
@@ -300,4 +296,8 @@ function getDateVars() {
|
|
|
300
296
|
last90Days: toDays(-90),
|
|
301
297
|
next90Days: toDays(90)
|
|
302
298
|
};
|
|
299
|
+
}
|
|
300
|
+
function splitPathToTwoParts(path) {
|
|
301
|
+
const parts = path.split('.');
|
|
302
|
+
return [parts.shift(), parts.join('.')];
|
|
303
303
|
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/utils",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.4-alpha.1",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@hapi/topo": "^6.0.0",
|
|
9
|
+
"dedupe": "^3.0.2",
|
|
9
10
|
"deepmerge": "^4.2.2",
|
|
10
11
|
"flat-to-nested": "^1.1.1",
|
|
11
|
-
"graphlib": "^2.1.8"
|
|
12
|
+
"graphlib": "^2.1.8",
|
|
13
|
+
"object-path": "^0.11.8"
|
|
12
14
|
},
|
|
13
15
|
"peerDependencies": {
|
|
14
16
|
"moment": "2.x",
|
|
15
17
|
"rc-input-number": "7.x"
|
|
16
18
|
},
|
|
17
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "0b4936be557be918dbdf8196dadcbc7eb395906d"
|
|
18
20
|
}
|