Skip to content

Commit

Permalink
Merge pull request #754 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Dec 18, 2019
2 parents 5799383 + 78ec173 commit 18a294a
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ coverage
# build
/locale
/plugin
dayjs.min.js
/dayjs.min.js
/esm
index.d.ts
/index.d.ts

#dev
demo.js
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ class Dayjs {
locale(preset, object) {
if (!preset) return this.$L
const that = this.clone()
that.$L = parseLocale(preset, object, true)
const nextLocaleName = parseLocale(preset, object, true)
if (nextLocaleName) that.$L = nextLocaleName
return that
}

Expand Down
1 change: 1 addition & 0 deletions src/locale/fa.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const locale = {
weekdays: 'یک‌شنبه_دوشنبه_سه‌شنبه_چهارشنبه_پنج‌شنبه_جمعه_شنبه'.split('_'),
weekdaysShort: 'یک\u200cشنبه_دوشنبه_سه\u200cشنبه_چهارشنبه_پنج\u200cشنبه_جمعه_شنبه'.split('_'),
weekdaysMin: 'ی_د_س_چ_پ_ج_ش'.split('_'),
weekStart: 6,
months: 'ژانویه_فوریه_مارس_آوریل_مه_ژوئن_ژوئیه_اوت_سپتامبر_اکتبر_نوامبر_دسامبر'.split('_'),
monthsShort: 'ژانویه_فوریه_مارس_آوریل_مه_ژوئن_ژوئیه_اوت_سپتامبر_اکتبر_نوامبر_دسامبر'.split('_'),
ordinal: n => n,
Expand Down
2 changes: 1 addition & 1 deletion src/locale/it.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const locale = {
},
relativeTime: {
future: 'tra %s',
past: 'da %s',
past: '%s fa',
s: 'qualche secondo',
m: 'un minuto',
mm: '%d minuti',
Expand Down
15 changes: 15 additions & 0 deletions src/locale/mn.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ const locale = {
LL: 'YYYY оны MMMMын D',
LLL: 'YYYY оны MMMMын D HH:mm',
LLLL: 'dddd, YYYY оны MMMMын D HH:mm'
},
relativeTime: {
future: '%s',
past: '%s',
s: 'саяхан',
m: 'м',
mm: '%dм',
h: '1ц',
hh: '%dц',
d: '1ө',
dd: '%dө',
M: '1с',
MM: '%dс',
y: '1ж',
yy: '%dж'
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/locale/sv.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const locale = {
ordinal: (n) => {
const b = n % 10
const o = (b === 1) || (b === 2) ? 'a' : 'e'
return `${n}${o}`
return `[${n}${o}]`
},
formats: {
LT: 'HH:mm',
Expand Down
15 changes: 15 additions & 0 deletions src/locale/zh-cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ const locale = {
MM: '%d 个月',
y: '1 年',
yy: '%d 年'
},
meridiem: (hour, minute) => {
const hm = (hour * 100) + minute
if (hm < 600) {
return '凌晨'
} else if (hm < 900) {
return '早上'
} else if (hm < 1130) {
return '上午'
} else if (hm < 1230) {
return '中午'
} else if (hm < 1800) {
return '下午'
}
return '晚上'
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default (o, c, dayjs) => { // locale needed later
weekdaysMin: instance =>
(instance ? instance.format('dd') : getShort(this, 'weekdaysMin', 'weekdays', 2)),
weekdaysShort: instance =>
(instance ? instance.format('ddd') : getShort(this, 'weekdaysShort', 'weekdays', 3))
(instance ? instance.format('ddd') : getShort(this, 'weekdaysShort', 'weekdays', 3)),
longDateFormat: format => this.$locale().formats[format]
}
}
proto.localeData = function () {
Expand Down
9 changes: 9 additions & 0 deletions test/locale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,12 @@ describe('Instance locale inheritance', () => {
expect(dayjs.locale()).toBe(moment.locale())
})
})


it('Not supported locale string fallback to previous one', () => {
const D = dayjs()
const DFormat = D.format()
const D2 = D.locale('not_supported_locale_string')
const D2Format = D2.format()
expect(DFormat).toBe(D2Format)
})
21 changes: 21 additions & 0 deletions test/locale/sv.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import advancedFormat from '../../src/plugin/advancedFormat'
import '../../src/locale/sv'

dayjs.extend(advancedFormat)

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

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

it('Swedish locale Do 1a not format to 1am', () => {
expect(dayjs('2019-01-01').locale('sv').format('dddd Do MMMM'))
.toBe('tisdag 1a januari')
expect(dayjs('2019-01-02').locale('sv').format('dddd Do MMMM'))
.toBe('onsdag 2a januari')
})
21 changes: 21 additions & 0 deletions test/locale/zh-cn.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import '../../src/locale/zh-cn'

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

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

it('Meridiem', () => {
const dayjsObj = dayjs().locale('zh-cn')
const momentObj = moment().locale('zh-cn')
for (let i = 0; i <= 24; i += 1) {
expect(dayjsObj.add(i, 'hour').format('A'))
.toEqual(momentObj.clone().add(i, 'hour').format('A'))
}
})
11 changes: 9 additions & 2 deletions test/plugin/localeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import localeData from '../../src/plugin/localeData'
import localizedFormat from '../../src/plugin/localizedFormat'
import '../../src/locale/zh-cn'

dayjs.extend(localizedFormat)
dayjs.extend(localeData)

beforeEach(() => {
Expand All @@ -14,7 +16,7 @@ afterEach(() => {
MockDate.reset()
})

it('instance localeData', () => {
it('Instance localeData', () => {
const d = dayjs()
const m = moment()
const dayjsLocaleData = dayjs().localeData()
Expand All @@ -28,9 +30,14 @@ it('instance localeData', () => {
expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin())
expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m))
expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort())
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL']
longDateFormats.forEach((f) => {
expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f))
})
})

it('global localeData', () => {

it('Global localeData', () => {
dayjs.locale('zh-cn')
moment.locale('zh-cn')
let dayjsLocaleData = dayjs.localeData()
Expand Down
8 changes: 5 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference path="./locale/index.d.ts" />

export = dayjs;
declare function dayjs (date?: dayjs.ConfigType, option?: dayjs.OptionType, locale?: string): dayjs.Dayjs

Expand Down Expand Up @@ -91,18 +93,18 @@ declare namespace dayjs {

locale(): string

locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }): Dayjs
locale(preset: string | ILocale, object?: Partial<ILocale>): Dayjs
}

export type PluginFunc = (option: any, c: typeof Dayjs, d: typeof dayjs) => void

export function extend(plugin: PluginFunc, option?: any): Dayjs

export function locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }, isLocal?: boolean): string
export function locale(preset: string | ILocale, object?: Partial<ILocale>, isLocal?: boolean): string

export function isDayjs(d: any): d is Dayjs

export function unix(t: number): Dayjs

const Ls : { [key: string] : { [key: string]: any } }
const Ls : { [key: string] : ILocale }
}
11 changes: 11 additions & 0 deletions types/locale/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="./types.d.ts" />

declare module 'dayjs/locale/*' {
namespace locale {
interface Locale extends ILocale {}
}

const locale: locale.Locale

export = locale
}
33 changes: 33 additions & 0 deletions types/locale/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
declare interface ILocale {
name: string
weekdays?: string[]
months?: string[]
weekStart?: number
weekdaysShort?: string[]
monthsShort?: string[]
weekdaysMin?: string[]
ordinal?: (n: number) => number | string
formats: Partial<{
LT: string
LTS: string
L: string
LL: string
LLL: string
LLLL: string
}>
relativeTime: Partial<{
future: string
past: string
s: string
m: string
mm: string
h: string
hh: string
d: string
dd: string
M: string
MM: string
y: string
yy: string
}>
}

0 comments on commit 18a294a

Please sign in to comment.