Skip to content

Commit

Permalink
refactor: add bigIntToString() types functionPatch 2 (#44)
Browse files Browse the repository at this point in the history
* fix: Uncaught TypeError: Do not know how to serialize a BigInt #42

* make Copied.test cases consistent

* refactor: add `bigIntToString()` types function
  • Loading branch information
gregfenton authored Apr 5, 2024
1 parent fa241c4 commit e5857f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
8 changes: 4 additions & 4 deletions core/src/comps/Copied.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useState } from 'react';
import { useStore } from '../store';
import { useSectionStore, type SectionElementResult } from '../store/Section';
import { useShowToolsStore } from '../store/ShowTools';
import { useSectionStore } from '../store/Section';
import { type TagType } from '../store/Types';
import { type SectionElementResult } from '../store/Section';
import { bigIntToString } from '../types';

export type CopiedOption<T extends object> = {
value?: T;
Expand Down Expand Up @@ -33,11 +33,11 @@ export const Copied = <T extends object, K extends TagType>(props: CopiedProps<T
} else if (typeof value === 'number' && isNaN(value)) {
copyText = 'NaN';
} else if (typeof value === 'bigint') {
copyText = value + 'n';
copyText = bigIntToString(value);
} else if (value instanceof Date) {
copyText = value.toLocaleString();
} else {
copyText = JSON.stringify(value, (_, v) => (typeof v === 'bigint' ? v + 'n' : v), 2);
copyText = JSON.stringify(value, (_, v) => (typeof v === 'bigint' ? bigIntToString(v) : v), 2);
}
onCopied && onCopied(copyText, value);
setCopied(true);
Expand Down
18 changes: 15 additions & 3 deletions core/src/types/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { FC, Fragment, PropsWithChildren, useEffect, useState } from 'react';
import { useTypesStore } from '../store/Types';
import { useStore } from '../store';
import { useTypesStore } from '../store/Types';
import { ValueQuote } from '../symbol';
import { Copied } from '../comps/Copied';

export const bigIntToString = (bi?: BigInt | string) => {
if (bi === undefined) {
return '0n';
} else if (typeof bi === 'string') {
try {
bi = BigInt(bi);
} catch (e) {
return '0n';
}
}
return bi ? bi.toString() + 'n' : '0n';
};

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

0 comments on commit e5857f3

Please sign in to comment.