From f9d865ebef9f1ab1d31e84945d844abc6fdbfc14 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 May 2018 11:38:24 +0200 Subject: [PATCH] Refactor code-style --- .prettierignore | 3 + index.js | 26 +++---- package.json | 20 ++++-- readme.md | 12 ++-- test.js | 183 ++++++++++++++++++++++-------------------------- 5 files changed, 118 insertions(+), 126 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..8c91667 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +coverage/ +unist-util-find-before.js +unist-util-find-before.min.js diff --git a/index.js b/index.js index ea750d6..e24298c 100644 --- a/index.js +++ b/index.js @@ -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 } diff --git a/package.json b/package.json index edbfc44..c37cc0d 100644 --- a/package.json +++ b/package.json @@ -26,22 +26,22 @@ "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, @@ -49,8 +49,16 @@ "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", diff --git a/readme.md b/readme.md index 904098c..cc79a85 100644 --- a/readme.md +++ b/readme.md @@ -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: diff --git a/test.js b/test.js index 19843fc..1239e3e 100644 --- a/test.js +++ b/test.js @@ -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() +})