Skip to content

Commit 8ad941b

Browse files
muraken720wooorm
authored andcommitted
Add support for specifying a separator
Closes GH-2
1 parent 228b4da commit 8ad941b

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
*
1616
* @param {NLCSTNode|Array.<NLCSTNode>} node - Node to to
1717
* stringify.
18+
* @param {string} separator - Value to separate each item
19+
* with.
1820
* @return {string} - Stringified `node`.
1921
*/
20-
function nlcstToString(node) {
22+
function nlcstToString(node, separator) {
2123
var values;
2224
var length;
2325
var children;
2426

27+
separator = separator || '';
28+
2529
if (typeof node.value === 'string') {
2630
return node.value;
2731
}
@@ -40,10 +44,10 @@ function nlcstToString(node) {
4044
values = [];
4145

4246
while (length--) {
43-
values[length] = nlcstToString(children[length]);
47+
values[length] = nlcstToString(children[length], separator);
4448
}
4549

46-
return values.join('');
50+
return values.join(separator);
4751
}
4852

4953
/*

nlcst-to-string.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
*
1717
* @param {NLCSTNode|Array.<NLCSTNode>} node - Node to to
1818
* stringify.
19+
* @param {string} separator - Value to separate each item
20+
* with.
1921
* @return {string} - Stringified `node`.
2022
*/
21-
function nlcstToString(node) {
23+
function nlcstToString(node, separator) {
2224
var values;
2325
var length;
2426
var children;
2527

28+
separator = separator || '';
29+
2630
if (typeof node.value === 'string') {
2731
return node.value;
2832
}
@@ -41,10 +45,10 @@ function nlcstToString(node) {
4145
values = [];
4246

4347
while (length--) {
44-
values[length] = nlcstToString(children[length]);
48+
values[length] = nlcstToString(children[length], separator);
4549
}
4650

47-
return values.join('');
51+
return values.join(separator);
4852
}
4953

5054
/*

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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ AT&T
5353

5454
## API
5555

56-
### toString(node)
56+
### toString(node\[, separator\])
5757

5858
Stringify an [NLCST](https://github.com/wooorm/nlcst) [node](https://github.com/wooorm/nlcst#node)
5959
(or an array of NLCST nodes).
6060

6161
**Signatures**
6262

63-
* `toString(node)`;
64-
* `toString(nodes)`.
63+
* `toString(node[, separator])`;
64+
* `toString(nodes[, separator])`.
6565

6666
**Parameters**
6767

@@ -70,6 +70,9 @@ Stringify an [NLCST](https://github.com/wooorm/nlcst) [node](https://github.com/
7070

7171
* `nodes` (`Array.<NLCSTNode>`) — Nodes to to stringify.
7272

73+
* `separator` (`string`, optional, default: `''`) — Value to separate
74+
each item with.
75+
7376
**Returns**
7477

7578
`string` — Stringified `node` / `nodes`.

test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,37 @@ describe('toString()', function () {
104104
}
105105
]), 'AT&T');
106106
});
107+
108+
it('should concatenate `children` with a separator', function () {
109+
equal(toString({
110+
'children': [
111+
{
112+
'children': [
113+
{
114+
'value': 'AT'
115+
},
116+
{
117+
'value': '&'
118+
},
119+
{
120+
'value': 'T'
121+
}
122+
]
123+
},
124+
{
125+
'children': [
126+
{
127+
'value': 'AT'
128+
},
129+
{
130+
'value': '&'
131+
},
132+
{
133+
'value': 'T'
134+
}
135+
]
136+
}
137+
]
138+
}, ' '), 'AT & T AT & T');
139+
});
107140
});

0 commit comments

Comments
 (0)