|
13 | 13 | /** |
14 | 14 | * Build a node. |
15 | 15 | * |
16 | | - * @param type |
| 16 | + * @template {string} T |
| 17 | + * @template {Props} P |
| 18 | + * @template {Array<Node>} C |
| 19 | + * |
| 20 | + * @overload |
| 21 | + * @param {T} type |
| 22 | + * @returns {{type: T}} |
| 23 | + * |
| 24 | + * @overload |
| 25 | + * @param {T} type |
| 26 | + * @param {P} props |
| 27 | + * @returns {{type: T} & P} |
| 28 | + * |
| 29 | + * @overload |
| 30 | + * @param {T} type |
| 31 | + * @param {string} value |
| 32 | + * @returns {{type: T, value: string}} |
| 33 | + * |
| 34 | + * @overload |
| 35 | + * @param {T} type |
| 36 | + * @param {P} props |
| 37 | + * @param {string} value |
| 38 | + * @returns {{type: T, value: string} & P} |
| 39 | + * |
| 40 | + * @overload |
| 41 | + * @param {T} type |
| 42 | + * @param {C} children |
| 43 | + * @returns {{type: T, children: C}} |
| 44 | + * |
| 45 | + * @overload |
| 46 | + * @param {T} type |
| 47 | + * @param {P} props |
| 48 | + * @param {C} children |
| 49 | + * @returns {{type: T, children: C} & P} |
| 50 | + * |
| 51 | + * @param {string} type |
17 | 52 | * Node type. |
18 | | - * @param props |
19 | | - * Fields assigned to node. |
20 | | - * @param value |
| 53 | + * @param {ChildrenOrValue | Props | null | undefined} [props] |
| 54 | + * Fields assigned to node (default: `undefined`). |
| 55 | + * @param {ChildrenOrValue | null | undefined} [value] |
21 | 56 | * Children of node or value of `node` (cast to string). |
22 | | - * @returns |
| 57 | + * @returns {Node} |
23 | 58 | * Built node. |
24 | 59 | */ |
25 | | -export const u = |
26 | | - /** |
27 | | - * @type {( |
28 | | - * (<T extends string>(type: T) => {type: T}) & |
29 | | - * (<T extends string, P extends Props>(type: T, props: P) => {type: T} & P) & |
30 | | - * (<T extends string>(type: T, value: string) => {type: T, value: string}) & |
31 | | - * (<T extends string, P extends Props>(type: T, props: P, value: string) => {type: T, value: string} & P) & |
32 | | - * (<T extends string, C extends Array<Node>>(type: T, children: C) => {type: T, children: C}) & |
33 | | - * (<T extends string, P extends Props, C extends Array<Node>>(type: T, props: P, children: C) => {type: T, children: C} & P) |
34 | | - * )} |
35 | | - */ |
36 | | - ( |
37 | | - /** |
38 | | - * @param {string} type |
39 | | - * @param {Props | ChildrenOrValue | null | undefined} [props] |
40 | | - * @param {ChildrenOrValue | null | undefined} [value] |
41 | | - * @returns {Node} |
42 | | - */ |
43 | | - function (type, props, value) { |
44 | | - /** @type {Node} */ |
45 | | - const node = {type: String(type)} |
| 60 | +export function u(type, props, value) { |
| 61 | + /** @type {Node} */ |
| 62 | + const node = {type: String(type)} |
46 | 63 |
|
47 | | - if ( |
48 | | - (value === undefined || value === null) && |
49 | | - (typeof props === 'string' || Array.isArray(props)) |
50 | | - ) { |
51 | | - value = props |
52 | | - } else { |
53 | | - Object.assign(node, props) |
54 | | - } |
| 64 | + if ( |
| 65 | + (value === undefined || value === null) && |
| 66 | + (typeof props === 'string' || Array.isArray(props)) |
| 67 | + ) { |
| 68 | + value = props |
| 69 | + } else { |
| 70 | + Object.assign(node, props) |
| 71 | + } |
55 | 72 |
|
56 | | - if (Array.isArray(value)) { |
57 | | - // @ts-expect-error: create a parent. |
58 | | - node.children = value |
59 | | - } else if (value !== undefined && value !== null) { |
60 | | - // @ts-expect-error: create a literal. |
61 | | - node.value = String(value) |
62 | | - } |
| 73 | + if (Array.isArray(value)) { |
| 74 | + // @ts-expect-error: create a parent. |
| 75 | + node.children = value |
| 76 | + } else if (value !== undefined && value !== null) { |
| 77 | + // @ts-expect-error: create a literal. |
| 78 | + node.value = String(value) |
| 79 | + } |
63 | 80 |
|
64 | | - return node |
65 | | - } |
66 | | - ) |
| 81 | + return node |
| 82 | +} |
0 commit comments