diff --git a/src/nodes/typeNodes/NestedTypeNode.ts b/src/nodes/typeNodes/NestedTypeNode.ts index 0ce90c36..01cc4ca6 100644 --- a/src/nodes/typeNodes/NestedTypeNode.ts +++ b/src/nodes/typeNodes/NestedTypeNode.ts @@ -1,6 +1,7 @@ +import { Node, isNode } from '../Node'; import { FixedSizeTypeNode } from './FixedSizeTypeNode'; import { SizePrefixTypeNode } from './SizePrefixTypeNode'; -import { TypeNode } from './TypeNode'; +import { TYPE_NODES, TypeNode } from './TypeNode'; export type NestedTypeNode = | TType @@ -44,13 +45,12 @@ export function transformNestedTypeNode< } } -export function isNestedTypeNode( - typeNode: TypeNode -): typeNode is NestedTypeNode { - // const kinds = Array.isArray(kind) ? kind : [kind]; - // return !!node && (kinds as NodeKind[]).includes(node.kind); - return ( - typeNode.kind === 'fixedSizeTypeNode' || - typeNode.kind === 'sizePrefixTypeNode' - ); +export function isNestedTypeNode( + node: Node | null | undefined, + kind: TKind | TKind[] +): node is NestedTypeNode> { + if (!isNode(node, TYPE_NODES)) return false; + const kinds = Array.isArray(kind) ? kind : [kind]; + const resolved = resolveNestedTypeNode(node); + return !!node && kinds.includes(resolved.kind as TKind); } diff --git a/test/nodes/typeNodes/NestedTypeNode.test.ts b/test/nodes/typeNodes/NestedTypeNode.test.ts new file mode 100644 index 00000000..0bb88546 --- /dev/null +++ b/test/nodes/typeNodes/NestedTypeNode.test.ts @@ -0,0 +1,54 @@ +import test from 'ava'; +import { + fixedSizeTypeNode, + isNestedTypeNode, + numberTypeNode, + publicKeyTypeNode, + resolveNestedTypeNode, + sizePrefixTypeNode, + stringTypeNode, + transformNestedTypeNode, +} from '../../../src'; + +test('it resolved nested type nodes', (t) => { + const node = sizePrefixTypeNode( + fixedSizeTypeNode(stringTypeNode(), 32), + numberTypeNode('u8') + ); + t.deepEqual(resolveNestedTypeNode(node), stringTypeNode()); +}); + +test('it returns the same instance when resolving nested types nodes', (t) => { + const node = numberTypeNode('u8'); + t.is(resolveNestedTypeNode(node), node); +}); + +test('it transforms nested type nodes', (t) => { + const node = sizePrefixTypeNode( + fixedSizeTypeNode(stringTypeNode(), 32), + numberTypeNode('u8') + ); + const transformedNode = transformNestedTypeNode(node, () => + publicKeyTypeNode() + ); + t.deepEqual( + transformedNode, + sizePrefixTypeNode( + fixedSizeTypeNode(publicKeyTypeNode(), 32), + numberTypeNode('u8') + ) + ); +}); + +test('it checks if a node is a nested type', (t) => { + const flatNode = numberTypeNode('u64'); + t.true(isNestedTypeNode(flatNode, 'numberTypeNode')); + t.false(isNestedTypeNode(flatNode, 'stringTypeNode')); + + const nestedNode = sizePrefixTypeNode( + fixedSizeTypeNode(numberTypeNode('u64'), 32), + numberTypeNode('u8') + ); + t.true(isNestedTypeNode(nestedNode, 'numberTypeNode')); + t.false(isNestedTypeNode(nestedNode, 'stringTypeNode')); +}); diff --git a/test/nodes/typeNodes/TypeNode.typetest.ts b/test/nodes/typeNodes/NestedTypeNode.typetest.ts similarity index 94% rename from test/nodes/typeNodes/TypeNode.typetest.ts rename to test/nodes/typeNodes/NestedTypeNode.typetest.ts index 131d6f84..6bc76b10 100644 --- a/test/nodes/typeNodes/TypeNode.typetest.ts +++ b/test/nodes/typeNodes/NestedTypeNode.typetest.ts @@ -9,7 +9,7 @@ import { } from '../../../src'; { - // [ResolveNestedTypeNode]: it constraints the nested type of a node. + // [NestedTypeNode]: it constraints the nested type of a node. const stringNestedNode = fixedSizeTypeNode(stringTypeNode(), 32); const numberNestedNode = fixedSizeTypeNode(numberTypeNode('u32'), 32); stringNestedNode satisfies NestedTypeNode; diff --git a/test/nodes/typeNodes/TypeNode.test.ts b/test/nodes/typeNodes/TypeNode.test.ts deleted file mode 100644 index 58d0e8c4..00000000 --- a/test/nodes/typeNodes/TypeNode.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import test from 'ava'; -import { - fixedSizeTypeNode, - numberTypeNode, - resolveNestedTypeNode, - sizePrefixTypeNode, - stringTypeNode, -} from '../../../src'; - -test('it resolved nested type nodes', (t) => { - const node = sizePrefixTypeNode( - fixedSizeTypeNode(stringTypeNode(), 32), - numberTypeNode('u8') - ); - t.deepEqual(resolveNestedTypeNode(node), stringTypeNode()); -}); - -test('it returns the same instance when resolving nested types nodes', (t) => { - const node = numberTypeNode('u8'); - t.is(resolveNestedTypeNode(node), node); -});