web-one 0.0.2 → 0.0.4
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 +11 -10
- package/lib/index.js +172 -71
- package/package.json +2 -2
- package/src/index.ts +197 -66
- package/tsconfig.json +1 -1
package/lib/form.js
CHANGED
|
@@ -8,11 +8,12 @@ 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
|
-
|
|
11
|
+
var obj = {};
|
|
12
|
+
var keys = formData.keys();
|
|
13
|
+
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
14
|
+
var key = keys_1[_i];
|
|
15
|
+
var attr = attrs[key];
|
|
16
|
+
var v = formData.get(key);
|
|
16
17
|
if (attr) {
|
|
17
18
|
obj[key] = v;
|
|
18
19
|
if (v && typeof v === "string") {
|
|
@@ -22,7 +23,7 @@ function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErr
|
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
else if (attr.type === "datetime" || attr.type === "date") {
|
|
25
|
-
|
|
26
|
+
var d = new Date(v);
|
|
26
27
|
if (d.toString() !== "Invalid Date") {
|
|
27
28
|
obj[key] = d;
|
|
28
29
|
}
|
|
@@ -34,11 +35,11 @@ function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErr
|
|
|
34
35
|
obj[key] = v.split(",");
|
|
35
36
|
}
|
|
36
37
|
else if (attr.type === "integers" || attr.type === "numbers") {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
for (
|
|
38
|
+
var s = v.split(",");
|
|
39
|
+
var nums = [];
|
|
40
|
+
for (var i = 0; i < s.length; i++) {
|
|
40
41
|
if (!isNaN(s[i])) {
|
|
41
|
-
|
|
42
|
+
var num = parseFloat(s[i]);
|
|
42
43
|
nums.push(num);
|
|
43
44
|
}
|
|
44
45
|
else if (includeErrorForNumbers) {
|
package/lib/index.js
CHANGED
|
@@ -286,10 +286,61 @@ function buildSortSearch(params, fields, sortStr) {
|
|
|
286
286
|
return sorts;
|
|
287
287
|
}
|
|
288
288
|
exports.buildSortSearch = buildSortSearch;
|
|
289
|
-
function
|
|
290
|
-
|
|
289
|
+
function formatInteger(v, groupSeparator) {
|
|
290
|
+
if (groupSeparator === void 0) { groupSeparator = ","; }
|
|
291
|
+
if (v == null || !Number.isFinite(v)) {
|
|
292
|
+
return "";
|
|
293
|
+
}
|
|
294
|
+
var isNegative = v < 0;
|
|
295
|
+
var n = Math.abs(Math.trunc(v));
|
|
296
|
+
if (n < 1000) {
|
|
297
|
+
return isNegative ? "-" + n : "" + n;
|
|
298
|
+
}
|
|
299
|
+
var result = "";
|
|
300
|
+
var count = 0;
|
|
301
|
+
while (n > 0) {
|
|
302
|
+
var digit = n % 10;
|
|
303
|
+
n = (n / 10) | 0;
|
|
304
|
+
if (count > 0 && count % 3 === 0) {
|
|
305
|
+
result = groupSeparator + result;
|
|
306
|
+
}
|
|
307
|
+
result = digit + result;
|
|
308
|
+
count++;
|
|
309
|
+
}
|
|
310
|
+
return isNegative ? "-" + result : result;
|
|
311
|
+
}
|
|
312
|
+
exports.formatInteger = formatInteger;
|
|
313
|
+
function formatNumber(v, scale, d, g) {
|
|
314
|
+
if (v == null) {
|
|
315
|
+
return "";
|
|
316
|
+
}
|
|
317
|
+
if (!d && !g) {
|
|
318
|
+
g = ",";
|
|
319
|
+
d = ".";
|
|
320
|
+
}
|
|
321
|
+
else if (!g) {
|
|
322
|
+
g = d === "," ? "." : ",";
|
|
323
|
+
}
|
|
324
|
+
var s = scale === 0 || scale ? v.toFixed(scale) : v.toString();
|
|
325
|
+
var x = s.split(".", 2);
|
|
326
|
+
var y = x[0];
|
|
327
|
+
var arr = [];
|
|
328
|
+
var len = y.length - 1;
|
|
329
|
+
for (var k = 0; k < len; k++) {
|
|
330
|
+
arr.push(y[len - k]);
|
|
331
|
+
if ((k + 1) % 3 === 0) {
|
|
332
|
+
arr.push(g);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
arr.push(y[0]);
|
|
336
|
+
if (x.length === 1) {
|
|
337
|
+
return arr.reverse().join("");
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
return arr.reverse().join("") + d + x[1];
|
|
341
|
+
}
|
|
291
342
|
}
|
|
292
|
-
exports.
|
|
343
|
+
exports.formatNumber = formatNumber;
|
|
293
344
|
function getPage(page) {
|
|
294
345
|
var num = getNumber(page);
|
|
295
346
|
return num === undefined || num < 1 ? 1 : num;
|
|
@@ -317,8 +368,8 @@ function clone(obj) {
|
|
|
317
368
|
if (Array.isArray(obj)) {
|
|
318
369
|
var arr = [];
|
|
319
370
|
for (var _i = 0, obj_1 = obj; _i < obj_1.length; _i++) {
|
|
320
|
-
var
|
|
321
|
-
var c = clone(
|
|
371
|
+
var sub_1 = obj_1[_i];
|
|
372
|
+
var c = clone(sub_1);
|
|
322
373
|
arr.push(c);
|
|
323
374
|
}
|
|
324
375
|
return arr;
|
|
@@ -359,101 +410,86 @@ function datetimeToString(date) {
|
|
|
359
410
|
return year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
|
|
360
411
|
}
|
|
361
412
|
exports.datetimeToString = datetimeToString;
|
|
362
|
-
function formatDate(d,
|
|
363
|
-
if (!d) {
|
|
413
|
+
function formatDate(d, format) {
|
|
414
|
+
if (!d || !format) {
|
|
364
415
|
return "";
|
|
365
416
|
}
|
|
366
|
-
var
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
var
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
fm = true;
|
|
385
|
-
}
|
|
386
|
-
if (iyear === -1) {
|
|
387
|
-
iyear = items.indexOf("YY");
|
|
388
|
-
fy = full ? full : false;
|
|
389
|
-
}
|
|
390
|
-
arr[iday] = getD(d.getDate(), fd);
|
|
391
|
-
arr[im] = getD(d.getMonth() + 1, fm);
|
|
392
|
-
arr[iyear] = getYear(d.getFullYear(), fy);
|
|
393
|
-
var s = detectSeparator(format);
|
|
394
|
-
var e = detectLastSeparator(format);
|
|
395
|
-
var l = items.length === 4 ? format[format.length - 1] : "";
|
|
396
|
-
return arr[0] + s + arr[1] + e + arr[2] + l;
|
|
397
|
-
}
|
|
398
|
-
exports.formatDate = formatDate;
|
|
399
|
-
function detectSeparator(format) {
|
|
400
|
-
var len = format.length;
|
|
401
|
-
for (var i = 0; i < len; i++) {
|
|
402
|
-
var c = format[i];
|
|
403
|
-
if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
|
|
404
|
-
return c;
|
|
417
|
+
var y = d.getFullYear();
|
|
418
|
+
var m = d.getMonth() + 1;
|
|
419
|
+
var day = d.getDate();
|
|
420
|
+
var out = "";
|
|
421
|
+
var i = 0;
|
|
422
|
+
while (i < format.length) {
|
|
423
|
+
var c = format.charCodeAt(i);
|
|
424
|
+
if (c === 121) {
|
|
425
|
+
var len = count(format, i, 121);
|
|
426
|
+
if (len >= 4) {
|
|
427
|
+
out += y.toString();
|
|
428
|
+
i += 4;
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
out += shortYear(y);
|
|
432
|
+
i += 2;
|
|
433
|
+
}
|
|
434
|
+
continue;
|
|
405
435
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
436
|
+
if (c === 77) {
|
|
437
|
+
var len = count(format, i, 77);
|
|
438
|
+
out += len >= 2 ? pad(m) : m.toString();
|
|
439
|
+
i += len >= 2 ? 2 : 1;
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
if (c === 100) {
|
|
443
|
+
var len = count(format, i, 100);
|
|
444
|
+
out += len >= 2 ? pad(day) : day.toString();
|
|
445
|
+
i += len >= 2 ? 2 : 1;
|
|
446
|
+
continue;
|
|
415
447
|
}
|
|
448
|
+
out += format[i];
|
|
449
|
+
i++;
|
|
416
450
|
}
|
|
417
|
-
return
|
|
451
|
+
return out;
|
|
418
452
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
return s.substring(s.length - 2);
|
|
453
|
+
exports.formatDate = formatDate;
|
|
454
|
+
function shortYear(y) {
|
|
455
|
+
return (y % 100 + 100) % 100 < 10
|
|
456
|
+
? "0" + ((y % 100 + 100) % 100)
|
|
457
|
+
: "" + ((y % 100 + 100) % 100);
|
|
425
458
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
459
|
+
function count(s, i, ch) {
|
|
460
|
+
var n = 0;
|
|
461
|
+
while (i + n < s.length && s.charCodeAt(i + n) === ch) {
|
|
462
|
+
n++;
|
|
463
|
+
}
|
|
464
|
+
return n;
|
|
429
465
|
}
|
|
430
|
-
function formatDateTime(date, dateFormat
|
|
466
|
+
function formatDateTime(date, dateFormat) {
|
|
431
467
|
if (!date) {
|
|
432
468
|
return "";
|
|
433
469
|
}
|
|
434
|
-
var sd = formatDate(date, dateFormat
|
|
470
|
+
var sd = formatDate(date, dateFormat);
|
|
435
471
|
if (sd.length === 0) {
|
|
436
472
|
return sd;
|
|
437
473
|
}
|
|
438
474
|
return sd + " " + formatTime(date);
|
|
439
475
|
}
|
|
440
476
|
exports.formatDateTime = formatDateTime;
|
|
441
|
-
function formatLongDateTime(date, dateFormat
|
|
477
|
+
function formatLongDateTime(date, dateFormat) {
|
|
442
478
|
if (!date) {
|
|
443
479
|
return "";
|
|
444
480
|
}
|
|
445
|
-
var sd = formatDate(date, dateFormat
|
|
481
|
+
var sd = formatDate(date, dateFormat);
|
|
446
482
|
if (sd.length === 0) {
|
|
447
483
|
return sd;
|
|
448
484
|
}
|
|
449
485
|
return sd + " " + formatLongTime(date);
|
|
450
486
|
}
|
|
451
487
|
exports.formatLongDateTime = formatLongDateTime;
|
|
452
|
-
function formatFullDateTime(date, dateFormat, s
|
|
488
|
+
function formatFullDateTime(date, dateFormat, s) {
|
|
453
489
|
if (!date) {
|
|
454
490
|
return "";
|
|
455
491
|
}
|
|
456
|
-
var sd = formatDate(date, dateFormat
|
|
492
|
+
var sd = formatDate(date, dateFormat);
|
|
457
493
|
if (sd.length === 0) {
|
|
458
494
|
return sd;
|
|
459
495
|
}
|
|
@@ -537,3 +573,68 @@ function formatPhone(phone) {
|
|
|
537
573
|
return formatter.formatPhone(phone);
|
|
538
574
|
}
|
|
539
575
|
exports.formatPhone = formatPhone;
|
|
576
|
+
function rebuildPath(items, lang) {
|
|
577
|
+
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
|
|
578
|
+
var item = items_1[_i];
|
|
579
|
+
item.path = item.type === "content" ? "/" + lang + item.path : item.path + "?lang=" + lang;
|
|
580
|
+
var children = item.children;
|
|
581
|
+
if (children && children.length > 0) {
|
|
582
|
+
rebuildPath(children, lang);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
exports.rebuildPath = rebuildPath;
|
|
587
|
+
function sub(n1, n2) {
|
|
588
|
+
if (!n1 && !n2) {
|
|
589
|
+
return 0;
|
|
590
|
+
}
|
|
591
|
+
else if (n1 && n2) {
|
|
592
|
+
return n1 - n2;
|
|
593
|
+
}
|
|
594
|
+
else if (n1) {
|
|
595
|
+
return n1;
|
|
596
|
+
}
|
|
597
|
+
else if (n2) {
|
|
598
|
+
return -n2;
|
|
599
|
+
}
|
|
600
|
+
return 0;
|
|
601
|
+
}
|
|
602
|
+
exports.sub = sub;
|
|
603
|
+
function subMenuItem(p1, p2) {
|
|
604
|
+
return sub(p1.sequence, p2.sequence);
|
|
605
|
+
}
|
|
606
|
+
function toMenuItems(m) {
|
|
607
|
+
var ps = getRoot(m);
|
|
608
|
+
for (var _i = 0, ps_1 = ps; _i < ps_1.length; _i++) {
|
|
609
|
+
var p = ps_1[_i];
|
|
610
|
+
getChildren(p, m);
|
|
611
|
+
}
|
|
612
|
+
return ps.sort(subMenuItem);
|
|
613
|
+
}
|
|
614
|
+
exports.toMenuItems = toMenuItems;
|
|
615
|
+
function getRoot(ms) {
|
|
616
|
+
var ps = [];
|
|
617
|
+
for (var _i = 0, ms_1 = ms; _i < ms_1.length; _i++) {
|
|
618
|
+
var m = ms_1[_i];
|
|
619
|
+
if (!m.parent || m.parent.length === 0) {
|
|
620
|
+
delete m.parent;
|
|
621
|
+
ps.push(m);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return ps.sort(subMenuItem);
|
|
625
|
+
}
|
|
626
|
+
function getChildren(m, all) {
|
|
627
|
+
var children = [];
|
|
628
|
+
for (var _i = 0, all_1 = all; _i < all_1.length; _i++) {
|
|
629
|
+
var s = all_1[_i];
|
|
630
|
+
if (s.parent === m.id) {
|
|
631
|
+
delete s.parent;
|
|
632
|
+
children.push(s);
|
|
633
|
+
getChildren(s, all);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
if (children.length > 0) {
|
|
637
|
+
children.sort(subMenuItem);
|
|
638
|
+
m.children = children;
|
|
639
|
+
}
|
|
640
|
+
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -271,9 +271,65 @@ export function buildSortSearch(params: StringMap, fields: string[], sortStr?: s
|
|
|
271
271
|
return sorts
|
|
272
272
|
}
|
|
273
273
|
|
|
274
|
-
export function
|
|
275
|
-
|
|
274
|
+
export function formatInteger(v?: number | null, groupSeparator: string = ","): string {
|
|
275
|
+
if (v == null || !Number.isFinite(v)) {
|
|
276
|
+
return ""
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const isNegative = v < 0
|
|
280
|
+
let n = Math.abs(Math.trunc(v))
|
|
281
|
+
|
|
282
|
+
// Fast path for small numbers (no separator needed)
|
|
283
|
+
if (n < 1000) {
|
|
284
|
+
return isNegative ? `-${n}` : `${n}`
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
let result = ""
|
|
288
|
+
let count = 0
|
|
289
|
+
|
|
290
|
+
while (n > 0) {
|
|
291
|
+
const digit = n % 10
|
|
292
|
+
n = (n / 10) | 0 // faster floor for positive integers
|
|
293
|
+
|
|
294
|
+
if (count > 0 && count % 3 === 0) {
|
|
295
|
+
result = groupSeparator + result
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
result = digit + result
|
|
299
|
+
count++
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return isNegative ? `-${result}` : result
|
|
276
303
|
}
|
|
304
|
+
export function formatNumber(v?: number | null, scale?: number, d?: string | null, g?: string): string {
|
|
305
|
+
if (v == null) {
|
|
306
|
+
return ""
|
|
307
|
+
}
|
|
308
|
+
if (!d && !g) {
|
|
309
|
+
g = ","
|
|
310
|
+
d = "."
|
|
311
|
+
} else if (!g) {
|
|
312
|
+
g = d === "," ? "." : ","
|
|
313
|
+
}
|
|
314
|
+
const s = scale === 0 || scale ? v.toFixed(scale) : v.toString()
|
|
315
|
+
const x = s.split(".", 2)
|
|
316
|
+
const y = x[0]
|
|
317
|
+
const arr: string[] = []
|
|
318
|
+
const len = y.length - 1
|
|
319
|
+
for (let k = 0; k < len; k++) {
|
|
320
|
+
arr.push(y[len - k])
|
|
321
|
+
if ((k + 1) % 3 === 0) {
|
|
322
|
+
arr.push(g)
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
arr.push(y[0])
|
|
326
|
+
if (x.length === 1) {
|
|
327
|
+
return arr.reverse().join("")
|
|
328
|
+
} else {
|
|
329
|
+
return arr.reverse().join("") + d + x[1]
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
277
333
|
export function getPage(page?: string): number {
|
|
278
334
|
const num = getNumber(page)
|
|
279
335
|
return num === undefined || num < 1 ? 1 : num
|
|
@@ -337,97 +393,92 @@ export function datetimeToString(date?: Date | string): string | undefined {
|
|
|
337
393
|
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
|
|
338
394
|
}
|
|
339
395
|
|
|
340
|
-
export function formatDate(d: Date | null | undefined,
|
|
341
|
-
if (!d) {
|
|
396
|
+
export function formatDate(d: Date | null | undefined, format?: string): string {
|
|
397
|
+
if (!d || !format) {
|
|
342
398
|
return ""
|
|
343
399
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
let
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
iyear = items.indexOf("YY")
|
|
366
|
-
fy = full ? full : false
|
|
367
|
-
}
|
|
368
|
-
arr[iday] = getD(d.getDate(), fd)
|
|
369
|
-
arr[im] = getD(d.getMonth() + 1, fm)
|
|
370
|
-
arr[iyear] = getYear(d.getFullYear(), fy)
|
|
371
|
-
const s = detectSeparator(format)
|
|
372
|
-
const e = detectLastSeparator(format)
|
|
373
|
-
const l = items.length === 4 ? format[format.length - 1] : ""
|
|
374
|
-
return arr[0] + s + arr[1] + e + arr[2] + l
|
|
375
|
-
}
|
|
376
|
-
function detectSeparator(format: string): string {
|
|
377
|
-
const len = format.length
|
|
378
|
-
for (let i = 0; i < len; i++) {
|
|
379
|
-
const c = format[i]
|
|
380
|
-
if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
|
|
381
|
-
return c
|
|
400
|
+
const y = d.getFullYear()
|
|
401
|
+
const m = d.getMonth() + 1
|
|
402
|
+
const day = d.getDate()
|
|
403
|
+
|
|
404
|
+
let out = ""
|
|
405
|
+
let i = 0
|
|
406
|
+
|
|
407
|
+
while (i < format.length) {
|
|
408
|
+
const c = format.charCodeAt(i)
|
|
409
|
+
|
|
410
|
+
// yyyy / yy
|
|
411
|
+
if (c === 121 /* y */) {
|
|
412
|
+
const len = count(format, i, 121)
|
|
413
|
+
if (len >= 4) {
|
|
414
|
+
out += y.toString()
|
|
415
|
+
i += 4
|
|
416
|
+
} else {
|
|
417
|
+
out += shortYear(y)
|
|
418
|
+
i += 2
|
|
419
|
+
}
|
|
420
|
+
continue
|
|
382
421
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
422
|
+
|
|
423
|
+
// MM / M
|
|
424
|
+
if (c === 77 /* M */) {
|
|
425
|
+
const len = count(format, i, 77)
|
|
426
|
+
out += len >= 2 ? pad(m) : m.toString()
|
|
427
|
+
i += len >= 2 ? 2 : 1
|
|
428
|
+
continue
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// dd / d
|
|
432
|
+
if (c === 100 /* d */) {
|
|
433
|
+
const len = count(format, i, 100)
|
|
434
|
+
out += len >= 2 ? pad(day) : day.toString()
|
|
435
|
+
i += len >= 2 ? 2 : 1
|
|
436
|
+
continue
|
|
392
437
|
}
|
|
438
|
+
|
|
439
|
+
// literal char
|
|
440
|
+
out += format[i]
|
|
441
|
+
i++
|
|
393
442
|
}
|
|
394
|
-
return
|
|
443
|
+
return out
|
|
395
444
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
const s = y.toString()
|
|
401
|
-
return s.substring(s.length - 2)
|
|
445
|
+
function shortYear(y: number): string {
|
|
446
|
+
return (y % 100 + 100) % 100 < 10
|
|
447
|
+
? "0" + ((y % 100 + 100) % 100)
|
|
448
|
+
: "" + ((y % 100 + 100) % 100)
|
|
402
449
|
}
|
|
403
|
-
function
|
|
404
|
-
|
|
450
|
+
function count(s: string, i: number, ch: number): number {
|
|
451
|
+
let n = 0
|
|
452
|
+
while (i + n < s.length && s.charCodeAt(i + n) === ch) {
|
|
453
|
+
n++
|
|
454
|
+
}
|
|
455
|
+
return n
|
|
405
456
|
}
|
|
406
|
-
export function formatDateTime(date: Date | null | undefined, dateFormat?: string
|
|
457
|
+
export function formatDateTime(date: Date | null | undefined, dateFormat?: string): string {
|
|
407
458
|
if (!date) {
|
|
408
459
|
return ""
|
|
409
460
|
}
|
|
410
|
-
const sd = formatDate(date, dateFormat
|
|
461
|
+
const sd = formatDate(date, dateFormat)
|
|
411
462
|
if (sd.length === 0) {
|
|
412
463
|
return sd
|
|
413
464
|
}
|
|
414
465
|
return sd + " " + formatTime(date)
|
|
415
466
|
}
|
|
416
|
-
export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string
|
|
467
|
+
export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string): string {
|
|
417
468
|
if (!date) {
|
|
418
469
|
return ""
|
|
419
470
|
}
|
|
420
|
-
const sd = formatDate(date, dateFormat
|
|
471
|
+
const sd = formatDate(date, dateFormat)
|
|
421
472
|
if (sd.length === 0) {
|
|
422
473
|
return sd
|
|
423
474
|
}
|
|
424
475
|
return sd + " " + formatLongTime(date)
|
|
425
476
|
}
|
|
426
|
-
export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string
|
|
477
|
+
export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string): string {
|
|
427
478
|
if (!date) {
|
|
428
479
|
return ""
|
|
429
480
|
}
|
|
430
|
-
const sd = formatDate(date, dateFormat
|
|
481
|
+
const sd = formatDate(date, dateFormat)
|
|
431
482
|
if (sd.length === 0) {
|
|
432
483
|
return sd
|
|
433
484
|
}
|
|
@@ -500,3 +551,83 @@ export class formatter {
|
|
|
500
551
|
export function formatPhone(phone?: string | null): string {
|
|
501
552
|
return formatter.formatPhone(phone)
|
|
502
553
|
}
|
|
554
|
+
|
|
555
|
+
export interface MenuItem {
|
|
556
|
+
id: string
|
|
557
|
+
name: string
|
|
558
|
+
path: string
|
|
559
|
+
resource?: string
|
|
560
|
+
icon?: string
|
|
561
|
+
sequence?: number
|
|
562
|
+
prefetch?: boolean
|
|
563
|
+
type?: string
|
|
564
|
+
children?: MenuItem[]
|
|
565
|
+
}
|
|
566
|
+
export interface Category {
|
|
567
|
+
id: string
|
|
568
|
+
name: string
|
|
569
|
+
path: string
|
|
570
|
+
resource?: string
|
|
571
|
+
icon?: string
|
|
572
|
+
sequence?: number
|
|
573
|
+
prefetch?: boolean
|
|
574
|
+
type?: string
|
|
575
|
+
parent?: string
|
|
576
|
+
children?: MenuItem[]
|
|
577
|
+
}
|
|
578
|
+
export function rebuildPath(items: MenuItem[], lang: string) {
|
|
579
|
+
for (const item of items) {
|
|
580
|
+
item.path = item.type === "content" ? `/${lang}${item.path}` : `${item.path}?lang=${lang}`
|
|
581
|
+
const children = item.children
|
|
582
|
+
if (children && children.length > 0) {
|
|
583
|
+
rebuildPath(children, lang)
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export function sub(n1?: number, n2?: number): number {
|
|
589
|
+
if (!n1 && !n2) {
|
|
590
|
+
return 0
|
|
591
|
+
} else if (n1 && n2) {
|
|
592
|
+
return n1 - n2
|
|
593
|
+
} else if (n1) {
|
|
594
|
+
return n1
|
|
595
|
+
} else if (n2) {
|
|
596
|
+
return -n2
|
|
597
|
+
}
|
|
598
|
+
return 0
|
|
599
|
+
}
|
|
600
|
+
function subMenuItem(p1: MenuItem, p2: MenuItem): number {
|
|
601
|
+
return sub(p1.sequence, p2.sequence)
|
|
602
|
+
}
|
|
603
|
+
export function toMenuItems(m: Category[]): MenuItem[] {
|
|
604
|
+
const ps: Category[] = getRoot(m)
|
|
605
|
+
for (const p of ps) {
|
|
606
|
+
getChildren(p, m)
|
|
607
|
+
}
|
|
608
|
+
return ps.sort(subMenuItem)
|
|
609
|
+
}
|
|
610
|
+
function getRoot(ms: Category[]): Category[] {
|
|
611
|
+
const ps: Category[] = []
|
|
612
|
+
for (const m of ms) {
|
|
613
|
+
if (!m.parent || m.parent.length === 0) {
|
|
614
|
+
delete m.parent
|
|
615
|
+
ps.push(m)
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return ps.sort(subMenuItem)
|
|
619
|
+
}
|
|
620
|
+
function getChildren(m: Category, all: Category[]) {
|
|
621
|
+
const children: MenuItem[] = []
|
|
622
|
+
for (const s of all) {
|
|
623
|
+
if (s.parent === m.id) {
|
|
624
|
+
delete s.parent
|
|
625
|
+
children.push(s)
|
|
626
|
+
getChildren(s, all)
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (children.length > 0) {
|
|
630
|
+
children.sort(subMenuItem)
|
|
631
|
+
m.children = children
|
|
632
|
+
}
|
|
633
|
+
}
|