-
-
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
5 changed files
with
118 additions
and
126 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
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 |
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,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 | ||
} |
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
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,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() | ||
}) |