-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
nlcst-to-string.js | ||
nlcst-to-string.min.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = nlcstToString; | ||
module.exports = nlcstToString | ||
|
||
/* Stringify a NLCST node or list of nodes. */ | ||
function nlcstToString(node, separator) { | ||
var sep = separator || ''; | ||
var values; | ||
var length; | ||
var children; | ||
var sep = separator || '' | ||
var values | ||
var length | ||
var children | ||
|
||
if (!node || (!('length' in node) && !node.type)) { | ||
throw new Error('Expected node, not `' + node + '`'); | ||
throw new Error('Expected node, not `' + node + '`') | ||
} | ||
|
||
if (typeof node.value === 'string') { | ||
return node.value; | ||
return node.value | ||
} | ||
|
||
children = 'length' in node ? node : node.children; | ||
length = children.length; | ||
children = 'length' in node ? node : node.children | ||
length = children.length | ||
|
||
/* Shortcut: This is pretty common, and a small performance win. */ | ||
if (length === 1 && 'value' in children[0]) { | ||
return children[0].value; | ||
return children[0].value | ||
} | ||
|
||
values = []; | ||
values = [] | ||
|
||
while (length--) { | ||
values[length] = nlcstToString(children[length], sep); | ||
values[length] = nlcstToString(children[length], sep) | ||
} | ||
|
||
return values.join(sep); | ||
return values.join(sep) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,56 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var test = require('tape'); | ||
var u = require('unist-builder'); | ||
var toString = require('.'); | ||
var test = require('tape') | ||
var u = require('unist-builder') | ||
var toString = require('.') | ||
|
||
test('toString()', function (t) { | ||
test('toString()', function(t) { | ||
t.throws( | ||
function () { | ||
toString(); | ||
function() { | ||
toString() | ||
}, | ||
/undefined/, | ||
'should throw when not given a node (#1)' | ||
); | ||
) | ||
|
||
t.throws( | ||
function () { | ||
toString({value: 'foo'}); | ||
function() { | ||
toString({value: 'foo'}) | ||
}, | ||
/\[object Object]/, | ||
'should throw when not given a node (#2)' | ||
); | ||
) | ||
|
||
t.equal( | ||
toString(u('foo', 'AT')), | ||
'AT', | ||
'should support texts' | ||
); | ||
t.equal(toString(u('foo', 'AT')), 'AT', 'should support texts') | ||
|
||
t.equal( | ||
toString(u('foo', [ | ||
u('bar', 'AT'), | ||
u('bar', '&'), | ||
u('bar', 'T') | ||
])), | ||
toString(u('foo', [u('bar', 'AT'), u('bar', '&'), u('bar', 'T')])), | ||
'AT&T', | ||
'should support parents' | ||
); | ||
) | ||
|
||
t.equal( | ||
toString([ | ||
u('bar', 'AT'), | ||
u('bar', '&'), | ||
u('bar', 'T') | ||
]), | ||
toString([u('bar', 'AT'), u('bar', '&'), u('bar', 'T')]), | ||
'AT&T', | ||
'should support nodes' | ||
); | ||
) | ||
|
||
t.equal( | ||
toString(u('foo', [ | ||
u('bar', 'AT'), | ||
u('foo', [u('bar', '&')]), | ||
u('bar', 'T') | ||
])), | ||
toString( | ||
u('foo', [u('bar', 'AT'), u('foo', [u('bar', '&')]), u('bar', 'T')]) | ||
), | ||
'AT&T', | ||
'should support parents with mixed children' | ||
); | ||
) | ||
|
||
t.equal( | ||
toString(u('foo', [ | ||
u('bar', 'AT'), | ||
u('foo', [u('bar', '&')]), | ||
u('bar', 'T') | ||
]), ','), | ||
toString( | ||
u('foo', [u('bar', 'AT'), u('foo', [u('bar', '&')]), u('bar', 'T')]), | ||
',' | ||
), | ||
'AT,&,T', | ||
'should support separators' | ||
); | ||
) | ||
|
||
t.end(); | ||
}); | ||
t.end() | ||
}) |