-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
}); |