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