web-one 0.0.1 → 0.0.3
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 +59 -0
- package/lib/index.js +110 -66
- package/package.json +2 -2
- package/src/form.ts +59 -0
- package/src/index.ts +120 -64
- package/tsconfig.json +4 -1
package/lib/form.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function fromFormData(formData, attrs, includeUndefine, includeErrorForNumbers) {
|
|
4
|
+
if (!attrs) {
|
|
5
|
+
return fromFormDataWithAttributes(formData, {}, includeUndefine, includeErrorForNumbers);
|
|
6
|
+
}
|
|
7
|
+
return fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErrorForNumbers);
|
|
8
|
+
}
|
|
9
|
+
exports.fromFormData = fromFormData;
|
|
10
|
+
function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErrorForNumbers) {
|
|
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);
|
|
17
|
+
if (attr) {
|
|
18
|
+
obj[key] = v;
|
|
19
|
+
if (v && typeof v === "string") {
|
|
20
|
+
if (attr.type === "number" || attr.type === "integer") {
|
|
21
|
+
if (!isNaN(v)) {
|
|
22
|
+
obj[key] = parseFloat(v);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else if (attr.type === "datetime" || attr.type === "date") {
|
|
26
|
+
var d = new Date(v);
|
|
27
|
+
if (d.toString() !== "Invalid Date") {
|
|
28
|
+
obj[key] = d;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (attr.type === "boolean") {
|
|
32
|
+
obj[key] = v === "true";
|
|
33
|
+
}
|
|
34
|
+
else if (attr.type === "strings") {
|
|
35
|
+
obj[key] = v.split(",");
|
|
36
|
+
}
|
|
37
|
+
else if (attr.type === "integers" || attr.type === "numbers") {
|
|
38
|
+
var s = v.split(",");
|
|
39
|
+
var nums = [];
|
|
40
|
+
for (var i = 0; i < s.length; i++) {
|
|
41
|
+
if (!isNaN(s[i])) {
|
|
42
|
+
var num = parseFloat(s[i]);
|
|
43
|
+
nums.push(num);
|
|
44
|
+
}
|
|
45
|
+
else if (includeErrorForNumbers) {
|
|
46
|
+
nums.push(s[i]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
obj[key] = nums;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (includeUndefine) {
|
|
54
|
+
obj[key] = v;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return obj;
|
|
58
|
+
}
|
|
59
|
+
exports.fromFormDataWithAttributes = fromFormDataWithAttributes;
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
function __export(m) {
|
|
3
|
+
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
4
|
+
}
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
__export(require("./form"));
|
|
3
7
|
var resources = (function () {
|
|
4
8
|
function resources() {
|
|
5
9
|
}
|
|
@@ -282,6 +286,61 @@ function buildSortSearch(params, fields, sortStr) {
|
|
|
282
286
|
return sorts;
|
|
283
287
|
}
|
|
284
288
|
exports.buildSortSearch = buildSortSearch;
|
|
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
|
+
}
|
|
342
|
+
}
|
|
343
|
+
exports.formatNumber = formatNumber;
|
|
285
344
|
function getDateFormat(profile) {
|
|
286
345
|
return "M/D/YYYY";
|
|
287
346
|
}
|
|
@@ -355,101 +414,86 @@ function datetimeToString(date) {
|
|
|
355
414
|
return year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
|
|
356
415
|
}
|
|
357
416
|
exports.datetimeToString = datetimeToString;
|
|
358
|
-
function formatDate(d,
|
|
359
|
-
if (!d) {
|
|
417
|
+
function formatDate(d, format) {
|
|
418
|
+
if (!d || !format) {
|
|
360
419
|
return "";
|
|
361
420
|
}
|
|
362
|
-
var
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
var
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
fm = true;
|
|
381
|
-
}
|
|
382
|
-
if (iyear === -1) {
|
|
383
|
-
iyear = items.indexOf("YY");
|
|
384
|
-
fy = full ? full : false;
|
|
385
|
-
}
|
|
386
|
-
arr[iday] = getD(d.getDate(), fd);
|
|
387
|
-
arr[im] = getD(d.getMonth() + 1, fm);
|
|
388
|
-
arr[iyear] = getYear(d.getFullYear(), fy);
|
|
389
|
-
var s = detectSeparator(format);
|
|
390
|
-
var e = detectLastSeparator(format);
|
|
391
|
-
var l = items.length === 4 ? format[format.length - 1] : "";
|
|
392
|
-
return arr[0] + s + arr[1] + e + arr[2] + l;
|
|
393
|
-
}
|
|
394
|
-
exports.formatDate = formatDate;
|
|
395
|
-
function detectSeparator(format) {
|
|
396
|
-
var len = format.length;
|
|
397
|
-
for (var i = 0; i < len; i++) {
|
|
398
|
-
var c = format[i];
|
|
399
|
-
if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
|
|
400
|
-
return c;
|
|
421
|
+
var y = d.getFullYear();
|
|
422
|
+
var m = d.getMonth() + 1;
|
|
423
|
+
var day = d.getDate();
|
|
424
|
+
var out = "";
|
|
425
|
+
var i = 0;
|
|
426
|
+
while (i < format.length) {
|
|
427
|
+
var c = format.charCodeAt(i);
|
|
428
|
+
if (c === 121) {
|
|
429
|
+
var len = count(format, i, 121);
|
|
430
|
+
if (len >= 4) {
|
|
431
|
+
out += y.toString();
|
|
432
|
+
i += 4;
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
out += shortYear(y);
|
|
436
|
+
i += 2;
|
|
437
|
+
}
|
|
438
|
+
continue;
|
|
401
439
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
440
|
+
if (c === 77) {
|
|
441
|
+
var len = count(format, i, 77);
|
|
442
|
+
out += len >= 2 ? pad(m) : m.toString();
|
|
443
|
+
i += len >= 2 ? 2 : 1;
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
if (c === 100) {
|
|
447
|
+
var len = count(format, i, 100);
|
|
448
|
+
out += len >= 2 ? pad(day) : day.toString();
|
|
449
|
+
i += len >= 2 ? 2 : 1;
|
|
450
|
+
continue;
|
|
411
451
|
}
|
|
452
|
+
out += format[i];
|
|
453
|
+
i++;
|
|
412
454
|
}
|
|
413
|
-
return
|
|
455
|
+
return out;
|
|
414
456
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
return s.substring(s.length - 2);
|
|
457
|
+
exports.formatDate = formatDate;
|
|
458
|
+
function shortYear(y) {
|
|
459
|
+
return (y % 100 + 100) % 100 < 10
|
|
460
|
+
? "0" + ((y % 100 + 100) % 100)
|
|
461
|
+
: "" + ((y % 100 + 100) % 100);
|
|
421
462
|
}
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
463
|
+
function count(s, i, ch) {
|
|
464
|
+
var n = 0;
|
|
465
|
+
while (i + n < s.length && s.charCodeAt(i + n) === ch) {
|
|
466
|
+
n++;
|
|
467
|
+
}
|
|
468
|
+
return n;
|
|
425
469
|
}
|
|
426
|
-
function formatDateTime(date, dateFormat
|
|
470
|
+
function formatDateTime(date, dateFormat) {
|
|
427
471
|
if (!date) {
|
|
428
472
|
return "";
|
|
429
473
|
}
|
|
430
|
-
var sd = formatDate(date, dateFormat
|
|
474
|
+
var sd = formatDate(date, dateFormat);
|
|
431
475
|
if (sd.length === 0) {
|
|
432
476
|
return sd;
|
|
433
477
|
}
|
|
434
478
|
return sd + " " + formatTime(date);
|
|
435
479
|
}
|
|
436
480
|
exports.formatDateTime = formatDateTime;
|
|
437
|
-
function formatLongDateTime(date, dateFormat
|
|
481
|
+
function formatLongDateTime(date, dateFormat) {
|
|
438
482
|
if (!date) {
|
|
439
483
|
return "";
|
|
440
484
|
}
|
|
441
|
-
var sd = formatDate(date, dateFormat
|
|
485
|
+
var sd = formatDate(date, dateFormat);
|
|
442
486
|
if (sd.length === 0) {
|
|
443
487
|
return sd;
|
|
444
488
|
}
|
|
445
489
|
return sd + " " + formatLongTime(date);
|
|
446
490
|
}
|
|
447
491
|
exports.formatLongDateTime = formatLongDateTime;
|
|
448
|
-
function formatFullDateTime(date, dateFormat, s
|
|
492
|
+
function formatFullDateTime(date, dateFormat, s) {
|
|
449
493
|
if (!date) {
|
|
450
494
|
return "";
|
|
451
495
|
}
|
|
452
|
-
var sd = formatDate(date, dateFormat
|
|
496
|
+
var sd = formatDate(date, dateFormat);
|
|
453
497
|
if (sd.length === 0) {
|
|
454
498
|
return sd;
|
|
455
499
|
}
|
package/package.json
CHANGED
package/src/form.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export type DataType = 'ObjectId' | 'date' | 'datetime' | 'time'
|
|
2
|
+
| 'boolean' | 'number' | 'integer' | 'string' | 'text'
|
|
3
|
+
| 'object' | 'array' | 'binary'
|
|
4
|
+
| 'primitives' | 'booleans' | 'numbers' | 'integers' | 'strings' | 'dates' | 'datetimes' | 'times';
|
|
5
|
+
|
|
6
|
+
export interface Attribute {
|
|
7
|
+
type?: DataType;
|
|
8
|
+
}
|
|
9
|
+
export interface Attributes {
|
|
10
|
+
[key: string]: Attribute;
|
|
11
|
+
}
|
|
12
|
+
export function fromFormData<T>(formData: FormData, attrs?: Attributes, includeUndefine?: boolean, includeErrorForNumbers?: boolean): T {
|
|
13
|
+
if (!attrs) {
|
|
14
|
+
return fromFormDataWithAttributes(formData, {}, includeUndefine, includeErrorForNumbers)
|
|
15
|
+
}
|
|
16
|
+
return fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErrorForNumbers)
|
|
17
|
+
}
|
|
18
|
+
export function fromFormDataWithAttributes<T>(formData: FormData, attrs: Attributes, includeUndefine?: boolean, includeErrorForNumbers?: boolean): T {
|
|
19
|
+
const obj = {} as any
|
|
20
|
+
const keys = formData.keys()
|
|
21
|
+
for (const key of keys) {
|
|
22
|
+
const attr: Attribute = attrs[key]
|
|
23
|
+
const v = formData.get(key)
|
|
24
|
+
if (attr) {
|
|
25
|
+
obj[key] = v
|
|
26
|
+
if (v && typeof v === "string") {
|
|
27
|
+
if (attr.type === "number" || attr.type === "integer") {
|
|
28
|
+
if (!isNaN(v as any)) {
|
|
29
|
+
obj[key] = parseFloat(v)
|
|
30
|
+
}
|
|
31
|
+
} else if (attr.type === "datetime" || attr.type === "date") {
|
|
32
|
+
const d = new Date(v)
|
|
33
|
+
if (d.toString() !== "Invalid Date") {
|
|
34
|
+
obj[key] = d
|
|
35
|
+
}
|
|
36
|
+
} else if (attr.type === "boolean") {
|
|
37
|
+
obj[key] = v === "true"
|
|
38
|
+
} else if (attr.type === "strings") {
|
|
39
|
+
obj[key] = v.split(",")
|
|
40
|
+
} else if (attr.type === "integers" || attr.type === "numbers") {
|
|
41
|
+
const s = v.split(",")
|
|
42
|
+
const nums = []
|
|
43
|
+
for (let i = 0; i < s.length; i++) {
|
|
44
|
+
if (!isNaN(s[i] as any)) {
|
|
45
|
+
const num = parseFloat(s[i])
|
|
46
|
+
nums.push(num)
|
|
47
|
+
} else if (includeErrorForNumbers) {
|
|
48
|
+
nums.push(s[i])
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
obj[key] = nums
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
} else if (includeUndefine) {
|
|
55
|
+
obj[key] = v
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return obj
|
|
59
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export * from "./form"
|
|
2
|
+
|
|
1
3
|
export class resources {
|
|
2
4
|
static limits = [12, 24, 60, 100, 120, 180, 300, 600]
|
|
3
5
|
static page = "page"
|
|
@@ -269,6 +271,65 @@ export function buildSortSearch(params: StringMap, fields: string[], sortStr?: s
|
|
|
269
271
|
return sorts
|
|
270
272
|
}
|
|
271
273
|
|
|
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
|
|
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
|
+
|
|
272
333
|
export function getDateFormat(profile?: string): string {
|
|
273
334
|
return "M/D/YYYY"
|
|
274
335
|
}
|
|
@@ -335,97 +396,92 @@ export function datetimeToString(date?: Date | string): string | undefined {
|
|
|
335
396
|
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
|
|
336
397
|
}
|
|
337
398
|
|
|
338
|
-
export function formatDate(d: Date | null | undefined,
|
|
339
|
-
if (!d) {
|
|
399
|
+
export function formatDate(d: Date | null | undefined, format?: string): string {
|
|
400
|
+
if (!d || !format) {
|
|
340
401
|
return ""
|
|
341
402
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
let
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
iyear = items.indexOf("YY")
|
|
364
|
-
fy = full ? full : false
|
|
365
|
-
}
|
|
366
|
-
arr[iday] = getD(d.getDate(), fd)
|
|
367
|
-
arr[im] = getD(d.getMonth() + 1, fm)
|
|
368
|
-
arr[iyear] = getYear(d.getFullYear(), fy)
|
|
369
|
-
const s = detectSeparator(format)
|
|
370
|
-
const e = detectLastSeparator(format)
|
|
371
|
-
const l = items.length === 4 ? format[format.length - 1] : ""
|
|
372
|
-
return arr[0] + s + arr[1] + e + arr[2] + l
|
|
373
|
-
}
|
|
374
|
-
function detectSeparator(format: string): string {
|
|
375
|
-
const len = format.length
|
|
376
|
-
for (let i = 0; i < len; i++) {
|
|
377
|
-
const c = format[i]
|
|
378
|
-
if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
|
|
379
|
-
return c
|
|
403
|
+
const y = d.getFullYear()
|
|
404
|
+
const m = d.getMonth() + 1
|
|
405
|
+
const day = d.getDate()
|
|
406
|
+
|
|
407
|
+
let out = ""
|
|
408
|
+
let i = 0
|
|
409
|
+
|
|
410
|
+
while (i < format.length) {
|
|
411
|
+
const c = format.charCodeAt(i)
|
|
412
|
+
|
|
413
|
+
// yyyy / yy
|
|
414
|
+
if (c === 121 /* y */) {
|
|
415
|
+
const len = count(format, i, 121)
|
|
416
|
+
if (len >= 4) {
|
|
417
|
+
out += y.toString()
|
|
418
|
+
i += 4
|
|
419
|
+
} else {
|
|
420
|
+
out += shortYear(y)
|
|
421
|
+
i += 2
|
|
422
|
+
}
|
|
423
|
+
continue
|
|
380
424
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
|
|
389
|
-
return c
|
|
425
|
+
|
|
426
|
+
// MM / M
|
|
427
|
+
if (c === 77 /* M */) {
|
|
428
|
+
const len = count(format, i, 77)
|
|
429
|
+
out += len >= 2 ? pad(m) : m.toString()
|
|
430
|
+
i += len >= 2 ? 2 : 1
|
|
431
|
+
continue
|
|
390
432
|
}
|
|
433
|
+
|
|
434
|
+
// dd / d
|
|
435
|
+
if (c === 100 /* d */) {
|
|
436
|
+
const len = count(format, i, 100)
|
|
437
|
+
out += len >= 2 ? pad(day) : day.toString()
|
|
438
|
+
i += len >= 2 ? 2 : 1
|
|
439
|
+
continue
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// literal char
|
|
443
|
+
out += format[i]
|
|
444
|
+
i++
|
|
391
445
|
}
|
|
392
|
-
return
|
|
446
|
+
return out
|
|
393
447
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
const s = y.toString()
|
|
399
|
-
return s.substring(s.length - 2)
|
|
448
|
+
function shortYear(y: number): string {
|
|
449
|
+
return (y % 100 + 100) % 100 < 10
|
|
450
|
+
? "0" + ((y % 100 + 100) % 100)
|
|
451
|
+
: "" + ((y % 100 + 100) % 100)
|
|
400
452
|
}
|
|
401
|
-
function
|
|
402
|
-
|
|
453
|
+
function count(s: string, i: number, ch: number): number {
|
|
454
|
+
let n = 0
|
|
455
|
+
while (i + n < s.length && s.charCodeAt(i + n) === ch) {
|
|
456
|
+
n++
|
|
457
|
+
}
|
|
458
|
+
return n
|
|
403
459
|
}
|
|
404
|
-
export function formatDateTime(date: Date | null | undefined, dateFormat?: string
|
|
460
|
+
export function formatDateTime(date: Date | null | undefined, dateFormat?: string): string {
|
|
405
461
|
if (!date) {
|
|
406
462
|
return ""
|
|
407
463
|
}
|
|
408
|
-
const sd = formatDate(date, dateFormat
|
|
464
|
+
const sd = formatDate(date, dateFormat)
|
|
409
465
|
if (sd.length === 0) {
|
|
410
466
|
return sd
|
|
411
467
|
}
|
|
412
468
|
return sd + " " + formatTime(date)
|
|
413
469
|
}
|
|
414
|
-
export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string
|
|
470
|
+
export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string): string {
|
|
415
471
|
if (!date) {
|
|
416
472
|
return ""
|
|
417
473
|
}
|
|
418
|
-
const sd = formatDate(date, dateFormat
|
|
474
|
+
const sd = formatDate(date, dateFormat)
|
|
419
475
|
if (sd.length === 0) {
|
|
420
476
|
return sd
|
|
421
477
|
}
|
|
422
478
|
return sd + " " + formatLongTime(date)
|
|
423
479
|
}
|
|
424
|
-
export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string
|
|
480
|
+
export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string): string {
|
|
425
481
|
if (!date) {
|
|
426
482
|
return ""
|
|
427
483
|
}
|
|
428
|
-
const sd = formatDate(date, dateFormat
|
|
484
|
+
const sd = formatDate(date, dateFormat)
|
|
429
485
|
if (sd.length === 0) {
|
|
430
486
|
return sd
|
|
431
487
|
}
|