-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from saknarak/master
buddhistEra plugin
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { FORMAT_DEFAULT } from '../../constant' | ||
|
||
export default (o, c) => { // locale needed later | ||
const proto = c.prototype | ||
const oldFormat = proto.format | ||
// extend en locale here | ||
proto.format = function (formatStr, localeObject) { | ||
const yearBias = 543 | ||
const utils = this.$utils() | ||
const locale = localeObject || this.$locale() | ||
const str = formatStr || FORMAT_DEFAULT | ||
const result = str.replace(/BBBB|BB/g, (match) => { | ||
switch (match) { | ||
case 'BB': | ||
return utils.padStart(String(this.$y + yearBias).slice(-2), 2, '0') | ||
default: // BBBB | ||
return utils.padStart(String(this.$y + yearBias), 4, '0') | ||
} | ||
}) | ||
return oldFormat.bind(this)(result, locale) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import MockDate from 'mockdate' | ||
import moment from 'moment' | ||
import dayjs from '../../src' | ||
import buddhistEra from '../../src/plugin/buddhistEra' | ||
|
||
dayjs.extend(buddhistEra) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
it('Format empty string', () => { | ||
expect(dayjs().format()).toBe(moment().format()) | ||
}) | ||
|
||
it('Format Buddhist Era 2 digit', () => { | ||
expect(dayjs().format('BB')).toBe(`${(moment().year() + 543) % 100}`) | ||
}) | ||
|
||
it('Format Buddhist Era 4 digit', () => { | ||
expect(dayjs().format('BBBB')).toBe(`${moment().year() + 543}`) | ||
}) | ||
|
||
it('Format Buddhist Era 4 digit with other format', () => { | ||
const format = 'D MMM BBBB' | ||
const today = moment() | ||
const momentDate = today.format(format).replace('BBBB', today.year() + 543) | ||
expect(dayjs().format(format)).toBe(momentDate) | ||
}) |