web-one 0.0.4 → 0.0.6
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/form.js +10 -11
- package/lib/index.js +224 -162
- package/package.json +1 -1
- package/src/index.ts +95 -25
- package/tsconfig.json +1 -1
package/lib/form.js
CHANGED
|
@@ -8,12 +8,11 @@ function fromFormData(formData, attrs, includeUndefine, includeErrorForNumbers)
|
|
|
8
8
|
}
|
|
9
9
|
exports.fromFormData = fromFormData;
|
|
10
10
|
function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErrorForNumbers) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
for (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var v = formData.get(key);
|
|
11
|
+
const obj = {};
|
|
12
|
+
const keys = formData.keys();
|
|
13
|
+
for (const key of keys) {
|
|
14
|
+
const attr = attrs[key];
|
|
15
|
+
const v = formData.get(key);
|
|
17
16
|
if (attr) {
|
|
18
17
|
obj[key] = v;
|
|
19
18
|
if (v && typeof v === "string") {
|
|
@@ -23,7 +22,7 @@ function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErr
|
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
24
|
else if (attr.type === "datetime" || attr.type === "date") {
|
|
26
|
-
|
|
25
|
+
const d = new Date(v);
|
|
27
26
|
if (d.toString() !== "Invalid Date") {
|
|
28
27
|
obj[key] = d;
|
|
29
28
|
}
|
|
@@ -35,11 +34,11 @@ function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErr
|
|
|
35
34
|
obj[key] = v.split(",");
|
|
36
35
|
}
|
|
37
36
|
else if (attr.type === "integers" || attr.type === "numbers") {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
for (
|
|
37
|
+
const s = v.split(",");
|
|
38
|
+
const nums = [];
|
|
39
|
+
for (let i = 0; i < s.length; i++) {
|
|
41
40
|
if (!isNaN(s[i])) {
|
|
42
|
-
|
|
41
|
+
const num = parseFloat(s[i]);
|
|
43
42
|
nums.push(num);
|
|
44
43
|
}
|
|
45
44
|
else if (includeErrorForNumbers) {
|
package/lib/index.js
CHANGED
|
@@ -4,43 +4,88 @@ function __export(m) {
|
|
|
4
4
|
}
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
__export(require("./form"));
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
resources.limits = [12, 24, 60, 100, 120, 180, 300, 600];
|
|
11
|
-
resources.page = "page";
|
|
12
|
-
resources.limit = "limit";
|
|
13
|
-
resources.defaultLimit = 12;
|
|
14
|
-
resources.sort = "sort";
|
|
15
|
-
return resources;
|
|
16
|
-
}());
|
|
7
|
+
class resources {
|
|
8
|
+
}
|
|
17
9
|
exports.resources = resources;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
10
|
+
resources.limits = [12, 24, 60, 100, 120, 180, 300, 600];
|
|
11
|
+
resources.page = "page";
|
|
12
|
+
resources.limit = "limit";
|
|
13
|
+
resources.defaultLimit = 12;
|
|
14
|
+
resources.sort = "sort";
|
|
15
|
+
function getRecordValue(v) {
|
|
16
|
+
if (typeof v === "string") {
|
|
17
|
+
return v;
|
|
18
|
+
}
|
|
19
|
+
else if (Array.isArray(v)) {
|
|
20
|
+
return v.length > 0 ? v[v.length - 1] : undefined;
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
exports.getRecordValue = getRecordValue;
|
|
25
|
+
function removePage(obj, pageKey) {
|
|
26
|
+
const arr = [];
|
|
27
|
+
const keys = Object.keys(obj);
|
|
28
|
+
const page = pageKey ? pageKey : resources.page;
|
|
29
|
+
for (const k of keys) {
|
|
30
|
+
if (k !== page) {
|
|
31
|
+
const v = obj[k];
|
|
32
|
+
if (typeof v === "string") {
|
|
33
|
+
arr.push(`${k}=${encodeURI(v)}`);
|
|
34
|
+
}
|
|
35
|
+
else if (Array.isArray(v)) {
|
|
36
|
+
const x = v;
|
|
37
|
+
if (x.length > 0) {
|
|
38
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
26
41
|
}
|
|
27
42
|
}
|
|
28
43
|
return arr.length === 0 ? "" : arr.join("&");
|
|
29
44
|
}
|
|
30
45
|
exports.removePage = removePage;
|
|
31
|
-
function removeSort(obj) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (k !==
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
function removeSort(obj, sortKey) {
|
|
47
|
+
const arr = [];
|
|
48
|
+
const keys = Object.keys(obj);
|
|
49
|
+
const sort = sortKey ? sortKey : resources.sort;
|
|
50
|
+
for (const k of keys) {
|
|
51
|
+
if (k !== sort) {
|
|
52
|
+
const v = obj[k];
|
|
53
|
+
if (typeof v === "string") {
|
|
54
|
+
arr.push(`${k}=${encodeURI(v)}`);
|
|
55
|
+
}
|
|
56
|
+
else if (Array.isArray(v)) {
|
|
57
|
+
const x = v;
|
|
58
|
+
if (x.length > 0) {
|
|
59
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
39
62
|
}
|
|
40
63
|
}
|
|
41
64
|
return arr.length === 0 ? "" : arr.join("&");
|
|
42
65
|
}
|
|
43
66
|
exports.removeSort = removeSort;
|
|
67
|
+
function removeLimit(obj, limitKey, pageKey) {
|
|
68
|
+
const arr = [];
|
|
69
|
+
const keys = Object.keys(obj);
|
|
70
|
+
const page = pageKey ? pageKey : resources.page;
|
|
71
|
+
const limit = limitKey ? limitKey : resources.limit;
|
|
72
|
+
for (const k of keys) {
|
|
73
|
+
if (k !== page && k !== limit) {
|
|
74
|
+
const v = obj[k];
|
|
75
|
+
if (typeof v === "string") {
|
|
76
|
+
arr.push(`${k}=${encodeURI(v)}`);
|
|
77
|
+
}
|
|
78
|
+
else if (Array.isArray(v)) {
|
|
79
|
+
const x = v;
|
|
80
|
+
if (x.length > 0) {
|
|
81
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return arr.length === 0 ? "" : arr.join("&");
|
|
87
|
+
}
|
|
88
|
+
exports.removeLimit = removeLimit;
|
|
44
89
|
function getSortString(field, sort) {
|
|
45
90
|
if (field === sort.field) {
|
|
46
91
|
return sort.type === "-" ? field : "-" + field;
|
|
@@ -48,25 +93,42 @@ function getSortString(field, sort) {
|
|
|
48
93
|
return field;
|
|
49
94
|
}
|
|
50
95
|
exports.getSortString = getSortString;
|
|
51
|
-
function buildFilter(obj, dates, nums, arr) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
96
|
+
function buildFilter(obj, defaultLimit, dates, nums, arr, limitKey, pageKey) {
|
|
97
|
+
const filter = fromParams(obj, arr);
|
|
98
|
+
const page = pageKey ? pageKey : resources.page;
|
|
99
|
+
const limit = limitKey ? limitKey : resources.limit;
|
|
100
|
+
filter[page] = getPage(filter[page]);
|
|
101
|
+
filter[limit] = getLimit(filter[limit], defaultLimit);
|
|
55
102
|
format(filter, dates, nums);
|
|
56
103
|
return filter;
|
|
57
104
|
}
|
|
58
105
|
exports.buildFilter = buildFilter;
|
|
59
106
|
function fromParams(obj, arr) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
for (
|
|
63
|
-
var key = keys_3[_i];
|
|
107
|
+
const s = {};
|
|
108
|
+
const keys = Object.keys(obj);
|
|
109
|
+
for (const key of keys) {
|
|
64
110
|
if (inArray(key, arr)) {
|
|
65
|
-
|
|
66
|
-
|
|
111
|
+
const v = obj[key];
|
|
112
|
+
if (typeof v === "string") {
|
|
113
|
+
const x = v.split(",");
|
|
114
|
+
setValue(s, key, x);
|
|
115
|
+
}
|
|
116
|
+
else if (Array.isArray(v)) {
|
|
117
|
+
const x = v;
|
|
118
|
+
setValue(s, key, x);
|
|
119
|
+
}
|
|
67
120
|
}
|
|
68
121
|
else {
|
|
69
|
-
|
|
122
|
+
const v = obj[key];
|
|
123
|
+
if (typeof v === "string") {
|
|
124
|
+
setValue(s, key, v);
|
|
125
|
+
}
|
|
126
|
+
else if (Array.isArray(v)) {
|
|
127
|
+
const x = v;
|
|
128
|
+
if (x.length > 0) {
|
|
129
|
+
setValue(s, key, x[x.length - 1]);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
70
132
|
}
|
|
71
133
|
}
|
|
72
134
|
return s;
|
|
@@ -76,8 +138,7 @@ function inArray(s, arr) {
|
|
|
76
138
|
if (!arr || arr.length === 0) {
|
|
77
139
|
return false;
|
|
78
140
|
}
|
|
79
|
-
for (
|
|
80
|
-
var a = arr_1[_i];
|
|
141
|
+
for (const a of arr) {
|
|
81
142
|
if (s === a) {
|
|
82
143
|
return true;
|
|
83
144
|
}
|
|
@@ -86,26 +147,26 @@ function inArray(s, arr) {
|
|
|
86
147
|
}
|
|
87
148
|
exports.inArray = inArray;
|
|
88
149
|
function setValue(o, key, value) {
|
|
89
|
-
|
|
90
|
-
|
|
150
|
+
const obj = o;
|
|
151
|
+
let replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".");
|
|
91
152
|
if (replaceKey.indexOf(".") === 0) {
|
|
92
153
|
replaceKey = replaceKey.slice(1, replaceKey.length);
|
|
93
154
|
}
|
|
94
|
-
|
|
95
|
-
|
|
155
|
+
const keys = replaceKey.split(".");
|
|
156
|
+
const firstKey = keys.shift();
|
|
96
157
|
if (!firstKey) {
|
|
97
158
|
return;
|
|
98
159
|
}
|
|
99
|
-
|
|
160
|
+
const isArrayKey = /\[([0-9]+)\]/.test(firstKey);
|
|
100
161
|
if (keys.length > 0) {
|
|
101
|
-
|
|
102
|
-
|
|
162
|
+
const firstKeyValue = obj[firstKey] || {};
|
|
163
|
+
const returnValue = setValue(firstKeyValue, keys.join("."), value);
|
|
103
164
|
return setKey(obj, isArrayKey, firstKey, returnValue);
|
|
104
165
|
}
|
|
105
166
|
return setKey(obj, isArrayKey, firstKey, value);
|
|
106
167
|
}
|
|
107
168
|
exports.setValue = setValue;
|
|
108
|
-
|
|
169
|
+
const setKey = (_object, _isArrayKey, _key, _nextValue) => {
|
|
109
170
|
if (_isArrayKey) {
|
|
110
171
|
if (_object.length > _key) {
|
|
111
172
|
_object[_key] = _nextValue;
|
|
@@ -119,8 +180,8 @@ var setKey = function (_object, _isArrayKey, _key, _nextValue) {
|
|
|
119
180
|
}
|
|
120
181
|
return _object;
|
|
121
182
|
};
|
|
122
|
-
|
|
123
|
-
|
|
183
|
+
const _datereg = "/Date(";
|
|
184
|
+
const _re = /-?\d+/;
|
|
124
185
|
function toDate(v) {
|
|
125
186
|
if (!v) {
|
|
126
187
|
return null;
|
|
@@ -131,11 +192,11 @@ function toDate(v) {
|
|
|
131
192
|
else if (typeof v === "number") {
|
|
132
193
|
return new Date(v);
|
|
133
194
|
}
|
|
134
|
-
|
|
195
|
+
const i = v.indexOf(_datereg);
|
|
135
196
|
if (i >= 0) {
|
|
136
|
-
|
|
197
|
+
const m = _re.exec(v);
|
|
137
198
|
if (m !== null) {
|
|
138
|
-
|
|
199
|
+
const d = parseInt(m[0], 10);
|
|
139
200
|
return new Date(d);
|
|
140
201
|
}
|
|
141
202
|
else {
|
|
@@ -147,23 +208,22 @@ function toDate(v) {
|
|
|
147
208
|
return new Date(v);
|
|
148
209
|
}
|
|
149
210
|
else {
|
|
150
|
-
|
|
211
|
+
const d = parseInt(v, 10);
|
|
151
212
|
return new Date(d);
|
|
152
213
|
}
|
|
153
214
|
}
|
|
154
215
|
}
|
|
155
216
|
function format(obj, dates, nums) {
|
|
156
|
-
|
|
217
|
+
const o = obj;
|
|
157
218
|
if (dates && dates.length > 0) {
|
|
158
|
-
for (
|
|
159
|
-
|
|
160
|
-
var v = o[s];
|
|
219
|
+
for (const s of dates) {
|
|
220
|
+
const v = o[s];
|
|
161
221
|
if (v) {
|
|
162
222
|
if (v instanceof Date) {
|
|
163
223
|
continue;
|
|
164
224
|
}
|
|
165
225
|
if (typeof v === "string" || typeof v === "number") {
|
|
166
|
-
|
|
226
|
+
const d = toDate(v);
|
|
167
227
|
if (d) {
|
|
168
228
|
if (!(d instanceof Date) || d.toString() === "Invalid Date") {
|
|
169
229
|
delete o[s];
|
|
@@ -174,15 +234,14 @@ function format(obj, dates, nums) {
|
|
|
174
234
|
}
|
|
175
235
|
}
|
|
176
236
|
else if (typeof v === "object") {
|
|
177
|
-
|
|
178
|
-
for (
|
|
179
|
-
|
|
180
|
-
var v2 = v[key];
|
|
237
|
+
const keys = Object.keys(v);
|
|
238
|
+
for (const key of keys) {
|
|
239
|
+
const v2 = v[key];
|
|
181
240
|
if (v2 instanceof Date) {
|
|
182
241
|
continue;
|
|
183
242
|
}
|
|
184
243
|
if (typeof v2 === "string" || typeof v2 === "number") {
|
|
185
|
-
|
|
244
|
+
const d2 = toDate(v2);
|
|
186
245
|
if (d2) {
|
|
187
246
|
if (!(d2 instanceof Date) || d2.toString() === "Invalid Date") {
|
|
188
247
|
delete v[key];
|
|
@@ -198,9 +257,8 @@ function format(obj, dates, nums) {
|
|
|
198
257
|
}
|
|
199
258
|
}
|
|
200
259
|
if (nums && nums.length > 0) {
|
|
201
|
-
for (
|
|
202
|
-
|
|
203
|
-
var v = o[s];
|
|
260
|
+
for (const s of nums) {
|
|
261
|
+
const v = o[s];
|
|
204
262
|
if (v) {
|
|
205
263
|
if (v instanceof Date) {
|
|
206
264
|
delete o[s];
|
|
@@ -215,15 +273,14 @@ function format(obj, dates, nums) {
|
|
|
215
273
|
continue;
|
|
216
274
|
}
|
|
217
275
|
else {
|
|
218
|
-
|
|
276
|
+
const i = parseFloat(v);
|
|
219
277
|
o[s] = i;
|
|
220
278
|
}
|
|
221
279
|
}
|
|
222
280
|
else if (typeof v === "object") {
|
|
223
|
-
|
|
224
|
-
for (
|
|
225
|
-
|
|
226
|
-
var v2 = v[key];
|
|
281
|
+
const keys = Object.keys(v);
|
|
282
|
+
for (const key of keys) {
|
|
283
|
+
const v2 = v[key];
|
|
227
284
|
if (v2 instanceof Date) {
|
|
228
285
|
delete o[key];
|
|
229
286
|
continue;
|
|
@@ -236,7 +293,7 @@ function format(obj, dates, nums) {
|
|
|
236
293
|
delete v[key];
|
|
237
294
|
}
|
|
238
295
|
else {
|
|
239
|
-
|
|
296
|
+
const i = parseFloat(v2);
|
|
240
297
|
v[key] = i;
|
|
241
298
|
}
|
|
242
299
|
}
|
|
@@ -261,8 +318,19 @@ function buildSort(s) {
|
|
|
261
318
|
}
|
|
262
319
|
exports.buildSort = buildSort;
|
|
263
320
|
function buildSortFromParams(params) {
|
|
264
|
-
|
|
265
|
-
|
|
321
|
+
const s = params[resources.sort];
|
|
322
|
+
if (s !== undefined) {
|
|
323
|
+
if (typeof s === "string") {
|
|
324
|
+
return buildSort(s);
|
|
325
|
+
}
|
|
326
|
+
else if (Array.isArray(s)) {
|
|
327
|
+
const x = s;
|
|
328
|
+
if (x.length > 0) {
|
|
329
|
+
return buildSort(x[x.length - 1]);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return buildSort(undefined);
|
|
266
334
|
}
|
|
267
335
|
exports.buildSortFromParams = buildSortFromParams;
|
|
268
336
|
function renderSort(field, sort) {
|
|
@@ -273,11 +341,11 @@ function renderSort(field, sort) {
|
|
|
273
341
|
}
|
|
274
342
|
exports.renderSort = renderSort;
|
|
275
343
|
function buildSortSearch(params, fields, sortStr) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
for (
|
|
344
|
+
const search = removeSort(params);
|
|
345
|
+
const sort = buildSort(sortStr);
|
|
346
|
+
let sorts = {};
|
|
347
|
+
const prefix = search.length > 0 ? "?" + search + "&" : "?";
|
|
348
|
+
for (let i = 0; i < fields.length; i++) {
|
|
281
349
|
sorts[fields[i]] = {
|
|
282
350
|
url: prefix + resources.sort + "=" + getSortString(fields[i], sort),
|
|
283
351
|
tag: renderSort(fields[i], sort),
|
|
@@ -286,20 +354,19 @@ function buildSortSearch(params, fields, sortStr) {
|
|
|
286
354
|
return sorts;
|
|
287
355
|
}
|
|
288
356
|
exports.buildSortSearch = buildSortSearch;
|
|
289
|
-
function formatInteger(v, groupSeparator) {
|
|
290
|
-
if (groupSeparator === void 0) { groupSeparator = ","; }
|
|
357
|
+
function formatInteger(v, groupSeparator = ",") {
|
|
291
358
|
if (v == null || !Number.isFinite(v)) {
|
|
292
359
|
return "";
|
|
293
360
|
}
|
|
294
|
-
|
|
295
|
-
|
|
361
|
+
const isNegative = v < 0;
|
|
362
|
+
let n = Math.abs(Math.trunc(v));
|
|
296
363
|
if (n < 1000) {
|
|
297
|
-
return isNegative ?
|
|
364
|
+
return isNegative ? `-${n}` : `${n}`;
|
|
298
365
|
}
|
|
299
|
-
|
|
300
|
-
|
|
366
|
+
let result = "";
|
|
367
|
+
let count = 0;
|
|
301
368
|
while (n > 0) {
|
|
302
|
-
|
|
369
|
+
const digit = n % 10;
|
|
303
370
|
n = (n / 10) | 0;
|
|
304
371
|
if (count > 0 && count % 3 === 0) {
|
|
305
372
|
result = groupSeparator + result;
|
|
@@ -307,7 +374,7 @@ function formatInteger(v, groupSeparator) {
|
|
|
307
374
|
result = digit + result;
|
|
308
375
|
count++;
|
|
309
376
|
}
|
|
310
|
-
return isNegative ?
|
|
377
|
+
return isNegative ? `-${result}` : result;
|
|
311
378
|
}
|
|
312
379
|
exports.formatInteger = formatInteger;
|
|
313
380
|
function formatNumber(v, scale, d, g) {
|
|
@@ -321,12 +388,12 @@ function formatNumber(v, scale, d, g) {
|
|
|
321
388
|
else if (!g) {
|
|
322
389
|
g = d === "," ? "." : ",";
|
|
323
390
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
for (
|
|
391
|
+
const s = scale === 0 || scale ? v.toFixed(scale) : v.toString();
|
|
392
|
+
const x = s.split(".", 2);
|
|
393
|
+
const y = x[0];
|
|
394
|
+
const arr = [];
|
|
395
|
+
const len = y.length - 1;
|
|
396
|
+
for (let k = 0; k < len; k++) {
|
|
330
397
|
arr.push(y[len - k]);
|
|
331
398
|
if ((k + 1) % 3 === 0) {
|
|
332
399
|
arr.push(g);
|
|
@@ -342,13 +409,13 @@ function formatNumber(v, scale, d, g) {
|
|
|
342
409
|
}
|
|
343
410
|
exports.formatNumber = formatNumber;
|
|
344
411
|
function getPage(page) {
|
|
345
|
-
|
|
412
|
+
const num = getNumber(page);
|
|
346
413
|
return num === undefined || num < 1 ? 1 : num;
|
|
347
414
|
}
|
|
348
415
|
exports.getPage = getPage;
|
|
349
|
-
function getLimit(limit) {
|
|
350
|
-
|
|
351
|
-
return num === undefined || num < 1 ?
|
|
416
|
+
function getLimit(limit, defaultLimit) {
|
|
417
|
+
const num = getNumber(limit);
|
|
418
|
+
return num === undefined || num < 1 ? defaultLimit : num;
|
|
352
419
|
}
|
|
353
420
|
exports.getLimit = getLimit;
|
|
354
421
|
function getNumber(num, defaultNum) {
|
|
@@ -366,19 +433,17 @@ function clone(obj) {
|
|
|
366
433
|
return obj;
|
|
367
434
|
}
|
|
368
435
|
if (Array.isArray(obj)) {
|
|
369
|
-
|
|
370
|
-
for (
|
|
371
|
-
|
|
372
|
-
var c = clone(sub_1);
|
|
436
|
+
const arr = [];
|
|
437
|
+
for (const sub of obj) {
|
|
438
|
+
const c = clone(sub);
|
|
373
439
|
arr.push(c);
|
|
374
440
|
}
|
|
375
441
|
return arr;
|
|
376
442
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
for (
|
|
380
|
-
|
|
381
|
-
var v = obj[k];
|
|
443
|
+
const x = {};
|
|
444
|
+
const keys = Object.keys(obj);
|
|
445
|
+
for (const k of keys) {
|
|
446
|
+
const v = obj[k];
|
|
382
447
|
if (v instanceof Date) {
|
|
383
448
|
x[k] = new Date(v.getTime());
|
|
384
449
|
}
|
|
@@ -400,29 +465,29 @@ function datetimeToString(date) {
|
|
|
400
465
|
if (!date || date === "") {
|
|
401
466
|
return undefined;
|
|
402
467
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
return year
|
|
468
|
+
const d2 = typeof date !== "string" ? date : new Date(date);
|
|
469
|
+
const year = d2.getFullYear();
|
|
470
|
+
const month = pad(d2.getMonth() + 1);
|
|
471
|
+
const day = pad(d2.getDate());
|
|
472
|
+
const hours = pad(d2.getHours());
|
|
473
|
+
const minutes = pad(d2.getMinutes());
|
|
474
|
+
const seconds = pad(d2.getSeconds());
|
|
475
|
+
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
|
|
411
476
|
}
|
|
412
477
|
exports.datetimeToString = datetimeToString;
|
|
413
478
|
function formatDate(d, format) {
|
|
414
479
|
if (!d || !format) {
|
|
415
480
|
return "";
|
|
416
481
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
482
|
+
const y = d.getFullYear();
|
|
483
|
+
const m = d.getMonth() + 1;
|
|
484
|
+
const day = d.getDate();
|
|
485
|
+
let out = "";
|
|
486
|
+
let i = 0;
|
|
422
487
|
while (i < format.length) {
|
|
423
|
-
|
|
488
|
+
const c = format.charCodeAt(i);
|
|
424
489
|
if (c === 121) {
|
|
425
|
-
|
|
490
|
+
const len = count(format, i, 121);
|
|
426
491
|
if (len >= 4) {
|
|
427
492
|
out += y.toString();
|
|
428
493
|
i += 4;
|
|
@@ -434,13 +499,13 @@ function formatDate(d, format) {
|
|
|
434
499
|
continue;
|
|
435
500
|
}
|
|
436
501
|
if (c === 77) {
|
|
437
|
-
|
|
502
|
+
const len = count(format, i, 77);
|
|
438
503
|
out += len >= 2 ? pad(m) : m.toString();
|
|
439
504
|
i += len >= 2 ? 2 : 1;
|
|
440
505
|
continue;
|
|
441
506
|
}
|
|
442
507
|
if (c === 100) {
|
|
443
|
-
|
|
508
|
+
const len = count(format, i, 100);
|
|
444
509
|
out += len >= 2 ? pad(day) : day.toString();
|
|
445
510
|
i += len >= 2 ? 2 : 1;
|
|
446
511
|
continue;
|
|
@@ -457,7 +522,7 @@ function shortYear(y) {
|
|
|
457
522
|
: "" + ((y % 100 + 100) % 100);
|
|
458
523
|
}
|
|
459
524
|
function count(s, i, ch) {
|
|
460
|
-
|
|
525
|
+
let n = 0;
|
|
461
526
|
while (i + n < s.length && s.charCodeAt(i + n) === ch) {
|
|
462
527
|
n++;
|
|
463
528
|
}
|
|
@@ -467,7 +532,7 @@ function formatDateTime(date, dateFormat) {
|
|
|
467
532
|
if (!date) {
|
|
468
533
|
return "";
|
|
469
534
|
}
|
|
470
|
-
|
|
535
|
+
const sd = formatDate(date, dateFormat);
|
|
471
536
|
if (sd.length === 0) {
|
|
472
537
|
return sd;
|
|
473
538
|
}
|
|
@@ -478,7 +543,7 @@ function formatLongDateTime(date, dateFormat) {
|
|
|
478
543
|
if (!date) {
|
|
479
544
|
return "";
|
|
480
545
|
}
|
|
481
|
-
|
|
546
|
+
const sd = formatDate(date, dateFormat);
|
|
482
547
|
if (sd.length === 0) {
|
|
483
548
|
return sd;
|
|
484
549
|
}
|
|
@@ -489,7 +554,7 @@ function formatFullDateTime(date, dateFormat, s) {
|
|
|
489
554
|
if (!date) {
|
|
490
555
|
return "";
|
|
491
556
|
}
|
|
492
|
-
|
|
557
|
+
const sd = formatDate(date, dateFormat);
|
|
493
558
|
if (sd.length === 0) {
|
|
494
559
|
return sd;
|
|
495
560
|
}
|
|
@@ -505,12 +570,12 @@ function formatLongTime(d) {
|
|
|
505
570
|
}
|
|
506
571
|
exports.formatLongTime = formatLongTime;
|
|
507
572
|
function formatFullTime(d, s) {
|
|
508
|
-
|
|
573
|
+
const se = s && s.length > 0 ? s : ".";
|
|
509
574
|
return formatLongTime(d) + se + pad3(d.getMilliseconds());
|
|
510
575
|
}
|
|
511
576
|
exports.formatFullTime = formatFullTime;
|
|
512
577
|
function dateToString(d, milli) {
|
|
513
|
-
|
|
578
|
+
const s = `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`;
|
|
514
579
|
if (milli) {
|
|
515
580
|
return s + pad3(d.getMilliseconds());
|
|
516
581
|
}
|
|
@@ -526,58 +591,54 @@ function pad3(n) {
|
|
|
526
591
|
}
|
|
527
592
|
return n < 10 ? "00" + n : "0" + n.toString();
|
|
528
593
|
}
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
}
|
|
532
|
-
formatter.removePhoneFormat = function (phone) {
|
|
594
|
+
class formatter {
|
|
595
|
+
static removePhoneFormat(phone) {
|
|
533
596
|
if (phone) {
|
|
534
597
|
return phone.replace(formatter.phone, "");
|
|
535
598
|
}
|
|
536
599
|
else {
|
|
537
600
|
return phone;
|
|
538
601
|
}
|
|
539
|
-
}
|
|
540
|
-
|
|
602
|
+
}
|
|
603
|
+
static formatPhone(phone) {
|
|
541
604
|
if (!phone) {
|
|
542
605
|
return "";
|
|
543
606
|
}
|
|
544
|
-
|
|
545
|
-
|
|
607
|
+
let s = phone;
|
|
608
|
+
const x = formatter.removePhoneFormat(phone);
|
|
546
609
|
if (x.length === 10) {
|
|
547
|
-
|
|
610
|
+
const USNumber = x.match(formatter.usPhone);
|
|
548
611
|
if (USNumber != null) {
|
|
549
|
-
s = USNumber[1]
|
|
612
|
+
s = `${USNumber[1]} ${USNumber[2]}-${USNumber[3]}`;
|
|
550
613
|
}
|
|
551
614
|
}
|
|
552
615
|
else if (x.length <= 3 && x.length > 0) {
|
|
553
616
|
s = x;
|
|
554
617
|
}
|
|
555
618
|
else if (x.length > 3 && x.length < 7) {
|
|
556
|
-
s = x.substring(0, 3)
|
|
619
|
+
s = `${x.substring(0, 3)} ${x.substring(3, x.length)}`;
|
|
557
620
|
}
|
|
558
621
|
else if (x.length >= 7 && x.length < 10) {
|
|
559
|
-
s = x.substring(0, 3)
|
|
622
|
+
s = `${x.substring(0, 3)} ${x.substring(3, 6)}-${x.substring(6, x.length)}`;
|
|
560
623
|
}
|
|
561
624
|
else if (x.length >= 11) {
|
|
562
|
-
|
|
563
|
-
s = x.substring(0, l - 7)
|
|
625
|
+
const l = x.length;
|
|
626
|
+
s = `${x.substring(0, l - 7)} ${x.substring(l - 7, l - 4)}-${x.substring(l - 4, l)}`;
|
|
564
627
|
}
|
|
565
628
|
return s;
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
formatter.usPhone = /(\d{3})(\d{3})(\d{4})/;
|
|
569
|
-
return formatter;
|
|
570
|
-
}());
|
|
629
|
+
}
|
|
630
|
+
}
|
|
571
631
|
exports.formatter = formatter;
|
|
632
|
+
formatter.phone = / |\-|\.|\(|\)/g;
|
|
633
|
+
formatter.usPhone = /(\d{3})(\d{3})(\d{4})/;
|
|
572
634
|
function formatPhone(phone) {
|
|
573
635
|
return formatter.formatPhone(phone);
|
|
574
636
|
}
|
|
575
637
|
exports.formatPhone = formatPhone;
|
|
576
638
|
function rebuildPath(items, lang) {
|
|
577
|
-
for (
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
var children = item.children;
|
|
639
|
+
for (const item of items) {
|
|
640
|
+
item.path = item.type === "content" ? `/${lang}${item.path}` : `${item.path}?lang=${lang}`;
|
|
641
|
+
const children = item.children;
|
|
581
642
|
if (children && children.length > 0) {
|
|
582
643
|
rebuildPath(children, lang);
|
|
583
644
|
}
|
|
@@ -604,18 +665,16 @@ function subMenuItem(p1, p2) {
|
|
|
604
665
|
return sub(p1.sequence, p2.sequence);
|
|
605
666
|
}
|
|
606
667
|
function toMenuItems(m) {
|
|
607
|
-
|
|
608
|
-
for (
|
|
609
|
-
var p = ps_1[_i];
|
|
668
|
+
const ps = getRoot(m);
|
|
669
|
+
for (const p of ps) {
|
|
610
670
|
getChildren(p, m);
|
|
611
671
|
}
|
|
612
672
|
return ps.sort(subMenuItem);
|
|
613
673
|
}
|
|
614
674
|
exports.toMenuItems = toMenuItems;
|
|
615
675
|
function getRoot(ms) {
|
|
616
|
-
|
|
617
|
-
for (
|
|
618
|
-
var m = ms_1[_i];
|
|
676
|
+
const ps = [];
|
|
677
|
+
for (const m of ms) {
|
|
619
678
|
if (!m.parent || m.parent.length === 0) {
|
|
620
679
|
delete m.parent;
|
|
621
680
|
ps.push(m);
|
|
@@ -624,9 +683,8 @@ function getRoot(ms) {
|
|
|
624
683
|
return ps.sort(subMenuItem);
|
|
625
684
|
}
|
|
626
685
|
function getChildren(m, all) {
|
|
627
|
-
|
|
628
|
-
for (
|
|
629
|
-
var s = all_1[_i];
|
|
686
|
+
const children = [];
|
|
687
|
+
for (const s of all) {
|
|
630
688
|
if (s.parent === m.id) {
|
|
631
689
|
delete s.parent;
|
|
632
690
|
children.push(s);
|
|
@@ -638,3 +696,7 @@ function getChildren(m, all) {
|
|
|
638
696
|
m.children = children;
|
|
639
697
|
}
|
|
640
698
|
}
|
|
699
|
+
function cloneArray(arr) {
|
|
700
|
+
return arr.map(item => (Object.assign({}, item)));
|
|
701
|
+
}
|
|
702
|
+
exports.cloneArray = cloneArray;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -7,36 +7,75 @@ export class resources {
|
|
|
7
7
|
static defaultLimit = 12
|
|
8
8
|
static sort = "sort"
|
|
9
9
|
}
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
export function getRecordValue(v: string | string[] | undefined): string | undefined {
|
|
11
|
+
if (typeof v === "string") {
|
|
12
|
+
return v
|
|
13
|
+
} else if (Array.isArray(v)) {
|
|
14
|
+
return v.length > 0 ? v[v.length - 1] : undefined
|
|
15
|
+
}
|
|
16
|
+
return undefined
|
|
16
17
|
}
|
|
17
|
-
export
|
|
18
|
+
export type StringMap = Record<string, string | string[] | undefined>
|
|
19
|
+
export function removePage(obj: Record<string, string | string[] | undefined>, pageKey?: string): string {
|
|
18
20
|
const arr: string[] = []
|
|
19
21
|
const keys = Object.keys(obj)
|
|
22
|
+
const page = pageKey ? pageKey : resources.page
|
|
20
23
|
for (const k of keys) {
|
|
21
|
-
if (k !==
|
|
24
|
+
if (k !== page) {
|
|
22
25
|
const v = obj[k]
|
|
23
|
-
|
|
26
|
+
if (typeof v === "string") {
|
|
27
|
+
arr.push(`${k}=${encodeURI(v)}`)
|
|
28
|
+
} else if (Array.isArray(v)) {
|
|
29
|
+
const x = v as string[]
|
|
30
|
+
if (x.length > 0) {
|
|
31
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
24
34
|
}
|
|
25
35
|
}
|
|
26
36
|
return arr.length === 0 ? "" : arr.join("&")
|
|
27
37
|
}
|
|
28
38
|
|
|
29
|
-
export function removeSort(obj:
|
|
39
|
+
export function removeSort(obj: Record<string, string | string[] | undefined>, sortKey?: string): string {
|
|
40
|
+
const arr: string[] = []
|
|
41
|
+
const keys = Object.keys(obj)
|
|
42
|
+
const sort = sortKey ? sortKey : resources.sort
|
|
43
|
+
for (const k of keys) {
|
|
44
|
+
if (k !== sort) {
|
|
45
|
+
const v = obj[k]
|
|
46
|
+
if (typeof v === "string") {
|
|
47
|
+
arr.push(`${k}=${encodeURI(v)}`)
|
|
48
|
+
} else if (Array.isArray(v)) {
|
|
49
|
+
const x = v as string[]
|
|
50
|
+
if (x.length > 0) {
|
|
51
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return arr.length === 0 ? "" : arr.join("&")
|
|
57
|
+
}
|
|
58
|
+
export function removeLimit(obj: Record<string, string | string[] | undefined>, limitKey?: string, pageKey?: string): string {
|
|
30
59
|
const arr: string[] = []
|
|
31
60
|
const keys = Object.keys(obj)
|
|
61
|
+
const page = pageKey ? pageKey : resources.page
|
|
62
|
+
const limit = limitKey ? limitKey : resources.limit
|
|
32
63
|
for (const k of keys) {
|
|
33
|
-
if (k !==
|
|
64
|
+
if (k !== page && k !== limit) {
|
|
34
65
|
const v = obj[k]
|
|
35
|
-
|
|
66
|
+
if (typeof v === "string") {
|
|
67
|
+
arr.push(`${k}=${encodeURI(v)}`)
|
|
68
|
+
} else if (Array.isArray(v)) {
|
|
69
|
+
const x = v as string[]
|
|
70
|
+
if (x.length > 0) {
|
|
71
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
36
74
|
}
|
|
37
75
|
}
|
|
38
76
|
return arr.length === 0 ? "" : arr.join("&")
|
|
39
77
|
}
|
|
78
|
+
|
|
40
79
|
export type SearchParams = {
|
|
41
80
|
q?: string
|
|
42
81
|
page?: string
|
|
@@ -60,22 +99,39 @@ export function getSortString(field: string, sort: Sort): string {
|
|
|
60
99
|
}
|
|
61
100
|
return field
|
|
62
101
|
}
|
|
63
|
-
export function buildFilter<T>(obj:
|
|
102
|
+
export function buildFilter<T>(obj: Record<string, string | string[] | undefined>, defaultLimit: number,dates?: string[], nums?: string[], arr?: string[], limitKey?: string, pageKey?: string): T {
|
|
64
103
|
const filter: any = fromParams<T>(obj, arr)
|
|
65
|
-
|
|
66
|
-
|
|
104
|
+
const page = pageKey ? pageKey : resources.page
|
|
105
|
+
const limit = limitKey ? limitKey : resources.limit
|
|
106
|
+
filter[page] = getPage(filter[page] as string)
|
|
107
|
+
filter[limit] = getLimit(filter[limit] as string, defaultLimit)
|
|
67
108
|
format(filter, dates, nums)
|
|
68
109
|
return filter
|
|
69
110
|
}
|
|
70
|
-
export function fromParams<T>(obj:
|
|
111
|
+
export function fromParams<T>(obj: Record<string, string | string[] | undefined>, arr?: string[]): T {
|
|
71
112
|
const s: any = {}
|
|
72
113
|
const keys = Object.keys(obj)
|
|
73
114
|
for (const key of keys) {
|
|
74
115
|
if (inArray(key, arr)) {
|
|
75
|
-
const
|
|
76
|
-
|
|
116
|
+
const v = obj[key]
|
|
117
|
+
if (typeof v === "string") {
|
|
118
|
+
const x = v.split(",")
|
|
119
|
+
setValue(s, key, x)
|
|
120
|
+
} else if (Array.isArray(v)) {
|
|
121
|
+
const x: string[] = v as string[]
|
|
122
|
+
setValue(s, key, x)
|
|
123
|
+
}
|
|
124
|
+
|
|
77
125
|
} else {
|
|
78
|
-
|
|
126
|
+
const v = obj[key]
|
|
127
|
+
if (typeof v === "string") {
|
|
128
|
+
setValue(s, key, v)
|
|
129
|
+
} else if (Array.isArray(v)) {
|
|
130
|
+
const x: string[] = v as string[]
|
|
131
|
+
if (x.length > 0) {
|
|
132
|
+
setValue(s, key, x[x.length - 1])
|
|
133
|
+
}
|
|
134
|
+
}
|
|
79
135
|
}
|
|
80
136
|
}
|
|
81
137
|
return s
|
|
@@ -247,9 +303,19 @@ export function buildSort(s?: string): Sort {
|
|
|
247
303
|
return { field: s.startsWith("+") ? s.substring(1) : s, type: "+" }
|
|
248
304
|
}
|
|
249
305
|
}
|
|
250
|
-
export function buildSortFromParams(params:
|
|
251
|
-
const s = params[resources.sort]
|
|
252
|
-
|
|
306
|
+
export function buildSortFromParams(params: Record<string, string | string[] | undefined>): Sort {
|
|
307
|
+
const s = params[resources.sort]
|
|
308
|
+
if (s !== undefined) {
|
|
309
|
+
if (typeof s === "string") {
|
|
310
|
+
return buildSort(s)
|
|
311
|
+
} else if (Array.isArray(s)) {
|
|
312
|
+
const x: string[] = s as string[]
|
|
313
|
+
if (x.length > 0) {
|
|
314
|
+
return buildSort(x[x.length - 1])
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return buildSort(undefined)
|
|
253
319
|
}
|
|
254
320
|
export function renderSort(field: string, sort: Sort): string {
|
|
255
321
|
if (field === sort.field) {
|
|
@@ -257,7 +323,7 @@ export function renderSort(field: string, sort: Sort): string {
|
|
|
257
323
|
}
|
|
258
324
|
return ""
|
|
259
325
|
}
|
|
260
|
-
export function buildSortSearch(params:
|
|
326
|
+
export function buildSortSearch(params: Record<string, string | string[] | undefined>, fields: string[], sortStr?: string): SortMap {
|
|
261
327
|
const search = removeSort(params)
|
|
262
328
|
const sort = buildSort(sortStr)
|
|
263
329
|
let sorts: SortMap = {}
|
|
@@ -334,9 +400,9 @@ export function getPage(page?: string): number {
|
|
|
334
400
|
const num = getNumber(page)
|
|
335
401
|
return num === undefined || num < 1 ? 1 : num
|
|
336
402
|
}
|
|
337
|
-
export function getLimit(limit
|
|
403
|
+
export function getLimit(limit: string | undefined, defaultLimit: number): number {
|
|
338
404
|
const num = getNumber(limit)
|
|
339
|
-
return num === undefined || num < 1 ?
|
|
405
|
+
return num === undefined || num < 1 ? defaultLimit : num
|
|
340
406
|
}
|
|
341
407
|
export function getNumber(num?: string, defaultNum?: number): number | undefined {
|
|
342
408
|
return !num || num.length === 0 ? defaultNum : isNaN(num as any) ? undefined : parseInt(num, 10)
|
|
@@ -631,3 +697,7 @@ function getChildren(m: Category, all: Category[]) {
|
|
|
631
697
|
m.children = children
|
|
632
698
|
}
|
|
633
699
|
}
|
|
700
|
+
|
|
701
|
+
export function cloneArray<T>(arr: T[]): T[] {
|
|
702
|
+
return arr.map(item => ({ ...item }));
|
|
703
|
+
}
|