-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathnodeUtils.ts
43 lines (35 loc) · 1.32 KB
/
nodeUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { create, isNode as NodeRepr_isNode } from './src/NodeRepr.gen';
import type { t as NodeRepr_t } from './src/NodeRepr.gen';
import invariant from 'invariant';
export type ElemNode = NodeRepr_t | number;
export type { NodeRepr_t };
export function resolve(n : ElemNode): NodeRepr_t {
if (typeof n === 'number')
return create("const", {value: n}, []);
invariant(isNode(n), `Whoops, expecting a Node type here! Got: ${typeof n}`);
return n;
}
export function isNode(n: unknown): n is NodeRepr_t {
// We cannot pass `unknown` type to the underlying method generated from ReScript,
// but we'd like to keep the signature of this method's API to be more semantically correct (use `unknown` instead of `any`).
// That's why we're using "@ts-expect-error" here.
// Once this resolved, the TS error pops up and we can remove it.
// @ts-expect-error
return NodeRepr_isNode(n);
}
export function createNode(
kind: string,
props,
children: Array<ElemNode>
): NodeRepr_t {
return create(kind, props, children.map(resolve));
}
// Utility function for addressing multiple output channels from a given graph node
export function unpack(node: NodeRepr_t, numChannels: number): Array<NodeRepr_t> {
return Array.from({length: numChannels}, (v, i) => {
return {
...node,
outputChannel: i,
};
});
}