From e06a598d1af90f990ecafa0081cf84d1bc08c589 Mon Sep 17 00:00:00 2001 From: hcwhan Date: Mon, 2 Mar 2020 20:52:16 +0800 Subject: [PATCH 1/4] chore: draft add isoWeek plugin (#771) --- src/plugin/isoWeekOfYear/index.js | 32 +++++++++++ test/plugin/isoWeekOfYear.test.js | 96 +++++++++++++++++++++++++++++++ types/plugin/isoWeekOfYear.d.ts | 13 +++++ 3 files changed, 141 insertions(+) create mode 100644 src/plugin/isoWeekOfYear/index.js create mode 100644 test/plugin/isoWeekOfYear.test.js create mode 100644 types/plugin/isoWeekOfYear.d.ts diff --git a/src/plugin/isoWeekOfYear/index.js b/src/plugin/isoWeekOfYear/index.js new file mode 100644 index 000000000..480064189 --- /dev/null +++ b/src/plugin/isoWeekOfYear/index.js @@ -0,0 +1,32 @@ +export default (o, c, d) => { + const days = function (day) { + const weekDay = day.day() + return weekDay === 0 ? 7 : weekDay + } + + const getYearFirstThursday = function (year) { + const yearFirstDay = d().year(year).startOf('year') + let addDiffDays = 4 - days(yearFirstDay) + if (days(yearFirstDay) > 4) { + addDiffDays += 7 + } + return yearFirstDay.add(addDiffDays, 'day') + } + + const proto = c.prototype + + proto.isoWeekYear = function () { + const nowWeekThursday = d(this).add((4 - days(this)), 'day') + return nowWeekThursday.year() + } + + proto.isoWeek = function (isoWeek = null) { + if (isoWeek !== null) { + return this.add((isoWeek - this.isoWeek()) * 7, 'day') + } + + const nowWeekThursday = d(this).add((4 - days(this)), 'day') + const diffWeekThursday = getYearFirstThursday(this.isoWeekYear()) + return nowWeekThursday.diff(diffWeekThursday, 'week') + 1 + } +} diff --git a/test/plugin/isoWeekOfYear.test.js b/test/plugin/isoWeekOfYear.test.js new file mode 100644 index 000000000..ebca13e06 --- /dev/null +++ b/test/plugin/isoWeekOfYear.test.js @@ -0,0 +1,96 @@ +import MockDate from 'mockdate' +import dayjs from '../../src' +import isoWeekOfYear from '../../src/plugin/isoWeekOfYear' + +dayjs.extend(isoWeekOfYear) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('isoWeek of year', () => { + expect(dayjs().isoWeek(1).isoWeek()).toBe(1) + expect(dayjs().isoWeek(27).isoWeek()).toBe(27) + + + expect(dayjs('20191223').isoWeekYear()).toBe(2019) + expect(dayjs('20191223').isoWeek()).toBe(52) + expect(dayjs('20191224').isoWeekYear()).toBe(2019) + expect(dayjs('20191224').isoWeek()).toBe(52) + expect(dayjs('20191225').isoWeekYear()).toBe(2019) + expect(dayjs('20191225').isoWeek()).toBe(52) + expect(dayjs('20191226').isoWeekYear()).toBe(2019) + expect(dayjs('20191226').isoWeek()).toBe(52) + expect(dayjs('20191227').isoWeekYear()).toBe(2019) + expect(dayjs('20191227').isoWeek()).toBe(52) + expect(dayjs('20191228').isoWeekYear()).toBe(2019) + expect(dayjs('20191228').isoWeek()).toBe(52) + expect(dayjs('20191229').isoWeekYear()).toBe(2019) + expect(dayjs('20191229').isoWeek()).toBe(52) + + expect(dayjs('20191230').isoWeekYear()).toBe(2020) + expect(dayjs('20191230').isoWeek()).toBe(1) + expect(dayjs('20191231').isoWeekYear()).toBe(2020) + expect(dayjs('20191231').isoWeek()).toBe(1) + expect(dayjs('20200101').isoWeekYear()).toBe(2020) + expect(dayjs('20200101').isoWeek()).toBe(1) + expect(dayjs('20200102').isoWeekYear()).toBe(2020) + expect(dayjs('20200102').isoWeek()).toBe(1) + expect(dayjs('20200103').isoWeekYear()).toBe(2020) + expect(dayjs('20200103').isoWeek()).toBe(1) + expect(dayjs('20200104').isoWeekYear()).toBe(2020) + expect(dayjs('20200104').isoWeek()).toBe(1) + expect(dayjs('20200105').isoWeekYear()).toBe(2020) + expect(dayjs('20200105').isoWeek()).toBe(1) + + expect(dayjs('20200106').isoWeekYear()).toBe(2020) + expect(dayjs('20200106').isoWeek()).toBe(2) + expect(dayjs('20200107').isoWeekYear()).toBe(2020) + expect(dayjs('20200107').isoWeek()).toBe(2) + + + expect(dayjs('20201223').isoWeekYear()).toBe(2020) + expect(dayjs('20201223').isoWeek()).toBe(52) + expect(dayjs('20201224').isoWeekYear()).toBe(2020) + expect(dayjs('20201224').isoWeek()).toBe(52) + expect(dayjs('20201225').isoWeekYear()).toBe(2020) + expect(dayjs('20201225').isoWeek()).toBe(52) + expect(dayjs('20201226').isoWeekYear()).toBe(2020) + expect(dayjs('20201226').isoWeek()).toBe(52) + expect(dayjs('20201227').isoWeekYear()).toBe(2020) + expect(dayjs('20201227').isoWeek()).toBe(52) + + expect(dayjs('20201228').isoWeekYear()).toBe(2020) + expect(dayjs('20201228').isoWeek()).toBe(53) + expect(dayjs('20201229').isoWeekYear()).toBe(2020) + expect(dayjs('20201229').isoWeek()).toBe(53) + expect(dayjs('20201230').isoWeekYear()).toBe(2020) + expect(dayjs('20201230').isoWeek()).toBe(53) + expect(dayjs('20201231').isoWeekYear()).toBe(2020) + expect(dayjs('20201231').isoWeek()).toBe(53) + expect(dayjs('20210101').isoWeekYear()).toBe(2020) + expect(dayjs('20210101').isoWeek()).toBe(53) + expect(dayjs('20210102').isoWeekYear()).toBe(2020) + expect(dayjs('20210102').isoWeek()).toBe(53) + expect(dayjs('20210103').isoWeekYear()).toBe(2020) + expect(dayjs('20210103').isoWeek()).toBe(53) + + expect(dayjs('20210104').isoWeekYear()).toBe(2021) + expect(dayjs('20210104').isoWeek()).toBe(1) + expect(dayjs('20210105').isoWeekYear()).toBe(2021) + expect(dayjs('20210105').isoWeek()).toBe(1) + expect(dayjs('20210106').isoWeekYear()).toBe(2021) + expect(dayjs('20210106').isoWeek()).toBe(1) + expect(dayjs('20210107').isoWeekYear()).toBe(2021) + expect(dayjs('20210107').isoWeek()).toBe(1) + expect(dayjs('20210108').isoWeekYear()).toBe(2021) + expect(dayjs('20210108').isoWeek()).toBe(1) + expect(dayjs('20210109').isoWeekYear()).toBe(2021) + expect(dayjs('20210109').isoWeek()).toBe(1) + expect(dayjs('20210110').isoWeekYear()).toBe(2021) + expect(dayjs('20210110').isoWeek()).toBe(1) +}) diff --git a/types/plugin/isoWeekOfYear.d.ts b/types/plugin/isoWeekOfYear.d.ts new file mode 100644 index 000000000..1ab61f585 --- /dev/null +++ b/types/plugin/isoWeekOfYear.d.ts @@ -0,0 +1,13 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isoWeekYear(): number + isoWeek(): number + + isoWeek(value: number): Dayjs + } +} From 28a2207ef9849afbac15dd29267b2e7a09cd3c16 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 3 Mar 2020 01:11:00 +0800 Subject: [PATCH 2/4] fix: Add IsoWeek plugin (#811) --- src/plugin/isoWeek/index.js | 51 +++++++++++++++++++ src/plugin/isoWeekOfYear/index.js | 32 ------------ ...{isoWeekOfYear.test.js => isoWeek.test.js} | 33 +++++++++++- types/plugin/isoWeek.d.ts | 27 ++++++++++ types/plugin/isoWeekOfYear.d.ts | 13 ----- 5 files changed, 109 insertions(+), 47 deletions(-) create mode 100644 src/plugin/isoWeek/index.js delete mode 100644 src/plugin/isoWeekOfYear/index.js rename test/plugin/{isoWeekOfYear.test.js => isoWeek.test.js} (77%) create mode 100644 types/plugin/isoWeek.d.ts delete mode 100644 types/plugin/isoWeekOfYear.d.ts diff --git a/src/plugin/isoWeek/index.js b/src/plugin/isoWeek/index.js new file mode 100644 index 000000000..79e6fab33 --- /dev/null +++ b/src/plugin/isoWeek/index.js @@ -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) + } +} diff --git a/src/plugin/isoWeekOfYear/index.js b/src/plugin/isoWeekOfYear/index.js deleted file mode 100644 index 480064189..000000000 --- a/src/plugin/isoWeekOfYear/index.js +++ /dev/null @@ -1,32 +0,0 @@ -export default (o, c, d) => { - const days = function (day) { - const weekDay = day.day() - return weekDay === 0 ? 7 : weekDay - } - - const getYearFirstThursday = function (year) { - const yearFirstDay = d().year(year).startOf('year') - let addDiffDays = 4 - days(yearFirstDay) - if (days(yearFirstDay) > 4) { - addDiffDays += 7 - } - return yearFirstDay.add(addDiffDays, 'day') - } - - const proto = c.prototype - - proto.isoWeekYear = function () { - const nowWeekThursday = d(this).add((4 - days(this)), 'day') - return nowWeekThursday.year() - } - - proto.isoWeek = function (isoWeek = null) { - if (isoWeek !== null) { - return this.add((isoWeek - this.isoWeek()) * 7, 'day') - } - - const nowWeekThursday = d(this).add((4 - days(this)), 'day') - const diffWeekThursday = getYearFirstThursday(this.isoWeekYear()) - return nowWeekThursday.diff(diffWeekThursday, 'week') + 1 - } -} diff --git a/test/plugin/isoWeekOfYear.test.js b/test/plugin/isoWeek.test.js similarity index 77% rename from test/plugin/isoWeekOfYear.test.js rename to test/plugin/isoWeek.test.js index ebca13e06..8c2157f2f 100644 --- a/test/plugin/isoWeekOfYear.test.js +++ b/test/plugin/isoWeek.test.js @@ -1,8 +1,9 @@ import MockDate from 'mockdate' +import moment from 'moment' import dayjs from '../../src' -import isoWeekOfYear from '../../src/plugin/isoWeekOfYear' +import isoWeek from '../../src/plugin/isoWeek' -dayjs.extend(isoWeekOfYear) +dayjs.extend(isoWeek) beforeEach(() => { MockDate.set(new Date()) @@ -12,6 +13,34 @@ afterEach(() => { MockDate.reset() }) +it('get isoWeek', () => { + expect(dayjs().isoWeek()).toBe(moment().isoWeek()) +}) + +it('set isoWeek', () => { + expect(dayjs().isoWeek(1).valueOf()).toBe(moment().isoWeek(1).valueOf()) + expect(dayjs().isoWeek(52).valueOf()).toBe(moment().isoWeek(52).valueOf()) +}) + +it('get isoWeekYear', () => { + expect(dayjs().isoWeekYear()).toBe(moment().isoWeekYear()) +}) + +it('startOf/endOf isoWeek', () => { + const ISOWEEK = 'isoWeek' + expect(dayjs().startOf(ISOWEEK).valueOf()).toBe(moment().startOf(ISOWEEK).valueOf()) + expect(dayjs().endOf(ISOWEEK).valueOf()).toBe(moment().endOf(ISOWEEK).valueOf()) +}) + +it('isoWeekday', () => { + expect(dayjs().isoWeekday()).toBe(moment().isoWeekday()) + expect(dayjs('20200301').isoWeekday(1).valueOf()).toBe(moment('20200301').isoWeekday(1).valueOf()) // Sunday this.day() -> 0 + for (let i = 0; i < 7; i += 1) { + expect(dayjs().add(i, 'day').isoWeekday()).toBe(moment().add(i, 'day').isoWeekday()) + expect(dayjs().isoWeekday(i).valueOf()).toBe(moment().isoWeekday(i).valueOf()) + } +}) + it('isoWeek of year', () => { expect(dayjs().isoWeek(1).isoWeek()).toBe(1) expect(dayjs().isoWeek(27).isoWeek()).toBe(27) diff --git a/types/plugin/isoWeek.d.ts b/types/plugin/isoWeek.d.ts new file mode 100644 index 000000000..f60cf95f3 --- /dev/null +++ b/types/plugin/isoWeek.d.ts @@ -0,0 +1,27 @@ +import { PluginFunc, QUnitType, ConfigType } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +type ISOUnitType = UnitType | 'isoWeek'; + +declare module 'dayjs' { + interface Dayjs { + isoWeekYear(): number + isoWeek(): number + isoWeek(value: number): Dayjs + + isoWeekday(): number + isoWeekday(value: number): Dayjs + + startOf(unit: ISOUnitType): Dayjs + + endOf(unit: ISOUnitType): Dayjs + + isSame(date: ConfigType, unit?: ISOUnitType): boolean + + isBefore(date: ConfigType, unit?: ISOUnitType): boolean + + isAfter(date: ConfigType, unit?: ISOUnitType): boolean + } +} diff --git a/types/plugin/isoWeekOfYear.d.ts b/types/plugin/isoWeekOfYear.d.ts deleted file mode 100644 index 1ab61f585..000000000 --- a/types/plugin/isoWeekOfYear.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { PluginFunc } from 'dayjs' - -declare const plugin: PluginFunc -export = plugin - -declare module 'dayjs' { - interface Dayjs { - isoWeekYear(): number - isoWeek(): number - - isoWeek(value: number): Dayjs - } -} From b54150edeb80eb0100fa5e81292804406f08eaae Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 5 Mar 2020 12:19:10 +0800 Subject: [PATCH 3/4] docs: Update doc link --- docs/en/API-reference.md | 2 +- docs/en/I18n.md | 2 +- docs/en/Installation.md | 2 +- docs/en/Plugin.md | 2 +- docs/es-es/API-reference.md | 2 +- docs/es-es/I18n.md | 2 +- docs/es-es/Installation.md | 2 +- docs/es-es/Plugin.md | 2 +- docs/ja/API-reference.md | 2 +- docs/ja/I18n.md | 2 +- docs/ja/Installation.md | 2 +- docs/ja/Plugin.md | 2 +- docs/ko/API-reference.md | 2 +- docs/ko/I18n.md | 2 +- docs/ko/Installation.md | 2 +- docs/ko/Plugin.md | 2 +- docs/pt-br/API-reference.md | 2 +- docs/pt-br/I18n.md | 2 +- docs/pt-br/Installation.md | 2 +- docs/pt-br/Plugin.md | 2 +- docs/zh-cn/API-reference.md | 2 +- docs/zh-cn/I18n.md | 2 +- docs/zh-cn/Installation.md | 2 +- docs/zh-cn/Plugin.md | 2 +- docs/zh-cn/README.zh-CN.md | 8 ++++---- 25 files changed, 28 insertions(+), 28 deletions(-) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index c4b50f09e..6ffa38928 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -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. ------------- diff --git a/docs/en/I18n.md b/docs/en/I18n.md index 006c365e5..a4e684411 100644 --- a/docs/en/I18n.md +++ b/docs/en/I18n.md @@ -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. ------------- diff --git a/docs/en/Installation.md b/docs/en/Installation.md index 2738f7261..fe90dc25b 100644 --- a/docs/en/Installation.md +++ b/docs/en/Installation.md @@ -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. ------------- diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index e69798f96..d31961550 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -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. ------------- diff --git a/docs/es-es/API-reference.md b/docs/es-es/API-reference.md index 22f2fd3f6..1f5cdc923 100644 --- a/docs/es-es/API-reference.md +++ b/docs/es-es/API-reference.md @@ -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. ------------- diff --git a/docs/es-es/I18n.md b/docs/es-es/I18n.md index b9a69d015..fa13e4890 100644 --- a/docs/es-es/I18n.md +++ b/docs/es-es/I18n.md @@ -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. ------------- diff --git a/docs/es-es/Installation.md b/docs/es-es/Installation.md index 6abc31abf..6ace039ea 100644 --- a/docs/es-es/Installation.md +++ b/docs/es-es/Installation.md @@ -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. ------------- diff --git a/docs/es-es/Plugin.md b/docs/es-es/Plugin.md index a80df8d23..4a37669f7 100644 --- a/docs/es-es/Plugin.md +++ b/docs/es-es/Plugin.md @@ -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. ------------- diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index 0ccce6b27..0b467c59b 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -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. ------------- diff --git a/docs/ja/I18n.md b/docs/ja/I18n.md index 59c62c65d..161db83aa 100644 --- a/docs/ja/I18n.md +++ b/docs/ja/I18n.md @@ -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. ------------- diff --git a/docs/ja/Installation.md b/docs/ja/Installation.md index b3d471e82..0fba4a112 100644 --- a/docs/ja/Installation.md +++ b/docs/ja/Installation.md @@ -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. ------------- diff --git a/docs/ja/Plugin.md b/docs/ja/Plugin.md index 600bb94ae..fc9eb6257 100644 --- a/docs/ja/Plugin.md +++ b/docs/ja/Plugin.md @@ -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. ------------- diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index 763fa9e8a..903302f40 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -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. ------------- diff --git a/docs/ko/I18n.md b/docs/ko/I18n.md index e96fece9b..d053504ac 100644 --- a/docs/ko/I18n.md +++ b/docs/ko/I18n.md @@ -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. ------------- diff --git a/docs/ko/Installation.md b/docs/ko/Installation.md index d3c96145c..0da6b6171 100644 --- a/docs/ko/Installation.md +++ b/docs/ko/Installation.md @@ -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. ------------- diff --git a/docs/ko/Plugin.md b/docs/ko/Plugin.md index 836909b31..990cfe7c4 100644 --- a/docs/ko/Plugin.md +++ b/docs/ko/Plugin.md @@ -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. ------------- diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index 62c296859..3015deb7a 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -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. ------------- diff --git a/docs/pt-br/I18n.md b/docs/pt-br/I18n.md index bc76192c5..e2d8e24d1 100644 --- a/docs/pt-br/I18n.md +++ b/docs/pt-br/I18n.md @@ -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. ------------- diff --git a/docs/pt-br/Installation.md b/docs/pt-br/Installation.md index bbe81fa5e..0ed7de3d8 100644 --- a/docs/pt-br/Installation.md +++ b/docs/pt-br/Installation.md @@ -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. ------------- diff --git a/docs/pt-br/Plugin.md b/docs/pt-br/Plugin.md index 6b4561701..a5c82bd31 100644 --- a/docs/pt-br/Plugin.md +++ b/docs/pt-br/Plugin.md @@ -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. ------------- diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index 236ef73c3..7cec9915a 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -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) 查看更多信息。 ------------- diff --git a/docs/zh-cn/I18n.md b/docs/zh-cn/I18n.md index 3f9cb2f49..5c6de06be 100644 --- a/docs/zh-cn/I18n.md +++ b/docs/zh-cn/I18n.md @@ -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) 查看更多信息。 ------------- diff --git a/docs/zh-cn/Installation.md b/docs/zh-cn/Installation.md index f4a0a9355..395a88792 100644 --- a/docs/zh-cn/Installation.md +++ b/docs/zh-cn/Installation.md @@ -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) 查看更多信息。 ------------- diff --git a/docs/zh-cn/Plugin.md b/docs/zh-cn/Plugin.md index 8838b280b..cd1640b7f 100644 --- a/docs/zh-cn/Plugin.md +++ b/docs/zh-cn/Plugin.md @@ -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) 查看更多信息。 ------------- diff --git a/docs/zh-cn/README.zh-CN.md b/docs/zh-cn/README.zh-CN.md index 3fd8ed4a6..aecc858b4 100644 --- a/docs/zh-cn/README.zh-CN.md +++ b/docs/zh-cn/README.zh-CN.md @@ -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 @@ -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 @@ -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) ### 插件 @@ -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) ## 开源协议 From 48687152cf5bee6a4c1b8ceea4bda8b9bab9be10 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 6 Mar 2020 11:37:06 +0800 Subject: [PATCH 4/4] fix: Fix unsupported locale fallback to previous one (#819) --- src/index.js | 4 ++-- test/locale.test.js | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 9c4b93c12..8d0863559 100644 --- a/src/index.js +++ b/src/index.js @@ -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) => { diff --git a/test/locale.test.js b/test/locale.test.js index 48e1f5666..7faab1d08 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -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 }) @@ -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') })