Skip to content

refactor: add bigIntToString() types functionPatch 2 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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