-
-
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fd2502e
feat: Add negative year support
klm-turing 52981d2
Update parse.test.js
klm-turing e0ceca2
Merge branch 'iamkun:dev' into feat-negative-year-support
klm-turing a61414b
Update manipulate test
klm-turing fe9c593
Merge branch 'feat-negative-year-support' of https://github.com/klm-t…
klm-turing f35ef09
Add negative years comparison test
klm-turing ebdb006
Create negativeYear Plugin
klm-turing ff31f4c
Removed cfg.date check and moment from test
klm-turing ebeb0db
make negativeYear return the original untouched date by default
klm-turing 982c197
Make sure that negativeYear plugin worked in harmony with other plugins
klm-turing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export default (_, c, dayjs) => { | ||
const proto = c.prototype | ||
|
||
const parseDate = (cfg) => { | ||
const { date } = cfg | ||
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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('-2021-01-03') | ||
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {PluginFunc} from 'dayjs' | ||
|
||
declare const plugin: PluginFunc | ||
export = plugin |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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