Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 21, 2016
1 parent cd1df3e commit bb6e412
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 188 deletions.
8 changes: 1 addition & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{json,html,svg,css,mdastrc,eslintrc}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
79 changes: 26 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,41 @@
/**
* @author Titus Wormer
* @copyright 2015 Titus Wormer
* @license MIT
* @module unist:util:find-before
* @fileoverview Utility to find a node before another node.
*/

'use strict';

/* eslint-env commonjs */

/*
* Dependencies.
*/

var is = require('unist-util-is');

/**
* Find a node before `index` in `parent` which passes
* `test`.
*
* @param {Node} parent - Parent to search in.
* @param {number|Node} index - (Position of) node to
* search before.
* @param {*} test - See `wooorm/unist-util-is`.
* @return {Node?} - A child node of `parent` which passes
* `test`.
*/
module.exports = findBefore;

/* Find a node before `index` in `parent` which passes
* `test`. */
function findBefore(parent, index, test) {
var children;
var child;
var children;
var child;

if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node');
}
if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node');
}

children = parent.children;
children = parent.children;

if (index && index.type) {
index = children.indexOf(index);
}
if (index && index.type) {
index = children.indexOf(index);
}

if (isNaN(index) || index < 0 || index === Infinity) {
throw new Error('Expected positive finite index or child node');
}
if (isNaN(index) || index < 0 || index === Infinity) {
throw new Error('Expected positive finite index or child node');
}

/* Performance. */
if (index > children.length) {
index = children.length;
}
/* Performance. */
if (index > children.length) {
index = children.length;
}

while (index--) {
child = children[index];
while (index--) {
child = children[index];

if (is(test, child, index, parent)) {
return child;
}
if (is(test, child, index, parent)) {
return child;
}
}

return null;
return null;
}

/*
* Expose.
*/

module.exports = findBefore;
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,33 @@
],
"devDependencies": {
"browserify": "^11.0.0",
"eslint": "^1.0.0",
"esmangle": "^1.0.0",
"istanbul": "^0.3.0",
"jscs": "^2.0.0",
"jscs-jsdoc": "^1.0.0",
"mdast": "^1.0.0",
"mdast-comment-config": "^1.0.0",
"mdast-github": "^1.0.0",
"mdast-lint": "^1.0.0",
"mdast-slug": "^1.0.0",
"mdast-validate-links": "^1.0.0",
"mocha": "^2.0.0"
"mocha": "^2.0.0",
"xo": "^0.17.1"
},
"scripts": {
"test-api": "mocha --check-leaks test.js",
"test-coverage": "istanbul cover _mocha -- test.js",
"test-travis": "npm run test-coverage",
"test": "npm run test-api",
"lint-api": "eslint .",
"lint-style": "jscs --reporter inline .",
"lint": "npm run lint-api && npm run lint-style",
"lint": "xo",
"make": "npm run lint && npm run test-coverage",
"bundle": "browserify index.js --no-builtins -s unistUtilFindBefore > unist-util-find-before.js",
"postbundle": "esmangle unist-util-find-before.js > unist-util-find-before.min.js",
"build-md": "mdast . --quiet",
"build": "npm run bundle && npm run build-md"
},
"xo": {
"space": true,
"ignore": [
"unist-util-find-before.js"
]
}
}
221 changes: 100 additions & 121 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,132 +1,111 @@
/**
* @author Titus Wormer
* @copyright 2015 Titus Wormer
* @license MIT
* @module unist:util:find-before
* @fileoverview Test suite for `unit-util-find-before`.
*/

'use strict';

/* eslint-env node, mocha */

/*
* Dependencies.
*/
/* eslint-env mocha */

var assert = require('assert');
var mdast = require('mdast');
var findBefore = require('./');

/*
* Methods.
*/

var equal = assert.strictEqual;

/*
* Fixture.
*/

var ast = mdast.parse('Some *emphasis*, **strongness**, and `code`.');
var paragraph = ast.children[0];
var tree = mdast.parse('Some *emphasis*, **importance**, and `code`.');
var paragraph = tree.children[0];
var children = paragraph.children;

/*
* Tests.
*/

describe('unist-util-find-before', function () {
it('should fail without parent', function () {
assert.throws(function () {
findBefore();
}, /Expected parent node/);
});

it('should fail without parent node', function () {
assert.throws(function () {
findBefore({
'type': 'foo'
});
}, /Expected parent node/);
});

it('should fail without index', function () {
assert.throws(function () {
findBefore({
'type': 'foo',
'children': []
});
}, /Expected positive finite index or child node/);

assert.throws(function () {
findBefore({
'type': 'foo',
'children': []
}, -1);
}, /Expected positive finite index or child node/);

assert.throws(function () {
findBefore({
'type': 'foo',
'children': []
}, {
'type': 'bar'
});
}, /Expected positive finite index or child node/);
});

it('should fail for invalid `test`', function () {
assert.throws(function () {
findBefore({
'type': 'foo',
'children': [{
'type': 'bar'
}]
}, 1, false);
}, /Expected function, string, or node as test/);

assert.throws(function () {
findBefore({
'type': 'foo',
'children': [{
'type': 'bar'
}]
}, 1, true);
}, /Expected function, string, or node as test/);
});

it('should return the preceding node when without `test`', function () {
equal(findBefore(paragraph, children[1]), children[0]);
equal(findBefore(paragraph, 1), children[0]);
equal(findBefore(paragraph, 0), null);
});

it('should return `node` when given a `node` and existing', function () {
equal(findBefore(paragraph, 100, children[0]), children[0]);
equal(findBefore(paragraph, children[1], children[0]), children[0]);
equal(findBefore(paragraph, 1, children[0]), children[0]);
equal(findBefore(paragraph, children[0], children[0]), null);
equal(findBefore(paragraph, 0, children[0]), null);
equal(findBefore(paragraph, 1, children[1]), null);
});

it('should return a child when given a `type` and existing', function () {
equal(findBefore(paragraph, 100, 'strong'), children[3]);
equal(findBefore(paragraph, 3, 'strong'), null);
equal(findBefore(paragraph, children[4], 'strong'), children[3]);
equal(findBefore(paragraph, children[3], 'strong'), null);
});

it('should return a child when given a `test` and existing', function () {
/** Test */
function test(node, n) {
return n === 3;
}

equal(findBefore(paragraph, 100, test), children[3]);
equal(findBefore(paragraph, 3, test), null);
equal(findBefore(paragraph, children[4], test), children[3]);
equal(findBefore(paragraph, children[3], test), null);
});
it('should fail without parent', function () {
assert.throws(
function () {
findBefore();
},
/Expected parent node/
);
});

it('should fail without parent node', function () {
assert.throws(
function () {
findBefore({
type: 'foo'
});
},
/Expected parent node/
);
});

it('should fail without index', function () {
assert.throws(
function () {
findBefore({type: 'foo', children: []});
},
/Expected positive finite index or child node/
);

assert.throws(
function () {
findBefore({type: 'foo', children: []}, -1);
},
/Expected positive finite index or child node/
);

assert.throws(
function () {
findBefore({type: 'foo', children: []}, {type: 'bar'});
},
/Expected positive finite index or child node/
);
});

it('should fail for invalid `test`', function () {
assert.throws(
function () {
findBefore({
type: 'foo',
children: [{type: 'bar'}]
}, 1, false);
},
/Expected function, string, or node as test/
);

assert.throws(
function () {
findBefore({
type: 'foo',
children: [{type: 'bar'}]
}, 1, true);
},
/Expected function, string, or node as test/
);
});

it('should return the preceding node when without `test`', function () {
assert.strictEqual(findBefore(paragraph, children[1]), children[0]);
assert.strictEqual(findBefore(paragraph, 1), children[0]);
assert.strictEqual(findBefore(paragraph, 0), null);
});

it('should return `node` when given a `node` and existing', function () {
assert.strictEqual(findBefore(paragraph, 100, children[0]), children[0]);
assert.strictEqual(findBefore(paragraph, children[1], children[0]), children[0]);
assert.strictEqual(findBefore(paragraph, 1, children[0]), children[0]);
assert.strictEqual(findBefore(paragraph, children[0], children[0]), null);
assert.strictEqual(findBefore(paragraph, 0, children[0]), null);
assert.strictEqual(findBefore(paragraph, 1, children[1]), null);
});

it('should return a child when given a `type` and existing', function () {
assert.strictEqual(findBefore(paragraph, 100, 'strong'), children[3]);
assert.strictEqual(findBefore(paragraph, 3, 'strong'), null);
assert.strictEqual(findBefore(paragraph, children[4], 'strong'), children[3]);
assert.strictEqual(findBefore(paragraph, children[3], 'strong'), null);
});

it('should return a child when given a `test` and existing', function () {
assert.strictEqual(findBefore(paragraph, 100, test), children[3]);
assert.strictEqual(findBefore(paragraph, 3, test), null);
assert.strictEqual(findBefore(paragraph, children[4], test), children[3]);
assert.strictEqual(findBefore(paragraph, children[3], test), null);

function test(node, n) {
return n === 3;
}
});
});

0 comments on commit bb6e412

Please sign in to comment.