diff --git a/README.md b/README.md index d218e781..3562f955 100644 --- a/README.md +++ b/README.md @@ -60,13 +60,14 @@ const Component = () => ( value={object} // just define it valueTypes={[ + { + is: (value) => typeof value === 'string' && value.startsWith('https://i.imgur.com'), + Component: (props) => {props.value}/, + }, + // or createDataType( - (value) => typeof value === 'string' && - value.startsWith('https://i.imgur.com'), - (props) => { - return {props.value}/ - } + (value) => typeof value === 'string' && value.startsWith('https://i.imgur.com'), + (props) => {props.value}/, ) ]} /> diff --git a/src/components/DataKeyPair.tsx b/src/components/DataKeyPair.tsx index de13561b..10bc31e6 100644 --- a/src/components/DataKeyPair.tsx +++ b/src/components/DataKeyPair.tsx @@ -226,18 +226,20 @@ export const DataKeyPair: React.FC = (props) => { : null } { - (isRoot && rootName !== false) - ? <>"{rootName}" - : isRoot && rootName === false - ? <> - : !isRoot && KeyRenderer.when(downstreamProps) + (isRoot + ? ( + rootName !== false + ? (quotesOnKeys ? <>"{rootName}" : <>{rootName}) + : null + ) + : KeyRenderer.when(downstreamProps) ? : nestedIndex === undefined && ( isNumberKey - ? {key} + ? {key} : quotesOnKeys ? <>"{key}" : <>{key} ) + ) } { ( diff --git a/src/components/DataTypes/Object.tsx b/src/components/DataTypes/Object.tsx index 4e4869dd..0610addc 100644 --- a/src/components/DataTypes/Object.tsx +++ b/src/components/DataTypes/Object.tsx @@ -65,7 +65,7 @@ export const PreObjectType: React.FC> = (props) => { {isTrap} @@ -123,7 +123,6 @@ export const ObjectType: React.FC> = (props) => { if (iterator && !Array.isArray(value)) { const elements = [] if (value instanceof Map) { - let _count = 0 for (const item of value) { // fixme: key might be a object, array, or any value for the `Map` const [k, value] = item @@ -132,7 +131,6 @@ export const ObjectType: React.FC> = (props) => { ) - _count++ } } else { let count = 0 diff --git a/src/hooks/useInspect.ts b/src/hooks/useInspect.ts index 639da586..f80c0b94 100644 --- a/src/hooks/useInspect.ts +++ b/src/hooks/useInspect.ts @@ -20,29 +20,31 @@ export function useInspect (path: (string | number)[], value: any, nestedIndex?: store => store.defaultInspectDepth) useEffect(() => { const inspect = getInspectCache(path, nestedIndex) - if (inspect === undefined) { - if (nestedIndex !== undefined) { - setInspectCache(path, false, nestedIndex) - } else { - // do not inspect when it is a cycle reference, otherwise there will have a loop - const inspect = isTrap - ? false - : depth < defaultInspectDepth - setInspectCache(path, inspect) - } + if (inspect !== undefined) { + return + } + if (nestedIndex !== undefined) { + setInspectCache(path, false, nestedIndex) + } else { + // do not inspect when it is a cycle reference, otherwise there will have a loop + const inspect = isTrap + ? false + : depth < defaultInspectDepth + setInspectCache(path, inspect) } }, [defaultInspectDepth, depth, getInspectCache, isTrap, nestedIndex, path, setInspectCache]) const [inspect, set] = useState(() => { const shouldInspect = getInspectCache(path, nestedIndex) - if (shouldInspect === undefined) { - if (nestedIndex !== undefined) { - return false - } - return isTrap - ? false - : depth < defaultInspectDepth + if (shouldInspect !== undefined) { + return shouldInspect + } + if (nestedIndex !== undefined) { + return false } - return shouldInspect + return isTrap + ? false + : depth < defaultInspectDepth + }) const setInspect = useCallback>>((apply) => { set((oldState) => { diff --git a/src/stores/typeRegistry.tsx b/src/stores/typeRegistry.tsx index 2fa5eea4..2579a7bc 100644 --- a/src/stores/typeRegistry.tsx +++ b/src/stores/typeRegistry.tsx @@ -52,7 +52,7 @@ export const { } = createStore>() const objectType: DataType = { - is: (value): value is object => typeof value === 'object', + is: (value) => typeof value === 'object', Component: ObjectType, PreComponent: PreObjectType, PostComponent: PostObjectType @@ -110,7 +110,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is boolean => typeof value === 'boolean', + is: (value) => typeof value === 'boolean', ...createEasyType( 'bool', ({ value }) => <>{value ? 'true' : 'false'}, @@ -133,7 +133,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is Date => value instanceof Date, + is: (value) => value instanceof Date, ...createEasyType( 'date', ({ value }) => <>{value.toLocaleTimeString('en-us', displayOptions)}, @@ -146,7 +146,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is null => value === null, + is: (value) => value === null, ...createEasyType( 'null', () => { @@ -171,7 +171,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is undefined => value === undefined, + is: (value) => value === undefined, ...createEasyType( 'undefined', () => { @@ -198,7 +198,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is string => typeof value === 'string', + is: (value) => typeof value === 'string', ...createEasyType( 'string', (props) => { @@ -238,7 +238,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is Function => typeof value === 'function', + is: (value) => typeof value === 'function', Component: FunctionType, PreComponent: PreFunctionType, PostComponent: PostFunctionType @@ -249,7 +249,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is number => typeof value === 'number' && isNaN(value), + is: (value) => typeof value === 'number' && isNaN(value), ...createEasyType( 'NaN', () => { @@ -276,7 +276,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is number => typeof value === 'number' && + is: (value) => typeof value === 'number' && !isInt(value), ...createEasyType( 'float', @@ -291,7 +291,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is number => typeof value === 'number' && isInt(value), + is: (value) => typeof value === 'number' && isInt(value), ...createEasyType( 'int', ({ value }) => <>{`${value}`}, @@ -305,7 +305,7 @@ export function predefined (): DataType[] { registerType( { - is: (value): value is bigint => typeof value === 'bigint', + is: (value) => typeof value === 'bigint', ...createEasyType( 'bigint', ({ value }) => <>{`${value}n`}, diff --git a/src/type.ts b/src/type.ts index 473e3d08..19eba13b 100644 --- a/src/type.ts +++ b/src/type.ts @@ -25,7 +25,7 @@ export type DataType = { /** * Whether the value belongs to the data type */ - is: (value: unknown) => value is ValueType + is: (value: unknown) => boolean Component: React.ComponentType> Editor?: React.ComponentType> PreComponent?: React.ComponentType> diff --git a/src/utils/index.ts b/src/utils/index.ts index 2d6773c0..12d3e7a2 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -28,7 +28,7 @@ export function createDataType ( is: (value: unknown) => boolean, Component: React.ComponentType> ): { - is: (value: unknown) => value is ValueType + is: (value: unknown) => boolean Component: React.ComponentType> } // case 2: you only render with a single component with editor @@ -37,7 +37,7 @@ export function createDataType ( Component: React.ComponentType>, Editor: React.ComponentType> ): { - is: (value: unknown) => value is ValueType + is: (value: unknown) => boolean Component: React.ComponentType> Editor: React.ComponentType> } @@ -49,7 +49,7 @@ export function createDataType ( PreComponent: React.ComponentType>, PostComponent: React.ComponentType> ): { - is: (value: unknown) => value is ValueType + is: (value: unknown) => boolean Component: React.ComponentType> PreComponent: React.ComponentType> PostComponent: React.ComponentType> @@ -62,7 +62,7 @@ export function createDataType ( PreComponent: React.ComponentType>, PostComponent: React.ComponentType> ): { - is: (value: unknown) => value is ValueType + is: (value: unknown) => boolean Component: React.ComponentType> Editor: React.ComponentType> PreComponent: React.ComponentType> diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 1018dcda..f7d68dfc 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -141,7 +141,7 @@ describe('render with multiple instances', () => { value={undefined} valueTypes={[ { - is: (() => true) as any, + is: () => true, Component: () => { return <>first viewer } @@ -153,7 +153,7 @@ describe('render with multiple instances', () => { value={undefined} valueTypes={[ { - is: (() => true) as any, + is: () => true, Component: () => { return <>second viewer } @@ -233,7 +233,7 @@ describe('render with props', () => { render() render( typeof value === 'string', + is: (value) => typeof value === 'string', Component: (props) => { expectTypeOf(props.value).toMatchTypeOf() return null