Skip to content

Commit f717b75

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

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lib/index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,32 @@
44
*/
55

66
/**
7-
* Stringify one nlcst node or list of nodes.
7+
* Get the text content of a node or list of nodes.
88
*
9-
* @param {Root|Content|Array<Content>} node
10-
* @param {string} [separator='']
9+
* Prefers the node’s plain-text fields, otherwise serializes its children, and
10+
* if the given value is an array, serialize the nodes in it.
11+
*
12+
* @param {Root | Content | Array<Content>} value
13+
* Node or list of nodes to serialize.
14+
* @param {string | null | undefined} [separator='']
15+
* Separator to use.
1116
* @returns {string}
17+
* Result.
1218
*/
13-
export function toString(node, separator = '') {
19+
// To do next major: remove `separator`.
20+
export function toString(value, separator) {
1421
let index = -1
1522

16-
if (!node || (!Array.isArray(node) && !node.type)) {
17-
throw new Error('Expected node, not `' + node + '`')
23+
if (!value || (!Array.isArray(value) && !value.type)) {
24+
throw new Error('Expected node, not `' + value + '`')
1825
}
1926

2027
// @ts-expect-error Looks like a literal.
21-
if (typeof node.value === 'string') return node.value
28+
if (typeof value.value === 'string') return value.value
2229

2330
/** @type {Array<Content|Root>} */
2431
// @ts-expect-error Looks like a list of nodes or parent.
25-
const children = (Array.isArray(node) ? node : node.children) || []
32+
const children = (Array.isArray(value) ? value : value.children) || []
2633

2734
// Shortcut: This is pretty common, and a small performance win.
2835
if (children.length === 1 && 'value' in children[0]) {
@@ -36,5 +43,5 @@ export function toString(node, separator = '') {
3643
values[index] = toString(children[index], separator)
3744
}
3845

39-
return values.join(separator)
46+
return values.join(separator || '')
4047
}

0 commit comments

Comments
 (0)