From db66598451f2bef637bd0acdb5098a09ae7e612a Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 11 Apr 2018 18:01:40 +0800 Subject: [PATCH 1/3] feat(api): add year(), month() --- src/index.js | 8 ++++++++ test/get-set.test.js | 11 +++++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/get-set.test.js diff --git a/src/index.js b/src/index.js index 3fab13494..c2469439c 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,14 @@ class Dayjs { return false } + year() { + return this.date.getFullYear() + } + + month() { + return this.date.getMonth() + } + unix() { const zonePad = this.utc ? this.date.getTimezoneOffset() * 60 * 1000 : 0 return Math.floor((this.date.getTime() + zonePad) / 1000) diff --git a/test/get-set.test.js b/test/get-set.test.js new file mode 100644 index 000000000..5e7eb9163 --- /dev/null +++ b/test/get-set.test.js @@ -0,0 +1,11 @@ +import moment from 'moment' +import dayjs from '../src' + +test('Year', () => { + expect(dayjs().year()).toBe(moment().year()) +}) + +test('Month', () => { + expect(dayjs().month()).toBe(moment().month()) +}) + From 6443b7fb75ebccbfaf4327d759125e76e0f194c5 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 11 Apr 2018 18:15:38 +0800 Subject: [PATCH 2/3] feat(startOf): add startOf() --- src/index.js | 12 ++++++++++++ test/manipulate.test.js | 15 +++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/manipulate.test.js diff --git a/src/index.js b/src/index.js index c2469439c..6e54e4054 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,7 @@ class Dayjs { parseConfig(config) { if (!config) return new Date() + if (config instanceof Date) return config if (/^(\d){8}$/.test(config)) { this.utc = true const y = config.substr(0, 4) @@ -33,6 +34,17 @@ class Dayjs { toString() { return this.date.toUTCString() } + + startOf(arg) { + switch (arg) { + case 'year': + return new Dayjs(new Date(this.year(), 0, 1)) + case 'month': + return new Dayjs(new Date(this.year(), this.month(), 1)) + default: + return this + } + } } export default config => ( diff --git a/test/manipulate.test.js b/test/manipulate.test.js new file mode 100644 index 000000000..b1c2f4873 --- /dev/null +++ b/test/manipulate.test.js @@ -0,0 +1,15 @@ +import moment from 'moment' +import dayjs from '../src' + +test('StartOfYear', () => { + expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix()) +}) + +test('StartOfMonth', () => { + expect(dayjs().startOf('month').unix()).toBe(moment().startOf('month').unix()) +}) + +test('StartOfOther', () => { + expect(dayjs().startOf('otherString').unix()).toBe(moment().startOf('otherString').unix()) +}) + From 4665d0a3999094c0c9921d02a360eedcd473d576 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 11 Apr 2018 18:26:12 +0800 Subject: [PATCH 3/3] fix(parse): parse other string throw Invalid date --- src/index.js | 2 +- test/manipulate.test.js | 6 +++--- test/parse.test.js | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 6e54e4054..390ade6cf 100644 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,7 @@ class Dayjs { const d = config.substr(6, 2) return `${y}-${m}-${d}` } - return false + return config } year() { diff --git a/test/manipulate.test.js b/test/manipulate.test.js index b1c2f4873..c367e8c09 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -1,15 +1,15 @@ import moment from 'moment' import dayjs from '../src' -test('StartOfYear', () => { +test('StartOf Year', () => { expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix()) }) -test('StartOfMonth', () => { +test('StartOf Month', () => { expect(dayjs().startOf('month').unix()).toBe(moment().startOf('month').unix()) }) -test('StartOfOther', () => { +test('StartOf Other', () => { expect(dayjs().startOf('otherString').unix()).toBe(moment().startOf('otherString').unix()) }) diff --git a/test/parse.test.js b/test/parse.test.js index 5e0bd1ba5..1f2670204 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -8,3 +8,8 @@ test('Now', () => { test('String 20130208', () => { expect(dayjs('20130208').unix()).toBe(moment('20130208').unix()) }) + +test('String Other', () => { + global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn + expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase()) +})