Skip to content

Commit

Permalink
fix(): format API update and locale file update
Browse files Browse the repository at this point in the history
Merge pull request #228 from iamkun/feature/iamkun update format API
  • Loading branch information
iamkun authored Jun 6, 2018
2 parents c223371 + d37f456 commit 5ca48f0
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 112 deletions.
10 changes: 10 additions & 0 deletions docs/en/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019'
| `D` | 1-31 | The day of the month |
| `DD` | 01-31 | The day of the month, 2-digits |
| `d` | 0-6 | The day of the week, with Sunday as 0 |
| `dd` | Su-Sa | The min name of the day of the week |
| `ddd` | Sun-Sat | The short name of the day of the week |
| `dddd` | Sunday-Saturday | The name of the day of the week |
| `H` | 0-23 | The hour |
| `HH` | 00-23 | The hour, 2-digits |
Expand Down Expand Up @@ -378,6 +380,8 @@ dayjs().isAfter(dayjs()); // false

### Is Leap Year `.isLeapYear()`

**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead**

Returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.

```js
Expand All @@ -391,3 +395,9 @@ dayjs('2000-01-01').isLeapYear(); // true
`.from` `.to` `.fromNow` `.toNow` to get relative time

plugin [`RelativeTime`](./Plugin.md#relativetime)

### IsLeapYear

`.isLeapYear` to get is a leap year or not

plugin [`IsLeapYear`](./Plugin.md#isleapyear)
3 changes: 3 additions & 0 deletions docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ Template of a Day.js locale Object.
const localeObject = {
name: 'es', // name String
weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
months: 'Enero_Febrero ... '.split('_'), // months Array
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
ordinal: n => `${n}º`, // ordinal Function (number) => return number + output
relativeTime: { // relative time format strings, keep %s %d as the same
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
Expand Down
10 changes: 10 additions & 0 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ Returns the `string` of relative time to X.
| 11 months to 17months | y | a year ago |
| 18 months+ | yy | 2 years ago ... 20 years ago |

### IsLeapYear
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.

```javascript
import isLeapYear from 'dayjs/plugin/isLeapYear'

dayjs.extend(isLeapYear)

dayjs('2000-01-01').isLeapYear(); // true
```

## Customize

Expand Down
10 changes: 10 additions & 0 deletions docs/ja/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ dayjs().format('{YYYY} MM-DDTHH:mm:ssZ[Z]'); // "{2014} 09-08T08:02:17-05:00Z"
| `D` | 1-31 ||
| `DD` | 01-31 | 2桁の日 |
| `d` | 0-6 | 曜日 (日曜は0) |
| `dd` | Su-Sa | The min name of the day of the week |
| `ddd` | Sun-Sat | 曜日の略称 |
| `dddd` | Sunday-Saturday | 曜日名 |
| `H` | 0-23 | 時間 |
| `HH` | 00-23 | 2桁の時間 |
Expand Down Expand Up @@ -448,6 +450,8 @@ dayjs().isAfter(dayjs()); // false

#### Is Leap Year

**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead**

* Boolean を返します

その年がうるう年かどうかをチェックします。
Expand All @@ -464,3 +468,9 @@ dayjs('2000-01-01').isLeapYear(); // true
`.from` `.to` `.fromNow` `.toNow` to get relative time

plugin [`RelativeTime`](./Plugin.md#relativetime)

### IsLeapYear

`.isLeapYear` to get is a leap year or not

plugin [`IsLeapYear`](./Plugin.md#isleapyear)
3 changes: 3 additions & 0 deletions docs/ja/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ dayjs().locale('es').format() // ロケールを特定のインスタンスに
const localeObject = {
name: 'es', // ロケール名を表す文字列
weekdays: 'Domingo_Lunes ...'.split('_'), // 曜日の配列
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
months: 'Enero_Febrero ... '.split('_'), // 月の配列
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
ordinal: n => `${n}º`, // 序数 Function (number) => return number + output
relativeTime: { // relative time format strings, keep %s %d as the same
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
Expand Down
10 changes: 10 additions & 0 deletions docs/ja/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ Returns the `string` of relative time to X.
| 11 months to 17months | y | a year ago |
| 18 months+ | yy | 2 years ago ... 20 years ago |

### IsLeapYear
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.

```javascript
import isLeapYear from 'dayjs/plugin/isLeapYear'

dayjs.extend(isLeapYear)

dayjs('2000-01-01').isLeapYear(); // true
```

## カスタマイズ

Expand Down
10 changes: 10 additions & 0 deletions docs/ko/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019'
| `D` | 1-31 ||
| `DD` | 01-31 | 일, 두 자리로 표현 |
| `d` | 0-6 | 요일, 일요일은 0 |
| `dd` | Su-Sa | The min name of the day of the week |
| `ddd` | Sun-Sat | The short name of the day of the week |
| `dddd` | Sunday-Saturday | 요일 이름 |
| `H` | 0-23 | 시간 |
| `HH` | 00-23 | 시간, 두 자리로 표현 |
Expand Down Expand Up @@ -378,6 +380,8 @@ dayjs().isAfter(dayjs()); // false

### Is Leap Year `.isLeapYear()`

**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead**

`Dayjs` 값이 윤년인지를 확인합니다. 반환 타입은 `boolean` 입니다.

```js
Expand All @@ -391,3 +395,9 @@ dayjs('2000-01-01').isLeapYear(); // true
`.from` `.to` `.fromNow` `.toNow`에 대한 상대 시간을 가져옵니다.

플러그인 [`RelativeTime`](./Plugin.md#relativetime)

### IsLeapYear

`.isLeapYear` to get is a leap year or not

plugin [`IsLeapYear`](./Plugin.md#isleapyear)
3 changes: 3 additions & 0 deletions docs/ko/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ Day.js locale 오브젝트 템플릿 입니다.
const localeObject = {
name: 'es', // name String
weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
months: 'Enero_Febrero ... '.split('_'), // months Array
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
ordinal: n => `${n}º`, // ordinal Function (number) => return number + output
relativeTime = { // relative time format strings, keep %s %d as the same
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
Expand Down
10 changes: 10 additions & 0 deletions docs/ko/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ X 시간부터 상대시간을 `string`으로 반환합니다.
| 11 달 ~ 17 달 | y | 일년 전 |
| 18 달 이상 | yy | 2 년 전 ~ 20 년 전 |

### IsLeapYear
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.

```javascript
import isLeapYear from 'dayjs/plugin/isLeapYear'

dayjs.extend(isLeapYear)

dayjs('2000-01-01').isLeapYear(); // true
```

## Customize

Expand Down
10 changes: 10 additions & 0 deletions docs/pt-br/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ Lista de todos os formatos disponíveis:
| `D` | 1-31 | Dia do mês |
| `DD` | 01-31 | Dia do mês, com 2 dígitos |
| `d` | 0-6 | Dia da semana (Domingo = 0) |
| `dd` | Su-Sa | The min name of the day of the week |
| `ddd` | Sun-Sat | The short name of the day of the week |
| `dddd` | Sunday-Saturday | Nome do dia da semana |
| `H` | 0-23 | Hora |
| `HH` | 00-23 | Hora, com 2 dígitos |
Expand Down Expand Up @@ -426,6 +428,8 @@ dayjs().isAfter(dayjs()); // false

#### Ano Bissexto

**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead**

* retorna um Boolean

Se um ano é bissexto.
Expand All @@ -442,3 +446,9 @@ dayjs('2000-01-01').isLeapYear(); // true
`.from` `.to` `.fromNow` `.toNow` to get relative time

plugin [`RelativeTime`](./Plugin.md#relativetime)

### IsLeapYear

`.isLeapYear` to get is a leap year or not

plugin [`IsLeapYear`](./Plugin.md#isleapyear)
3 changes: 3 additions & 0 deletions docs/pt-br/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ Template de um objeto de locale do Day.js.
const objetoLocale = {
name: 'es', // name: String
weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays: Array
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
months: 'Enero_Febrero ... '.split('_'), // months: Array
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
ordinal: n => `${n}º`, // ordinal: Function (number) => return number + saída
relativeTime: { // relative time format strings, keep %s %d as the same
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
Expand Down
10 changes: 10 additions & 0 deletions docs/pt-br/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ Returns the `string` of relative time to X.
| 11 months to 17months | y | a year ago |
| 18 months+ | yy | 2 years ago ... 20 years ago |

### IsLeapYear
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.

```javascript
import isLeapYear from 'dayjs/plugin/isLeapYear'

dayjs.extend(isLeapYear)

dayjs('2000-01-01').isLeapYear(); // true
```

## Customizar

Expand Down
13 changes: 12 additions & 1 deletion docs/zh-cn/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ dayjs().format("{YYYY} MM-DDTHH:mm:ssZ[Z]"); // "{2014} 09-08T08:02:17-05:00Z"
| `D` | 1-31 | 月份里的一天 |
| `DD` | 01-31 | 月份里的一天,两位数 |
| `d` | 0-6 | 一周中的一天,星期天是 0 |
| `dd` | Su-Sa | 最简写的一周中一天的名称 |
| `ddd` | Sun-Sat | 简写的一周中一天的名称 |
| `dddd` | Sunday-Saturday | 一周中一天的名称 |
| `H` | 0-23 | 小时 |
| `HH` | 00-23 | 小时,两位数 |
Expand Down Expand Up @@ -324,6 +326,9 @@ dayjs().isAfter(Dayjs);
dayjs().isAfter(dayjs()); // false
```
#### 是否闰年

**[已废弃] 将在 1.7.0 废弃, 使用 [`IsLeapYear 插件`](./Plugin.md#isleapyear) 代替**

- return Boolean

是否闰年。
Expand All @@ -338,4 +343,10 @@ dayjs('2000-01-01').isLeapYear(); // true

`.from` `.to` `.fromNow` `.toNow` 获得相对时间

插件 [`RelativeTime`](./Plugin.md#relativetime)
插件 [`RelativeTime`](./Plugin.md#relativetime)

### IsLeapYear

`.isLeapYear` 获得是否闰年

插件 [`IsLeapYear`](./Plugin.md#isleapyear)
3 changes: 3 additions & 0 deletions docs/zh-cn/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ Day.js 的语言配置模版
const localeObject = {
name: 'es', // 语言名 String
weekdays: 'Domingo_Lunes ...'.split('_'), // 星期 Array
weekdaysShort: 'Sun_M'.split('_'), // 可选, 短的星期 Array, 如果没提供则使用前三个字符
weekdaysMin: 'Su_Mo'.split('_'), // 可选, 最短的星期 Array, 如果没提供则使用前两个字符
months: 'Enero_Febrero ... '.split('_'), // 月份 Array
monthsShort: 'Jan_F'.split('_'), // 可选, 短的月份 Array, 如果没提供则使用前三个字符
ordinal: n => `${n}º`, // 序号生成工厂函数 Function (number) => return number + output
relativeTime: { // 相对时间, %s %d 不用翻译
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
Expand Down
11 changes: 11 additions & 0 deletions docs/zh-cn/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ dayjs().toNow()
| 11 月 到 17月 | y | 1 年前 |
| 18 月以上 | yy | 2 年前 ... 20 年前 |

## IsLeapYear
- IsLeapYear 增加了 `.isLeapYear` API 返回一个 `boolean` 来展示一个 `Dayjs`'s 的年份是不是闰年.

```javascript
import isLeapYear from 'dayjs/plugin/isLeapYear'

dayjs.extend(isLeapYear)

dayjs('2000-01-01').isLeapYear(); // true
```

## 自定义

你可以根据需要自由的编写一个Day.js插件
Expand Down
15 changes: 10 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,16 @@ class Dayjs {
}


format(formatStr, localeObject) {
format(formatStr) {
const str = formatStr || C.FORMAT_DEFAULT
const zoneStr = Utils.padZoneStr(this.$d.getTimezoneOffset())
const locale = localeObject || this.$locale()
const locale = this.$locale()
const {
weekdays, months, weekdaysShort, monthsShort
weekdays, months
} = locale
const getShort = (arr, index, full, length) => (
(arr && arr[index]) || full[index].substr(0, length)
)
return str.replace(C.REGEX_FORMAT, (match) => {
if (match.indexOf('[') > -1) return match.replace(/\[|\]/g, '')
switch (match) {
Expand All @@ -293,7 +296,7 @@ class Dayjs {
case 'MM':
return Utils.padStart(this.$M + 1, 2, '0')
case 'MMM':
return (monthsShort && monthsShort[this.$M]) || months[this.$M].slice(0, 3)
return getShort(locale.monthsShort, this.$M, months, 3)
case 'MMMM':
return months[this.$M]
case 'D':
Expand All @@ -302,8 +305,10 @@ class Dayjs {
return Utils.padStart(this.$D, 2, '0')
case 'd':
return String(this.$W)
case 'dd':
return getShort(locale.weekdaysMin, this.$W, weekdays, 2)
case 'ddd':
return (weekdaysShort && weekdaysShort[this.$W]) || weekdays[this.$M].substr(0, 3)
return getShort(locale.weekdaysShort, this.$W, weekdays, 3)
case 'dddd':
return weekdays[this.$W]
case 'H':
Expand Down
3 changes: 2 additions & 1 deletion src/locale/th.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import dayjs from 'dayjs'
const locale = {
name: 'th',
weekdays: 'อาทิตย์_จันทร์_อังคาร_พุธ_พฤหัสบดี_ศุกร์_เสาร์'.split('_'),
weekdaysShort: 'อา._จ._อ._พ._พฤ._ศ._ส.'.split('_'),
weekdaysShort: 'อาทิตย์_จันทร์_อังคาร_พุธ_พฤหัส_ศุกร์_เสาร์'.split('_'),
weekdaysMin: 'อา._จ._อ._พ._พฤ._ศ._ส.'.split('_'),
months: 'มกราคม_กุมภาพันธ์_มีนาคม_เมษายน_พฤษภาคม_มิถุนายน_กรกฏาคม_สิงหาคม_กันยายน_ตุลาคม_พฤศจิกายน_ธันวาคม'.split('_'),
monthsShort: 'ม.ค._ก.พ._มี.ค._เม.ย._พ.ค._มิ.ย._ก.ค._ส.ค._ก.ย._ต.ค._พ.ย._ธ.ค.'.split('_'),
relativeTime: {
Expand Down
3 changes: 3 additions & 0 deletions src/locale/zh-cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import dayjs from 'dayjs'
const locale = {
name: 'zh-cn',
weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'),
weekdaysShort: '周日_周一_周二_周三_周四_周五_周六'.split('_'),
weekdaysMin: '日_一_二_三_四_五_六'.split('_'),
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
ordinal: n => n,
relativeTime: {
future: '%s内',
Expand Down
7 changes: 7 additions & 0 deletions src/plugin/isLeapYear/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (o, c) => {
const proto = c.prototype
proto.isLeapYear = function () {
return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0)
}
}

9 changes: 9 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import th from '../src/locale/th'

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -33,6 +34,8 @@ it('Format Day of Month D DD 1 - 31', () => {

it('Format Day of Week d Sun - Sat', () => {
expect(dayjs().format('d')).toBe(moment().format('d'))
expect(dayjs().format('dd')).toBe(moment().format('dd'))
expect(dayjs().format('ddd')).toBe(moment().format('ddd'))
expect(dayjs().format('dddd')).toBe(moment().format('dddd'))
})

Expand Down Expand Up @@ -95,6 +98,12 @@ it('Format Time Zone ZZ', () => {
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
})

it('Format ddd dd MMM with short locale', () => {
expect(dayjs().locale(th).format('dd')).toBe(moment().locale('th').format('dd'))
expect(dayjs().locale(th).format('ddd')).toBe(moment().locale('th').format('ddd'))
expect(dayjs().locale(th).format('MMM')).toBe(moment().locale('th').format('MMM'))
})

it('Format Complex with other string - : / ', () => {
const string = 'YY-M-D / HH:mm:ss'
expect(dayjs().format(string)).toBe(moment().format(string))
Expand Down
5 changes: 0 additions & 5 deletions test/locale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ it('set locale for one instance only', () => {
.toBe('Saturday 28, April')
})

it('set locale for this line only', () => {
expect(dayjs('2018-4-28').format(format, es))
.toBe('Sábado 28, Abril')
})

it('set global locale', () => {
dayjs.locale('en')
expect(dayjs('2018-4-28').format(format))
Expand Down
Loading

0 comments on commit 5ca48f0

Please sign in to comment.