-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add negative years support #2640
Changes from 7 commits
fd2502e
52981d2
e0ceca2
a61414b
fe9c593
f35ef09
ebdb006
ff31f4c
ebeb0db
982c197
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export default (_, c, dayjs) => { | ||
const proto = c.prototype | ||
|
||
const parseDate = (cfg) => { | ||
const { date } = cfg | ||
if (!date) return new Date() | ||
const newDate = new Date(date) | ||
const fullYear = newDate.getFullYear() | ||
if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) { | ||
return dayjs(newDate).subtract(fullYear * 2, 'year').toDate() | ||
} | ||
return newDate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should return date not newDate, cause There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iamkun Updated |
||
} | ||
|
||
const oldParse = proto.parse | ||
proto.parse = function (cfg) { | ||
cfg.date = parseDate.bind(this)(cfg) | ||
oldParse.bind(this)(cfg) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import moment from 'moment' | ||
import MockDate from 'mockdate' | ||
import dayjs from 'dayjs' | ||
import negativeYear from '../../src/plugin/negativeYear' | ||
import utc from '../../src/plugin/utc' | ||
import { REGEX_PARSE } from '../../src/constant' | ||
|
||
|
||
dayjs.extend(negativeYear) | ||
dayjs.extend(utc) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
describe('negativeYear', () => { | ||
it('parses negative years', () => { | ||
expect(dayjs('-2020-01-01').year()).toBe(-2020) | ||
const date = '-2021/01/03' | ||
const date2 = '01/03/-2021' | ||
const date3 = '01-03--2021' | ||
const d = date.match(REGEX_PARSE) | ||
expect(dayjs(date).format('YYYY-MM-DD')).toBe(`-${moment(date).format('YYYY-MM-DD')}`) | ||
expect(dayjs(date).format()).toBe(`-${moment(date).format()}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we remove moment in this test case? as far as i know, moment does not support nagative year There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iamkun Sure |
||
expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date') | ||
expect(dayjs(date3).format()).toBe('Invalid Date') | ||
expect(d).toBe(null) | ||
}) | ||
|
||
it('does not parse non-negative years', () => { | ||
expect(dayjs('2020-01-01').year()).toBe(2020) | ||
}) | ||
|
||
it('works with other plugins', () => { | ||
expect(dayjs.utc('-2020-01-01').year()).toBe(-2020) | ||
}) | ||
|
||
it('Add and subtract with negative years', () => { | ||
expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005')) | ||
expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007')) | ||
expect(dayjs('-2006').add(1, 'y').format('YYYY')).toBe(dayjs('-2005').format('YYYY')) | ||
expect(dayjs('-2006').subtract(1, 'y').format('YYYY')).toBe(dayjs('-2007').format('YYYY')) | ||
}) | ||
|
||
it('Compare date with negative years', () => { | ||
expect(dayjs('-2006').isAfter(dayjs('-2007'))).toBeTruthy() | ||
expect(dayjs('-2006').isBefore(dayjs('-2005'))).toBeTruthy() | ||
expect(dayjs('-2006').isSame('-2006')).toBeTruthy() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {PluginFunc} from 'dayjs' | ||
|
||
declare const plugin: PluginFunc | ||
export = plugin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can return the passed in the original cfg.date directly, without dealing with logic like !date
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iamkun Got you