Skip to content

Commit fac66a0

Browse files
committed
Refactor to move implementation to lib/
1 parent 667beb8 commit fac66a0

File tree

3 files changed

+64
-57
lines changed

3 files changed

+64
-57
lines changed

index.js

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,6 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Parent} Parent
4-
* @typedef {import('unist').Literal} Literal
5-
* @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
2+
* @typedef {import('./lib/index.js').Props} Props
3+
* @typedef {import('./lib/index.js').ChildrenOrValue} ChildrenOrValue
144
*/
155

16-
/**
17-
* Build a node.
18-
*
19-
* @param type
20-
* Node type.
21-
* @param [props]
22-
* Fields assigned to node.
23-
* @param [value]
24-
* Children of node or value of `node` (cast to string).
25-
* @returns
26-
* Built node.
27-
*/
28-
export const u = /**
29-
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps}
30-
*/ (
31-
/**
32-
* @param {string} type
33-
* @param {Props|ChildrenOrValue} [props]
34-
* @param {ChildrenOrValue} [value]
35-
* @returns {Node}
36-
*/
37-
function (type, props, value) {
38-
/** @type {Node} */
39-
const node = {type: String(type)}
40-
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-
}
49-
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-
}
57-
58-
return node
59-
}
60-
)
6+
export {u} from './lib/index.js'

lib/index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
* @typedef {import('unist').Literal} Literal
5+
* @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
14+
*/
15+
16+
/**
17+
* Build a node.
18+
*
19+
* @param type
20+
* Node type.
21+
* @param [props]
22+
* Fields assigned to node.
23+
* @param [value]
24+
* Children of node or value of `node` (cast to string).
25+
* @returns
26+
* Built node.
27+
*/
28+
export const u = /**
29+
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps}
30+
*/ (
31+
/**
32+
* @param {string} type
33+
* @param {Props|ChildrenOrValue} [props]
34+
* @param {ChildrenOrValue} [value]
35+
* @returns {Node}
36+
*/
37+
function (type, props, value) {
38+
/** @type {Node} */
39+
const node = {type: String(type)}
40+
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+
}
49+
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+
}
57+
58+
return node
59+
}
60+
)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"main": "index.js",
3636
"types": "index.d.ts",
3737
"files": [
38+
"lib/",
3839
"index.d.ts",
3940
"index.js"
4041
],

0 commit comments

Comments
 (0)