diff --git a/index.js b/index.js index f6d721a..20d2020 100644 --- a/index.js +++ b/index.js @@ -1,60 +1,6 @@ /** - * @typedef {import('unist').Node} Node - * @typedef {import('unist').Parent} Parent - * @typedef {import('unist').Literal} Literal - * @typedef {Record} Props - * @typedef {Array|string} ChildrenOrValue - * - * @typedef {(, C extends Array>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps - * @typedef {(>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps - * @typedef {(>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps - * @typedef {(>(type: T, children: C) => {type: T, children: C})} BuildParent - * @typedef {((type: T, value: string) => {type: T, value: string})} BuildLiteral - * @typedef {((type: T) => {type: T})} BuildVoid + * @typedef {import('./lib/index.js').Props} Props + * @typedef {import('./lib/index.js').ChildrenOrValue} ChildrenOrValue */ -/** - * Build a node. - * - * @param type - * Node type. - * @param [props] - * Fields assigned to node. - * @param [value] - * Children of node or value of `node` (cast to string). - * @returns - * Built node. - */ -export const u = /** - * @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps} - */ ( - /** - * @param {string} type - * @param {Props|ChildrenOrValue} [props] - * @param {ChildrenOrValue} [value] - * @returns {Node} - */ - function (type, props, value) { - /** @type {Node} */ - const 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)) { - // @ts-expect-error: create a parent. - node.children = value - } else if (value !== undefined && value !== null) { - // @ts-expect-error: create a literal. - node.value = String(value) - } - - return node - } -) +export {u} from './lib/index.js' diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..f6d721a --- /dev/null +++ b/lib/index.js @@ -0,0 +1,60 @@ +/** + * @typedef {import('unist').Node} Node + * @typedef {import('unist').Parent} Parent + * @typedef {import('unist').Literal} Literal + * @typedef {Record} Props + * @typedef {Array|string} ChildrenOrValue + * + * @typedef {(, C extends Array>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps + * @typedef {(>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps + * @typedef {(>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps + * @typedef {(>(type: T, children: C) => {type: T, children: C})} BuildParent + * @typedef {((type: T, value: string) => {type: T, value: string})} BuildLiteral + * @typedef {((type: T) => {type: T})} BuildVoid + */ + +/** + * Build a node. + * + * @param type + * Node type. + * @param [props] + * Fields assigned to node. + * @param [value] + * Children of node or value of `node` (cast to string). + * @returns + * Built node. + */ +export const u = /** + * @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps} + */ ( + /** + * @param {string} type + * @param {Props|ChildrenOrValue} [props] + * @param {ChildrenOrValue} [value] + * @returns {Node} + */ + function (type, props, value) { + /** @type {Node} */ + const 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)) { + // @ts-expect-error: create a parent. + node.children = value + } else if (value !== undefined && value !== null) { + // @ts-expect-error: create a literal. + node.value = String(value) + } + + return node + } +) diff --git a/package.json b/package.json index d41d779..a11b699 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "main": "index.js", "types": "index.d.ts", "files": [ + "lib/", "index.d.ts", "index.js" ],