ui-formatter 0.0.8 → 0.1.0
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/index.js +53 -68
- package/package.json +1 -1
- package/src/index.ts +61 -66
package/lib/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var et = "";
|
|
4
4
|
function getDateFormat(profile) {
|
|
5
|
-
return "M/
|
|
5
|
+
return "M/d/yyyy";
|
|
6
6
|
}
|
|
7
7
|
exports.getDateFormat = getDateFormat;
|
|
8
8
|
function addDays(d, n) {
|
|
@@ -11,73 +11,58 @@ function addDays(d, n) {
|
|
|
11
11
|
return newDate;
|
|
12
12
|
}
|
|
13
13
|
exports.addDays = addDays;
|
|
14
|
-
function formatDate(d,
|
|
15
|
-
if (!d) {
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
var format = dateFormat && dateFormat.length > 0 ? dateFormat : "M/D/YYYY";
|
|
19
|
-
if (upper) {
|
|
20
|
-
format = format.toUpperCase();
|
|
21
|
-
}
|
|
22
|
-
var arr = [et, et, et];
|
|
23
|
-
var items = format.split(/\/|\.| |-/);
|
|
24
|
-
var iday = items.indexOf("D");
|
|
25
|
-
var im = items.indexOf("M");
|
|
26
|
-
var iyear = items.indexOf("YYYY");
|
|
27
|
-
var fm = full ? full : false;
|
|
28
|
-
var fd = full ? full : false;
|
|
29
|
-
var fy = true;
|
|
30
|
-
if (iday === -1) {
|
|
31
|
-
iday = items.indexOf("DD");
|
|
32
|
-
fd = true;
|
|
33
|
-
}
|
|
34
|
-
if (im === -1) {
|
|
35
|
-
im = items.indexOf("MM");
|
|
36
|
-
fm = true;
|
|
37
|
-
}
|
|
38
|
-
if (iyear === -1) {
|
|
39
|
-
iyear = items.indexOf("YY");
|
|
40
|
-
fy = full ? full : false;
|
|
14
|
+
function formatDate(d, format) {
|
|
15
|
+
if (!d || !format) {
|
|
16
|
+
return "";
|
|
41
17
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
var
|
|
46
|
-
var
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
18
|
+
var y = d.getFullYear();
|
|
19
|
+
var m = d.getMonth() + 1;
|
|
20
|
+
var day = d.getDate();
|
|
21
|
+
var out = "";
|
|
22
|
+
var i = 0;
|
|
23
|
+
while (i < format.length) {
|
|
24
|
+
var c = format.charCodeAt(i);
|
|
25
|
+
if (c === 121) {
|
|
26
|
+
var len = count(format, i, 121);
|
|
27
|
+
if (len >= 4) {
|
|
28
|
+
out += y.toString();
|
|
29
|
+
i += 4;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
out += shortYear(y);
|
|
33
|
+
i += 2;
|
|
34
|
+
}
|
|
35
|
+
continue;
|
|
57
36
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
for (var i = len; i > -0; i--) {
|
|
64
|
-
var c = format[i];
|
|
65
|
-
if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
|
|
66
|
-
return c;
|
|
37
|
+
if (c === 77) {
|
|
38
|
+
var len = count(format, i, 77);
|
|
39
|
+
out += len >= 2 ? pad(m) : m.toString();
|
|
40
|
+
i += len >= 2 ? 2 : 1;
|
|
41
|
+
continue;
|
|
67
42
|
}
|
|
43
|
+
if (c === 100) {
|
|
44
|
+
var len = count(format, i, 100);
|
|
45
|
+
out += len >= 2 ? pad(day) : day.toString();
|
|
46
|
+
i += len >= 2 ? 2 : 1;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
out += format[i];
|
|
50
|
+
i++;
|
|
68
51
|
}
|
|
69
|
-
return
|
|
52
|
+
return out;
|
|
70
53
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return s.substring(s.length - 2);
|
|
54
|
+
exports.formatDate = formatDate;
|
|
55
|
+
function shortYear(y) {
|
|
56
|
+
return (y % 100 + 100) % 100 < 10
|
|
57
|
+
? "0" + ((y % 100 + 100) % 100)
|
|
58
|
+
: "" + ((y % 100 + 100) % 100);
|
|
77
59
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
60
|
+
function count(s, i, ch) {
|
|
61
|
+
var n = 0;
|
|
62
|
+
while (i + n < s.length && s.charCodeAt(i + n) === ch) {
|
|
63
|
+
n++;
|
|
64
|
+
}
|
|
65
|
+
return n;
|
|
81
66
|
}
|
|
82
67
|
function datetimeToString(date) {
|
|
83
68
|
if (!date || date === et) {
|
|
@@ -93,33 +78,33 @@ function datetimeToString(date) {
|
|
|
93
78
|
return year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
|
|
94
79
|
}
|
|
95
80
|
exports.datetimeToString = datetimeToString;
|
|
96
|
-
function formatDateTime(date, dateFormat
|
|
81
|
+
function formatDateTime(date, dateFormat) {
|
|
97
82
|
if (!date) {
|
|
98
83
|
return et;
|
|
99
84
|
}
|
|
100
|
-
var sd = formatDate(date, dateFormat
|
|
85
|
+
var sd = formatDate(date, dateFormat);
|
|
101
86
|
if (sd.length === 0) {
|
|
102
87
|
return sd;
|
|
103
88
|
}
|
|
104
89
|
return sd + " " + formatTime(date);
|
|
105
90
|
}
|
|
106
91
|
exports.formatDateTime = formatDateTime;
|
|
107
|
-
function formatLongDateTime(date, dateFormat
|
|
92
|
+
function formatLongDateTime(date, dateFormat) {
|
|
108
93
|
if (!date) {
|
|
109
94
|
return et;
|
|
110
95
|
}
|
|
111
|
-
var sd = formatDate(date, dateFormat
|
|
96
|
+
var sd = formatDate(date, dateFormat);
|
|
112
97
|
if (sd.length === 0) {
|
|
113
98
|
return sd;
|
|
114
99
|
}
|
|
115
100
|
return sd + " " + formatLongTime(date);
|
|
116
101
|
}
|
|
117
102
|
exports.formatLongDateTime = formatLongDateTime;
|
|
118
|
-
function formatFullDateTime(date, dateFormat, s
|
|
103
|
+
function formatFullDateTime(date, dateFormat, s) {
|
|
119
104
|
if (!date) {
|
|
120
105
|
return et;
|
|
121
106
|
}
|
|
122
|
-
var sd = formatDate(date, dateFormat
|
|
107
|
+
var sd = formatDate(date, dateFormat);
|
|
123
108
|
if (sd.length === 0) {
|
|
124
109
|
return sd;
|
|
125
110
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const et = ""
|
|
2
2
|
|
|
3
3
|
export function getDateFormat(profile?: string): string {
|
|
4
|
-
return "M/
|
|
4
|
+
return "M/d/yyyy"
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
export function addDays(d: Date, n: number): Date {
|
|
@@ -9,71 +9,66 @@ export function addDays(d: Date, n: number): Date {
|
|
|
9
9
|
newDate.setDate(newDate.getDate() + n)
|
|
10
10
|
return newDate
|
|
11
11
|
}
|
|
12
|
-
export function formatDate(d: Date | null | undefined,
|
|
13
|
-
if (!d) {
|
|
14
|
-
return
|
|
15
|
-
}
|
|
16
|
-
let format = dateFormat && dateFormat.length > 0 ? dateFormat : "M/D/YYYY"
|
|
17
|
-
if (upper) {
|
|
18
|
-
format = format.toUpperCase()
|
|
19
|
-
}
|
|
20
|
-
let arr = [et, et, et]
|
|
21
|
-
const items = format.split(/\/|\.| |-/)
|
|
22
|
-
let iday = items.indexOf("D")
|
|
23
|
-
let im = items.indexOf("M")
|
|
24
|
-
let iyear = items.indexOf("YYYY")
|
|
25
|
-
let fm = full ? full : false
|
|
26
|
-
let fd = full ? full : false
|
|
27
|
-
let fy = true
|
|
28
|
-
if (iday === -1) {
|
|
29
|
-
iday = items.indexOf("DD")
|
|
30
|
-
fd = true
|
|
31
|
-
}
|
|
32
|
-
if (im === -1) {
|
|
33
|
-
im = items.indexOf("MM")
|
|
34
|
-
fm = true
|
|
35
|
-
}
|
|
36
|
-
if (iyear === -1) {
|
|
37
|
-
iyear = items.indexOf("YY")
|
|
38
|
-
fy = full ? full : false
|
|
12
|
+
export function formatDate(d: Date | null | undefined, format?: string): any {
|
|
13
|
+
if (!d || !format) {
|
|
14
|
+
return ""
|
|
39
15
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
16
|
+
const y = d.getFullYear()
|
|
17
|
+
const m = d.getMonth() + 1
|
|
18
|
+
const day = d.getDate()
|
|
19
|
+
|
|
20
|
+
let out = ""
|
|
21
|
+
let i = 0
|
|
22
|
+
|
|
23
|
+
while (i < format.length) {
|
|
24
|
+
const c = format.charCodeAt(i)
|
|
25
|
+
|
|
26
|
+
// yyyy / yy
|
|
27
|
+
if (c === 121 /* y */) {
|
|
28
|
+
const len = count(format, i, 121)
|
|
29
|
+
if (len >= 4) {
|
|
30
|
+
out += y.toString()
|
|
31
|
+
i += 4
|
|
32
|
+
} else {
|
|
33
|
+
out += shortYear(y)
|
|
34
|
+
i += 2
|
|
35
|
+
}
|
|
36
|
+
continue
|
|
54
37
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
|
|
63
|
-
return c
|
|
38
|
+
|
|
39
|
+
// MM / M
|
|
40
|
+
if (c === 77 /* M */) {
|
|
41
|
+
const len = count(format, i, 77)
|
|
42
|
+
out += len >= 2 ? pad(m) : m.toString()
|
|
43
|
+
i += len >= 2 ? 2 : 1
|
|
44
|
+
continue
|
|
64
45
|
}
|
|
46
|
+
|
|
47
|
+
// dd / d
|
|
48
|
+
if (c === 100 /* d */) {
|
|
49
|
+
const len = count(format, i, 100)
|
|
50
|
+
out += len >= 2 ? pad(day) : day.toString()
|
|
51
|
+
i += len >= 2 ? 2 : 1
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// literal char
|
|
56
|
+
out += format[i]
|
|
57
|
+
i++
|
|
65
58
|
}
|
|
66
|
-
return
|
|
59
|
+
return out
|
|
67
60
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const s = y.toString()
|
|
73
|
-
return s.substring(s.length - 2)
|
|
61
|
+
function shortYear(y: number): string {
|
|
62
|
+
return (y % 100 + 100) % 100 < 10
|
|
63
|
+
? "0" + ((y % 100 + 100) % 100)
|
|
64
|
+
: "" + ((y % 100 + 100) % 100)
|
|
74
65
|
}
|
|
75
|
-
function
|
|
76
|
-
|
|
66
|
+
function count(s: string, i: number, ch: number): number {
|
|
67
|
+
let n = 0
|
|
68
|
+
while (i + n < s.length && s.charCodeAt(i + n) === ch) {
|
|
69
|
+
n++
|
|
70
|
+
}
|
|
71
|
+
return n
|
|
77
72
|
}
|
|
78
73
|
export function datetimeToString(date?: Date | string): any {
|
|
79
74
|
if (!date || date === et) {
|
|
@@ -88,31 +83,31 @@ export function datetimeToString(date?: Date | string): any {
|
|
|
88
83
|
const seconds = pad(d2.getSeconds())
|
|
89
84
|
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
|
|
90
85
|
}
|
|
91
|
-
export function formatDateTime(date: Date | null | undefined, dateFormat?: string
|
|
86
|
+
export function formatDateTime(date: Date | null | undefined, dateFormat?: string): any {
|
|
92
87
|
if (!date) {
|
|
93
88
|
return et
|
|
94
89
|
}
|
|
95
|
-
const sd = formatDate(date, dateFormat
|
|
90
|
+
const sd = formatDate(date, dateFormat)
|
|
96
91
|
if (sd.length === 0) {
|
|
97
92
|
return sd
|
|
98
93
|
}
|
|
99
94
|
return sd + " " + formatTime(date)
|
|
100
95
|
}
|
|
101
|
-
export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string
|
|
96
|
+
export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string): any {
|
|
102
97
|
if (!date) {
|
|
103
98
|
return et
|
|
104
99
|
}
|
|
105
|
-
const sd = formatDate(date, dateFormat
|
|
100
|
+
const sd = formatDate(date, dateFormat)
|
|
106
101
|
if (sd.length === 0) {
|
|
107
102
|
return sd
|
|
108
103
|
}
|
|
109
104
|
return sd + " " + formatLongTime(date)
|
|
110
105
|
}
|
|
111
|
-
export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string
|
|
106
|
+
export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string): any {
|
|
112
107
|
if (!date) {
|
|
113
108
|
return et
|
|
114
109
|
}
|
|
115
|
-
const sd = formatDate(date, dateFormat
|
|
110
|
+
const sd = formatDate(date, dateFormat)
|
|
116
111
|
if (sd.length === 0) {
|
|
117
112
|
return sd
|
|
118
113
|
}
|