-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
131 lines (114 loc) · 3.68 KB
/
index.ts
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
import {createCustomFormatFn, createFormat} from '../../src/format/create-format';
import {createParse, parseOrThrowWrapper} from '../../src/parse/create-parse';
import {defaultFormatters} from '../../src/format/default-formatters';
import defaultParsers from '../../src/parse/default-parsers';
import {
ParserObj,
FormatterObj,
FormatByTemplateFn,
ParseByTemplateFn,
ParseByTemplateOrThrowFn,
ParserData,
Formatter,
} from '../../src/utils/basic-types';
import {extend} from '../../src/utils/utils';
export * from '../../src/default-exports';
const monthsShort = ['янв', 'фев', 'мар', 'апр', 'май', 'июн', 'июл', 'авг', 'сен', 'окт', 'ноя', 'дек'];
const monthsFull = [
'январ',
'феврал',
'март',
'апрел',
'май',
'июн',
'июл',
'август',
'сентябр',
'октябр',
'ноябр',
'декабр'
];
const monthsGenitive = [
'январ',
'феврал',
'март',
'апрел',
'май',
'июн',
'июл',
'август',
'сентябр',
'октябр',
'ноябр',
'декабр'
];
const weekdays2char = ['Якш', 'Душ', 'Сеш', 'Чор', 'Пай', 'Жум', 'Шан'];
const weekdays3char = ['Якш', 'Душ', 'Сеш', 'Чор', 'Пай', 'Жум', 'Шан'];
const weekdaysFull = ['якшанба', 'душанба', 'сешанба', 'чоршанба', 'пайшанба', 'жума', 'шанба'];
const meridiemUppercase = ['ТО', 'ТК'];
const meridiemLowercase = ['то', 'тк'];
const meridiemFull = ['т.о.', 'т.к.'];
export const formatters: FormatterObj = {
// Month: янв, фев, ..., дек
MMM: (date) => monthsShort[date.getMonth()],
// Month: 'январ', 'феврал', ..., декабр
MMMM: (date) => monthsFull[date.getMonth()],
// Day of week: Якш, Душ, ..., Шан
dd: (date) => weekdays2char[date.getDay()],
// Day of week: Якш, Душ, ..., Шан
ddd: (date) => weekdays3char[date.getDay()],
// Day of week: якшанба, душанба, ..., шанба
dddd: (date) => weekdaysFull[date.getDay()],
// TO, TK
A: (date) => meridiemUppercase[date.getHours() < 12 ? 0 : 1],
// то, тк
a: (date) => meridiemLowercase[date.getHours() < 12 ? 0 : 1],
// т.о., т.к.
aa: (date) => meridiemFull[date.getHours() < 12 ? 0 : 1],
Mo: ordinalFormatter('M'),
Do: ordinalFormatter('D'),
DDDo: ordinalFormatter('DDD'),
do: ordinalFormatter('d'),
Qo: ordinalFormatter('Q'),
Wo: ordinalFormatter('W'),
};
function ordinalFormatter(formatterToken: string): Formatter {
return (date) => defaultFormatters[formatterToken](date);
}
const parsers: ParserObj = {
MMM: [
monthsShort.join('|'),
(date, value) => {
const index = monthsShort.indexOf(value.toLowerCase());
date.setMonth(index);
},
],
MMMM: [
monthsFull.concat(monthsGenitive).join('|'),
(date, value) => {
value = value.toLowerCase();
let index = monthsFull.indexOf(value);
if (index < 0) index = monthsGenitive.indexOf(value);
date.setMonth(index);
},
],
Mo: ordinalParser('M'),
Do: ordinalParser('D'),
DDDo: ordinalParser('DDD'),
do: ordinalParser('d'),
Qo: ordinalParser('Q'),
Wo: ordinalParser('W'),
};
// Generate ordinal version of parsers: M -> Mo, D -> Do, etc.
function ordinalParser(token: string): ParserData {
return [
'\\d+',
(date, value) => {
defaultParsers[token][1](date, value);
},
];
}
export const format: FormatByTemplateFn = createFormat(extend(defaultFormatters, formatters));
export const createCustomFormat = createCustomFormatFn(extend(defaultFormatters, formatters));
export const parse: ParseByTemplateFn = createParse(extend(defaultParsers, parsers));
export const parseOrThrow: ParseByTemplateOrThrowFn = parseOrThrowWrapper(parse);