Skip to content

Commit

Permalink
Merge pull request #1350 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Jan 22, 2021
2 parents 907478e + 82ef9a3 commit 8d6ad64
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ script:
- codecov
after_success:
- if [ "$TRAVIS_BRANCH" == "master" ] && [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then
npx travis-deploy-once --pro && npm run build && npm run babel && npm install -g @semantic-release/changelog @semantic-release/git semantic-release && semantic-release && curl ${TriggerUrl} && npm run test:sauce;
npx travis-deploy-once --pro && npm run build && npm run babel && npm install -g @semantic-release/changelog @semantic-release/git semantic-release && semantic-release && echo release success && curl ${TriggerUrl} && npm run test:sauce;
fi
branches:
except:
Expand Down
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

1 change: 1 addition & 0 deletions src/locale/pl.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const locale = {
monthsShort: 'sty_lut_mar_kwi_maj_cze_lip_sie_wrz_paź_lis_gru'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
yearStart: 4,
relativeTime: {
future: 'za %s',
past: '%s temu',
Expand Down
1 change: 1 addition & 0 deletions src/locale/pt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const locale = {
monthsShort: 'Jan_Fev_Mar_Abr_Mai_Jun_Jul_Ago_Set_Out_Nov_Dez'.split('_'),
ordinal: n => `${n}º`,
weekStart: 1,
yearStart: 4,
formats: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
Expand Down
15 changes: 15 additions & 0 deletions src/locale/sl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ const locale = {
LL: 'D. MMMM YYYY',
LLL: 'D. MMMM YYYY H:mm',
LLLL: 'dddd, D. MMMM YYYY H:mm'
},
relativeTime: {
future: 'čez %s',
past: 'pred %s',
s: 'nekaj sekund',
m: 'minuta',
mm: '%d minut',
h: 'ura',
hh: '%d ur',
d: 'dan',
dd: '%d dni',
M: 'mesec',
MM: '%d mesecev',
y: 'leto',
yy: '%d let'
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const match4 = /\d{4}/ // 0000 - 9999
const match1to2 = /\d\d?/ // 0 - 99
const matchSigned = /[+-]?\d+/ // -inf - inf
const matchOffset = /[+-]\d\d:?(\d\d)?/ // +00:00 -00:00 +0000 or -0000 +00
const matchWord = /\d*[^\s\d-:/()]+/ // Word
const matchWord = /\d*[^\s\d-_:/()]+/ // Word

let locale = {}

Expand Down
101 changes: 79 additions & 22 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK, REGEX_FORMAT } from '../../constant'
import {
MILLISECONDS_A_DAY,
MILLISECONDS_A_HOUR,
MILLISECONDS_A_MINUTE,
MILLISECONDS_A_SECOND,
MILLISECONDS_A_WEEK,
REGEX_FORMAT
} from '../../constant'

const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365
const MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30
Expand All @@ -16,7 +23,7 @@ const unitToMS = {
weeks: MILLISECONDS_A_WEEK
}

const isDuration = d => (d instanceof Duration) // eslint-disable-line no-use-before-define
const isDuration = d => d instanceof Duration // eslint-disable-line no-use-before-define

let $d
let $u
Expand All @@ -25,6 +32,30 @@ const wrapper = (input, instance, unit) =>
new Duration(input, unit, instance.$l) // eslint-disable-line no-use-before-define

const prettyUnit = unit => `${$u.p(unit)}s`
const isNegative = number => number < 0
const roundNumber = number =>
(isNegative(number) ? Math.ceil(number) : Math.floor(number))
const absolute = number => Math.abs(number)
const getNumberUnitFormat = (number, unit) => {
if (!number) {
return {
negative: false,
format: ''
}
}

if (isNegative(number)) {
return {
negative: true,
format: `${absolute(number)}${unit}`
}
}

return {
negative: false,
format: `${number}${unit}`
}
}

class Duration {
constructor(input, unit, locale) {
Expand All @@ -49,8 +80,14 @@ class Duration {
const d = input.match(durationRegex)
if (d) {
[,,
this.$d.years, this.$d.months, this.$d.weeks,
this.$d.days, this.$d.hours, this.$d.minutes, this.$d.seconds] = d
this.$d.years,
this.$d.months,
this.$d.weeks,
this.$d.days,
this.$d.hours,
this.$d.minutes,
this.$d.seconds
] = d
this.calMilliseconds()
return this
}
Expand All @@ -66,39 +103,54 @@ class Duration {

parseFromMilliseconds() {
let { $ms } = this
this.$d.years = Math.floor($ms / MILLISECONDS_A_YEAR)
this.$d.years = roundNumber($ms / MILLISECONDS_A_YEAR)
$ms %= MILLISECONDS_A_YEAR
this.$d.months = Math.floor($ms / MILLISECONDS_A_MONTH)
this.$d.months = roundNumber($ms / MILLISECONDS_A_MONTH)
$ms %= MILLISECONDS_A_MONTH
this.$d.days = Math.floor($ms / MILLISECONDS_A_DAY)
this.$d.days = roundNumber($ms / MILLISECONDS_A_DAY)
$ms %= MILLISECONDS_A_DAY
this.$d.hours = Math.floor($ms / MILLISECONDS_A_HOUR)
this.$d.hours = roundNumber($ms / MILLISECONDS_A_HOUR)
$ms %= MILLISECONDS_A_HOUR
this.$d.minutes = Math.floor($ms / MILLISECONDS_A_MINUTE)
this.$d.minutes = roundNumber($ms / MILLISECONDS_A_MINUTE)
$ms %= MILLISECONDS_A_MINUTE
this.$d.seconds = Math.floor($ms / MILLISECONDS_A_SECOND)
this.$d.seconds = roundNumber($ms / MILLISECONDS_A_SECOND)
$ms %= MILLISECONDS_A_SECOND
this.$d.milliseconds = $ms
}

toISOString() {
const Y = this.$d.years ? `${this.$d.years}Y` : ''
const M = this.$d.months ? `${this.$d.months}M` : ''
const Y = getNumberUnitFormat(this.$d.years, 'Y')
const M = getNumberUnitFormat(this.$d.months, 'M')

let days = +this.$d.days || 0
if (this.$d.weeks) {
days += this.$d.weeks * 7
}
const D = days ? `${days}D` : ''
const H = this.$d.hours ? `${this.$d.hours}H` : ''
const m = this.$d.minutes ? `${this.$d.minutes}M` : ''

const D = getNumberUnitFormat(days, 'D')
const H = getNumberUnitFormat(this.$d.hours, 'H')
const m = getNumberUnitFormat(this.$d.minutes, 'M')

let seconds = this.$d.seconds || 0
if (this.$d.milliseconds) {
seconds += this.$d.milliseconds / 1000
}
const S = seconds ? `${seconds}S` : ''
const T = (H || m || S) ? 'T' : ''
const result = `P${Y}${M}${D}${T}${H}${m}${S}`
return result === 'P' ? 'P0D' : result

const S = getNumberUnitFormat(seconds, 'S')

const negativeMode =
Y.negative ||
M.negative ||
D.negative ||
H.negative ||
m.negative ||
S.negative

const T = H.format || m.format || S.format ? 'T' : ''
const P = negativeMode ? '-' : ''

const result = `${P}P${Y.format}${M.format}${D.format}${T}${H.format}${m.format}${S.format}`
return result === 'P' || result === '-P' ? 'P0D' : result
}

toJSON() {
Expand Down Expand Up @@ -136,11 +188,11 @@ class Duration {
if (pUnit === 'milliseconds') {
base %= 1000
} else if (pUnit === 'weeks') {
base = Math.floor(base / unitToMS[pUnit])
base = roundNumber(base / unitToMS[pUnit])
} else {
base = this.$d[pUnit]
}
return base
return base === 0 ? 0 : base // a === 0 will be true on both 0 and -0
}

add(input, unit, isSubtract) {
Expand All @@ -152,6 +204,7 @@ class Duration {
} else {
another = wrapper(input, this).$ms
}

return wrapper(this.$ms + (another * (isSubtract ? -1 : 1)), this)
}

Expand All @@ -170,7 +223,10 @@ class Duration {
}

humanize(withSuffix) {
return $d().add(this.$ms, 'ms').locale(this.$l).fromNow(!withSuffix)
return $d()
.add(this.$ms, 'ms')
.locale(this.$l)
.fromNow(!withSuffix)
}

milliseconds() { return this.get('milliseconds') }
Expand All @@ -190,6 +246,7 @@ class Duration {
years() { return this.get('years') }
asYears() { return this.as('years') }
}

export default (option, Dayjs, dayjs) => {
$d = dayjs
$u = dayjs().$utils()
Expand Down
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))
})
})
9 changes: 9 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,12 @@ describe('meridiem locale', () => {
expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input)
})
})

it('parse a string for MMM month format with underscore delimiter', () => {
const input = 'Jan_2021'
const format = 'MMM_YYYY'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '21_Jan_2021_123523'
const format2 = 'DD_MMM_YYYY_hhmmss'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
Loading

0 comments on commit 8d6ad64

Please sign in to comment.