Skip to content

Commit 9be56e9

Browse files
committed
Add support for passing a list of nodes
1 parent e2f1d0b commit 9be56e9

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
/**
1414
* Stringify an NLCST node.
1515
*
16-
* @param {NLCSTNode} node - Node to to stringify.
16+
* @param {NLCSTNode|Array.<NLCSTNode>} node - Node to to
17+
* stringify.
1718
* @return {string} - Stringified `node`.
1819
*/
1920
function nlcstToString(node) {
@@ -25,7 +26,7 @@ function nlcstToString(node) {
2526
return node.value;
2627
}
2728

28-
children = node.children;
29+
children = 'length' in node ? node : node.children;
2930
length = children.length;
3031

3132
/*

nlcst-to-string.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
/**
1515
* Stringify an NLCST node.
1616
*
17-
* @param {NLCSTNode} node - Node to to stringify.
17+
* @param {NLCSTNode|Array.<NLCSTNode>} node - Node to to
18+
* stringify.
1819
* @return {string} - Stringified `node`.
1920
*/
2021
function nlcstToString(node) {
@@ -26,7 +27,7 @@ function nlcstToString(node) {
2627
return node.value;
2728
}
2829

29-
children = node.children;
30+
children = 'length' in node ? node : node.children;
3031
length = children.length;
3132

3233
/*

nlcst-to-string.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,24 @@ AT&T
5555

5656
### toString(node)
5757

58-
Stringify an [NLCST](https://github.com/wooorm/nlcst) [node](https://github.com/wooorm/nlcst#node).
58+
Stringify an [NLCST](https://github.com/wooorm/nlcst) [node](https://github.com/wooorm/nlcst#node)
59+
(or an array of NLCST nodes).
60+
61+
**Signatures**
62+
63+
* `toString(node)`;
64+
* `toString(nodes)`.
5965

6066
**Parameters**
6167

6268
* `node` ([`NLCSTNode`](https://github.com/wooorm/nlcst#node))
6369
— Node to to stringify.
6470

71+
* `nodes` (`Array.<NLCSTNode>`) — Nodes to to stringify.
72+
6573
**Returns**
6674

67-
`string` — Stringified `node`.
75+
`string` — Stringified `node` / `nodes`.
6876

6977
## License
7078

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,22 @@ describe('toString()', function () {
8686
]
8787
}), 'AT&T');
8888
});
89+
90+
it('should concatenate a list of nodes', function () {
91+
equal(toString([
92+
{
93+
'value': 'AT'
94+
},
95+
{
96+
'children': [
97+
{
98+
'value': '&'
99+
}
100+
]
101+
},
102+
{
103+
'value': 'T'
104+
}
105+
]), 'AT&T');
106+
});
89107
});

0 commit comments

Comments
 (0)