Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make lowercase localized formats fallback to slightly modified … #557

Merged
merged 3 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/plugin/localizedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ export default (o, c, d) => {
L: 'MM/DD/YYYY',
LL: 'MMMM D, YYYY',
LLL: 'MMMM D, YYYY h:mm A',
LLLL: 'dddd, MMMM D, YYYY h:mm A',
l: 'M/D/YYYY',
ll: 'MMM D, YYYY',
lll: 'MMM D, YYYY h:mm A',
llll: 'ddd, MMM D, YYYY h:mm A'
LLLL: 'dddd, MMMM D, YYYY h:mm A'
}
d.en.formats = englishFormats
proto.format = function (formatStr) {
const locale = this.$locale()
const formats = locale.formats || {}
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) =>
a || formats[b] || englishFormats[b])
const t = format => format.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
proto.format = function (formatStr = FORMAT_DEFAULT) {
const { formats = {} } = this.$locale()
const result = formatStr.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) => {
const B = b && b.toUpperCase()
return a || formats[b] || englishFormats[b] || t(formats[B])
})
return oldFormat.call(this, result)
}
}
Expand Down
40 changes: 37 additions & 3 deletions test/plugin/localizedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import es from '../../src/locale/es'
import znCn from '../../src/locale/zh-cn'
import localizedFormat from '../../src/plugin/localizedFormat'

dayjs.extend(localizedFormat)
Expand All @@ -17,7 +18,7 @@ afterEach(() => {
it('Declares English localized formats', () => {
expect(dayjs.en).toBeDefined()
expect(dayjs.en.formats).toBeDefined();
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(dayjs.en.formats[option]).toBeDefined())
})

Expand All @@ -30,20 +31,35 @@ it('Should not interpolate characters inside square brackets', () => {
expect(actualDate.format('YYYY [l] YYYY')).toBe('1970 l 1970')
expect(actualDate.format('l [l] l')).toBe('1/1/1970 l 1/1/1970')
expect(actualDate.format('[L LL LLL LLLL]')).toBe(expectedDate.format('[L LL LLL LLLL]'))


const localeFormats = {
L: '[MMMM MM DD dddd]'
}
const mockedDayJsLocale = {
...es,
name: 'fake-locale',
formats: {
...localeFormats
}
}
const fakeDate = dayjs(date, { locale: mockedDayJsLocale })

expect(fakeDate.locale('fake-locale').format('l')).toEqual('MMMM MM DD dddd')
})

it('Recognizes localized format options', () => {
const { formats } = dayjs.en
const date = dayjs();
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(date.format(option)).toBe(date.format(formats[option])))
})

it('Uses correct English formats', () => {
const date = new Date()
const actualDate = dayjs(date)
const expectedDate = moment(date);
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(actualDate.format(option)).toBe(expectedDate.format(option)))
})

Expand Down Expand Up @@ -72,3 +88,21 @@ it('Uses the locale of the dayjs instance', () => {
const spanishDate = dayjs(date, { locale: es })
expect(englishDate.format('L LTS')).not.toBe(spanishDate.format('L LTS'))
})


it('Uses the localized lowercase formats if defined', () => {
const date = new Date()
const znDate = dayjs(date, { locale: znCn });
['l', 'll', 'lll', 'llll'].forEach(option => expect(znDate.format(option)).toBe(znDate.format(znCn.formats[option])))
})

it('Uses the localized uppercase formats as a base for lowercase formats, if not defined', () => {
const date = new Date()
const spanishDate = dayjs(date, { locale: es });

['l', 'll', 'lll', 'llll'].forEach((option) => {
const upperCaseFormat = es.formats[option.toUpperCase()]
const adaptedFormat = upperCaseFormat.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
expect(spanishDate.format(option)).toBe(spanishDate.format(adaptedFormat))
})
})