|
| 1 | +import MockDate from 'mockdate' |
| 2 | +// import moment from 'moment' |
| 3 | +import dayjs from '../../src' |
| 4 | +import preParsePostFormat from '../../src/plugin/preParsePostFormat' |
| 5 | +import localeData from '../../src/plugin/localeData' |
| 6 | +import duration from '../../src/plugin/duration' |
| 7 | +import calendar from '../../src/plugin/calendar' |
| 8 | +import objectSupport from '../../src/plugin/objectSupport' |
| 9 | +import customParseFormat from '../../src/plugin/customParseFormat' |
| 10 | +import relativeTime from '../../src/plugin/relativeTime' |
| 11 | +import utc from '../../src/plugin/utc' |
| 12 | +import arraySupport from '../../src/plugin/arraySupport' |
| 13 | +import en from '../../src/locale/en' |
| 14 | + |
| 15 | +dayjs.extend(utc) |
| 16 | +dayjs.extend(localeData) |
| 17 | +dayjs.extend(customParseFormat) |
| 18 | +dayjs.extend(arraySupport) |
| 19 | +dayjs.extend(objectSupport) |
| 20 | +dayjs.extend(calendar) |
| 21 | +dayjs.extend(duration) |
| 22 | +dayjs.extend(relativeTime) |
| 23 | +dayjs.extend(preParsePostFormat) |
| 24 | + |
| 25 | +const symbolMap = { |
| 26 | + 1: '!', |
| 27 | + 2: '@', |
| 28 | + 3: '#', |
| 29 | + 4: '$', |
| 30 | + 5: '%', |
| 31 | + 6: '^', |
| 32 | + 7: '&', |
| 33 | + 8: '*', |
| 34 | + 9: '(', |
| 35 | + 0: ')' |
| 36 | +} |
| 37 | +const numberMap = { |
| 38 | + '!': '1', |
| 39 | + '@': '2', |
| 40 | + '#': '3', |
| 41 | + $: '4', |
| 42 | + '%': '5', |
| 43 | + '^': '6', |
| 44 | + '&': '7', |
| 45 | + '*': '8', |
| 46 | + '(': '9', |
| 47 | + ')': '0' |
| 48 | +} |
| 49 | + |
| 50 | +const localeCustomizations = { |
| 51 | + ...en, |
| 52 | + preparse(string) { |
| 53 | + if (typeof string !== 'string') { |
| 54 | + // console.error('preparse - Expected string, got', { |
| 55 | + // string |
| 56 | + // }) |
| 57 | + throw new Error(`preparse - Expected string, got ${typeof string}`) |
| 58 | + } |
| 59 | + try { |
| 60 | + const res = string.replace(/[!@#$%^&*()]/g, match => numberMap[match]) |
| 61 | + // console.log('Called custom preparse', { string, res }) |
| 62 | + return res |
| 63 | + } catch (error) { |
| 64 | + const errorMsg = `Unexpected error during preparse of '${string}' - ${error}` |
| 65 | + // console.error(errorMsg) |
| 66 | + throw new Error(errorMsg) |
| 67 | + } |
| 68 | + }, |
| 69 | + postformat(string) { |
| 70 | + if (typeof string !== 'string') { |
| 71 | + // console.error('postformat - Expected string, got', { |
| 72 | + // string |
| 73 | + // }) |
| 74 | + throw new Error(`postformat - Expected string, got ${typeof string}`) |
| 75 | + } |
| 76 | + try { |
| 77 | + const res = string.replace(/\d/g, match => symbolMap[match]) |
| 78 | + // console.log('Called custom postformat', { string, res }) |
| 79 | + return res |
| 80 | + } catch (error) { |
| 81 | + const errorMsg = `Unexpected error during postFormat of '${string}' - ${error}` |
| 82 | + // console.error(errorMsg) |
| 83 | + throw new Error(errorMsg) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +beforeEach(() => { |
| 89 | + MockDate.set(new Date()) |
| 90 | + dayjs.locale('symbol', localeCustomizations) |
| 91 | +}) |
| 92 | + |
| 93 | +afterEach(() => { |
| 94 | + MockDate.reset() |
| 95 | + dayjs.locale('symbol', null) |
| 96 | +}) |
| 97 | + |
| 98 | +describe('preparse and postformat', () => { |
| 99 | + describe('transform', () => { |
| 100 | + const TEST_DATE = '@)!@-)*-@&' |
| 101 | + const TEST_NUM = 1346025600 |
| 102 | + it('preparse string + format', () => |
| 103 | + expect(dayjs.utc(TEST_DATE, 'YYYY-MM-DD').unix()).toBe(TEST_NUM)) |
| 104 | + it('preparse ISO8601 string', () => |
| 105 | + expect(dayjs.utc(TEST_DATE).unix()).toBe(TEST_NUM)) |
| 106 | + it('postformat', () => |
| 107 | + expect(dayjs |
| 108 | + .unix(TEST_NUM) |
| 109 | + .utc() |
| 110 | + .format('YYYY-MM-DD')) |
| 111 | + .toBe(TEST_DATE)) |
| 112 | + }) |
| 113 | + |
| 114 | + describe('transform from', () => { |
| 115 | + dayjs.locale('symbol', localeCustomizations) |
| 116 | + const start = dayjs([2007, 1, 28]) |
| 117 | + |
| 118 | + const t1 = dayjs([2007, 1, 28]).add({ s: 90 }) |
| 119 | + it('postformat should work on dayjs.fn.from', () => |
| 120 | + expect(start.from(t1, true)).toBe('@ minutes')) |
| 121 | + |
| 122 | + const t2 = dayjs().add(6, 'd') |
| 123 | + it('postformat should work on dayjs.fn.fromNow', () => |
| 124 | + expect(t2.fromNow(true)).toBe('^ days')) |
| 125 | + |
| 126 | + it('postformat should work on dayjs.duration.fn.humanize', () => |
| 127 | + expect(dayjs.duration(10, 'h').humanize()).toBe('!) hours')) |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | +describe('calendar day', () => { |
| 132 | + const a = dayjs() |
| 133 | + .hour(12) |
| 134 | + .minute(0) |
| 135 | + .second(0) |
| 136 | + |
| 137 | + it('today at the same time', () => |
| 138 | + expect(dayjs(a).calendar()).toBe('Today at !@:)) PM')) |
| 139 | + |
| 140 | + it('Now plus 25 min', () => |
| 141 | + expect(dayjs(a) |
| 142 | + .add({ m: 25 }) |
| 143 | + .calendar()) |
| 144 | + .toBe('Today at !@:@% PM')) |
| 145 | + |
| 146 | + it('Now plus 1 hour', () => |
| 147 | + expect(dayjs(a) |
| 148 | + .add({ h: 1 }) |
| 149 | + .calendar()) |
| 150 | + .toBe('Today at !:)) PM')) |
| 151 | + |
| 152 | + it('tomorrow at the same time', () => |
| 153 | + expect(dayjs(a) |
| 154 | + .add({ d: 1 }) |
| 155 | + .calendar()) |
| 156 | + .toBe('Tomorrow at !@:)) PM')) |
| 157 | + |
| 158 | + it('Now minus 1 hour', () => |
| 159 | + expect(dayjs(a) |
| 160 | + .subtract({ h: 1 }) |
| 161 | + .calendar()) |
| 162 | + .toBe('Today at !!:)) AM')) |
| 163 | + |
| 164 | + it('yesterday at the same time', () => |
| 165 | + expect(dayjs(a) |
| 166 | + .subtract({ d: 1 }) |
| 167 | + .calendar()) |
| 168 | + .toBe('Yesterday at !@:)) PM')) |
| 169 | +}) |
0 commit comments