Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 6, 2018
1 parent d2cdf37 commit 25486f3
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 161 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
retext-emoji.js
retext-emoji.min.js
158 changes: 80 additions & 78 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,126 +1,128 @@
'use strict';
'use strict'

var affixEmoticonModifier = require('nlcst-affix-emoticon-modifier');
var emoticonModifier = require('nlcst-emoticon-modifier');
var emojiModifier = require('nlcst-emoji-modifier');
var emoticons = require('emoticon');
var toString = require('nlcst-to-string');
var gemoji = require('gemoji');
var visit = require('unist-util-visit');
var affixEmoticonModifier = require('nlcst-affix-emoticon-modifier')
var emoticonModifier = require('nlcst-emoticon-modifier')
var emojiModifier = require('nlcst-emoji-modifier')
var emoticons = require('emoticon')
var toString = require('nlcst-to-string')
var gemoji = require('gemoji')
var visit = require('unist-util-visit')

module.exports = emoji;
module.exports = emoji

var EMOTICON_NODE = 'EmoticonNode';
var type = 'EmoticonNode'

/* Map of visitors. */
// Map of visitors.
var fns = {
encode: toEmoji,
decode: toGemoji
};

var unicodes = gemoji.unicode;
var names = gemoji.name;

var shortcodes = {};

(function () {
var key;
var shortcode;
var result = {};
var length = emoticons.length;
var index = -1;
var count;
var offset;
var subset;
var name;

for (key in names) {
shortcode = ':' + key + ':';
shortcodes[shortcode] = names[key];
shortcodes[shortcode].shortcode = shortcode;
}
}

while (++index < length) {
name = emoticons[index].name;
subset = emoticons[index].emoticons;
count = subset.length;
offset = -1;
var unicodes = gemoji.unicode
var names = gemoji.name

while (++offset < count) {
result[subset[offset]] = names[name];
}
}
var shortcodes = {}

emoticons = result;
})();
init()

/* Attacher. */
function emoji(options) {
var Parser = this.Parser;
var proto = Parser.prototype;
var convert = (options || {}).convert;
var fn;
var Parser = this.Parser
var proto = Parser.prototype
var convert = (options || {}).convert
var fn

proto.useFirst('tokenizeSentence', emoticonModifier);
proto.useFirst('tokenizeSentence', emojiModifier);
proto.useFirst('tokenizeParagraph', affixEmoticonModifier);
proto.useFirst('tokenizeSentence', emoticonModifier)
proto.useFirst('tokenizeSentence', emojiModifier)
proto.useFirst('tokenizeParagraph', affixEmoticonModifier)

if (convert !== null && convert !== undefined) {
fn = fns[convert];
fn = fns[convert]

if (!fn) {
throw new TypeError(
'Illegal invocation: `' + convert +
'` is not a valid value for ' +
'`options.convert` in `retext#use(emoji, options)`'
);
'Illegal invocation: `' +
convert +
'` is not a valid value for ' +
'`options.convert` in `retext#use(emoji, options)`'
)
}
}

return transformer;
return transformer

function transformer(node) {
visit(node, EMOTICON_NODE, visitor);
visit(node, type, visitor)
}

function visitor(node) {
var data = node.data;
var value = toString(node);
var info;
var data = node.data
var value = toString(node)
var info

if (fn) {
fn(node);
fn(node)
}

info = unicodes[value] || shortcodes[value] || emoticons[value];
info = unicodes[value] || shortcodes[value] || emoticons[value]

if (!data) {
data = {};
node.data = data;
data = {}
node.data = data
}

data.names = info.names.concat();
data.description = info.description;
data.tags = info.tags.concat();
data.names = info.names.concat()
data.description = info.description
data.tags = info.tags.concat()
}
}

/* Replace a unicode emoji with a short-code. */
// Replace a unicode emoji with a short-code.
function toGemoji(node) {
var value = toString(node);
var info = (unicodes[value] || emoticons[value] || {}).shortcode;
var value = toString(node)
var info = (unicodes[value] || emoticons[value] || {}).shortcode

if (info) {
node.value = info;
node.value = info
}
}

/* Replace a short-code with a unicode emoji. */
// Replace a short-code with a unicode emoji.
function toEmoji(node) {
var value = toString(node);
var info = (shortcodes[value] || emoticons[value] || {}).emoji;
var value = toString(node)
var info = (shortcodes[value] || emoticons[value] || {}).emoji

if (info) {
node.value = info;
node.value = info
}
}

function init() {
var key
var shortcode
var result = {}
var length = emoticons.length
var index = -1
var count
var offset
var subset
var name

for (key in names) {
shortcode = ':' + key + ':'
shortcodes[shortcode] = names[key]
shortcodes[shortcode].shortcode = shortcode
}

while (++index < length) {
name = emoticons[index].name
subset = emoticons[index].emoticons
count = subset.length
offset = -1

while (++offset < count) {
result[subset[offset]] = names[name]
}
}

emoticons = result
}
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,38 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.1",
"nyc": "^13.0.0",
"prettier": "^1.14.3",
"remark-cli": "^6.0.0",
"remark-preset-wooorm": "^4.0.0",
"retext": "^5.0.0",
"tape": "^4.0.0",
"xo": "^0.23.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"build-bundle": "browserify index.js --bare -s retextEmoji > retext-emoji.js",
"build-mangle": "esmangle retext-emoji.js > retext-emoji.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"
Expand Down
23 changes: 12 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ npm install retext-emoji
## Usage

```javascript
var retext = require('retext');
var emoji = require('retext-emoji');
var retext = require('retext')
var emoji = require('retext-emoji')

var file = retext()
.use(emoji, {convert: 'encode'})
.processSync('I’m going to bed. :zzz:');
.processSync('I’m going to bed. :zzz:')

console.log(String(file));
console.log(String(file))
```

Yields:
Expand Down Expand Up @@ -51,21 +51,22 @@ equivalent (`❤️` and `<3` to `:heart:`).

### `EmoticonNode`

`retext-emoji` adds a new node to [NLCST][]: `Emoticon` ([Symbol][]).
`retext-emoji` adds a new node to [nlcst][]:
**Emoticon** ([**Literal**][literal]).

Whether emoji (`❤️`), emoticon (`<3`), or gemoji (`:heart:`), all are
classified as `EmoticonNode`s.

```idl
interface Emoticon < Symbol {
type: "EmoticonNode";
data: EmoticonData;
type: "EmoticonNode"
data: EmoticonData
}
interface EmoticonData {
names: [string];
description: string | null;
tags: [string];
names: [string][]
description: string?
tags: [string]
}
```

Expand Down Expand Up @@ -114,7 +115,7 @@ repository, organisation, or community you agree to abide by its terms.

[nlcst]: https://github.com/syntax-tree/nlcst

[symbol]: https://github.com/syntax-tree/nlcst#symbol
[literal]: https://github.com/syntax-tree/nlcst#literal

[gemoji]: https://github.com/wooorm/gemoji/#supported-gemoji

Expand Down
Loading

0 comments on commit 25486f3

Please sign in to comment.