Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 2, 2018
1 parent 17dbde1 commit 454e368
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 72 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
nlcst-to-string.js
nlcst-to-string.min.js
28 changes: 14 additions & 14 deletions index.js
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)
}
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,38 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.1",
"nyc": "^11.0.0",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.0.0",
"unist-builder": "^1.0.1",
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . --quiet --frail --output",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s nlcstToString > nlcst-to-string.js",
"build-mangle": "esmangle nlcst-to-string.js > nlcst-to-string.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"guard-for-in": "off",
Expand Down
22 changes: 12 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ npm install nlcst-to-string
## Usage

```javascript
var toString = require('nlcst-to-string');

console.log(toString({
type: 'WordNode',
children: [
{type: 'TextNode', value: 'AT'},
{type: 'PunctuationNode', value: '&'},
{type: 'TextNode', value: 'T'}
]
})); //=> 'AT&T'
var toString = require('nlcst-to-string')

console.log(
toString({
type: 'WordNode',
children: [
{type: 'TextNode', value: 'AT'},
{type: 'PunctuationNode', value: '&'},
{type: 'TextNode', value: 'T'}
]
})
) // => 'AT&T'
```

## API
Expand Down
69 changes: 27 additions & 42 deletions test.js
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()
})

0 comments on commit 454e368

Please sign in to comment.