From 1e432a7af0045ba767d0f4ca8fde1b63dba28b9f Mon Sep 17 00:00:00 2001 From: sak Date: Wed, 6 Jun 2018 19:32:27 +0700 Subject: [PATCH 1/5] buddhistEra plugin - BBBB for B.E. year - BB for B.E. year 2 digit --- src/plugin/buddhistEra/index.js | 20 ++++++++++++++++++++ test/plugin/buddhistEra.test.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/plugin/buddhistEra/index.js create mode 100644 test/plugin/buddhistEra.test.js diff --git a/src/plugin/buddhistEra/index.js b/src/plugin/buddhistEra/index.js new file mode 100644 index 000000000..0357d90aa --- /dev/null +++ b/src/plugin/buddhistEra/index.js @@ -0,0 +1,20 @@ +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 locale = localeObject || this.$locale() + const str = formatStr || FORMAT_DEFAULT + const result = str.replace(/BBBB|BB/g, (match) => { + switch (match) { + case 'BB': + return (this.$y + 543) % 100 + default: // BBBB + return this.$y + 543 + } + }) + return oldFormat.bind(this)(result, locale) + } +} diff --git a/test/plugin/buddhistEra.test.js b/test/plugin/buddhistEra.test.js new file mode 100644 index 000000000..2482702f8 --- /dev/null +++ b/test/plugin/buddhistEra.test.js @@ -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) +}) From 122a4652045c0c9abc5be7cd40bd3011b297bb05 Mon Sep 17 00:00:00 2001 From: sak Date: Wed, 6 Jun 2018 23:52:04 +0700 Subject: [PATCH 2/5] BuddhistEra plugin document --- docs/en/Plugin.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index 32b903e24..310bceab0 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -124,6 +124,26 @@ dayjs.extend(isLeapYear) dayjs('2000-01-01').isLeapYear(); // true ``` +### BuddhistEra +- BuddhistEra extends `dayjs().format` API to supply Buddhist Era (B.E.) format options. +- Buddhist Era is a year numbering system that primarily used in mainland Southeast Asian countries of Cambodia, Laos, Myanmar and Thailand as well as in Sri Lanka and Chinese populations of Malaysia and Singapore for religious or official occasions ([Wikipedia](https://en.wikipedia.org/wiki/Buddhist_calendar)) +- To calculate BE year manually, just add 543 to year. For example 26 May 1977 AD/CE should display as 26 May 2520 BE (1977 + 543) + +```javascript +import buddhistEra from 'dayjs/plugin/buddhistEra' + +dayjs.extend(buddhistEra) + +dayjs().format('BBBB BB') +``` + +List of added formats: + +| Format | Output | Description | +| ------ | ---------------- | ------------------------------------- | +| `BBBB` | 2561 | Full BE Year (Year + 543) | +| `BB` | 61 | 2-digit of BE Year | + ## Customize You could build your own Day.js plugin to meet different needs. From c75c3600ed1ba0f09e77fd9e1d5e6bb41556210f Mon Sep 17 00:00:00 2001 From: sak Date: Fri, 8 Jun 2018 00:27:11 +0700 Subject: [PATCH 3/5] update to yearBias --- src/plugin/buddhistEra/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/plugin/buddhistEra/index.js b/src/plugin/buddhistEra/index.js index 0357d90aa..231dc58af 100644 --- a/src/plugin/buddhistEra/index.js +++ b/src/plugin/buddhistEra/index.js @@ -5,14 +5,15 @@ export default (o, c) => { // locale needed later const oldFormat = proto.format // extend en locale here proto.format = function (formatStr, localeObject) { + const yearBias = 543 const locale = localeObject || this.$locale() const str = formatStr || FORMAT_DEFAULT const result = str.replace(/BBBB|BB/g, (match) => { switch (match) { case 'BB': - return (this.$y + 543) % 100 + return (this.$y + yearBias) % 100 default: // BBBB - return this.$y + 543 + return this.$y + yearBias } }) return oldFormat.bind(this)(result, locale) From 4676213e0c4f2b741650dba0386a96f250d80baa Mon Sep 17 00:00:00 2001 From: sak Date: Mon, 11 Jun 2018 09:18:38 +0700 Subject: [PATCH 4/5] add padStart in buddhistYear plugin --- src/plugin/buddhistEra/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/plugin/buddhistEra/index.js b/src/plugin/buddhistEra/index.js index 231dc58af..95f593d7f 100644 --- a/src/plugin/buddhistEra/index.js +++ b/src/plugin/buddhistEra/index.js @@ -6,14 +6,15 @@ export default (o, c) => { // locale needed later // 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 (this.$y + yearBias) % 100 + return utils.padStart(String((this.$y + yearBias) % 100), 2, '0') default: // BBBB - return this.$y + yearBias + return utils.padStart(String(this.$y + yearBias), 4, '0') } }) return oldFormat.bind(this)(result, locale) From 68a1c9f51f93b9afb429976de5c4dba7c2950ab0 Mon Sep 17 00:00:00 2001 From: sak Date: Mon, 11 Jun 2018 10:00:36 +0700 Subject: [PATCH 5/5] 2 digit year with slice(-2) --- src/plugin/buddhistEra/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugin/buddhistEra/index.js b/src/plugin/buddhistEra/index.js index 95f593d7f..8f878e437 100644 --- a/src/plugin/buddhistEra/index.js +++ b/src/plugin/buddhistEra/index.js @@ -12,7 +12,7 @@ export default (o, c) => { // locale needed later const result = str.replace(/BBBB|BB/g, (match) => { switch (match) { case 'BB': - return utils.padStart(String((this.$y + yearBias) % 100), 2, '0') + return utils.padStart(String(this.$y + yearBias).slice(-2), 2, '0') default: // BBBB return utils.padStart(String(this.$y + yearBias), 4, '0') }