-
-
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.
- Loading branch information
Showing
3 changed files
with
66 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 @@ | ||
// eslint-disable-next-line valid-typeof | ||
const isBigInt = num => typeof num === 'bigint' | ||
export default (o, c, dayjs) => { | ||
const proto = c.prototype | ||
const parseDate = (cfg) => { | ||
const { date } = cfg | ||
if (isBigInt(date)) { | ||
return Number(date) | ||
} | ||
return date | ||
} | ||
|
||
const oldParse = proto.parse | ||
proto.parse = function (cfg) { | ||
cfg.date = parseDate.bind(this)(cfg) | ||
oldParse.bind(this)(cfg) | ||
} | ||
|
||
|
||
const oldUnix = dayjs.unix | ||
dayjs.unix = function (timestamp) { | ||
const ts = isBigInt(timestamp) ? Number(timestamp) : timestamp | ||
return oldUnix(ts) | ||
} | ||
} |
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,30 @@ | ||
import MockDate from 'mockdate' | ||
import moment from 'moment' | ||
import dayjs from '../../src' | ||
import bigIntSupport from '../../src/plugin/bigIntSupport' | ||
|
||
dayjs.extend(bigIntSupport) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
/* global BigInt */ | ||
|
||
it('Parse BigInt ts and tsms', () => { | ||
const tsms = 1666310421101 | ||
const tsmsBig = BigInt(tsms) | ||
const ts = 1666311003 | ||
const tsBig = BigInt(ts) | ||
const momentTsms = moment(tsms) | ||
const momentTs = moment.unix(ts) | ||
expect(dayjs(tsms).valueOf()).toBe(momentTsms.valueOf()) | ||
expect(dayjs(tsms).valueOf()).toBe(dayjs(tsmsBig).valueOf()) | ||
expect(dayjs.unix(ts).valueOf()).toBe(momentTs.valueOf()) | ||
expect(dayjs.unix(tsBig).valueOf()).toBe(dayjs.unix(tsBig).valueOf()) | ||
}) | ||
|
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,11 @@ | ||
import { PluginFunc } from 'dayjs' | ||
|
||
declare module 'dayjs' { | ||
interface ConfigTypeMap { | ||
bigIntSupport: BigInt | ||
} | ||
export function unix(t: BigInt): Dayjs | ||
} | ||
|
||
declare const plugin: PluginFunc | ||
export = plugin |