deno.land / x / dayjs@v1.11.5 / test / plugin / preParsePostFormat.test.js

preParsePostFormat.test.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
150
151
152
153
154
155
import MockDate from 'mockdate'import dayjs from '../../src'import preParsePostFormat from '../../src/plugin/preParsePostFormat'import localeData from '../../src/plugin/localeData'import duration from '../../src/plugin/duration'import calendar from '../../src/plugin/calendar'import objectSupport from '../../src/plugin/objectSupport'import customParseFormat from '../../src/plugin/customParseFormat'import relativeTime from '../../src/plugin/relativeTime'import utc from '../../src/plugin/utc'import arraySupport from '../../src/plugin/arraySupport'import en from '../../src/locale/en'
dayjs.extend(utc)dayjs.extend(localeData)dayjs.extend(customParseFormat)dayjs.extend(arraySupport)dayjs.extend(objectSupport)dayjs.extend(calendar)dayjs.extend(duration)dayjs.extend(relativeTime)dayjs.extend(preParsePostFormat)
const symbolMap = { 1: '!', 2: '@', 3: '#', 4: '$', 5: '%', 6: '^', 7: '&', 8: '*', 9: '(', 0: ')'}const numberMap = { '!': '1', '@': '2', '#': '3', $: '4', '%': '5', '^': '6', '&': '7', '*': '8', '(': '9', ')': '0'}
const localeCustomizations = { ...en, preparse(string) { if (typeof string !== 'string') { throw new Error(`preparse - Expected string, got ${typeof string}`) } try { return string.replace(/[!@#$%^&*()]/g, match => numberMap[match]) } catch (error) { throw new Error(`Unexpected error during preparse of '${string}' - ${error}`) } }, postformat(string) { if (typeof string !== 'string') { throw new Error(`postformat - Expected string, got ${typeof string}`) } try { return string.replace(/\d/g, match => symbolMap[match]) } catch (error) { throw new Error(`Unexpected error during postFormat of '${string}' - ${error}`) } }}
beforeEach(() => { MockDate.set(new Date()) dayjs.locale('symbol', localeCustomizations)})
afterEach(() => { MockDate.reset() dayjs.locale('symbol', null)})
describe('preparse and postformat', () => { describe('transform', () => { const TEST_DATE = '@)!@-)*-@&' const TEST_NUM = 1346025600 it('preparse string + format', () => expect(dayjs.utc(TEST_DATE, 'YYYY-MM-DD').unix()).toBe(TEST_NUM)) it('preparse ISO8601 string', () => expect(dayjs.utc(TEST_DATE).unix()).toBe(TEST_NUM)) it('postformat', () => expect(dayjs .unix(TEST_NUM) .utc() .format('YYYY-MM-DD')) .toBe(TEST_DATE)) })
describe('transform from', () => { dayjs.locale('symbol', localeCustomizations) const start = dayjs([2007, 1, 28])
const t1 = dayjs([2007, 1, 28]).add({ s: 90 }) it('postformat should work on dayjs.fn.from', () => expect(start.from(t1, true)).toBe('@ minutes'))
const t2 = dayjs().add(6, 'd') it('postformat should work on dayjs.fn.fromNow', () => expect(t2.fromNow(true)).toBe('^ days'))
it('postformat should work on dayjs.duration.fn.humanize', () => expect(dayjs.duration(10, 'h').humanize()).toBe('!) hours')) })})
describe('calendar day', () => { const a = dayjs() .hour(12) .minute(0) .second(0)
it('today at the same time', () => expect(dayjs(a).calendar()).toBe('Today at !@:)) PM'))
it('Now plus 25 min', () => expect(dayjs(a) .add({ m: 25 }) .calendar()) .toBe('Today at !@:@% PM'))
it('Now plus 1 hour', () => expect(dayjs(a) .add({ h: 1 }) .calendar()) .toBe('Today at !:)) PM'))
it('tomorrow at the same time', () => expect(dayjs(a) .add({ d: 1 }) .calendar()) .toBe('Tomorrow at !@:)) PM'))
it('Now minus 1 hour', () => expect(dayjs(a) .subtract({ h: 1 }) .calendar()) .toBe('Today at !!:)) AM'))
it('yesterday at the same time', () => expect(dayjs(a) .subtract({ d: 1 }) .calendar()) .toBe('Yesterday at !@:)) PM'))})
dayjs

Version Info

Tagged at
a year ago