Skip to content

Commit e5857f3

Browse files
authored
refactor: add bigIntToString() types functionPatch 2 (#44)
* fix: Uncaught TypeError: Do not know how to serialize a BigInt #42 * make Copied.test cases consistent * refactor: add `bigIntToString()` types function
1 parent fa241c4 commit e5857f3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

core/src/comps/Copied.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useState } from 'react';
22
import { useStore } from '../store';
3+
import { useSectionStore, type SectionElementResult } from '../store/Section';
34
import { useShowToolsStore } from '../store/ShowTools';
4-
import { useSectionStore } from '../store/Section';
55
import { type TagType } from '../store/Types';
6-
import { type SectionElementResult } from '../store/Section';
6+
import { bigIntToString } from '../types';
77

88
export type CopiedOption<T extends object> = {
99
value?: T;
@@ -33,11 +33,11 @@ export const Copied = <T extends object, K extends TagType>(props: CopiedProps<T
3333
} else if (typeof value === 'number' && isNaN(value)) {
3434
copyText = 'NaN';
3535
} else if (typeof value === 'bigint') {
36-
copyText = value + 'n';
36+
copyText = bigIntToString(value);
3737
} else if (value instanceof Date) {
3838
copyText = value.toLocaleString();
3939
} else {
40-
copyText = JSON.stringify(value, (_, v) => (typeof v === 'bigint' ? v + 'n' : v), 2);
40+
copyText = JSON.stringify(value, (_, v) => (typeof v === 'bigint' ? bigIntToString(v) : v), 2);
4141
}
4242
onCopied && onCopied(copyText, value);
4343
setCopied(true);

core/src/types/index.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { FC, Fragment, PropsWithChildren, useEffect, useState } from 'react';
2-
import { useTypesStore } from '../store/Types';
32
import { useStore } from '../store';
3+
import { useTypesStore } from '../store/Types';
44
import { ValueQuote } from '../symbol';
5-
import { Copied } from '../comps/Copied';
5+
6+
export const bigIntToString = (bi?: BigInt | string) => {
7+
if (bi === undefined) {
8+
return '0n';
9+
} else if (typeof bi === 'string') {
10+
try {
11+
bi = BigInt(bi);
12+
} catch (e) {
13+
return '0n';
14+
}
15+
}
16+
return bi ? bi.toString() + 'n' : '0n';
17+
};
618

719
export const SetComp: FC<PropsWithChildren<{ value: unknown; keyName: string | number }>> = ({ value, keyName }) => {
820
const { Set: Comp = {}, displayDataTypes } = useTypesStore();
@@ -222,7 +234,7 @@ export const TypeBigint: FC<{ children?: BigInt } & Omit<TypeProps, 'children'>>
222234
{displayDataTypes && (type || <Comp {...reset} style={style} />)}
223235
{child || (
224236
<Comp {...reset} className="w-rjv-value">
225-
{children?.toString() + 'n'}
237+
{bigIntToString(children?.toString())}
226238
</Comp>
227239
)}
228240
</Fragment>

0 commit comments

Comments
 (0)