Skip to content

Commit

Permalink
feat: Recognize localizable format L, LT and LTS in custom parse formats
Browse files Browse the repository at this point in the history
* Translate localizable formats for date parsing as well. Only for the numeric format tokens for the time being.
* Expose the function getLocale via Utils to get access to te global default locale.
  • Loading branch information
prantlf committed Oct 14, 2018
1 parent bb543f2 commit 881e66c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ dayjs('05/02/69 1:02:03 PM -05:00', { format: 'MM/DD/YY H:mm:ss A Z' })
| `A` | AM PM | Post or ante meridiem, upper-case |
| `a` | am pm | Post or ante meridiem, lower-case |

If the plugin [LocalizableFormat](#localizableformat) is installed, format tokens "L", "LT" and "LTS" can be used too. They will be translated to the concrete tokens above using the `locale` parameter passed to the `dayjs` constructor, or using the global `dayjs` locale.

### TimeZone
- TimeZone extends `dayjs()` and `dayjs().format` APIs to support the most important usage scenatrios - parsing from a specific time zone and formatting in other time zone.

Expand Down
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ const parseLocale = (preset, object, isLocal) => {
return l
}

const getLocale = (locale) => {
if (typeof locale === 'string') {
// Get locale by its name.
return Ls[locale]
}
if (locale) {
// Register a locale object by its name.
const { name } = locale
Ls[name] = locale
} else {
// Get the de4fault locale.
locale = Ls[L]
}
return locale
}

const dayjs = (date, c) => {
if (isDayjs(date)) {
if (c) {
Expand All @@ -48,6 +64,7 @@ const wrapper = (date, instance, cloneDate) =>

const Utils = U // for plugin use
Utils.parseLocale = parseLocale
Utils.getLocale = getLocale
Utils.isDayjs = isDayjs
Utils.wrapper = wrapper

Expand Down
12 changes: 12 additions & 0 deletions src/plugin/localizableFormat/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FORMAT_DEFAULT } from '../../constant'
import Utils from '../../utils'

export default (o, c, d) => {
const proto = c.prototype
const oldParse = proto.parse
const oldFormat = proto.format
const englishFormats = {
LTS: 'h:mm:ss A',
Expand All @@ -12,6 +14,16 @@ export default (o, c, d) => {
LLLL: 'dddd, MMMM D, YYYY h:mm A'
}
d.en.formats = englishFormats
proto.parse = function (cfg) {
const format = cfg && cfg.format
if (format) {
const locale = Utils.getLocale(cfg.locale)
const formats = locale.formats || {}
cfg.format = format.replace(/LTS|LT|L/g, match =>
formats[match] || englishFormats[match])
}
oldParse.call(this, cfg)
}
proto.format = function (formatStr) {
const locale = this.$locale()
const formats = locale.formats || {}
Expand Down
70 changes: 70 additions & 0 deletions test/plugin/localizableCustomParseFormat.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import es from '../../src/locale/es'
import customParseFormat from '../../src/plugin/customParseFormat'
import localizableFormat from '../../src/plugin/localizableFormat'

dayjs.extend(customParseFormat)
dayjs.extend(localizableFormat)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

it('does not break the built-in parsing', () => {
const input = '2018-05-02 01:02:03.004'
expect(dayjs(input).valueOf()).toBe(moment(input).valueOf())
})

it('does not break the non-localizable parsing', () => {
const input = '2.5.18'
const format = 'D.M.YY'
expect(dayjs(input, { format }).valueOf()).toBe(moment(input, format).valueOf())
})

it('parses English L', () => {
const input = '05/02/2018'
const format = 'L'
expect(dayjs(input, { format }).valueOf()).toBe(moment(input, format).valueOf())
})

it('parses English LT', () => {
const input = '05/02/2018 12:31 PM'
const format = 'L LT'
expect(dayjs(input, { format }).valueOf()).toBe(moment(input, format).valueOf())
})

it('parses English LTS', () => {
const input = '05/02/2018 12:31:32 PM'
const format = 'L LTS'
expect(dayjs(input, { format }).valueOf()).toBe(moment(input, format).valueOf())
})

it('parses Spanish L with locale es object', () => {
const input = '02/05/2018'
const format = 'L'
const date = new Date(2018, 4, 2)
expect(dayjs(input, { format, locale: es }).valueOf()).toBe(date.valueOf())
})

it('parses Spanish LT with locale es string', () => {
const input = '02/05/2018 12:31'
const format = 'L LT'
const date = new Date(2018, 4, 2, 12, 31)
expect(dayjs(input, { format, locale: 'es' }).valueOf()).toBe(date.valueOf())
})

it('parses Spanish LTS with the global locale', () => {
const input = '02/05/2018 12:31:32'
const format = 'L LTS'
const expectedDate = new Date(2018, 4, 2, 12, 31, 32)
dayjs.locale('es');
const actualDate = dayjs(input, { format })
dayjs.locale('en')
expect(actualDate.valueOf()).toBe(expectedDate.valueOf())
})

0 comments on commit 881e66c

Please sign in to comment.