Skip to content

Commit a20f14d

Browse files
committed
Refactor code-style
1 parent e17b205 commit a20f14d

File tree

2 files changed

+80
-79
lines changed

2 files changed

+80
-79
lines changed

lib/index.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
/**
2-
* @typedef {import('nlcst').Content} Content
3-
* @typedef {import('nlcst').Root} Root
2+
* @typedef {import('nlcst').Nodes} Nodes
43
*/
54

5+
/** @type {Readonly<Array<Nodes>>} */
6+
const emptyNodes = []
7+
68
/**
79
* Get the text content of a node or list of nodes.
810
*
911
* Prefers the node’s plain-text fields, otherwise serializes its children, and
1012
* if the given value is an array, serialize the nodes in it.
1113
*
12-
* @param {Root | Content | Array<Content>} value
14+
* @param {Array<Nodes> | Nodes} value
1315
* Node or list of nodes to serialize.
1416
* @param {string | null | undefined} [separator='']
15-
* Separator to use.
17+
* Separator to use (default: `''`).
1618
* @returns {string}
1719
* Result.
1820
*/
@@ -24,17 +26,9 @@ export function toString(value, separator) {
2426
throw new Error('Expected node, not `' + value + '`')
2527
}
2628

27-
// @ts-expect-error Looks like a literal.
28-
if (typeof value.value === 'string') return value.value
29-
30-
/** @type {Array<Content|Root>} */
31-
// @ts-expect-error Looks like a list of nodes or parent.
32-
const children = (Array.isArray(value) ? value : value.children) || []
29+
if ('value' in value) return value.value
3330

34-
// Shortcut: This is pretty common, and a small performance win.
35-
if (children.length === 1 && 'value' in children[0]) {
36-
return children[0].value
37-
}
31+
const children = (Array.isArray(value) ? value : value.children) || emptyNodes
3832

3933
/** @type {Array<string>} */
4034
const values = []

test.js

Lines changed: 72 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,87 @@ import assert from 'node:assert/strict'
22
import test from 'node:test'
33
import {u} from 'unist-builder'
44
import {toString} from './index.js'
5-
import * as mod from './index.js'
65

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

14-
assert.throws(
15-
() => {
16-
// @ts-expect-error: runtime.
13+
await t.test('should throw when not given a node (#1)', async function () {
14+
assert.throws(function () {
15+
// @ts-expect-error: check how the runtime handles no node.
1716
toString()
18-
},
19-
/undefined/,
20-
'should throw when not given a node (#1)'
21-
)
17+
})
18+
})
2219

23-
assert.throws(
24-
() => {
25-
// @ts-expect-error: missing `type`.
20+
await t.test('should throw when not given a node (#2)', async function () {
21+
assert.throws(function () {
22+
// @ts-expect-error: check how the runtime handles no `type`.
2623
toString({value: 'foo'})
27-
},
28-
/\[object Object]/,
29-
'should throw when not given a node (#2)'
30-
)
24+
})
25+
})
3126

32-
assert.equal(toString(u('TextNode', 'AT')), 'AT', 'should support texts')
27+
await t.test('should support texts', async function () {
28+
assert.equal(toString(u('TextNode', 'AT')), 'AT')
29+
})
3330

34-
assert.equal(
35-
toString(
36-
u('WordNode', [
37-
u('TextNode', 'AT'),
38-
u('SymbolNode', '&'),
39-
u('TextNode', 'T')
40-
])
41-
),
42-
'AT&T',
43-
'should support parents'
44-
)
31+
await t.test('should support parents', async function () {
32+
assert.equal(
33+
toString(
34+
u('WordNode', [
35+
u('TextNode', 'AT'),
36+
u('SymbolNode', '&'),
37+
u('TextNode', 'T')
38+
])
39+
),
40+
'AT&T'
41+
)
42+
})
4543

46-
assert.equal(
47-
toString([u('TextNode', 'AT'), u('SymbolNode', '&'), u('TextNode', 'T')]),
48-
'AT&T',
49-
'should support nodes'
50-
)
44+
await t.test('should support nodes', async function () {
45+
assert.equal(
46+
toString([u('TextNode', 'AT'), u('SymbolNode', '&'), u('TextNode', 'T')]),
47+
'AT&T'
48+
)
49+
})
5150

52-
assert.equal(
53-
toString(
54-
// @ts-expect-error: custom.
55-
u('WordNode', [
56-
u('TextNode', 'AT'),
57-
u('SomeNode', [u('TextNode', '&')]),
58-
u('TextNode', 'T')
59-
])
60-
),
61-
'AT&T',
62-
'should support parents with mixed children'
63-
)
51+
await t.test('should support parents with mixed children', async function () {
52+
assert.equal(
53+
toString(
54+
u('WordNode', [
55+
u('TextNode', 'AT'),
56+
u('SymbolNode', '&'),
57+
u('TextNode', 'T')
58+
])
59+
),
60+
'AT&T'
61+
)
62+
})
6463

65-
assert.equal(
66-
toString(
67-
// @ts-expect-error: custom.
68-
u('WordNode', [
69-
u('TextNode', 'AT'),
70-
u('WordNode', [u('TextNode', '&')]),
71-
u('TextNode', 'T')
72-
]),
73-
','
74-
),
75-
'AT,&,T',
76-
'should support separators'
77-
)
64+
await t.test('should support separators', async function () {
65+
assert.equal(
66+
toString(
67+
u('WordNode', [
68+
u('TextNode', 'AT'),
69+
u('SymbolNode', '&'),
70+
u('TextNode', 'T')
71+
]),
72+
','
73+
),
74+
'AT,&,T'
75+
)
76+
})
7877

79-
// @ts-expect-error: custom node.
80-
assert.equal(toString(u('VoidNode')), '', 'should support voids')
78+
await t.test('should support voids', async function () {
79+
assert.equal(
80+
toString(
81+
// @ts-expect-error: check how the runtime handles custom nodes.
82+
83+
u('VoidNode')
84+
),
85+
''
86+
)
87+
})
8188
})

0 commit comments

Comments
 (0)