Skip to content

Commit fbbd4ba

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent fac66a0 commit fbbd4ba

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

lib/index.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,60 @@
33
* @typedef {import('unist').Parent} Parent
44
* @typedef {import('unist').Literal} Literal
55
* @typedef {Record<string, unknown>} Props
6-
* @typedef {Array<Node>|string} ChildrenOrValue
7-
*
8-
* @typedef {(<T extends string, P extends Record<string, unknown>, C extends Array<Node>>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps
9-
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps
10-
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps
11-
* @typedef {(<T extends string, C extends Array<Node>>(type: T, children: C) => {type: T, children: C})} BuildParent
12-
* @typedef {(<T extends string>(type: T, value: string) => {type: T, value: string})} BuildLiteral
13-
* @typedef {(<T extends string>(type: T) => {type: T})} BuildVoid
6+
* @typedef {Array<Node> | string} ChildrenOrValue
147
*/
158

169
/**
1710
* Build a node.
1811
*
1912
* @param type
2013
* Node type.
21-
* @param [props]
14+
* @param props
2215
* Fields assigned to node.
23-
* @param [value]
16+
* @param value
2417
* Children of node or value of `node` (cast to string).
2518
* @returns
2619
* Built node.
2720
*/
28-
export const u = /**
29-
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps}
30-
*/ (
21+
export const u =
3122
/**
32-
* @param {string} type
33-
* @param {Props|ChildrenOrValue} [props]
34-
* @param {ChildrenOrValue} [value]
35-
* @returns {Node}
23+
* @type {(
24+
* (<T extends string>(type: T) => {type: T}) &
25+
* (<T extends string, P extends Props>(type: T, props: P) => {type: T} & P) &
26+
* (<T extends string>(type: T, value: string) => {type: T, value: string}) &
27+
* (<T extends string, P extends Props>(type: T, props: P, value: string) => {type: T, value: string} & P) &
28+
* (<T extends string, C extends Array<Node>>(type: T, children: C) => {type: T, children: C}) &
29+
* (<T extends string, P extends Props, C extends Array<Node>>(type: T, props: P, children: C) => {type: T, children: C} & P)
30+
* )}
3631
*/
37-
function (type, props, value) {
38-
/** @type {Node} */
39-
const node = {type: String(type)}
32+
(
33+
/**
34+
* @param {string} type
35+
* @param {Props | ChildrenOrValue | null | undefined} [props]
36+
* @param {ChildrenOrValue | null | undefined} [value]
37+
* @returns {Node}
38+
*/
39+
function (type, props, value) {
40+
/** @type {Node} */
41+
const node = {type: String(type)}
4042

41-
if (
42-
(value === undefined || value === null) &&
43-
(typeof props === 'string' || Array.isArray(props))
44-
) {
45-
value = props
46-
} else {
47-
Object.assign(node, props)
48-
}
43+
if (
44+
(value === undefined || value === null) &&
45+
(typeof props === 'string' || Array.isArray(props))
46+
) {
47+
value = props
48+
} else {
49+
Object.assign(node, props)
50+
}
4951

50-
if (Array.isArray(value)) {
51-
// @ts-expect-error: create a parent.
52-
node.children = value
53-
} else if (value !== undefined && value !== null) {
54-
// @ts-expect-error: create a literal.
55-
node.value = String(value)
56-
}
52+
if (Array.isArray(value)) {
53+
// @ts-expect-error: create a parent.
54+
node.children = value
55+
} else if (value !== undefined && value !== null) {
56+
// @ts-expect-error: create a literal.
57+
node.value = String(value)
58+
}
5759

58-
return node
59-
}
60-
)
60+
return node
61+
}
62+
)

0 commit comments

Comments
 (0)