Skip to content

Commit

Permalink
refactor: refactor ordinal && add test
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed May 21, 2018
1 parent c72dac0 commit a57aabe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/plugin/advancedFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { FORMAT_DEFAULT } from '../constant'
export default (o, c, d) => { // locale needed later
const proto = c.prototype
const oldFormat = proto.format
// eslint-disable-next-line no-nested-ternary
d.en.ordinal = (number) => {
let suffix = 'th'
if (![11, 12].includes(number)) {
suffix = ['st', 'nd', 'rd'][(number % 10) - 1] || suffix
}
return `${number}[${suffix}]`
const s = ['th', 'st', 'nd', 'rd']
const v = number % 100
return `[${number}${(s[(v - 20) % 10] || s[v] || s[0])}]`
}
// extend en locale here
proto.format = function (formatStr, localeObject) {
Expand Down
4 changes: 4 additions & 0 deletions test/plugin/advancedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ it('Format Day of Month Do 1 - 31', () => {
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
d = '2018-05-11'
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
d = '2018-05-12'
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
d = '2018-05-13'
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
d = '2018-05-22'
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
})
Expand Down

0 comments on commit a57aabe

Please sign in to comment.