Skip to content

Commit

Permalink
Merge pull request #230 from saknarak/master
Browse files Browse the repository at this point in the history
buddhistEra plugin
  • Loading branch information
allmoviestvshowslistsfilmography28 committed Jun 11, 2018
2 parents 0db92c6 + f901bff commit 1aace46
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions src/plugin/buddhistEra/index.js
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)
}
}
33 changes: 33 additions & 0 deletions test/plugin/buddhistEra.test.js
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)
})

0 comments on commit 1aace46

Please sign in to comment.