Skip to content

Commit 584bb38

Browse files
committed
Refactor code-style
1 parent 558c2cf commit 584bb38

File tree

5 files changed

+126
-144
lines changed

5 files changed

+126
-144
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
unist-util-find-all-before.js
3+
unist-util-find-all-before.min.js

index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
'use strict';
1+
'use strict'
22

3-
var is = require('unist-util-is');
3+
var is = require('unist-util-is')
44

5-
module.exports = findAllBefore;
5+
module.exports = findAllBefore
66

77
/* Find nodes before `index` in `parent` which pass `test`. */
88
function findAllBefore(parent, index, test) {
9-
var results = [];
10-
var children;
11-
var child;
9+
var results = []
10+
var children
11+
var child
1212

1313
if (!parent || !parent.type || !parent.children) {
14-
throw new Error('Expected parent node');
14+
throw new Error('Expected parent node')
1515
}
1616

17-
children = parent.children;
17+
children = parent.children
1818

1919
if (index && index.type) {
20-
index = children.indexOf(index);
20+
index = children.indexOf(index)
2121
}
2222

2323
if (isNaN(index) || index < 0 || index === Infinity) {
24-
throw new Error('Expected positive finite index or child node');
24+
throw new Error('Expected positive finite index or child node')
2525
}
2626

2727
/* Performance. */
2828
if (index > children.length) {
29-
index = children.length;
29+
index = children.length
3030
}
3131

3232
while (index--) {
33-
child = children[index];
33+
child = children[index]
3434

3535
if (is(test, child, index, parent)) {
36-
results.push(child);
36+
results.push(child)
3737
}
3838
}
3939

40-
return results;
40+
return results
4141
}

package.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,39 @@
2323
"devDependencies": {
2424
"browserify": "^16.0.0",
2525
"esmangle": "^1.0.0",
26+
"nyc": "^11.0.0",
27+
"prettier": "^1.12.1",
2628
"remark": "^9.0.0",
2729
"remark-cli": "^5.0.0",
2830
"remark-preset-wooorm": "^4.0.0",
29-
"nyc": "^11.0.0",
3031
"tape": "^4.6.2",
3132
"xo": "^0.20.0"
3233
},
3334
"scripts": {
34-
"build-md": "remark . -foq",
35+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3536
"build-bundle": "browserify index.js --no-builtins -s unistUtilFindAllBefore > unist-util-find-all-before.js",
3637
"build-mangle": "esmangle unist-util-find-all-before.js > unist-util-find-all-before.min.js",
37-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
38-
"lint": "xo",
38+
"build": "npm run build-bundle && npm run build-mangle",
3939
"test-api": "node test",
4040
"test-coverage": "nyc --reporter lcov tape test.js",
41-
"test": "npm run build && npm run lint && npm run test-coverage"
41+
"test": "npm run format && npm run build && npm run test-coverage"
4242
},
4343
"nyc": {
4444
"check-coverage": true,
4545
"lines": 100,
4646
"functions": 100,
4747
"branches": 100
4848
},
49+
"prettier": {
50+
"tabWidth": 2,
51+
"useTabs": false,
52+
"singleQuote": true,
53+
"bracketSpacing": false,
54+
"semi": false,
55+
"trailingComma": "none"
56+
},
4957
"xo": {
50-
"space": true,
58+
"prettier": true,
5159
"esnext": false,
5260
"rules": {
5361
"guard-for-in": "off",

readme.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ npm install unist-util-find-all-before
1313
## Usage
1414

1515
```js
16-
var remark = require('remark');
17-
var findAllBefore = require('unist-util-find-all-before');
16+
var remark = require('remark')
17+
var findAllBefore = require('unist-util-find-all-before')
1818

19-
var tree = remark().parse('Some _emphasis_, **importance**, and `code`.');
20-
var paragraph = tree.children[0];
21-
var code = paragraph.children[paragraph.children.length - 1];
19+
var tree = remark().parse('Some _emphasis_, **importance**, and `code`.')
20+
var paragraph = tree.children[0]
21+
var code = paragraph.children[paragraph.children.length - 1]
2222

23-
console.log(findAllBefore(paragraph, code, 'text'));
23+
console.log(findAllBefore(paragraph, code, 'text'))
2424
```
2525

2626
Yields:

test.js

Lines changed: 89 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,102 @@
1-
'use strict';
1+
'use strict'
22

3-
var assert = require('assert');
4-
var test = require('tape');
5-
var remark = require('remark');
6-
var findAllBefore = require('.');
3+
var assert = require('assert')
4+
var test = require('tape')
5+
var remark = require('remark')
6+
var findAllBefore = require('.')
77

8-
var tree = remark().parse('Some _emphasis_, **importance**, and `code`.');
9-
var paragraph = tree.children[0];
10-
var children = paragraph.children;
8+
var tree = remark().parse('Some _emphasis_, **importance**, and `code`.')
9+
var paragraph = tree.children[0]
10+
var children = paragraph.children
1111

12-
test('unist-util-find-all-before', function (t) {
12+
test('unist-util-find-all-before', function(t) {
1313
t.throws(
14-
function () {
15-
findAllBefore();
14+
function() {
15+
findAllBefore()
1616
},
1717
/Expected parent node/,
1818
'should fail without parent'
19-
);
19+
)
2020

2121
t.throws(
22-
function () {
23-
findAllBefore({type: 'foo'});
22+
function() {
23+
findAllBefore({type: 'foo'})
2424
},
2525
/Expected parent node/,
2626
'should fail without parent node'
27-
);
28-
29-
t.doesNotThrow(
30-
function () {
31-
assert.throws(
32-
function () {
33-
findAllBefore({type: 'foo', children: []});
34-
},
35-
/Expected positive finite index or child node/
36-
);
37-
38-
assert.throws(
39-
function () {
40-
findAllBefore({type: 'foo', children: []}, -1);
27+
)
28+
29+
t.doesNotThrow(function() {
30+
assert.throws(function() {
31+
findAllBefore({type: 'foo', children: []})
32+
}, /Expected positive finite index or child node/)
33+
34+
assert.throws(function() {
35+
findAllBefore({type: 'foo', children: []}, -1)
36+
}, /Expected positive finite index or child node/)
37+
38+
assert.throws(function() {
39+
findAllBefore({type: 'foo', children: []}, {type: 'bar'})
40+
}, /Expected positive finite index or child node/)
41+
}, 'should fail without index')
42+
43+
t.doesNotThrow(function() {
44+
assert.throws(function() {
45+
findAllBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false)
46+
}, /Expected function, string, or object as test/)
47+
48+
assert.throws(function() {
49+
findAllBefore(
50+
{
51+
type: 'foo',
52+
children: [{type: 'bar'}]
4153
},
42-
/Expected positive finite index or child node/
43-
);
44-
45-
assert.throws(
46-
function () {
47-
findAllBefore({type: 'foo', children: []}, {type: 'bar'});
48-
},
49-
/Expected positive finite index or child node/
50-
);
51-
},
52-
'should fail without index'
53-
);
54-
55-
t.doesNotThrow(
56-
function () {
57-
assert.throws(
58-
function () {
59-
findAllBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false);
60-
},
61-
/Expected function, string, or object as test/
62-
);
63-
64-
assert.throws(
65-
function () {
66-
findAllBefore({
67-
type: 'foo',
68-
children: [{type: 'bar'}]
69-
}, 1, true);
70-
},
71-
/Expected function, string, or object as test/
72-
);
73-
},
74-
'should fail for invalid `test`'
75-
);
76-
77-
t.doesNotThrow(
78-
function () {
79-
var res = [children[0]];
80-
81-
assert.deepEqual(findAllBefore(paragraph, children[1]), res);
82-
assert.deepEqual(findAllBefore(paragraph, 1), res);
83-
assert.deepEqual(findAllBefore(paragraph, 0), []);
84-
},
85-
'should return the preceding nodes when without `test`'
86-
);
87-
88-
t.doesNotThrow(
89-
function () {
90-
var res = [children[0]];
91-
92-
assert.deepEqual(findAllBefore(paragraph, 100, children[0]), res);
93-
assert.deepEqual(findAllBefore(paragraph, children[1], children[0]), res);
94-
assert.deepEqual(findAllBefore(paragraph, 1, children[0]), res);
95-
assert.deepEqual(findAllBefore(paragraph, children[0], children[0]), []);
96-
assert.deepEqual(findAllBefore(paragraph, 0, children[0]), []);
97-
assert.deepEqual(findAllBefore(paragraph, 1, children[1]), []);
98-
},
99-
'should return `[node]` when given a `node` and existing'
100-
);
101-
102-
t.doesNotThrow(
103-
function () {
104-
var result = [children[3]];
105-
106-
assert.deepEqual(findAllBefore(paragraph, 100, 'strong'), result);
107-
assert.deepEqual(findAllBefore(paragraph, 3, 'strong'), []);
108-
assert.deepEqual(findAllBefore(paragraph, children[4], 'strong'), result);
109-
assert.deepEqual(findAllBefore(paragraph, children[3], 'strong'), []);
110-
},
111-
'should return children when given a `type` and existing'
112-
);
113-
114-
t.doesNotThrow(
115-
function () {
116-
var res = children.slice(4).reverse();
117-
118-
assert.deepEqual(findAllBefore(paragraph, 100, test), res);
119-
assert.deepEqual(findAllBefore(paragraph, 3, test), []);
120-
assert.deepEqual(findAllBefore(paragraph, children[4], test), []);
121-
assert.deepEqual(findAllBefore(paragraph, children[3], test), []);
122-
123-
function test(node, n) {
124-
return n > 3;
125-
}
126-
},
127-
'should return children when given a `test` and existing'
128-
);
129-
130-
t.end();
131-
});
54+
1,
55+
true
56+
)
57+
}, /Expected function, string, or object as test/)
58+
}, 'should fail for invalid `test`')
59+
60+
t.doesNotThrow(function() {
61+
var res = [children[0]]
62+
63+
assert.deepEqual(findAllBefore(paragraph, children[1]), res)
64+
assert.deepEqual(findAllBefore(paragraph, 1), res)
65+
assert.deepEqual(findAllBefore(paragraph, 0), [])
66+
}, 'should return the preceding nodes when without `test`')
67+
68+
t.doesNotThrow(function() {
69+
var res = [children[0]]
70+
71+
assert.deepEqual(findAllBefore(paragraph, 100, children[0]), res)
72+
assert.deepEqual(findAllBefore(paragraph, children[1], children[0]), res)
73+
assert.deepEqual(findAllBefore(paragraph, 1, children[0]), res)
74+
assert.deepEqual(findAllBefore(paragraph, children[0], children[0]), [])
75+
assert.deepEqual(findAllBefore(paragraph, 0, children[0]), [])
76+
assert.deepEqual(findAllBefore(paragraph, 1, children[1]), [])
77+
}, 'should return `[node]` when given a `node` and existing')
78+
79+
t.doesNotThrow(function() {
80+
var result = [children[3]]
81+
82+
assert.deepEqual(findAllBefore(paragraph, 100, 'strong'), result)
83+
assert.deepEqual(findAllBefore(paragraph, 3, 'strong'), [])
84+
assert.deepEqual(findAllBefore(paragraph, children[4], 'strong'), result)
85+
assert.deepEqual(findAllBefore(paragraph, children[3], 'strong'), [])
86+
}, 'should return children when given a `type` and existing')
87+
88+
t.doesNotThrow(function() {
89+
var res = children.slice(4).reverse()
90+
91+
assert.deepEqual(findAllBefore(paragraph, 100, test), res)
92+
assert.deepEqual(findAllBefore(paragraph, 3, test), [])
93+
assert.deepEqual(findAllBefore(paragraph, children[4], test), [])
94+
assert.deepEqual(findAllBefore(paragraph, children[3], test), [])
95+
96+
function test(node, n) {
97+
return n > 3
98+
}
99+
}, 'should return children when given a `test` and existing')
100+
101+
t.end()
102+
})

0 commit comments

Comments
 (0)