Files
zhibo/node_modules/unist-builder/index.js
T
xiaoyu ac8f91d4d3 init
2023-05-22 14:23:20 +08:00

47 lines
1.8 KiB
JavaScript

/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Literal} Literal
* @typedef {Object.<string, unknown>} Props
* @typedef {Array.<Node>|string} ChildrenOrValue
*
* @typedef {(<T extends string, P extends Record<string, unknown>, C extends Node[]>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps
* @typedef {(<T extends string, C extends Node[]>(type: T, children: C) => {type: T, children: C})} BuildParent
* @typedef {(<T extends string>(type: T, value: string) => {type: T, value: string})} BuildLiteral
* @typedef {(<T extends string>(type: T) => {type: T})} BuildVoid
*/
export var u = /**
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps}
*/ (
/**
* @param {string} type Type of node
* @param {Props|ChildrenOrValue} [props] Additional properties for node (or `children` or `value`)
* @param {ChildrenOrValue} [value] `children` or `value` of node
* @returns {Node}
*/
function (type, props, value) {
/** @type {Node} */
var node = {type: String(type)}
if (
(value === undefined || value === null) &&
(typeof props === 'string' || Array.isArray(props))
) {
value = props
} else {
Object.assign(node, props)
}
if (Array.isArray(value)) {
node.children = value
} else if (value !== undefined && value !== null) {
node.value = String(value)
}
return node
}
)