Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 2, 2018
1 parent 8b24c8e commit f40c70f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
nlcst-affix-emoticon-modifier.js
nlcst-affix-emoticon-modifier.min.js
37 changes: 17 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Expand Down
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,29 +36,35 @@
"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,
"lines": 100,
"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"
Expand Down
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
55 changes: 29 additions & 26 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit f40c70f

Please sign in to comment.