Skip to content

Commit bb6e412

Browse files
committed
Refactor code-style
1 parent cd1df3e commit bb6e412

File tree

4 files changed

+136
-188
lines changed

4 files changed

+136
-188
lines changed

.editorconfig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ root = true
22

33
[*]
44
indent_style = space
5-
indent_size = 4
5+
indent_size = 2
66
end_of_line = lf
77
charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
10-
11-
[*.{json,html,svg,css,mdastrc,eslintrc}]
12-
indent_size = 2
13-
14-
[*.md]
15-
trim_trailing_whitespace = false

index.js

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,41 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2015 Titus Wormer
4-
* @license MIT
5-
* @module unist:util:find-before
6-
* @fileoverview Utility to find a node before another node.
7-
*/
8-
91
'use strict';
102

11-
/* eslint-env commonjs */
12-
13-
/*
14-
* Dependencies.
15-
*/
16-
173
var is = require('unist-util-is');
184

19-
/**
20-
* Find a node before `index` in `parent` which passes
21-
* `test`.
22-
*
23-
* @param {Node} parent - Parent to search in.
24-
* @param {number|Node} index - (Position of) node to
25-
* search before.
26-
* @param {*} test - See `wooorm/unist-util-is`.
27-
* @return {Node?} - A child node of `parent` which passes
28-
* `test`.
29-
*/
5+
module.exports = findBefore;
6+
7+
/* Find a node before `index` in `parent` which passes
8+
* `test`. */
309
function findBefore(parent, index, test) {
31-
var children;
32-
var child;
10+
var children;
11+
var child;
3312

34-
if (!parent || !parent.type || !parent.children) {
35-
throw new Error('Expected parent node');
36-
}
13+
if (!parent || !parent.type || !parent.children) {
14+
throw new Error('Expected parent node');
15+
}
3716

38-
children = parent.children;
17+
children = parent.children;
3918

40-
if (index && index.type) {
41-
index = children.indexOf(index);
42-
}
19+
if (index && index.type) {
20+
index = children.indexOf(index);
21+
}
4322

44-
if (isNaN(index) || index < 0 || index === Infinity) {
45-
throw new Error('Expected positive finite index or child node');
46-
}
23+
if (isNaN(index) || index < 0 || index === Infinity) {
24+
throw new Error('Expected positive finite index or child node');
25+
}
4726

48-
/* Performance. */
49-
if (index > children.length) {
50-
index = children.length;
51-
}
27+
/* Performance. */
28+
if (index > children.length) {
29+
index = children.length;
30+
}
5231

53-
while (index--) {
54-
child = children[index];
32+
while (index--) {
33+
child = children[index];
5534

56-
if (is(test, child, index, parent)) {
57-
return child;
58-
}
35+
if (is(test, child, index, parent)) {
36+
return child;
5937
}
38+
}
6039

61-
return null;
40+
return null;
6241
}
63-
64-
/*
65-
* Expose.
66-
*/
67-
68-
module.exports = findBefore;

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,33 @@
3333
],
3434
"devDependencies": {
3535
"browserify": "^11.0.0",
36-
"eslint": "^1.0.0",
3736
"esmangle": "^1.0.0",
3837
"istanbul": "^0.3.0",
39-
"jscs": "^2.0.0",
40-
"jscs-jsdoc": "^1.0.0",
4138
"mdast": "^1.0.0",
4239
"mdast-comment-config": "^1.0.0",
4340
"mdast-github": "^1.0.0",
4441
"mdast-lint": "^1.0.0",
4542
"mdast-slug": "^1.0.0",
4643
"mdast-validate-links": "^1.0.0",
47-
"mocha": "^2.0.0"
44+
"mocha": "^2.0.0",
45+
"xo": "^0.17.1"
4846
},
4947
"scripts": {
5048
"test-api": "mocha --check-leaks test.js",
5149
"test-coverage": "istanbul cover _mocha -- test.js",
5250
"test-travis": "npm run test-coverage",
5351
"test": "npm run test-api",
54-
"lint-api": "eslint .",
55-
"lint-style": "jscs --reporter inline .",
56-
"lint": "npm run lint-api && npm run lint-style",
52+
"lint": "xo",
5753
"make": "npm run lint && npm run test-coverage",
5854
"bundle": "browserify index.js --no-builtins -s unistUtilFindBefore > unist-util-find-before.js",
5955
"postbundle": "esmangle unist-util-find-before.js > unist-util-find-before.min.js",
6056
"build-md": "mdast . --quiet",
6157
"build": "npm run bundle && npm run build-md"
58+
},
59+
"xo": {
60+
"space": true,
61+
"ignore": [
62+
"unist-util-find-before.js"
63+
]
6264
}
6365
}

test.js

Lines changed: 100 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,111 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2015 Titus Wormer
4-
* @license MIT
5-
* @module unist:util:find-before
6-
* @fileoverview Test suite for `unit-util-find-before`.
7-
*/
8-
91
'use strict';
102

11-
/* eslint-env node, mocha */
12-
13-
/*
14-
* Dependencies.
15-
*/
3+
/* eslint-env mocha */
164

175
var assert = require('assert');
186
var mdast = require('mdast');
197
var findBefore = require('./');
208

21-
/*
22-
* Methods.
23-
*/
24-
25-
var equal = assert.strictEqual;
26-
27-
/*
28-
* Fixture.
29-
*/
30-
31-
var ast = mdast.parse('Some *emphasis*, **strongness**, and `code`.');
32-
var paragraph = ast.children[0];
9+
var tree = mdast.parse('Some *emphasis*, **importance**, and `code`.');
10+
var paragraph = tree.children[0];
3311
var children = paragraph.children;
3412

35-
/*
36-
* Tests.
37-
*/
38-
3913
describe('unist-util-find-before', function () {
40-
it('should fail without parent', function () {
41-
assert.throws(function () {
42-
findBefore();
43-
}, /Expected parent node/);
44-
});
45-
46-
it('should fail without parent node', function () {
47-
assert.throws(function () {
48-
findBefore({
49-
'type': 'foo'
50-
});
51-
}, /Expected parent node/);
52-
});
53-
54-
it('should fail without index', function () {
55-
assert.throws(function () {
56-
findBefore({
57-
'type': 'foo',
58-
'children': []
59-
});
60-
}, /Expected positive finite index or child node/);
61-
62-
assert.throws(function () {
63-
findBefore({
64-
'type': 'foo',
65-
'children': []
66-
}, -1);
67-
}, /Expected positive finite index or child node/);
68-
69-
assert.throws(function () {
70-
findBefore({
71-
'type': 'foo',
72-
'children': []
73-
}, {
74-
'type': 'bar'
75-
});
76-
}, /Expected positive finite index or child node/);
77-
});
78-
79-
it('should fail for invalid `test`', function () {
80-
assert.throws(function () {
81-
findBefore({
82-
'type': 'foo',
83-
'children': [{
84-
'type': 'bar'
85-
}]
86-
}, 1, false);
87-
}, /Expected function, string, or node as test/);
88-
89-
assert.throws(function () {
90-
findBefore({
91-
'type': 'foo',
92-
'children': [{
93-
'type': 'bar'
94-
}]
95-
}, 1, true);
96-
}, /Expected function, string, or node as test/);
97-
});
98-
99-
it('should return the preceding node when without `test`', function () {
100-
equal(findBefore(paragraph, children[1]), children[0]);
101-
equal(findBefore(paragraph, 1), children[0]);
102-
equal(findBefore(paragraph, 0), null);
103-
});
104-
105-
it('should return `node` when given a `node` and existing', function () {
106-
equal(findBefore(paragraph, 100, children[0]), children[0]);
107-
equal(findBefore(paragraph, children[1], children[0]), children[0]);
108-
equal(findBefore(paragraph, 1, children[0]), children[0]);
109-
equal(findBefore(paragraph, children[0], children[0]), null);
110-
equal(findBefore(paragraph, 0, children[0]), null);
111-
equal(findBefore(paragraph, 1, children[1]), null);
112-
});
113-
114-
it('should return a child when given a `type` and existing', function () {
115-
equal(findBefore(paragraph, 100, 'strong'), children[3]);
116-
equal(findBefore(paragraph, 3, 'strong'), null);
117-
equal(findBefore(paragraph, children[4], 'strong'), children[3]);
118-
equal(findBefore(paragraph, children[3], 'strong'), null);
119-
});
120-
121-
it('should return a child when given a `test` and existing', function () {
122-
/** Test */
123-
function test(node, n) {
124-
return n === 3;
125-
}
126-
127-
equal(findBefore(paragraph, 100, test), children[3]);
128-
equal(findBefore(paragraph, 3, test), null);
129-
equal(findBefore(paragraph, children[4], test), children[3]);
130-
equal(findBefore(paragraph, children[3], test), null);
131-
});
14+
it('should fail without parent', function () {
15+
assert.throws(
16+
function () {
17+
findBefore();
18+
},
19+
/Expected parent node/
20+
);
21+
});
22+
23+
it('should fail without parent node', function () {
24+
assert.throws(
25+
function () {
26+
findBefore({
27+
type: 'foo'
28+
});
29+
},
30+
/Expected parent node/
31+
);
32+
});
33+
34+
it('should fail without index', function () {
35+
assert.throws(
36+
function () {
37+
findBefore({type: 'foo', children: []});
38+
},
39+
/Expected positive finite index or child node/
40+
);
41+
42+
assert.throws(
43+
function () {
44+
findBefore({type: 'foo', children: []}, -1);
45+
},
46+
/Expected positive finite index or child node/
47+
);
48+
49+
assert.throws(
50+
function () {
51+
findBefore({type: 'foo', children: []}, {type: 'bar'});
52+
},
53+
/Expected positive finite index or child node/
54+
);
55+
});
56+
57+
it('should fail for invalid `test`', function () {
58+
assert.throws(
59+
function () {
60+
findBefore({
61+
type: 'foo',
62+
children: [{type: 'bar'}]
63+
}, 1, false);
64+
},
65+
/Expected function, string, or node as test/
66+
);
67+
68+
assert.throws(
69+
function () {
70+
findBefore({
71+
type: 'foo',
72+
children: [{type: 'bar'}]
73+
}, 1, true);
74+
},
75+
/Expected function, string, or node as test/
76+
);
77+
});
78+
79+
it('should return the preceding node when without `test`', function () {
80+
assert.strictEqual(findBefore(paragraph, children[1]), children[0]);
81+
assert.strictEqual(findBefore(paragraph, 1), children[0]);
82+
assert.strictEqual(findBefore(paragraph, 0), null);
83+
});
84+
85+
it('should return `node` when given a `node` and existing', function () {
86+
assert.strictEqual(findBefore(paragraph, 100, children[0]), children[0]);
87+
assert.strictEqual(findBefore(paragraph, children[1], children[0]), children[0]);
88+
assert.strictEqual(findBefore(paragraph, 1, children[0]), children[0]);
89+
assert.strictEqual(findBefore(paragraph, children[0], children[0]), null);
90+
assert.strictEqual(findBefore(paragraph, 0, children[0]), null);
91+
assert.strictEqual(findBefore(paragraph, 1, children[1]), null);
92+
});
93+
94+
it('should return a child when given a `type` and existing', function () {
95+
assert.strictEqual(findBefore(paragraph, 100, 'strong'), children[3]);
96+
assert.strictEqual(findBefore(paragraph, 3, 'strong'), null);
97+
assert.strictEqual(findBefore(paragraph, children[4], 'strong'), children[3]);
98+
assert.strictEqual(findBefore(paragraph, children[3], 'strong'), null);
99+
});
100+
101+
it('should return a child when given a `test` and existing', function () {
102+
assert.strictEqual(findBefore(paragraph, 100, test), children[3]);
103+
assert.strictEqual(findBefore(paragraph, 3, test), null);
104+
assert.strictEqual(findBefore(paragraph, children[4], test), children[3]);
105+
assert.strictEqual(findBefore(paragraph, children[3], test), null);
106+
107+
function test(node, n) {
108+
return n === 3;
109+
}
110+
});
132111
});

0 commit comments

Comments
 (0)