-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add arraySupport plugin (#1129)
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
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,25 @@ | ||
export default (o, c, dayjs) => { | ||
const proto = c.prototype | ||
const parseDate = (cfg) => { | ||
const { date, utc } = cfg | ||
if (Array.isArray(date)) { | ||
if (utc) { | ||
if (!date.length) { | ||
return new Date() | ||
} | ||
return new Date(Date.UTC.apply(null, date)) | ||
} | ||
if (date.length === 1) { | ||
return dayjs(String(date[0])).toDate() | ||
} | ||
return new (Function.prototype.bind.apply(Date, [null].concat(date)))() | ||
} | ||
return date | ||
} | ||
|
||
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 moment from 'moment' | ||
import dayjs from '../../src' | ||
import arraySupport from '../../src/plugin/arraySupport' | ||
import utc from '../../src/plugin/utc' | ||
|
||
dayjs.extend(utc) | ||
dayjs.extend(arraySupport) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
describe('parse empty array', () => { | ||
it('local', () => { | ||
expect(dayjs([]).format()) | ||
.toBe(moment([]).format()) | ||
}) | ||
it('utc', () => { | ||
expect(dayjs.utc([]).format()) | ||
.toBe(moment.utc([]).format()) | ||
}) | ||
}) | ||
|
||
const testArrs = [ | ||
[2010, 1, 14, 15, 25, 50, 125], | ||
[2010], | ||
[2010, 6], | ||
[2010, 6, 10] | ||
] | ||
|
||
describe('parse array local', () => { | ||
testArrs.forEach((testArr) => { | ||
it(testArr, () => { | ||
expect(dayjs(testArr).format()) | ||
.toBe(moment(testArr).format()) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('parse array utc', () => { | ||
testArrs.forEach((testArr) => { | ||
it(testArr, () => { | ||
expect(dayjs.utc(testArr).format()) | ||
.toBe(moment.utc(testArr).format()) | ||
}) | ||
}) | ||
}) |
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 |