@formatjs/intl 2.3.2 → 2.4.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/index.d.ts +17 -0
- package/index.d.ts.map +1 -0
- package/index.js +40 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/src/create-intl.d.ts +11 -0
- package/lib/src/create-intl.d.ts.map +1 -0
- package/lib/src/create-intl.js +55 -0
- package/lib/src/dateTime.d.ts +38 -0
- package/lib/src/dateTime.d.ts.map +1 -0
- package/lib/src/dateTime.js +117 -0
- package/lib/src/displayName.d.ts +6 -0
- package/lib/src/displayName.d.ts.map +1 -0
- package/lib/src/displayName.js +22 -0
- package/lib/src/error.d.ts +34 -0
- package/lib/src/error.d.ts.map +1 -0
- package/lib/src/error.js +88 -0
- package/lib/src/list.d.ts +11 -0
- package/lib/src/list.d.ts.map +1 -0
- package/lib/src/list.js +61 -0
- package/lib/src/message.d.ts +16 -0
- package/lib/src/message.d.ts.map +1 -0
- package/lib/src/message.js +103 -0
- package/lib/src/number.d.ts +17 -0
- package/lib/src/number.d.ts.map +1 -0
- package/lib/src/number.js +54 -0
- package/lib/src/plural.d.ts +7 -0
- package/lib/src/plural.d.ts.map +1 -0
- package/lib/src/plural.js +19 -0
- package/lib/src/relativeTime.d.ts +7 -0
- package/lib/src/relativeTime.d.ts.map +1 -0
- package/lib/src/relativeTime.js +29 -0
- package/lib/src/types.d.ts +103 -0
- package/lib/src/types.d.ts.map +1 -0
- package/lib/src/types.js +1 -0
- package/lib/src/utils.d.ts +12 -0
- package/lib/src/utils.d.ts.map +1 -0
- package/lib/src/utils.js +162 -0
- package/package.json +10 -10
- package/src/create-intl.d.ts +11 -0
- package/src/create-intl.d.ts.map +1 -0
- package/src/create-intl.js +59 -0
- package/src/dateTime.d.ts +38 -0
- package/src/dateTime.d.ts.map +1 -0
- package/src/dateTime.js +126 -0
- package/src/displayName.d.ts +6 -0
- package/src/displayName.d.ts.map +1 -0
- package/src/displayName.js +26 -0
- package/src/error.d.ts +34 -0
- package/src/error.d.ts.map +1 -0
- package/src/error.js +91 -0
- package/src/list.d.ts +11 -0
- package/src/list.d.ts.map +1 -0
- package/src/list.js +66 -0
- package/src/message.d.ts +16 -0
- package/src/message.d.ts.map +1 -0
- package/src/message.js +107 -0
- package/src/number.d.ts +17 -0
- package/src/number.d.ts.map +1 -0
- package/src/number.js +60 -0
- package/src/plural.d.ts +7 -0
- package/src/plural.d.ts.map +1 -0
- package/src/plural.js +23 -0
- package/src/relativeTime.d.ts +7 -0
- package/src/relativeTime.d.ts.map +1 -0
- package/src/relativeTime.js +33 -0
- package/src/types.d.ts +103 -0
- package/src/types.d.ts.map +1 -0
- package/src/types.js +2 -0
- package/src/utils.d.ts +12 -0
- package/src/utils.d.ts.map +1 -0
- package/src/utils.js +169 -0
- package/BUILD +0 -79
- package/CHANGELOG.md +0 -537
- package/index.ts +0 -38
- package/src/create-intl.ts +0 -159
- package/src/dateTime.ts +0 -204
- package/src/displayName.ts +0 -54
- package/src/error.ts +0 -106
- package/src/list.ts +0 -119
- package/src/message.ts +0 -246
- package/src/number.ts +0 -101
- package/src/plural.ts +0 -45
- package/src/relativeTime.ts +0 -74
- package/src/types.ts +0 -234
- package/src/utils.ts +0 -177
- package/tests/__snapshots__/formatDate.test.ts.snap +0 -13
- package/tests/__snapshots__/formatMessage.test.ts.snap +0 -45
- package/tests/__snapshots__/formatNumber.test.ts.snap +0 -13
- package/tests/__snapshots__/formatRelativeTime.test.ts.snap +0 -19
- package/tests/__snapshots__/formatTime.test.ts.snap +0 -13
- package/tests/create-intl.test.ts +0 -60
- package/tests/error.test.ts +0 -25
- package/tests/formatDate.test.ts +0 -167
- package/tests/formatDisplayNames.test.ts +0 -63
- package/tests/formatList.test.ts +0 -51
- package/tests/formatMessage.test.ts +0 -495
- package/tests/formatNumber.test.ts +0 -172
- package/tests/formatPlural.test.ts +0 -119
- package/tests/formatRelativeTime.test.ts +0 -145
- package/tests/formatTime.test.ts +0 -234
- package/tests/global.d.ts +0 -7
- package/tsconfig.json +0 -5
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/camelcase */
|
|
2
|
-
|
|
3
|
-
import {formatPlural as formatPluralFn} from '../src/plural'
|
|
4
|
-
|
|
5
|
-
import {IntlFormatters, IntlConfig} from '../src/types'
|
|
6
|
-
|
|
7
|
-
describe('format API', () => {
|
|
8
|
-
const {NODE_ENV} = process.env
|
|
9
|
-
|
|
10
|
-
let config: IntlConfig<any>
|
|
11
|
-
|
|
12
|
-
let getPluralRules: any
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
config = {
|
|
15
|
-
locale: 'en',
|
|
16
|
-
|
|
17
|
-
messages: {},
|
|
18
|
-
|
|
19
|
-
defaultLocale: 'en',
|
|
20
|
-
defaultFormats: {},
|
|
21
|
-
|
|
22
|
-
onError: jest.fn(),
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
getPluralRules = jest
|
|
26
|
-
.fn()
|
|
27
|
-
.mockImplementation((...args) => new Intl.PluralRules(...args))
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
afterEach(() => {
|
|
31
|
-
process.env.NODE_ENV = NODE_ENV
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
describe('formatPlural()', () => {
|
|
35
|
-
let pf: Intl.PluralRules
|
|
36
|
-
let formatPlural: IntlFormatters['formatPlural']
|
|
37
|
-
|
|
38
|
-
beforeEach(() => {
|
|
39
|
-
pf = new Intl.PluralRules(config.locale)
|
|
40
|
-
// @ts-ignore
|
|
41
|
-
formatPlural = formatPluralFn.bind(null, config, getPluralRules)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('should warn for invalid opt', function () {
|
|
45
|
-
// @ts-ignore
|
|
46
|
-
expect(formatPlural(0, {type: 'invalid'})).toBe('other')
|
|
47
|
-
expect(config.onError as jest.Mock).toHaveBeenCalledTimes(1)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
it('formats falsy values', () => {
|
|
51
|
-
// @ts-ignore
|
|
52
|
-
expect(formatPlural(undefined)).toBe(pf.select(undefined))
|
|
53
|
-
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
expect(formatPlural(false)).toBe(pf.select(false))
|
|
56
|
-
|
|
57
|
-
// @ts-ignore
|
|
58
|
-
expect(formatPlural(null)).toBe(pf.select(null))
|
|
59
|
-
|
|
60
|
-
// @ts-ignore
|
|
61
|
-
expect(formatPlural(NaN)).toBe(pf.select(NaN))
|
|
62
|
-
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
expect(formatPlural('')).toBe(pf.select(''))
|
|
65
|
-
expect(formatPlural(0)).toBe(pf.select(0))
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('formats integer values', () => {
|
|
69
|
-
expect(formatPlural(0)).toBe(pf.select(0))
|
|
70
|
-
expect(formatPlural(1)).toBe(pf.select(1))
|
|
71
|
-
expect(formatPlural(2)).toBe(pf.select(2))
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
it('formats decimal values', () => {
|
|
75
|
-
expect(formatPlural(0.1)).toBe(pf.select(0.1))
|
|
76
|
-
expect(formatPlural(1.0)).toBe(pf.select(1.0))
|
|
77
|
-
expect(formatPlural(1.1)).toBe(pf.select(1.1))
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
it('formats string values parsed as numbers', () => {
|
|
81
|
-
expect(Number('0')).toBe(0)
|
|
82
|
-
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
expect(formatPlural('0')).toBe(pf.select('0'))
|
|
85
|
-
expect(Number('1')).toBe(1)
|
|
86
|
-
|
|
87
|
-
// @ts-ignore
|
|
88
|
-
expect(formatPlural('1')).toBe(pf.select('1'))
|
|
89
|
-
|
|
90
|
-
expect(Number('0.1')).toBe(0.1)
|
|
91
|
-
|
|
92
|
-
// @ts-ignore
|
|
93
|
-
expect(formatPlural('0.1')).toBe(pf.select('0.1'))
|
|
94
|
-
expect(Number('1.0')).toBe(1.0)
|
|
95
|
-
|
|
96
|
-
// @ts-ignore
|
|
97
|
-
expect(formatPlural('1.0')).toBe(pf.select('1.0'))
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
describe('options', () => {
|
|
101
|
-
it('accepts empty options', () => {
|
|
102
|
-
expect(formatPlural(0, {})).toBe(pf.select(0))
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
it('accepts valid IntlPluralFormat options', () => {
|
|
106
|
-
expect(() => formatPlural(22, {type: 'ordinal'})).not.toThrow()
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
describe('ordinals', () => {
|
|
110
|
-
it('formats using ordinal plural rules', () => {
|
|
111
|
-
const opts = {type: 'ordinal'} as Intl.PluralRulesOptions
|
|
112
|
-
pf = new Intl.PluralRules(config.locale, opts)
|
|
113
|
-
|
|
114
|
-
expect(formatPlural(22, opts)).toBe(pf.select(22))
|
|
115
|
-
})
|
|
116
|
-
})
|
|
117
|
-
})
|
|
118
|
-
})
|
|
119
|
-
})
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/camelcase */
|
|
2
|
-
import {formatRelativeTime as formatRelativeTimeFn} from '../src/relativeTime'
|
|
3
|
-
import {IntlConfig, IntlFormatters} from '../src/types'
|
|
4
|
-
|
|
5
|
-
describe('format API', () => {
|
|
6
|
-
const {NODE_ENV} = process.env
|
|
7
|
-
|
|
8
|
-
let config: IntlConfig<any>
|
|
9
|
-
|
|
10
|
-
let getRelativeTimeFormat: any
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
config = {
|
|
13
|
-
locale: 'en',
|
|
14
|
-
|
|
15
|
-
messages: {},
|
|
16
|
-
formats: {
|
|
17
|
-
relative: {
|
|
18
|
-
seconds: {
|
|
19
|
-
style: 'narrow',
|
|
20
|
-
},
|
|
21
|
-
// @ts-ignore
|
|
22
|
-
missing: undefined,
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
defaultLocale: 'en',
|
|
26
|
-
defaultFormats: {},
|
|
27
|
-
|
|
28
|
-
onError: jest.fn(),
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
getRelativeTimeFormat = jest
|
|
32
|
-
.fn()
|
|
33
|
-
.mockImplementation((...args) => new Intl.RelativeTimeFormat(...args))
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
afterEach(() => {
|
|
37
|
-
process.env.NODE_ENV = NODE_ENV
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
describe('formatRelativeTime()', () => {
|
|
41
|
-
let rf: any
|
|
42
|
-
let formatRelativeTime: IntlFormatters['formatRelativeTime']
|
|
43
|
-
|
|
44
|
-
beforeEach(() => {
|
|
45
|
-
rf = new Intl.RelativeTimeFormat(config.locale, undefined)
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
formatRelativeTime = formatRelativeTimeFn.bind(
|
|
48
|
-
null,
|
|
49
|
-
config,
|
|
50
|
-
getRelativeTimeFormat
|
|
51
|
-
)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('falls back and warns when no value is provided', () => {
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
expect(formatRelativeTime()).toBe('undefined')
|
|
57
|
-
expect(
|
|
58
|
-
(config.onError as jest.Mock).mock.calls.map(c => c[0].code)
|
|
59
|
-
).toMatchSnapshot()
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('falls back and warns when a non-finite value is provided', () => {
|
|
63
|
-
expect(formatRelativeTime(NaN)).toBe('NaN')
|
|
64
|
-
expect(config.onError as jest.Mock).toHaveBeenCalledTimes(1)
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
it('formats falsy finite values', () => {
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
expect(formatRelativeTime(false)).toBe('in 0 seconds')
|
|
70
|
-
|
|
71
|
-
// @ts-ignore
|
|
72
|
-
expect(formatRelativeTime(null)).toBe('in 0 seconds')
|
|
73
|
-
expect(formatRelativeTime(0)).toBe(rf.format(0, 'second'))
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it('formats with short format', () => {
|
|
77
|
-
expect(formatRelativeTime(-59, 'second', {style: 'short'})).toBe(
|
|
78
|
-
'59 sec. ago'
|
|
79
|
-
)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
describe('options', () => {
|
|
83
|
-
it('accepts empty options', () => {
|
|
84
|
-
expect(formatRelativeTime(0, 'second', {})).toBe(rf.format(0, 'second'))
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
it('accepts valid IntlRelativeFormat options', () => {
|
|
88
|
-
expect(() =>
|
|
89
|
-
formatRelativeTime(0, 'second', {numeric: 'auto'})
|
|
90
|
-
).not.toThrow()
|
|
91
|
-
expect(() =>
|
|
92
|
-
formatRelativeTime(0, 'second', {style: 'short'})
|
|
93
|
-
).not.toThrow()
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
it('falls back and warns on invalid IntlRelativeFormat options', () => {
|
|
97
|
-
// @ts-ignore
|
|
98
|
-
expect(formatRelativeTime(0, 'invalid')).toBe('0')
|
|
99
|
-
expect(
|
|
100
|
-
(config.onError as jest.Mock).mock.calls.map(c => c[0].code)
|
|
101
|
-
).toMatchSnapshot()
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
it('uses configured named formats', () => {
|
|
105
|
-
const format = 'seconds'
|
|
106
|
-
|
|
107
|
-
const {locale, formats} = config
|
|
108
|
-
|
|
109
|
-
rf = new Intl.RelativeTimeFormat(locale, formats!.relative![format])
|
|
110
|
-
|
|
111
|
-
expect(formatRelativeTime(-120, 'second', {format})).toBe(
|
|
112
|
-
rf.format(-120, 'second', {style: 'narrow'})
|
|
113
|
-
)
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
it('uses named formats as defaults', () => {
|
|
117
|
-
const opts = {numeric: 'auto' as const}
|
|
118
|
-
const format = 'seconds'
|
|
119
|
-
|
|
120
|
-
const {locale, formats} = config
|
|
121
|
-
rf = new Intl.RelativeTimeFormat(locale, {
|
|
122
|
-
...opts,
|
|
123
|
-
...formats!.relative![format],
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
expect(formatRelativeTime(0, 'minute', {...opts, format})).toBe(
|
|
127
|
-
rf.format(0, 'minute', {numeric: 'auto'})
|
|
128
|
-
)
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
it('handles missing named formats and warns', () => {
|
|
132
|
-
const format = 'missing'
|
|
133
|
-
|
|
134
|
-
rf = new Intl.RelativeTimeFormat(config.locale, undefined)
|
|
135
|
-
|
|
136
|
-
expect(formatRelativeTime(-1, 'second', {format})).toBe(
|
|
137
|
-
rf.format(-1, 'second')
|
|
138
|
-
)
|
|
139
|
-
expect(
|
|
140
|
-
(config.onError as jest.Mock).mock.calls.map(c => c[0].code)
|
|
141
|
-
).toMatchSnapshot()
|
|
142
|
-
})
|
|
143
|
-
})
|
|
144
|
-
})
|
|
145
|
-
})
|
package/tests/formatTime.test.ts
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/camelcase */
|
|
2
|
-
import {DateTimeFormat} from '@formatjs/ecma402-abstract'
|
|
3
|
-
import {formatTime as formatTimeFn} from '../src/dateTime'
|
|
4
|
-
import {IntlConfig, IntlFormatters, Formatters} from '../src/types'
|
|
5
|
-
import '@formatjs/intl-datetimeformat/polyfill'
|
|
6
|
-
import '@formatjs/intl-datetimeformat/locale-data/en'
|
|
7
|
-
import '@formatjs/intl-datetimeformat/add-all-tz'
|
|
8
|
-
|
|
9
|
-
describe('format API', () => {
|
|
10
|
-
const {NODE_ENV} = process.env
|
|
11
|
-
|
|
12
|
-
let config: IntlConfig<any>
|
|
13
|
-
|
|
14
|
-
let getDateTimeFormat: Formatters['getDateTimeFormat'] = (
|
|
15
|
-
...args: ConstructorParameters<typeof Intl.DateTimeFormat>
|
|
16
|
-
) => new Intl.DateTimeFormat(...args) as DateTimeFormat
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
config = {
|
|
19
|
-
locale: 'en',
|
|
20
|
-
|
|
21
|
-
messages: {},
|
|
22
|
-
|
|
23
|
-
formats: {
|
|
24
|
-
time: {
|
|
25
|
-
'hour-only': {
|
|
26
|
-
hour: '2-digit',
|
|
27
|
-
hour12: false,
|
|
28
|
-
},
|
|
29
|
-
missing: undefined,
|
|
30
|
-
},
|
|
31
|
-
} as any,
|
|
32
|
-
|
|
33
|
-
defaultLocale: 'en',
|
|
34
|
-
defaultFormats: {},
|
|
35
|
-
|
|
36
|
-
onError: jest.fn(),
|
|
37
|
-
}
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
afterEach(() => {
|
|
41
|
-
process.env.NODE_ENV = NODE_ENV
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
describe('formatTime()', () => {
|
|
45
|
-
let df: Intl.DateTimeFormat
|
|
46
|
-
let formatTime: IntlFormatters['formatTime']
|
|
47
|
-
|
|
48
|
-
beforeEach(() => {
|
|
49
|
-
df = new Intl.DateTimeFormat(config.locale, {
|
|
50
|
-
hour: 'numeric',
|
|
51
|
-
minute: 'numeric',
|
|
52
|
-
})
|
|
53
|
-
// @ts-ignore
|
|
54
|
-
formatTime = formatTimeFn.bind(null, config, getDateTimeFormat)
|
|
55
|
-
;(config.onError as jest.Mock).mockClear()
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('render now if no value is provided', () => {
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
expect(formatTime()).toBe(df.format())
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
it('should not inject additional hour/minute when dateStyle are used', function () {
|
|
64
|
-
expect(config.onError).not.toHaveBeenCalled()
|
|
65
|
-
formatTimeFn(config as any, getDateTimeFormat, new Date(), {
|
|
66
|
-
dateStyle: 'short',
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
expect(config.onError).not.toHaveBeenCalled()
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('should not inject additional hour/minute when timeStyle are used', function () {
|
|
73
|
-
expect(config.onError).not.toHaveBeenCalled()
|
|
74
|
-
formatTimeFn(config as any, getDateTimeFormat, new Date(), {
|
|
75
|
-
timeStyle: 'short',
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
expect(config.onError).not.toHaveBeenCalled()
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
it('falls back and warns when a non-finite value is provided', () => {
|
|
82
|
-
expect(formatTime(NaN)).toBe('NaN')
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
expect(formatTime('')).toBe(df.format(''))
|
|
85
|
-
expect(config.onError as jest.Mock).toHaveBeenCalledTimes(1)
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
it('formats falsy finite values', () => {
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
expect(formatTime(false)).toBe(df.format(false))
|
|
91
|
-
// @ts-ignore
|
|
92
|
-
expect(formatTime(null)).toBe(df.format(null))
|
|
93
|
-
expect(formatTime(0)).toBe(df.format(0))
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
it('formats date instance values', () => {
|
|
97
|
-
expect(formatTime(new Date(0))).toBe(df.format(new Date(0)))
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
it('formats date string values', () => {
|
|
101
|
-
expect(formatTime(new Date(0).toString())).toBe(df.format(0))
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
it('formats date ms timestamp values', () => {
|
|
105
|
-
const timestamp = Date.now()
|
|
106
|
-
expect(formatTime(timestamp)).toBe(df.format(timestamp))
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
it('uses the time zone specified by the provider', () => {
|
|
110
|
-
const timestamp = Date.now()
|
|
111
|
-
config.timeZone = 'Africa/Johannesburg'
|
|
112
|
-
// @ts-ignore
|
|
113
|
-
formatTime = formatTimeFn.bind(null, config, getDateTimeFormat)
|
|
114
|
-
const johannesburgDf = new Intl.DateTimeFormat(config.locale, {
|
|
115
|
-
hour: 'numeric',
|
|
116
|
-
minute: 'numeric',
|
|
117
|
-
timeZone: 'Africa/Johannesburg',
|
|
118
|
-
})
|
|
119
|
-
expect(formatTime(timestamp)).toBe(johannesburgDf.format(timestamp))
|
|
120
|
-
config.timeZone = 'America/Chicago'
|
|
121
|
-
// @ts-ignore
|
|
122
|
-
formatTime = formatTimeFn.bind(null, config, getDateTimeFormat)
|
|
123
|
-
const chicagoDf = new Intl.DateTimeFormat(config.locale, {
|
|
124
|
-
hour: 'numeric',
|
|
125
|
-
minute: 'numeric',
|
|
126
|
-
timeZone: 'America/Chicago',
|
|
127
|
-
})
|
|
128
|
-
expect(formatTime(timestamp)).toBe(chicagoDf.format(timestamp))
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
describe('options', () => {
|
|
132
|
-
it('accepts empty options', () => {
|
|
133
|
-
expect(formatTime(0, {})).toBe(df.format(0))
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
it('accepts valid Intl.DateTimeFormat options', () => {
|
|
137
|
-
expect(() => formatTime(0, {hour: '2-digit'})).not.toThrow()
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
it('falls back and warns on invalid Intl.DateTimeFormat options', () => {
|
|
141
|
-
// @ts-expect-error just for test
|
|
142
|
-
expect(formatTime(0, {hour: 'invalid'})).toBe('0')
|
|
143
|
-
expect(
|
|
144
|
-
(config.onError as jest.Mock).mock.calls.map(c => c[0].code)
|
|
145
|
-
).toMatchSnapshot()
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
it('uses configured named formats', () => {
|
|
149
|
-
const date = new Date()
|
|
150
|
-
const format = 'hour-only'
|
|
151
|
-
|
|
152
|
-
const {locale, formats} = config
|
|
153
|
-
df = new Intl.DateTimeFormat(locale, formats!.time![format])
|
|
154
|
-
|
|
155
|
-
expect(formatTime(date, {format})).toBe(df.format(date))
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
it('uses named formats as defaults', () => {
|
|
159
|
-
const date = new Date()
|
|
160
|
-
const opts: Intl.DateTimeFormatOptions = {minute: '2-digit'}
|
|
161
|
-
const format = 'hour-only'
|
|
162
|
-
|
|
163
|
-
const {locale, formats} = config
|
|
164
|
-
df = new Intl.DateTimeFormat(locale, {
|
|
165
|
-
...opts,
|
|
166
|
-
...formats!.time![format],
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
expect(formatTime(date, {...opts, format})).toBe(df.format(date))
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
it('handles missing named formats and warns', () => {
|
|
173
|
-
const date = new Date()
|
|
174
|
-
const format = 'missing'
|
|
175
|
-
|
|
176
|
-
expect(formatTime(date, {format})).toBe(df.format(date))
|
|
177
|
-
expect(
|
|
178
|
-
(config.onError as jest.Mock).mock.calls.map(c => c[0].code)
|
|
179
|
-
).toMatchSnapshot()
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
it('should set default values', () => {
|
|
183
|
-
const date = new Date()
|
|
184
|
-
const {locale} = config
|
|
185
|
-
const day = 'numeric'
|
|
186
|
-
df = new Intl.DateTimeFormat(locale, {
|
|
187
|
-
hour: 'numeric',
|
|
188
|
-
minute: 'numeric',
|
|
189
|
-
day,
|
|
190
|
-
})
|
|
191
|
-
expect(formatTime(date, {day})).toBe(df.format(date))
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
it('should not set default values when second is provided', () => {
|
|
195
|
-
const date = new Date()
|
|
196
|
-
const {locale} = config
|
|
197
|
-
const second = 'numeric'
|
|
198
|
-
df = new Intl.DateTimeFormat(locale, {second})
|
|
199
|
-
expect(formatTime(date, {second})).toBe(df.format(date))
|
|
200
|
-
})
|
|
201
|
-
|
|
202
|
-
it('should not set default values when minute is provided', () => {
|
|
203
|
-
const date = new Date()
|
|
204
|
-
const {locale} = config
|
|
205
|
-
const minute = 'numeric'
|
|
206
|
-
df = new Intl.DateTimeFormat(locale, {minute})
|
|
207
|
-
expect(formatTime(date, {minute})).toBe(df.format(date))
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
it('should not set default values when hour is provided', () => {
|
|
211
|
-
const date = new Date()
|
|
212
|
-
const {locale} = config
|
|
213
|
-
const hour = 'numeric'
|
|
214
|
-
df = new Intl.DateTimeFormat(locale, {hour})
|
|
215
|
-
expect(formatTime(date, {hour})).toBe(df.format(date))
|
|
216
|
-
})
|
|
217
|
-
|
|
218
|
-
it('uses time zone specified in options over the one passed through by the provider', () => {
|
|
219
|
-
const timestamp = Date.now()
|
|
220
|
-
config.timeZone = 'Africa/Johannesburg'
|
|
221
|
-
// @ts-ignore
|
|
222
|
-
formatTime = formatTimeFn.bind(null, config, getDateTimeFormat)
|
|
223
|
-
const chicagoDf = new Intl.DateTimeFormat(config.locale, {
|
|
224
|
-
hour: 'numeric',
|
|
225
|
-
minute: 'numeric',
|
|
226
|
-
timeZone: 'America/Chicago',
|
|
227
|
-
})
|
|
228
|
-
expect(formatTime(timestamp, {timeZone: 'America/Chicago'})).toBe(
|
|
229
|
-
chicagoDf.format(timestamp)
|
|
230
|
-
)
|
|
231
|
-
})
|
|
232
|
-
})
|
|
233
|
-
})
|
|
234
|
-
})
|
package/tests/global.d.ts
DELETED