Skip to content

Commit

Permalink
fix: serialize bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest committed Jan 19, 2025
1 parent f4220c4 commit 599c8a4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function serialize(obj: unknown): string {
return JSON.stringify(obj, (_, value) => {
if (Binary.is(value)) return `b${value.byteLength}`
if (typeof value === 'string') return 's' + value
if (typeof value === 'bigint') return 'n' + value.toString()
if (typeof value === 'object') {
if (value instanceof Date) return 'd' + new Date(value).toJSON()
if (value === null) return null
Expand Down Expand Up @@ -35,7 +36,9 @@ export function deserialize(str: string): any {
? v.slice(1)
: v[0] === 'b'
? +v.slice(1)
: new Date(v.slice(1))
: v[0] === 'n'
? BigInt(v.slice(1))
: new Date(v.slice(1))
: v,
)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-dataview",
"description": "View Database in Koishi Console",
"version": "2.7.6",
"version": "2.7.7",
"main": "lib/index.cjs",
"types": "lib/index.d.ts",
"exports": {
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function serialize(obj: unknown): string {
return JSON.stringify(obj, (_, value) => {
if (Binary.is(value)) return `b${value.byteLength}`
if (typeof value === 'string') return 's' + value
if (typeof value === 'bigint') return 'n' + value.toString()
if (typeof value === 'object') {
if (value instanceof Date) return 'd' + new Date(value).toJSON()
if (value === null) return null
Expand Down Expand Up @@ -33,7 +34,9 @@ export function deserialize(str: string): unknown {
? v.slice(1)
: v[0] === 'b'
? undefined
: new Date(v.slice(1))
: v[0] === 'n'
? BigInt(v.slice(1))
: new Date(v.slice(1))
: v,
)
}

0 comments on commit 599c8a4

Please sign in to comment.