ui-formatter 0.0.1

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # ui-formatter
package/lib/index.js ADDED
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var et = "";
4
+ function getDateFormat(profile) {
5
+ return "M/D/YYYY";
6
+ }
7
+ exports.getDateFormat = getDateFormat;
8
+ function addDays(d, n) {
9
+ var newDate = new Date(d);
10
+ newDate.setDate(newDate.getDate() + n);
11
+ return newDate;
12
+ }
13
+ exports.addDays = addDays;
14
+ function formatDate(d, dateFormat, full, upper) {
15
+ if (!d) {
16
+ return et;
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;
41
+ }
42
+ arr[iday] = getD(d.getDate(), fd);
43
+ arr[im] = getD(d.getMonth() + 1, fm);
44
+ arr[iyear] = getYear(d.getFullYear(), fy);
45
+ var s = detectSeparator(format);
46
+ var e = detectLastSeparator(format);
47
+ var l = items.length === 4 ? format[format.length - 1] : et;
48
+ return arr[0] + s + arr[1] + e + arr[2] + l;
49
+ }
50
+ exports.formatDate = formatDate;
51
+ function detectSeparator(format) {
52
+ var len = format.length;
53
+ for (var i = 0; i < len; i++) {
54
+ var c = format[i];
55
+ if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
56
+ return c;
57
+ }
58
+ }
59
+ return "/";
60
+ }
61
+ function detectLastSeparator(format) {
62
+ var len = format.length - 3;
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;
67
+ }
68
+ }
69
+ return "/";
70
+ }
71
+ function getYear(y, full) {
72
+ if (full || (y <= 99 && y >= -99)) {
73
+ return y.toString();
74
+ }
75
+ var s = y.toString();
76
+ return s.substring(s.length - 2);
77
+ }
78
+ exports.getYear = getYear;
79
+ function getD(n, fu) {
80
+ return fu ? pad(n) : n.toString();
81
+ }
82
+ function datetimeToString(date) {
83
+ if (!date || date === et) {
84
+ return undefined;
85
+ }
86
+ var d2 = typeof date !== "string" ? date : new Date(date);
87
+ var year = d2.getFullYear();
88
+ var month = pad(d2.getMonth() + 1);
89
+ var day = pad(d2.getDate());
90
+ var hours = pad(d2.getHours());
91
+ var minutes = pad(d2.getMinutes());
92
+ var seconds = pad(d2.getSeconds());
93
+ return year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
94
+ }
95
+ exports.datetimeToString = datetimeToString;
96
+ function formatDateTime(date, dateFormat, full, upper) {
97
+ if (!date) {
98
+ return et;
99
+ }
100
+ var sd = formatDate(date, dateFormat, full, upper);
101
+ if (sd.length === 0) {
102
+ return sd;
103
+ }
104
+ return sd + " " + formatTime(date);
105
+ }
106
+ exports.formatDateTime = formatDateTime;
107
+ function formatLongDateTime(date, dateFormat, full, upper) {
108
+ if (!date) {
109
+ return et;
110
+ }
111
+ var sd = formatDate(date, dateFormat, full, upper);
112
+ if (sd.length === 0) {
113
+ return sd;
114
+ }
115
+ return sd + " " + formatLongTime(date);
116
+ }
117
+ exports.formatLongDateTime = formatLongDateTime;
118
+ function formatFullDateTime(date, dateFormat, s, full, upper) {
119
+ if (!date) {
120
+ return et;
121
+ }
122
+ var sd = formatDate(date, dateFormat, full, upper);
123
+ if (sd.length === 0) {
124
+ return sd;
125
+ }
126
+ return sd + " " + formatFullTime(date, s);
127
+ }
128
+ exports.formatFullDateTime = formatFullDateTime;
129
+ function formatTime(d) {
130
+ return pad(d.getHours()) + ":" + pad(d.getMinutes());
131
+ }
132
+ exports.formatTime = formatTime;
133
+ function formatLongTime(d) {
134
+ return pad(d.getHours()) + ":" + pad(d.getMinutes()) + ":" + pad(d.getSeconds());
135
+ }
136
+ exports.formatLongTime = formatLongTime;
137
+ function formatFullTime(d, s) {
138
+ var se = s && s.length > 0 ? s : ".";
139
+ return formatLongTime(d) + se + pad3(d.getMilliseconds());
140
+ }
141
+ exports.formatFullTime = formatFullTime;
142
+ function dateToString(d, milli) {
143
+ var s = "" + d.getFullYear() + pad(d.getMonth() + 1) + pad(d.getDate()) + pad(d.getHours()) + pad(d.getMinutes()) + pad(d.getSeconds());
144
+ if (milli) {
145
+ return s + pad3(d.getMilliseconds());
146
+ }
147
+ return s;
148
+ }
149
+ exports.dateToString = dateToString;
150
+ function pad(n) {
151
+ return n < 10 ? "0" + n : n.toString();
152
+ }
153
+ function pad3(n) {
154
+ if (n >= 100) {
155
+ return n.toString();
156
+ }
157
+ return n < 10 ? "00" + n : "0" + n.toString();
158
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "ui-formatter",
3
+ "version": "0.0.1",
4
+ "description": "Validate data",
5
+ "main": "./lib/index.js",
6
+ "types": "./src/index.ts",
7
+ "scripts": {
8
+ "build:lib": "tsc",
9
+ "clean:lib": "rimraf lib"
10
+ },
11
+ "devDependencies": {
12
+ "tslint": "5.10.0",
13
+ "typescript": "^3.8.3"
14
+ },
15
+ "publishConfig": {
16
+ "registry": "https://registry.npmjs.org/"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git@github.com/core-ts/ui-formatter"
21
+ },
22
+ "keywords": [
23
+ "ui",
24
+ "formatter",
25
+ "ui formatter",
26
+ "ui-formatter"
27
+ ]
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,146 @@
1
+ const et = ""
2
+
3
+ export function getDateFormat(profile?: string): string {
4
+ return "M/D/YYYY"
5
+ }
6
+
7
+ export function addDays(d: Date, n: number): Date {
8
+ const newDate = new Date(d)
9
+ newDate.setDate(newDate.getDate() + n)
10
+ return newDate
11
+ }
12
+ export function formatDate(d: Date | null | undefined, dateFormat?: string, full?: boolean, upper?: boolean): string {
13
+ if (!d) {
14
+ return et
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
39
+ }
40
+ arr[iday] = getD(d.getDate(), fd)
41
+ arr[im] = getD(d.getMonth() + 1, fm)
42
+ arr[iyear] = getYear(d.getFullYear(), fy)
43
+ const s = detectSeparator(format)
44
+ const e = detectLastSeparator(format)
45
+ const l = items.length === 4 ? format[format.length - 1] : et
46
+ return arr[0] + s + arr[1] + e + arr[2] + l
47
+ }
48
+ function detectSeparator(format: string): string {
49
+ const len = format.length
50
+ for (let i = 0; i < len; i++) {
51
+ const c = format[i]
52
+ if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
53
+ return c
54
+ }
55
+ }
56
+ return "/"
57
+ }
58
+ function detectLastSeparator(format: string): string {
59
+ const len = format.length - 3
60
+ for (let i = len; i > -0; i--) {
61
+ const c = format[i]
62
+ if (!((c >= "A" && c <= "Z") || (c >= "a" && c <= "z"))) {
63
+ return c
64
+ }
65
+ }
66
+ return "/"
67
+ }
68
+ export function getYear(y: number, full?: boolean): string {
69
+ if (full || (y <= 99 && y >= -99)) {
70
+ return y.toString()
71
+ }
72
+ const s = y.toString()
73
+ return s.substring(s.length - 2)
74
+ }
75
+ function getD(n: number, fu: boolean): string {
76
+ return fu ? pad(n) : n.toString()
77
+ }
78
+ export function datetimeToString(date?: Date | string): any {
79
+ if (!date || date === et) {
80
+ return undefined
81
+ }
82
+ const d2 = typeof date !== "string" ? date : new Date(date)
83
+ const year = d2.getFullYear()
84
+ const month = pad(d2.getMonth() + 1)
85
+ const day = pad(d2.getDate())
86
+ const hours = pad(d2.getHours())
87
+ const minutes = pad(d2.getMinutes())
88
+ const seconds = pad(d2.getSeconds())
89
+ return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
90
+ }
91
+ export function formatDateTime(date: Date | null | undefined, dateFormat?: string, full?: boolean, upper?: boolean): any {
92
+ if (!date) {
93
+ return et
94
+ }
95
+ const sd = formatDate(date, dateFormat, full, upper)
96
+ if (sd.length === 0) {
97
+ return sd
98
+ }
99
+ return sd + " " + formatTime(date)
100
+ }
101
+ export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string, full?: boolean, upper?: boolean): string {
102
+ if (!date) {
103
+ return et
104
+ }
105
+ const sd = formatDate(date, dateFormat, full, upper)
106
+ if (sd.length === 0) {
107
+ return sd
108
+ }
109
+ return sd + " " + formatLongTime(date)
110
+ }
111
+ export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string, full?: boolean, upper?: boolean): string {
112
+ if (!date) {
113
+ return et
114
+ }
115
+ const sd = formatDate(date, dateFormat, full, upper)
116
+ if (sd.length === 0) {
117
+ return sd
118
+ }
119
+ return sd + " " + formatFullTime(date, s)
120
+ }
121
+ export function formatTime(d: Date): string {
122
+ return pad(d.getHours()) + ":" + pad(d.getMinutes())
123
+ }
124
+ export function formatLongTime(d: Date): string {
125
+ return pad(d.getHours()) + ":" + pad(d.getMinutes()) + ":" + pad(d.getSeconds())
126
+ }
127
+ export function formatFullTime(d: Date, s?: string): string {
128
+ const se = s && s.length > 0 ? s : "."
129
+ return formatLongTime(d) + se + pad3(d.getMilliseconds())
130
+ }
131
+ export function dateToString(d: Date, milli?: boolean): string {
132
+ const s = `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`
133
+ if (milli) {
134
+ return s + pad3(d.getMilliseconds())
135
+ }
136
+ return s
137
+ }
138
+ function pad(n: number): string {
139
+ return n < 10 ? "0" + n : n.toString()
140
+ }
141
+ function pad3(n: number): string {
142
+ if (n >= 100) {
143
+ return n.toString()
144
+ }
145
+ return n < 10 ? "00" + n : "0" + n.toString()
146
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "compileOnSave": true,
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "baseUrl": "./src",
6
+ "outDir": "./lib",
7
+ "module": "commonjs",
8
+ "moduleResolution": "node",
9
+ "strict": true,
10
+ "pretty": true,
11
+ "sourceMap": false,
12
+ "declaration": false,
13
+ "experimentalDecorators": false,
14
+ "removeComments": true,
15
+ "lib": [
16
+ "es5",
17
+ "es6"
18
+ ]
19
+ },
20
+ "include": [
21
+ "src/**/*.ts"
22
+ ],
23
+ "exclude": [
24
+ "node_modules"
25
+ ]
26
+ }