@nocobase/utils 0.9.2-alpha.3 → 0.9.3-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 +1 -0
- package/lib/client.js +11 -0
- 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/package.json +5 -3
package/lib/client.d.ts
CHANGED
package/lib/client.js
CHANGED
|
@@ -112,4 +112,15 @@ Object.keys(_uid).forEach(function (key) {
|
|
|
112
112
|
return _uid[key];
|
|
113
113
|
}
|
|
114
114
|
});
|
|
115
|
+
});
|
|
116
|
+
var _jsonTemplates = require("./json-templates");
|
|
117
|
+
Object.keys(_jsonTemplates).forEach(function (key) {
|
|
118
|
+
if (key === "default" || key === "__esModule") return;
|
|
119
|
+
if (key in exports && exports[key] === _jsonTemplates[key]) return;
|
|
120
|
+
Object.defineProperty(exports, key, {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
get: function get() {
|
|
123
|
+
return _jsonTemplates[key];
|
|
124
|
+
}
|
|
125
|
+
});
|
|
115
126
|
});
|
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/package.json
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/utils",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3-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": "24979bc561537c0b84469c161eeef1a6de4c4684"
|
|
18
20
|
}
|