Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 30, 2018
1 parent dc36f6c commit b45057b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 88 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-modify-children.js
unist-util-modify-children.min.js
20 changes: 10 additions & 10 deletions index.js
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)
}
}
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,31 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.0",
"nyc": "^11.0.0",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.6.2",
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s unistUtilModifyChildren > unist-util-modify-children.js",
"build-mangle": "esmangle < unist-util-modify-children.js > unist-util-modify-children.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"
},
"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
18 changes: 10 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ npm install unist-util-modify-children
## Usage

```javascript
var remark = require('remark');
var modifyChildren = require('unist-util-modify-children');
var remark = require('remark')
var modifyChildren = require('unist-util-modify-children')

var doc = remark().use(plugin).process('This _and_ that');
var doc = remark()
.use(plugin)
.process('This _and_ that')

console.log(String(doc));
console.log(String(doc))

function plugin() {
return transformer;
return transformer
function transformer(tree) {
modifyChildren(modifier)(tree.children[0]);
modifyChildren(modifier)(tree.children[0])
}
}

function modifier(node, index, parent) {
if (node.type === 'emphasis') {
parent.children.splice(index, 1, {type: 'strong', children: node.children});
return index + 1;
parent.children.splice(index, 1, {type: 'strong', children: node.children})
return index + 1
}
}
```
Expand Down
130 changes: 65 additions & 65 deletions test.js
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()
})

0 comments on commit b45057b

Please # to comment.