-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-to-02.js
149 lines (131 loc) · 3.69 KB
/
01-to-02.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const v01_elem = document.getElementById('v01-in');
const v02_elem = document.getElementById('v02-out');
class Literal {
constructor(value) {
this.value = value;
}
to_v02() {
return [this];
}
to_string() {
return this.value.replace('%', '%%').replace('\\', '\\\\');
}
}
class Specifier {
constructor(letter, padding = null) {
this.letter = letter;
this.padding = padding;
}
to_v02() {
if (this.letter === 'c') {
return parse_v01('%a %b %_d %H:%M:%S %0Y');
}
if (this.letter === 'D' || this.letter === 'x') {
return parse_v01('%m/%d/%y');
}
if (this.letter === 'e') {
return parse_v01('%_d');
}
if (this.letter === 'F') {
return parse_v01('%0Y-%m-%d');
}
if (this.letter === 'G') {
return parse_v01('%0G');
}
if (this.letter === 'h') {
return parse_v01('%b');
}
if (this.letter === 'k') {
return parse_v01('%-H');
}
if (this.letter === 'l') {
return parse_v01('%-I');
}
if (this.letter === 'n') {
return parse_v01('\\n');
}
if (this.letter === 'p') {
return parse_v01('%P');
}
if (this.letter === 'P') {
return parse_v01('%p');
}
if (this.letter === 'r') {
return parse_v01('%I:%M:%S %P');
}
if (this.letter === 'R') {
return parse_v01('%H:%M');
}
if (this.letter === 't') {
return parse_v01('\\t');
}
if (this.letter === 'T' || this.letter === 'X') {
return parse_v01('%H:%M:%S');
}
if (this.letter === 'Y') {
return parse_v01('%0Y');
}
if (this.letter === 'Z') {
return parse_v01('GMT');
}
return [this];
}
to_string() {
if (this.padding !== null) {
return `%` + this.padding + this.letter;
} else {
return `%` + this.letter;
}
}
}
const padding = Object.freeze({
default: '',
none: '-',
space: '_',
zero: '0',
});
function parse_v01(s) {
let items = [];
while (true) {
if (s.startsWith('%')) {
s = s.substring(1);
if (s === '') {
break;
}
if (s.startsWith('%')) {
items.push(new Literal('%'));
s = s.substring(1);
} else if (s.startsWith('-')) {
items.push(new Specifier(s[1], padding.none));
s = s.substring(2);
} else if (s.startsWith('_')) {
items.push(new Specifier(s[1], padding.space));
s = s.substring(2);
} else if (s.startsWith('0')) {
items.push(new Specifier(s[1], padding.zero));
s = s.substring(2);
} else {
items.push(new Specifier(s[0], padding.default));
s = s.substring(1);
}
} else {
let index = 0;
while (s[index] !== '%' && s[index] !== undefined) {
index++;
}
if (s[index - 1] === undefined) {
break;
}
items.push(new Literal(s.substring(0, index)));
s = s.substring(index);
}
}
return items;
}
function convert() {
const v01_str = v01_elem.value;
const items = parse_v01(v01_str);
const v02_str = items.flatMap(item => item.to_v02()).map(item => item.to_string()).join('');
v02_elem.value = v02_str;
}
v01_elem.addEventListener('keyup', convert);