-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
101 additions
and
88 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-modify-children.js | ||
unist-util-modify-children.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,35 +1,35 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var iterate = require('array-iterate'); | ||
var iterate = require('array-iterate') | ||
|
||
module.exports = modifierFactory; | ||
module.exports = modifierFactory | ||
|
||
/* Turn `callback` into a child-modifier accepting a parent. | ||
* See `array-iterate` for more info. */ | ||
function modifierFactory(callback) { | ||
return iteratorFactory(wrapperFactory(callback)); | ||
return iteratorFactory(wrapperFactory(callback)) | ||
} | ||
|
||
/* Turn `callback` into a `iterator' accepting a parent. */ | ||
function iteratorFactory(callback) { | ||
return iterator; | ||
return iterator | ||
|
||
function iterator(parent) { | ||
var children = parent && parent.children; | ||
var children = parent && parent.children | ||
|
||
if (!children) { | ||
throw new Error('Missing children in `parent` for `modifier`'); | ||
throw new Error('Missing children in `parent` for `modifier`') | ||
} | ||
|
||
return iterate(children, callback, parent); | ||
return iterate(children, callback, parent) | ||
} | ||
} | ||
|
||
/* Pass the context as the third argument to `callback`. */ | ||
function wrapperFactory(callback) { | ||
return wrapper; | ||
return wrapper | ||
|
||
function wrapper(value, index) { | ||
return callback(value, index, this); | ||
return callback(value, index, this) | ||
} | ||
} |
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,105 +1,105 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var test = require('tape'); | ||
var modifyChildren = require('.'); | ||
var test = require('tape') | ||
var modifyChildren = require('.') | ||
|
||
var noop = Function.prototype; | ||
var noop = Function.prototype | ||
|
||
test('modifyChildren()', function (t) { | ||
test('modifyChildren()', function(t) { | ||
t.throws( | ||
function () { | ||
modifyChildren(noop)(); | ||
function() { | ||
modifyChildren(noop)() | ||
}, | ||
/Missing children in `parent`/, | ||
'should throw without node' | ||
); | ||
) | ||
|
||
t.throws( | ||
function () { | ||
modifyChildren(noop)({}); | ||
function() { | ||
modifyChildren(noop)({}) | ||
}, | ||
/Missing children in `parent`/, | ||
'should throw without parent' | ||
); | ||
) | ||
|
||
t.test('should invoke `fn` for each child in `parent`', function (st) { | ||
var values = [0, 1, 2, 3]; | ||
var context = {}; | ||
var n = -1; | ||
t.test('should invoke `fn` for each child in `parent`', function(st) { | ||
var values = [0, 1, 2, 3] | ||
var context = {} | ||
var n = -1 | ||
|
||
context.children = values; | ||
context.children = values | ||
|
||
modifyChildren(function (child, index, parent) { | ||
n++; | ||
st.strictEqual(child, values[n]); | ||
st.strictEqual(index, n); | ||
st.strictEqual(parent, context); | ||
})(context); | ||
modifyChildren(function(child, index, parent) { | ||
n++ | ||
st.strictEqual(child, values[n]) | ||
st.strictEqual(index, n) | ||
st.strictEqual(parent, context) | ||
})(context) | ||
|
||
st.end(); | ||
}); | ||
st.end() | ||
}) | ||
|
||
t.test('should work when new children are added', function (st) { | ||
var values = [0, 1, 2, 3, 4, 5, 6]; | ||
var n = -1; | ||
t.test('should work when new children are added', function(st) { | ||
var values = [0, 1, 2, 3, 4, 5, 6] | ||
var n = -1 | ||
|
||
modifyChildren(function (child, index, parent) { | ||
n++; | ||
modifyChildren(function(child, index, parent) { | ||
n++ | ||
|
||
if (index < 3) { | ||
parent.children.push(parent.children.length); | ||
parent.children.push(parent.children.length) | ||
} | ||
|
||
st.strictEqual(child, values[n]); | ||
st.strictEqual(index, values[n]); | ||
})({children: [0, 1, 2, 3]}); | ||
st.strictEqual(child, values[n]) | ||
st.strictEqual(index, values[n]) | ||
})({children: [0, 1, 2, 3]}) | ||
|
||
st.end(); | ||
}); | ||
st.end() | ||
}) | ||
|
||
t.test('should skip forwards', function (st) { | ||
var values = [0, 1, 2, 3]; | ||
var n = -1; | ||
var context = {}; | ||
t.test('should skip forwards', function(st) { | ||
var values = [0, 1, 2, 3] | ||
var n = -1 | ||
var context = {} | ||
|
||
context.children = [0, 1, 3]; | ||
context.children = [0, 1, 3] | ||
|
||
modifyChildren(function (child, index, parent) { | ||
st.strictEqual(child, values[++n]); | ||
modifyChildren(function(child, index, parent) { | ||
st.strictEqual(child, values[++n]) | ||
|
||
if (child === 1) { | ||
parent.children.splice(index + 1, 0, 2); | ||
return index + 1; | ||
parent.children.splice(index + 1, 0, 2) | ||
return index + 1 | ||
} | ||
})(context); | ||
})(context) | ||
|
||
st.deepEqual(context.children, values); | ||
st.deepEqual(context.children, values) | ||
|
||
st.end(); | ||
}); | ||
st.end() | ||
}) | ||
|
||
t.test('should skip backwards', function (st) { | ||
var invocations = [0, 1, -1, 0, 1, 2, 3]; | ||
var n = -1; | ||
var context = {}; | ||
var inserted; | ||
t.test('should skip backwards', function(st) { | ||
var invocations = [0, 1, -1, 0, 1, 2, 3] | ||
var n = -1 | ||
var context = {} | ||
var inserted | ||
|
||
context.children = [0, 1, 2, 3]; | ||
context.children = [0, 1, 2, 3] | ||
|
||
modifyChildren(function (child, index, parent) { | ||
st.strictEqual(child, invocations[++n]); | ||
modifyChildren(function(child, index, parent) { | ||
st.strictEqual(child, invocations[++n]) | ||
|
||
if (!inserted && child === 1) { | ||
inserted = true; | ||
parent.children.unshift(-1); | ||
return -1; | ||
inserted = true | ||
parent.children.unshift(-1) | ||
return -1 | ||
} | ||
})(context); | ||
})(context) | ||
|
||
st.deepEqual(context.children, [-1, 0, 1, 2, 3]); | ||
st.deepEqual(context.children, [-1, 0, 1, 2, 3]) | ||
|
||
st.end(); | ||
}); | ||
st.end() | ||
}) | ||
|
||
t.end(); | ||
}); | ||
t.end() | ||
}) |