unist utility to build trees
This is a tiny but useful utility for building trees.
Use this package when you need to create nodes.
This package is ESM only.
In Node.js (version 18+) with yarn:
yarn add @flex-development/unist-util-builder
See Git - Protocols | Yarn Β for details regarding installing from Git.
In Deno with esm.sh
:
import { u } from 'https://esm.sh/@flex-development/unist-util-builder'
In browsers with esm.sh
:
<script type="module">
import { u } from 'https://esm.sh/@flex-development/unist-util-builder'
</script>
import { u } from '@flex-development/unist-util-builder'
import type { Parent } from 'unist'
const tree: Parent = u('root', [
u('subtree', {
children: [
u('node', [u('leaf', 'leaf 1'), u('leaf', 'leaf 2')]),
u('leaf', { value: 'leaf 3' }),
u('void')
],
data: { id: 1 }
})
])
console.dir(tree, { depth: null })
...yields:
{
type: 'root',
children: [
{
type: 'subtree',
children: [
{
type: 'node',
children: [
{ type: 'leaf', value: 'leaf 1' },
{ type: 'leaf', value: 'leaf 2' }
]
},
{ type: 'leaf', value: 'leaf 3' },
{ type: 'void' }
],
data: { id: 1 }
}
]
}
This package exports the identifier u
. There is no default export.
Build a node using a child node array, properties object, or value.
If builder
is omitted, a void node (a node with only a type
field) will be created.
T
(Node
) - node to build
type
(Type<T>
) - node typebuilder
(Builder<T>
, optional) - node children, properties, or value
T
new node.
Create a parent.
T
(Type
) - node typeChildren
(BuilderChildren
) - node children
type
(T
) - node typechildren
(Children
) - node children
{ children: Children; type: T }
new parent node.
Build a node using a properties object.
π Properties of a node are all fields except
type
. If atype
field is on the builder object, it will be ignored.
T
(Type
) - node typeProperties
(BuilderProps
) - node properties
type
(T
) - node typeproperties
(Properties
) - node properties
Properties & { type: T }
new node.
Create a literal.
bigint
boolean
number
string
null
π Undefined literals must be created using a properties object, rather than a value. Passing
undefined
will create a void node (a node with only atype
field).
T
(Type
) - node typeValue
(BuilderValue
) - node value
type
(T
) - node typevalue
(Value
) - node value
{ type: T; value: Value }
new literal node.
Union of types that can be used to build any node (TypeScript type).
type AnyBuilder = BuilderChildren | BuilderProps | BuilderValue
Union of node children, properties, and value (TypeScript type).
See also: MatchChildren
, MatchProperties
type Builder<N extends Node = Node> =
| Extract<MatchValue<N, Type<N>>, BuilderValue>
| MatchChildren<N, Type<N>>
| MatchProperties<N, Type<N>>
List of child nodes used to build a parent (TypeScript type).
type BuilderChildren<T extends Node = Node> = T[]
Node properties object (TypeScript type).
π Properties of a node are all fields except
type
. If atype
field is present on a builder object, it will be ignored.
type BuilderProps = { [x: string]: unknown }
Union of values that can be used to build a literal (TypeScript type).
π Undefined literals must be created using a properties object, rather than a value. Passing
undefined
will create a void node (a node with only atype
field).
type BuilderValue = bigint | boolean | number | string | null
This package is fully typed with TypeScript.
esast-util-builder
β build esast nodes
See CONTRIBUTING.md
.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.