Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Apr 9, 2024
1 parent 5d943e6 commit 1eb39c8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 32 deletions.
20 changes: 10 additions & 10 deletions src/nodes/typeNodes/NestedTypeNode.ts
Original file line number Diff line number Diff line change
@@ -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 extends TypeNode> =
| TType
Expand Down Expand Up @@ -44,13 +45,12 @@ export function transformNestedTypeNode<
}
}

export function isNestedTypeNode<TType extends TypeNode>(
typeNode: TypeNode
): typeNode is NestedTypeNode<TType> {
// 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<TKind extends TypeNode['kind']>(
node: Node | null | undefined,
kind: TKind | TKind[]
): node is NestedTypeNode<Extract<TypeNode, { kind: TKind }>> {
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);
}
54 changes: 54 additions & 0 deletions test/nodes/typeNodes/NestedTypeNode.test.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringTypeNode>;
Expand Down
21 changes: 0 additions & 21 deletions test/nodes/typeNodes/TypeNode.test.ts

This file was deleted.

0 comments on commit 1eb39c8

Please sign in to comment.