Skip to content

Commit f9d865e

Browse files
committed
Refactor code-style
1 parent f8090c4 commit f9d865e

File tree

5 files changed

+118
-126
lines changed

5 files changed

+118
-126
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-before.js
3+
unist-util-find-before.min.js

index.js

Lines changed: 13 additions & 13 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 = findBefore;
5+
module.exports = findBefore
66

77
/* Find a node before `index` in `parent` which passes
88
* `test`. */
99
function findBefore(parent, index, test) {
10-
var children;
11-
var child;
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-
return child;
36+
return child
3737
}
3838
}
3939

40-
return null;
40+
return null
4141
}

package.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,39 @@
2626
"devDependencies": {
2727
"browserify": "^16.0.0",
2828
"esmangle": "^1.0.0",
29+
"nyc": "^11.0.0",
30+
"prettier": "^1.12.1",
2931
"remark": "^9.0.0",
3032
"remark-cli": "^5.0.0",
3133
"remark-preset-wooorm": "^4.0.0",
32-
"nyc": "^11.0.0",
3334
"tape": "^4.6.2",
3435
"xo": "^0.20.0"
3536
},
3637
"scripts": {
37-
"build-md": "remark . -foq",
38+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3839
"build-bundle": "browserify index.js --no-builtins -s unistUtilFindBefore > unist-util-find-before.js",
3940
"build-mangle": "esmangle unist-util-find-before.js > unist-util-find-before.min.js",
40-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
41-
"lint": "xo",
41+
"build": "npm run build-bundle && npm run build-mangle",
4242
"test-api": "node test",
4343
"test-coverage": "nyc --reporter lcov tape test.js",
44-
"test": "npm run build && npm run lint && npm run test-coverage"
44+
"test": "npm run format && npm run build && npm run test-coverage"
4545
},
4646
"nyc": {
4747
"check-coverage": true,
4848
"lines": 100,
4949
"functions": 100,
5050
"branches": 100
5151
},
52+
"prettier": {
53+
"tabWidth": 2,
54+
"useTabs": false,
55+
"singleQuote": true,
56+
"bracketSpacing": false,
57+
"semi": false,
58+
"trailingComma": "none"
59+
},
5260
"xo": {
53-
"space": true,
61+
"prettier": true,
5462
"esnext": false,
5563
"rules": {
5664
"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-before
1313
## Usage
1414

1515
```js
16-
var remark = require('remark');
17-
var findBefore = require('unist-util-find-before');
16+
var remark = require('remark')
17+
var findBefore = require('unist-util-find-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(findBefore(paragraph, code, 'emphasis'));
23+
console.log(findBefore(paragraph, code, 'emphasis'))
2424
```
2525

2626
Yields:

test.js

Lines changed: 82 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,109 @@
1-
'use strict';
1+
'use strict'
22

3-
var assert = require('assert');
4-
var test = require('tape');
5-
var remark = require('remark');
6-
var findBefore = require('.');
3+
var assert = require('assert')
4+
var test = require('tape')
5+
var remark = require('remark')
6+
var findBefore = 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-before', function (t) {
12+
test('unist-util-find-before', function(t) {
1313
t.throws(
14-
function () {
15-
findBefore();
14+
function() {
15+
findBefore()
1616
},
1717
/Expected parent node/,
1818
'should fail without parent'
19-
);
19+
)
2020

2121
t.throws(
22-
function () {
22+
function() {
2323
findBefore({
2424
type: 'foo'
25-
});
25+
})
2626
},
2727
/Expected parent node/,
2828
'should fail without parent node'
29-
);
29+
)
3030

31-
t.doesNotThrow(
32-
function () {
33-
assert.throws(
34-
function () {
35-
findBefore({type: 'foo', children: []});
36-
},
37-
/Expected positive finite index or child node/
38-
);
31+
t.doesNotThrow(function() {
32+
assert.throws(function() {
33+
findBefore({type: 'foo', children: []})
34+
}, /Expected positive finite index or child node/)
3935

40-
assert.throws(
41-
function () {
42-
findBefore({type: 'foo', children: []}, -1);
43-
},
44-
/Expected positive finite index or child node/
45-
);
36+
assert.throws(function() {
37+
findBefore({type: 'foo', children: []}, -1)
38+
}, /Expected positive finite index or child node/)
4639

47-
assert.throws(
48-
function () {
49-
findBefore({type: 'foo', children: []}, {type: 'bar'});
50-
},
51-
/Expected positive finite index or child node/
52-
);
53-
},
54-
'should fail without index'
55-
);
40+
assert.throws(function() {
41+
findBefore({type: 'foo', children: []}, {type: 'bar'})
42+
}, /Expected positive finite index or child node/)
43+
}, 'should fail without index')
5644

57-
t.doesNotThrow(
58-
function () {
59-
assert.throws(
60-
function () {
61-
findBefore({
62-
type: 'foo',
63-
children: [{type: 'bar'}]
64-
}, 1, false);
45+
t.doesNotThrow(function() {
46+
assert.throws(function() {
47+
findBefore(
48+
{
49+
type: 'foo',
50+
children: [{type: 'bar'}]
6551
},
66-
/Expected function, string, or object as test/
67-
);
52+
1,
53+
false
54+
)
55+
}, /Expected function, string, or object as test/)
6856

69-
assert.throws(
70-
function () {
71-
findBefore({
72-
type: 'foo',
73-
children: [{type: 'bar'}]
74-
}, 1, true);
57+
assert.throws(function() {
58+
findBefore(
59+
{
60+
type: 'foo',
61+
children: [{type: 'bar'}]
7562
},
76-
/Expected function, string, or object as test/
77-
);
78-
},
79-
'should fail for invalid `test`'
80-
);
63+
1,
64+
true
65+
)
66+
}, /Expected function, string, or object as test/)
67+
}, 'should fail for invalid `test`')
8168

82-
t.doesNotThrow(
83-
function () {
84-
assert.strictEqual(findBefore(paragraph, children[1]), children[0]);
85-
assert.strictEqual(findBefore(paragraph, 1), children[0]);
86-
assert.strictEqual(findBefore(paragraph, 0), null);
87-
},
88-
'should return the preceding node when without `test`'
89-
);
69+
t.doesNotThrow(function() {
70+
assert.strictEqual(findBefore(paragraph, children[1]), children[0])
71+
assert.strictEqual(findBefore(paragraph, 1), children[0])
72+
assert.strictEqual(findBefore(paragraph, 0), null)
73+
}, 'should return the preceding node when without `test`')
9074

91-
t.doesNotThrow(
92-
function () {
93-
assert.strictEqual(findBefore(paragraph, 100, children[0]), children[0]);
94-
assert.strictEqual(findBefore(paragraph, children[1], children[0]), children[0]);
95-
assert.strictEqual(findBefore(paragraph, 1, children[0]), children[0]);
96-
assert.strictEqual(findBefore(paragraph, children[0], children[0]), null);
97-
assert.strictEqual(findBefore(paragraph, 0, children[0]), null);
98-
assert.strictEqual(findBefore(paragraph, 1, children[1]), null);
99-
},
100-
'should return `node` when given a `node` and existing'
101-
);
75+
t.doesNotThrow(function() {
76+
assert.strictEqual(findBefore(paragraph, 100, children[0]), children[0])
77+
assert.strictEqual(
78+
findBefore(paragraph, children[1], children[0]),
79+
children[0]
80+
)
81+
assert.strictEqual(findBefore(paragraph, 1, children[0]), children[0])
82+
assert.strictEqual(findBefore(paragraph, children[0], children[0]), null)
83+
assert.strictEqual(findBefore(paragraph, 0, children[0]), null)
84+
assert.strictEqual(findBefore(paragraph, 1, children[1]), null)
85+
}, 'should return `node` when given a `node` and existing')
10286

103-
t.doesNotThrow(
104-
function () {
105-
assert.strictEqual(findBefore(paragraph, 100, 'strong'), children[3]);
106-
assert.strictEqual(findBefore(paragraph, 3, 'strong'), null);
107-
assert.strictEqual(findBefore(paragraph, children[4], 'strong'), children[3]);
108-
assert.strictEqual(findBefore(paragraph, children[3], 'strong'), null);
109-
},
110-
'should return a child when given a `type` and existing'
111-
);
87+
t.doesNotThrow(function() {
88+
assert.strictEqual(findBefore(paragraph, 100, 'strong'), children[3])
89+
assert.strictEqual(findBefore(paragraph, 3, 'strong'), null)
90+
assert.strictEqual(
91+
findBefore(paragraph, children[4], 'strong'),
92+
children[3]
93+
)
94+
assert.strictEqual(findBefore(paragraph, children[3], 'strong'), null)
95+
}, 'should return a child when given a `type` and existing')
11296

113-
t.doesNotThrow(
114-
function () {
115-
assert.strictEqual(findBefore(paragraph, 100, test), children[3]);
116-
assert.strictEqual(findBefore(paragraph, 3, test), null);
117-
assert.strictEqual(findBefore(paragraph, children[4], test), children[3]);
118-
assert.strictEqual(findBefore(paragraph, children[3], test), null);
97+
t.doesNotThrow(function() {
98+
assert.strictEqual(findBefore(paragraph, 100, test), children[3])
99+
assert.strictEqual(findBefore(paragraph, 3, test), null)
100+
assert.strictEqual(findBefore(paragraph, children[4], test), children[3])
101+
assert.strictEqual(findBefore(paragraph, children[3], test), null)
119102

120-
function test(node, n) {
121-
return n === 3;
122-
}
123-
},
124-
'should return a child when given a `test` and existing'
125-
);
103+
function test(node, n) {
104+
return n === 3
105+
}
106+
}, 'should return a child when given a `test` and existing')
126107

127-
t.end();
128-
});
108+
t.end()
109+
})

0 commit comments

Comments
 (0)