Skip to content

Commit

Permalink
Add bn ordinals support
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwinklermedel committed Jan 11, 2021
1 parent 8e995e9 commit 9bd8b11
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/locale/bn.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
// Bengali [bn]
import dayjs from 'dayjs'

const symbolMap = {
1: '১',
2: '২',
3: '৩',
4: '৪',
5: '৫',
6: '৬',
7: '৭',
8: '৮',
9: '৯',
0: '০'
}

const numberMap = {
'১': '1',
'২': '2',
'৩': '3',
'৪': '4',
'৫': '5',
'৬': '6',
'৭': '7',
'৮': '8',
'৯': '9',
'০': '0'
}

const locale = {
name: 'bn',
weekdays: 'রবিবার_সোমবার_মঙ্গলবার_বুধবার_বৃহস্পতিবার_শুক্রবার_শনিবার'.split('_'),
months: 'জানুয়ারী_ফেব্রুয়ারি_মার্চ_এপ্রিল_মে_জুন_জুলাই_আগস্ট_সেপ্টেম্বর_অক্টোবর_নভেম্বর_ডিসেম্বর'.split('_'),
months: 'জানুয়ারি_ফেব্রুয়ারি_মার্চ_এপ্রিল_মে_জুন_জুলাই_আগস্ট_সেপ্টেম্বর_অক্টোবর_নভেম্বর_ডিসেম্বর'.split('_'),
weekdaysShort: 'রবি_সোম_মঙ্গল_বুধ_বৃহস্পতি_শুক্র_শনি'.split('_'),
monthsShort: 'জানু_ফেব_মার্চ_এপ্র_মে_জুন_জুল_আগ_সেপ্ট_অক্টো_নভে_ডিসে'.split('_'),
weekdaysMin: 'রবি_সোম_মঙ্গ_বুধ_বৃহঃ_শুক্র_শনি'.split('_'),
preparse(string) {
return string.replace(/[১২৩৪৫৬৭৮৯০]/g, match => numberMap[match])
},
postformat(string) {
return string.replace(/\d/g, match => symbolMap[match])
},
ordinal: n => n,
formats: {
LT: 'A h:mm সময়',
Expand Down Expand Up @@ -37,4 +69,3 @@ const locale = {
dayjs.locale(locale, null, true)

export default locale

71 changes: 71 additions & 0 deletions test/locale/bn.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import localeData from '../../src/plugin/localeData'
import preParsePostFormat from '../../src/plugin/preParsePostFormat'
import '../../src/locale/bn'

dayjs.extend(localeData)
dayjs.extend(relativeTime)
dayjs.extend(preParsePostFormat)

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

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

it('Format Month with locale function', () => {
for (let i = 0; i <= 7; i += 1) {
const dayjsBN = dayjs()
.locale('bn')
.add(i, 'day')
const momentBN = moment()
.locale('bn')
.add(i, 'day')
const testFormat1 = 'DD MMMM YYYY MMM'
const testFormat2 = 'MMMM'
const testFormat3 = 'MMM'
expect(dayjsBN.format(testFormat1)).toEqual(momentBN.format(testFormat1))
expect(dayjsBN.format(testFormat2)).toEqual(momentBN.format(testFormat2))
expect(dayjsBN.format(testFormat3)).toEqual(momentBN.format(testFormat3))
}
})

it('Preparse with locale function', () => {
for (let i = 0; i <= 7; i += 1) {
dayjs.locale('bn')
const momentBN = moment()
.locale('bn')
.add(i, 'day')
expect(dayjs(momentBN.format()).format()).toEqual(momentBN.format())
}
})

it('RelativeTime: Time from X', () => {
const T = [
[44.4, 'second'], // a few seconds
[89.5, 'second'], // a minute
[130, 'second'], // two minutes
[43, 'minute'], // 44 minutes
[1, 'hour'], // 1 hour
[21, 'hour'], // 21 hours
[2, 'day'], // 2 days
[25, 'day'], // 25 days
[2, 'month'], // 2 months
[10, 'month'], // 10 months
[18, 'month'], // 2 years
[15, 'year'] // 15 years
]

T.forEach((t) => {
dayjs.locale('bn')
moment.locale('bn')
expect(dayjs().from(dayjs().add(t[0], t[1]))).toBe(moment().from(moment().add(t[0], t[1])))
expect(dayjs().from(dayjs().add(t[0], t[1]), true))
.toBe(moment().from(moment().add(t[0], t[1]), true))
})
})

0 comments on commit 9bd8b11

Please sign in to comment.