Skip to content

Commit f2edb95

Browse files
committed
Refactor code-style
1 parent 9da6964 commit f2edb95

File tree

5 files changed

+101
-86
lines changed

5 files changed

+101
-86
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @typedef {import('./lib/index.js').Props} Props
32
* @typedef {import('./lib/index.js').ChildrenOrValue} ChildrenOrValue
3+
* @typedef {import('./lib/index.js').Props} Props
44
*/
55

66
export {u} from './lib/index.js'

index.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {expectType, expectAssignable} from 'tsd'
2-
import type {Text, List, ListItem, HTML} from 'mdast'
1+
import {expectAssignable, expectType} from 'tsd'
2+
import type {HTML, List, ListItem, Text} from 'mdast'
33
import {u} from './index.js'
44

55
expectType<{type: 'example'}>(u('example'))

lib/index.js

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,70 @@
1313
/**
1414
* Build a node.
1515
*
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
1752
* 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]
2156
* Children of node or value of `node` (cast to string).
22-
* @returns
57+
* @returns {Node}
2358
* Built node.
2459
*/
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)}
4663

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+
}
5572

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+
}
6380

64-
return node
65-
}
66-
)
81+
return node
82+
}

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ const tree = u('root', [
7878
])
7979
])
8080

81-
console.dir(tree, {depth: null})
81+
console.dir(tree, {depth: undefined})
8282
```
8383

84-
results in the following tree:
84+
…yields:
8585

8686
```js
8787
{

test.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
import assert from 'node:assert/strict'
22
import test from 'node:test'
33
import {u} from './index.js'
4-
import * as mod from './index.js'
54

6-
test('u', function () {
7-
assert.deepEqual(
8-
Object.keys(mod).sort(),
9-
['u'],
10-
'should expose the public api'
11-
)
5+
test('u', async function (t) {
6+
await t.test('should expose the public api', async function () {
7+
assert.deepEqual(Object.keys(await import('./index.js')).sort(), ['u'])
8+
})
129

13-
assert.deepEqual(
14-
u('root', [
15-
u('subtree', {id: 1}),
16-
u('subtree', {id: 2}, [
17-
u('node', [u('leaf', 'leaf-1'), u('leaf', 'leaf-2'), u('leaf', '')]),
18-
u('leaf', {id: 3}, 'leaf-3')
19-
])
20-
]),
21-
{
22-
type: 'root',
23-
children: [
24-
{type: 'subtree', id: 1},
25-
{
26-
type: 'subtree',
27-
id: 2,
28-
children: [
29-
{
30-
type: 'node',
31-
children: [
32-
{type: 'leaf', value: 'leaf-1'},
33-
{type: 'leaf', value: 'leaf-2'},
34-
{type: 'leaf', value: ''}
35-
]
36-
},
37-
{type: 'leaf', id: 3, value: 'leaf-3'}
38-
]
39-
}
40-
]
41-
}
42-
)
10+
await t.test('should work', async function () {
11+
assert.deepEqual(
12+
u('root', [
13+
u('subtree', {id: 1}),
14+
u('subtree', {id: 2}, [
15+
u('node', [u('leaf', 'leaf-1'), u('leaf', 'leaf-2'), u('leaf', '')]),
16+
u('leaf', {id: 3}, 'leaf-3')
17+
])
18+
]),
19+
{
20+
type: 'root',
21+
children: [
22+
{type: 'subtree', id: 1},
23+
{
24+
type: 'subtree',
25+
id: 2,
26+
children: [
27+
{
28+
type: 'node',
29+
children: [
30+
{type: 'leaf', value: 'leaf-1'},
31+
{type: 'leaf', value: 'leaf-2'},
32+
{type: 'leaf', value: ''}
33+
]
34+
},
35+
{type: 'leaf', id: 3, value: 'leaf-3'}
36+
]
37+
}
38+
]
39+
}
40+
)
41+
})
4342
})

0 commit comments

Comments
 (0)