Repository: iamkun/dayjs
Pull request #2087 (2022-10-21 00:26:46)

fix: add BigIntSupport plugin

codecov[bot]: # [Codecov](https://codecov.io/gh/iamkun/dayjs/pull/2087?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#2087](https://codecov.io/gh/iamkun/dayjs/pull/2087?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (7abe4d2) into [dev](https://codecov.io/gh/iamkun/dayjs/commit/7ae1a1d8bdb076c1467f1ef8f1abfff6eca32345?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (7ae1a1d) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff            @@
##               dev     #2087   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          182       183    +1     
  Lines         2080      2096   +16     
  Branches       547       550    +3     
=========================================
+ Hits          2080      2096   +16     
```


| [Impacted Files](https://codecov.io/gh/iamkun/dayjs/pull/2087?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [src/plugin/bigIntSupport/index.js](https://codecov.io/gh/iamkun/dayjs/pull/2087/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3BsdWdpbi9iaWdJbnRTdXBwb3J0L2luZGV4Lmpz) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/src/plugin/bigIntSupport/index.js b/src/plugin/bigIntSupport/index.js
new file mode 100644
index 0000000..a90bae1
--- /dev/null
+++ b/src/plugin/bigIntSupport/index.js
@@ -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)
+  }
+}
diff --git a/types/plugin/bigIntSupport.d.ts b/types/plugin/bigIntSupport.d.ts
new file mode 100644
index 0000000..d9f2f39
--- /dev/null
+++ b/types/plugin/bigIntSupport.d.ts
@@ -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


Test patch:
diff --git a/test/plugin/bigIntSupport.test.js b/test/plugin/bigIntSupport.test.js
new file mode 100644
index 0000000..b2e7086
--- /dev/null
+++ b/test/plugin/bigIntSupport.test.js
@@ -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())
+})
+