Skip to content

Commit

Permalink
Merge pull request iamkun#823 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Mar 8, 2020
2 parents b9d64e1 + 4868715 commit ebbd348
Show file tree
Hide file tree
Showing 30 changed files with 253 additions and 35 deletions.
2 changes: 1 addition & 1 deletion docs/en/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/parse/parse) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/i18n/i18n) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/en/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/installation/installation) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/plugin/plugin) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/es-es/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/parse/parse) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/es-es/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/i18n/i18n) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/es-es/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/installation/installation) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/es-es/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/plugin/plugin) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/ja/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/parse/parse) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/ja/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/i18n/i18n) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/ja/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/installation/installation) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/ja/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/plugin/plugin) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/ko/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/parse/parse) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/ko/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/i18n/i18n) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/ko/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/installation/installation) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/ko/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/plugin/plugin) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/pt-br/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/parse/parse) for more information.

-------------

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 @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/i18n/i18n) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/pt-br/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/installation/installation) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/pt-br/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The document here **no longer** updates.

Please visit our website [https://day.js.org](https://day.js.org) for more information.
Please visit our website [https://day.js.org](https://day.js.org/docs/en/plugin/plugin) for more information.

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

此处的文档将**不再**更新。

请访问网站 [https://day.js.org](https://day.js.org) 查看更多信息。
请访问网站 [https://day.js.org](https://day.js.org/docs/zh-CN/parse/parse) 查看更多信息。

-------------

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 @@ -2,7 +2,7 @@

此处的文档将**不再**更新。

请访问网站 [https://day.js.org](https://day.js.org) 查看更多信息。
请访问网站 [https://day.js.org](https://day.js.org/docs/zh-CN/i18n/i18n) 查看更多信息。

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

此处的文档将**不再**更新。

请访问网站 [https://day.js.org](https://day.js.org) 查看更多信息。
请访问网站 [https://day.js.org](https://day.js.org/docs/zh-CN/installation/installation) 查看更多信息。

-------------

Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

此处的文档将**不再**更新。

请访问网站 [https://day.js.org](https://day.js.org) 查看更多信息。
请访问网站 [https://day.js.org](https://day.js.org/docs/zh-CN/plugin/plugin) 查看更多信息。

-------------

Expand Down
8 changes: 4 additions & 4 deletions docs/zh-cn/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dayjs()
npm install dayjs --save
```

📚[安装指南](https://day.js.org/docs/en/installation/installation)
📚[安装指南](https://day.js.org/docs/zh-CN/installation/installation)

### API

Expand All @@ -72,7 +72,7 @@ dayjs().add(1, 'year') // 处理
dayjs().isBefore(dayjs()) // 查询
```

📚[API 参考](https://day.js.org/docs/en/parse/parse)
📚[API 参考](https://day.js.org/docs/zh-CN/parse/parse)

### 国际化 I18n

Expand All @@ -90,7 +90,7 @@ dayjs('2018-05-05')
.format() // 在这个实例上使用简体中文
```

📚[国际化 I18n](https://day.js.org/docs/en/i18n/i18n)
📚[国际化 I18n](https://day.js.org/docs/zh-CN/i18n/i18n)

### 插件

Expand All @@ -104,7 +104,7 @@ dayjs.extend(advancedFormat) // 使用插件
dayjs().format('Q Do k kk X x') // 使用扩展后的API
```

📚[插件列表](https://day.js.org/docs/en/plugin/plugin)
📚[插件列表](https://day.js.org/docs/zh-CN/plugin/plugin)

## 开源协议

Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const parseLocale = (preset, object, isLocal) => {
Ls[name] = preset
l = name
}
if (!isLocal) L = l
return l
if (!isLocal && l) L = l
return l || (!isLocal && L)
}

const dayjs = (date, c, pl) => {
Expand Down
51 changes: 51 additions & 0 deletions src/plugin/isoWeek/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { D, W, Y } from '../../constant'

const isoWeekPrettyUnit = 'isoweek'

export default (o, c, d) => {
const getYearFirstThursday = (year) => {
const yearFirstDay = d().year(year).startOf(Y)
let addDiffDays = 4 - yearFirstDay.isoWeekday()
if (yearFirstDay.isoWeekday() > 4) {
addDiffDays += 7
}
return yearFirstDay.add(addDiffDays, D)
}

const getCurrentWeekThursday = ins => ins.add((4 - ins.isoWeekday()), D)

const proto = c.prototype

proto.isoWeekYear = function () {
const nowWeekThursday = getCurrentWeekThursday(this)
return nowWeekThursday.year()
}

proto.isoWeek = function (week) {
if (!this.$utils().u(week)) {
return this.add((week - this.isoWeek()) * 7, D)
}
const nowWeekThursday = getCurrentWeekThursday(this)
const diffWeekThursday = getYearFirstThursday(this.isoWeekYear())
return nowWeekThursday.diff(diffWeekThursday, W) + 1
}

proto.isoWeekday = function (week) {
if (!this.$utils().u(week)) {
return this.day(this.day() % 7 ? week : week - 7)
}
return this.day() || 7
}

const oldStartOf = proto.startOf
proto.startOf = function (units, startOf) {
const utils = this.$utils()
const isStartOf = !utils.u(startOf) ? startOf : true
const unit = utils.p(units)
if (unit === isoWeekPrettyUnit) {
return isStartOf ? this.date(this.date() - (this.isoWeekday() - 1)).startOf('day') :
this.date((this.date() - 1 - (this.isoWeekday() - 1)) + 7).endOf('day')
}
return oldStartOf.bind(this)(units, startOf)
}
}
25 changes: 20 additions & 5 deletions test/locale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ afterEach(() => {
})

const format = 'dddd D, MMMM'
const NOT_SUPPORTED_LOCALE_STRING = 'not_supported_locale_string'

it('Uses spanish locale through constructor', () => { // not recommend
expect(dayjs('2018-4-28', { locale: es })
Expand Down Expand Up @@ -123,10 +124,24 @@ describe('Instance locale inheritance', () => {
})


it('Not supported locale string fallback to previous one', () => {
it('Not supported locale string fallback to previous one (instance)', () => {
const D = dayjs()
const DFormat = D.format()
const D2 = D.locale('not_supported_locale_string')
const D2Format = D2.format()
expect(DFormat).toBe(D2Format)
expect(D.locale()).toBe('en')
const D2 = D.locale(NOT_SUPPORTED_LOCALE_STRING)
expect(D2.locale()).toBe('en')
expect(D2.format()).toBe(D.format())
const D3 = D2.locale('es')
expect(D3.locale()).toBe('es')
const D4 = D3.locale(NOT_SUPPORTED_LOCALE_STRING)
expect(D4.locale()).toBe('es')
})

it('Not supported locale string fallback to previous one (global)', () => {
expect(dayjs().locale()).toBe('en')
dayjs.locale(NOT_SUPPORTED_LOCALE_STRING)
expect(dayjs().locale()).toBe('en')
dayjs.locale('es')
expect(dayjs().locale()).toBe('es')
dayjs.locale(NOT_SUPPORTED_LOCALE_STRING)
expect(dayjs().locale()).toBe('es')
})
Loading

0 comments on commit ebbd348

Please sign in to comment.