From 344bdc0eed6843edb05723dc7853a41833d88f08 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 7 Jan 2021 00:09:22 +0800 Subject: [PATCH] fix: fix weekYear plugin missing locale bug (#1319) * fix: fix weekYear plugin missing locale bug fix #1304 --- src/plugin/weekYear/index.js | 3 +++ test/plugin/weekYear.test.js | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/plugin/weekYear/index.js b/src/plugin/weekYear/index.js index 1fa7577b7..b7a93e0d5 100644 --- a/src/plugin/weekYear/index.js +++ b/src/plugin/weekYear/index.js @@ -7,6 +7,9 @@ export default (o, c) => { if (weekOfYear === 1 && month === 11) { return year + 1 } + if (month === 0 && weekOfYear >= 52) { + return year - 1 + } return year } } diff --git a/test/plugin/weekYear.test.js b/test/plugin/weekYear.test.js index a23709e50..30955d267 100644 --- a/test/plugin/weekYear.test.js +++ b/test/plugin/weekYear.test.js @@ -3,6 +3,7 @@ import MockDate from 'mockdate' import dayjs from '../../src' import weekYear from '../../src/plugin/weekYear' import weekOfYear from '../../src/plugin/weekOfYear' +import '../../src/locale/en-gb' dayjs.extend(weekYear) dayjs.extend(weekOfYear) @@ -24,7 +25,25 @@ it('Week Year', () => { ] daySet.forEach((d) => { const [day, result] = d - expect(dayjs(day).weekYear()).toBe(result) - expect(dayjs(day).weekYear()).toBe(moment(day).weekYear()) + const dResult = dayjs(day).weekYear() + expect(dResult).toBe(result) + expect(dResult).toBe(moment(day).weekYear()) + }) +}) + +it('yearStart: 4', () => { + const daySet = [ + ['2020-12-31', 2020], + ['2021-01-01', 2020], + ['2021-01-02', 2020], + ['2021-01-03', 2020], + ['2021-01-04', 2021], + ['2021-01-05', 2021] + ] + daySet.forEach((d) => { + const [day, result] = d + const dResult = dayjs(day).locale('en-gb').weekYear() + expect(dResult).toBe(result) + expect(dResult).toBe(moment(day).locale('en-gb').weekYear()) }) })