From f40c70fc63ea0007696e7b034e07db9c9fdc87ae Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 May 2018 13:05:46 +0200 Subject: [PATCH] Refactor code-style --- .prettierignore | 3 +++ index.js | 37 +++++++++++++++------------------ package.json | 21 ++++++++++++------- readme.md | 14 ++++++------- test/index.js | 55 ++++++++++++++++++++++++++----------------------- 5 files changed, 70 insertions(+), 60 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..1e79753 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +coverage/ +nlcst-affix-emoticon-modifier.js +nlcst-affix-emoticon-modifier.min.js diff --git a/index.js b/index.js index d3be9f4..7357b9a 100644 --- a/index.js +++ b/index.js @@ -1,44 +1,41 @@ -'use strict'; +'use strict' -var modifier = require('unist-util-modify-children'); +var modifier = require('unist-util-modify-children') -module.exports = modifier(mergeAffixEmoticon); +module.exports = modifier(mergeAffixEmoticon) -var EMOTICON_NODE = 'EmoticonNode'; +var EMOTICON_NODE = 'EmoticonNode' /* Merge emoticons into an `EmoticonNode`. */ function mergeAffixEmoticon(child, index, parent) { - var children = child.children; - var position; - var node; - var prev; + var children = child.children + var position + var node + var prev if (children && children.length !== 0 && index !== 0) { - position = -1; + position = -1 while (children[++position]) { - node = children[position]; + node = children[position] if (node.type === EMOTICON_NODE) { - prev = parent.children[index - 1]; + prev = parent.children[index - 1] - prev.children = prev.children.concat( - children.slice(0, position + 1) - ); - - child.children = children.slice(position + 1); + prev.children = prev.children.concat(children.slice(0, position + 1)) + child.children = children.slice(position + 1) if (node.position && child.position && prev.position) { - prev.position.end = node.position.end; - child.position.start = node.position.end; + prev.position.end = node.position.end + child.position.start = node.position.end } /* Next, iterate over the node again. */ - return index; + return index } if (node.type !== 'WhiteSpaceNode') { - break; + break } } } diff --git a/package.json b/package.json index 5ff512b..7de0ae0 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "nlcst-emoji-modifier": "^2.0.1", "nlcst-emoticon-modifier": "^1.0.0", "nyc": "^11.0.0", + "prettier": "^1.12.1", "remark-cli": "^5.0.0", "remark-preset-wooorm": "^4.0.0", "retext-english": "^3.0.0", @@ -35,14 +36,13 @@ "xo": "^0.20.0" }, "scripts": { - "build-md": "remark . --quiet --frail", + "format": "remark . -qfo && prettier --write '**/*.js' && xo --fix", "build-bundle": "browserify index.js --bare -s nlcstAffixEmoticonModifier > nlcst-affix-emoticon-modifier.js", "build-mangle": "esmangle < nlcst-affix-emoticon-modifier.js > nlcst-affix-emoticon-modifier.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/index.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, @@ -50,14 +50,21 @@ "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", "no-var": "off", - "prefer-arrow-callback": "off", - "guard-for-in": "off" + "prefer-arrow-callback": "off" }, "ignore": [ "nlcst-affix-emoticon-modifier.js" diff --git a/readme.md b/readme.md index d8d2804..4c30be4 100644 --- a/readme.md +++ b/readme.md @@ -18,15 +18,15 @@ npm install nlcst-affix-emoticon-modifier ## Usage ```js -var affixEmoticon = require('nlcst-affix-emoticon-modifier'); -var emoticon = require('nlcst-emoticon-modifier'); -var inspect = require('unist-util-inspect'); -var english = require('parse-english')(); +var affixEmoticon = require('nlcst-affix-emoticon-modifier') +var emoticon = require('nlcst-emoticon-modifier') +var inspect = require('unist-util-inspect') +var english = require('parse-english')() -english.useFirst('tokenizeSentence', emoticon); -english.useFirst('tokenizeParagraph', affixEmoticon); +english.useFirst('tokenizeSentence', emoticon) +english.useFirst('tokenizeParagraph', affixEmoticon) -console.log(inspect(english.parse('Hey. :) How is it going?'))); +console.log(inspect(english.parse('Hey. :) How is it going?'))) ``` Yields: diff --git a/test/index.js b/test/index.js index 137ed7d..64832ba 100644 --- a/test/index.js +++ b/test/index.js @@ -1,66 +1,69 @@ -'use strict'; +'use strict' -var test = require('tape'); -var unified = require('unified'); -var english = require('retext-english'); -var emojiModifier = require('nlcst-emoji-modifier'); -var emoticonModifier = require('nlcst-emoticon-modifier'); -var remove = require('unist-util-remove-position'); -var modifier = require('..'); +var test = require('tape') +var unified = require('unified') +var english = require('retext-english') +var emojiModifier = require('nlcst-emoji-modifier') +var emoticonModifier = require('nlcst-emoticon-modifier') +var remove = require('unist-util-remove-position') +var modifier = require('..') -var lollipop = require('./fixtures/lollipop'); -var smile = require('./fixtures/smile'); +var lollipop = require('./fixtures/lollipop') +var smile = require('./fixtures/smile') -test('nlcst-affix-emoticon-modifier()', function (t) { +test('nlcst-affix-emoticon-modifier()', function(t) { t.throws( - function () { - modifier({}); + function() { + modifier({}) }, /Missing children in `parent`/, 'should throw when not given a parent' - ); + ) t.deepEqual( process('Lol! :lollipop: That’s cool.'), lollipop, 'should merge at sentence-start (1)' - ); + ) t.deepEqual( process('Lol! :lollipop: That’s cool.', true), remove(lollipop, true), 'should merge at sentence-start (1, positionless)' - ); + ) t.deepEqual( process('Lol! :) That’s cool.'), smile, 'should merge at sentence-start (2)' - ); + ) t.deepEqual( process('Lol! :) That’s cool.', true), remove(smile, true), 'should merge at sentence-start (2, positionless)' - ); + ) - t.end(); -}); + t.end() +}) /* Short-cut to access the CST. */ function process(fixture, positionless) { - var processor = unified().use(english).use(plugin).freeze(); + var processor = unified() + .use(english) + .use(plugin) + .freeze() if (positionless) { - processor.Parser.prototype.position = false; + processor.Parser.prototype.position = false } - return processor.runSync(processor.parse(fixture)); + return processor.runSync(processor.parse(fixture)) } /* Add modifier to processor. */ function plugin() { - this.Parser.prototype.useFirst('tokenizeSentence', emojiModifier); - this.Parser.prototype.useFirst('tokenizeSentence', emoticonModifier); - this.Parser.prototype.useFirst('tokenizeParagraph', modifier); + this.Parser.prototype.useFirst('tokenizeSentence', emojiModifier) + this.Parser.prototype.useFirst('tokenizeSentence', emoticonModifier) + this.Parser.prototype.useFirst('tokenizeParagraph', modifier) }