Skip to content

Commit

Permalink
Merge pull request #495 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
splashwizard authored Feb 14, 2019
2 parents fc95177 + 961b995 commit f49aff7
Show file tree
Hide file tree
Showing 26 changed files with 414 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Exactly the same as `dayjs#locale`, but only use locale in a specific instance.
```js
import 'dayjs/locale/es'
dayjs().locale('es').format() // use loaded locale locally
dayjs('2018-4-28', { locale: es }) // through constructor
dayjs('2018-4-28', { locale: 'es' }) // through constructor
```

## Installation
Expand Down
2 changes: 2 additions & 0 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ import isBetween from "dayjs/plugin/isBetween";
dayjs.extend(isBetween);

dayjs("2010-10-20").isBetween("2010-10-19", dayjs("2010-10-25"), "year");
dayjs('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '[)');
// '[' indicates inclusion, '(' indicates exclusion
```

### DayOfYear
Expand Down
2 changes: 2 additions & 0 deletions docs/es-es/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ import isBetween from "dayjs/plugin/isBetween";
dayjs.extend(isBetween);

dayjs("2010-10-20").isBetween("2010-10-19", dayjs("2010-10-25"), "year");
dayjs('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '[)');
// '[' indicates inclusion, '(' indicates exclusion
```

### DayOfYear
Expand Down
2 changes: 1 addition & 1 deletion docs/ja/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dayjs.locale('en') // switch back to default English locale globally
```js
import 'dayjs/locale/es'
dayjs().locale('es').format() // 読み込んだロケールを特定のインスタンスに適用
dayjs('2018-4-28', { locale: es }) // コンストラクタを通して適用
dayjs('2018-4-28', { locale: 'es' }) // コンストラクタを通して適用
```

## インストール
Expand Down
2 changes: 2 additions & 0 deletions docs/ja/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ import isBetween from 'dayjs/plugin/isBetween'
dayjs.extend(isBetween)

dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year');
dayjs('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '[)');
// '[' indicates inclusion, '(' indicates exclusion
```
### DayOfYear

Expand Down
2 changes: 1 addition & 1 deletion docs/ko/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dayjs.locale('en') // switch back to default English locale globally
```js
import 'dayjs/locale/es'
dayjs().locale('es').format() // 국지적으로 locale을 로드하여 사용합니다..
dayjs('2018-4-28', { locale: es }) // through constructor
dayjs('2018-4-28', { locale: 'es' }) // through constructor
```

## Installation
Expand Down
2 changes: 2 additions & 0 deletions docs/ko/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ import isBetween from 'dayjs/plugin/isBetween'
dayjs.extend(isBetween)

dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year');
dayjs('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '[)');
// '[' indicates inclusion, '(' indicates exclusion
```

### DayOfYear
Expand Down
2 changes: 1 addition & 1 deletion docs/pt-br/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Exatamente o mesmo que `dayjs#locale`, porém utilizando somente o *locale* em u
```js
import 'dayjs/locale/es'
dayjs().locale('es').format() // usar locale localmente
dayjs('2018-4-28', { locale: es }) // também pode ser feito no constructor
dayjs('2018-4-28', { locale: 'es' }) // também pode ser feito no constructor
```

## Instalação
Expand Down
2 changes: 2 additions & 0 deletions docs/pt-br/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ import isBetween from 'dayjs/plugin/isBetween'
dayjs.extend(isBetween)

dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year');
dayjs('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '[)');
// '[' indicates inclusion, '(' indicates exclusion
```
### DayOfYear

Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dayjs.locale('en') // 全局使用默认的英语语言
```js
import 'dayjs/locale/es'
dayjs().locale('es').format() // 局部修改语言配置
dayjs('2018-4-28', { locale: es }) // 在新建实例时指定
dayjs('2018-4-28', { locale: 'es' }) // 在新建实例时指定
```

## 安装
Expand Down
2 changes: 2 additions & 0 deletions docs/zh-cn/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ import isBetween from 'dayjs/plugin/isBetween'
dayjs.extend(isBetween)

dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year');
dayjs('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '[)');
// '[' 包含, '(' 不包含
```

### DayOfYear
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "2KB immutable date time library alternative to Moment.js with the same modern API ",
"main": "dayjs.min.js",
"types": "index.d.ts",
"module": "./esm/index.js",
"module": "dayjs.min.js",
"scripts": {
"test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && jest",
"test-tz": "jest test/timezone.test --coverage=false",
"lint": "./node_modules/.bin/eslint src/* test/* build/*",
"prettier": "prettier --write \"docs/**/*.md\"",
"babel": "cross-env BABEL_ENV=build babel src --out-dir esm --copy-files",
"build": "cross-env BABEL_ENV=build node build && npm run size",
"sauce": "npx karma start karma.sauce.conf.js",
Expand Down Expand Up @@ -86,6 +87,7 @@
"mockdate": "^2.0.2",
"moment": "^2.22.0",
"pre-commit": "^1.2.2",
"prettier": "^1.16.1",
"rollup": "^0.57.1",
"rollup-plugin-babel": "^4.0.0-beta.4",
"rollup-plugin-uglify": "^3.0.0",
Expand Down
7 changes: 7 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
useTabs: false,
printWidth: 80,
singleQuote: true,
trailingComma: 'none',
semi: false
}
6 changes: 0 additions & 6 deletions src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,3 @@ export const INVALID_DATE_STRING = 'Invalid Date'
// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/
export const REGEX_FORMAT = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g

export const en = {
name: 'en',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_')
}
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as C from './constant'
import U from './utils'
import en from './locale/en'

let L = 'en' // global locale
const Ls = {} // global loaded locale
Ls[L] = C.en
Ls[L] = en

const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-define

Expand Down Expand Up @@ -163,18 +164,18 @@ class Dayjs {
(isStartOf ? argumentStart : argumentEnd).slice(slice)
), this)
}

const { $W, $M, $D } = this
switch (unit) {
case C.Y:
return isStartOf ? instanceFactory(1, 0) :
instanceFactory(31, 11)
case C.M:
return isStartOf ? instanceFactory(1, this.$M) :
instanceFactory(0, this.$M + 1)
return isStartOf ? instanceFactory(1, $M) :
instanceFactory(0, $M + 1)
case C.W: {
const weekStart = this.$locale().weekStart || 0
return isStartOf ? instanceFactory(this.$D - (this.$W - weekStart), this.$M) :
instanceFactory(this.$D + (6 - (this.$W - weekStart)), this.$M)
const gap = ($W < weekStart ? $W + 7 : $W) - weekStart
return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M)
}
case C.D:
case C.DATE:
Expand Down
1 change: 1 addition & 0 deletions src/locale/ar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const locale = {
name: 'ar',
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
months: 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
weekStart: 6,
relativeTime: {
future: 'بعد %s',
past: 'منذ %s',
Expand Down
5 changes: 5 additions & 0 deletions src/locale/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
name: 'en',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_')
}
2 changes: 1 addition & 1 deletion src/locale/fi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const locale = {
weekdaysShort: 'su_ma_ti_ke_to_pe_la'.split('_'), // There is no short form of weekdays in Finnish except this 2 letter format so it is used for both 'weekdaysShort' and 'weekdaysMin'
weekdaysMin: 'su_ma_ti_ke_to_pe_la'.split('_'),
months: 'tammikuu_helmikuu_maaliskuu_huhtikuu_toukokuu_kesäkuu_heinäkuu_elokuu_syyskuu_lokakuu_marraskuu_joulukuu'.split('_'), // Note month names are not capitalized in Finnish
monthsShort: 'tammikuu_helmikuu_maaliskuu_huhtikuu_toukokuu_kesäkuu_heinäkuu_elokuu_syyskuu_lokakuu_marraskuu_joulukuu'.split('_'), // There is no short form of months in Finnish so just use the regular long form always
monthsShort: 'tammi_helmi_maalis_huhti_touko_kesä_heinä_elo_syys_loka_marras_joulu'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
/*
Expand Down
36 changes: 36 additions & 0 deletions src/locale/ms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import dayjs from 'dayjs'

const locale = {
name: 'ms',
weekdays: 'Ahad_Isnin_Selasa_Rabu_Khamis_Jumaat_Sabtu'.split('_'),
months: 'Januari_Februari_Mac_April_Mei_Jun_Julai_Ogos_September_Oktober_November_Disember'.split('_'),
weekStart: 1,
formats: {
LT: 'HH.mm',
LTS: 'HH.mm.ss',
L: 'DD/MM/YYYY',
LL: 'D MMMM YYYY',
LLL: 'D MMMM YYYY HH.mm',
LLLL: 'dddd, D MMMM YYYY HH.mm'
},
relativeTime: {
future: 'dalam %s',
past: '%s yang lepas',
s: 'beberapa saat',
m: 'seminit',
mm: '%d minit',
h: 'sejam',
hh: '%d jam',
d: 'sehari',
dd: '%d hari',
M: 'sebulan',
MM: '%d bulan',
y: 'setahun',
yy: '%d tahun'
},
ordinal: n => `${n}.`
}

dayjs.locale(locale, null, true)

export default locale
3 changes: 3 additions & 0 deletions src/locale/sv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import dayjs from 'dayjs'
const locale = {
name: 'sv',
weekdays: 'söndag_måndag_tisdag_onsdag_torsdag_fredag_lördag'.split('_'),
weekdaysShort: 'sön_mån_tis_ons_tor_fre_lör'.split('_'),
weekdaysMin: 'sö_må_ti_on_to_fr_lö'.split('_'),
months: 'januari_februari_mars_april_maj_juni_juli_augusti_september_oktober_november_december'.split('_'),
monthsShort: 'jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec'.split('_'),
weekStart: 1,
ordinal: (n) => {
const b = n % 10
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 @@ -129,7 +129,7 @@ const parseFormattedInput = (input, format) => {
}
const now = new Date()
return new Date(
year || now.getFullYear(), month - 1 || now.getMonth(), day || now.getDate(),
year || now.getFullYear(), month > 0 ? month - 1 : now.getMonth(), day || now.getDate(),
hours || 0, minutes || 0, seconds || 0, milliseconds || 0
)
} catch (e) {
Expand Down
11 changes: 8 additions & 3 deletions src/plugin/isBetween/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export default (o, c, d) => {
c.prototype.isBetween = function (a, b, u) {
c.prototype.isBetween = function (a, b, u, i) {
const dA = d(a)
const dB = d(b)
i = i || '()'
const dAi = i[0] === '('
const dBi = i[1] === ')'

return (this.isAfter(dA, u) && this.isBefore(dB, u))
|| (this.isBefore(dA, u) && this.isAfter(dB, u))
return ((dAi ? this.isAfter(dA, u) : !this.isBefore(dA, u)) &&
(dBi ? this.isBefore(dB, u) : !this.isAfter(dB, u)))
|| ((dAi ? this.isBefore(dA, u) : !this.isAfter(dA, u)) &&
(dBi ? this.isAfter(dB, u) : !this.isBefore(dB, u)))
}
}
7 changes: 5 additions & 2 deletions test/locale/keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ it('Locale keys', () => {

expect(months).toEqual(expect.any(Array))
// function pass date return string or number or null
for (let i = 1; i <= 31; i += 1) {
expect(ordinal(i)).toEqual(expect.anything())
if (name !== 'en') { // en ordinal set in advancedFormat
for (let i = 1; i <= 31; i += 1) {
expect(ordinal(i)).toEqual(expect.anything())
}
}

expect(dayjs().locale(name).$locale().name).toBe(name)
if (formats) {
expect(Object.keys(formats).sort()).toEqual(['L', 'LL', 'LLL', 'LLLL', 'LT', 'LTS'].sort())
Expand Down
13 changes: 9 additions & 4 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ describe('StartOf EndOf', () => {
})

it('StartOf week with locale', () => {
const testArr = ['zh-cn']
testArr.forEach((l) => {
expect(dayjs().locale(l).startOf('week').date()).toBe(moment().locale(l).startOf('week').date())
expect(dayjs().locale(l).endOf('week').date()).toBe(moment().locale(l).endOf('week').date())
const testDate = [undefined, '2019-02-10', '2019-02-11', '2019-02-12', '2019-02-13', '2019-02-14', '2019-02-15', '2019-02-16']
const testLocale = ['zh-cn', 'ar', 'en']
testDate.forEach((d) => {
testLocale.forEach((l) => {
expect(dayjs(d).locale(l).startOf('week').date())
.toBe(moment(d).locale(l).startOf('week').date())
expect(dayjs(d).locale(l).endOf('week').date())
.toBe(moment(d).locale(l).endOf('week').date())
})
})
})
})
Expand Down
6 changes: 6 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ it('parse padded string', () => {
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})

it('parse string January (getMonth() = 0)', () => {
const input = '01/01/2019'
const format = 'DD/MM/YYYY'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})

it('parse unpadded string', () => {
const input = '2.5.18 1:2:3.4 PM -0100'
const format = 'D.M.YY H:m:s.S A ZZ'
Expand Down
Loading

0 comments on commit f49aff7

Please sign in to comment.