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 f8090c4 commit f9d865e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 126 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
unist-util-find-before.js
unist-util-find-before.min.js
26 changes: 13 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
'use strict';
'use strict'

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

module.exports = findBefore;
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');
throw new Error('Expected parent node')
}

children = parent.children;
children = parent.children

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

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

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

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

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

return null;
return null
}
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,39 @@
"devDependencies": {
"browserify": "^16.0.0",
"esmangle": "^1.0.0",
"nyc": "^11.0.0",
"prettier": "^1.12.1",
"remark": "^9.0.0",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"nyc": "^11.0.0",
"tape": "^4.6.2",
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . -foq",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --no-builtins -s unistUtilFindBefore > unist-util-find-before.js",
"build-mangle": "esmangle unist-util-find-before.js > unist-util-find-before.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"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
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ npm install unist-util-find-before
## Usage

```js
var remark = require('remark');
var findBefore = require('unist-util-find-before');
var remark = require('remark')
var findBefore = require('unist-util-find-before')

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

console.log(findBefore(paragraph, code, 'emphasis'));
console.log(findBefore(paragraph, code, 'emphasis'))
```

Yields:
Expand Down
183 changes: 82 additions & 101 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,128 +1,109 @@
'use strict';
'use strict'

var assert = require('assert');
var test = require('tape');
var remark = require('remark');
var findBefore = require('.');
var assert = require('assert')
var test = require('tape')
var remark = require('remark')
var findBefore = require('.')

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

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

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

t.doesNotThrow(
function () {
assert.throws(
function () {
findBefore({type: 'foo', children: []});
},
/Expected positive finite index or child node/
);
t.doesNotThrow(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: []}, -1)
}, /Expected positive finite index or child node/)

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

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

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

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

t.doesNotThrow(
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);
},
'should return `node` when given a `node` and existing'
);
t.doesNotThrow(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)
}, 'should return `node` when given a `node` and existing')

t.doesNotThrow(
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);
},
'should return a child when given a `type` and existing'
);
t.doesNotThrow(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)
}, 'should return a child when given a `type` and existing')

t.doesNotThrow(
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);
t.doesNotThrow(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;
}
},
'should return a child when given a `test` and existing'
);
function test(node, n) {
return n === 3
}
}, 'should return a child when given a `test` and existing')

t.end();
});
t.end()
})

0 comments on commit f9d865e

Please sign in to comment.