nlcst-emoji-modifier
Advanced tools
Comparing version 2.0.1 to 2.0.2
282
index.js
@@ -1,12 +0,12 @@ | ||
'use strict'; | ||
'use strict' | ||
var toString = require('nlcst-to-string'); | ||
var modifier = require('unist-util-modify-children'); | ||
var gemoji = require('gemoji'); | ||
var toString = require('nlcst-to-string') | ||
var modifier = require('unist-util-modify-children') | ||
var gemoji = require('gemoji') | ||
module.exports = modifier(mergeEmoji); | ||
module.exports = modifier(mergeEmoji) | ||
var own = {}.hasOwnProperty; | ||
var own = {}.hasOwnProperty | ||
var EMOTICON_NODE = 'EmoticonNode'; | ||
var EMOTICON_NODE = 'EmoticonNode' | ||
@@ -23,11 +23,11 @@ /* Magic numbers. | ||
* back more than 4 times. */ | ||
var MAX_GEMOJI_PART_COUNT = 12; | ||
var MAX_GEMOJI_PART_COUNT = 12 | ||
var shortcodes = []; | ||
var unicodes = gemoji.unicode; | ||
var byName = gemoji.name; | ||
var key; | ||
var shortcodes = [] | ||
var unicodes = gemoji.unicode | ||
var byName = gemoji.name | ||
var key | ||
for (key in byName) { | ||
shortcodes.push(':' + key + ':'); | ||
shortcodes.push(':' + key + ':') | ||
} | ||
@@ -38,24 +38,24 @@ | ||
function mergeEmoji(child, index, parent) { | ||
var siblings = parent.children; | ||
var value = toString(child); | ||
var siblingIndex; | ||
var node; | ||
var nodes; | ||
var subvalue; | ||
var left; | ||
var right; | ||
var leftMatch; | ||
var rightMatch; | ||
var start; | ||
var pos; | ||
var end; | ||
var replace; | ||
var startIndex; | ||
var nextSibling; | ||
var nextNextSibling; | ||
var possibleEmoji; | ||
var maxSiblingIndex; | ||
var loopIndex; | ||
var lastSibling; | ||
var lastSiblingIndex; | ||
var siblings = parent.children | ||
var value = toString(child) | ||
var siblingIndex | ||
var node | ||
var nodes | ||
var subvalue | ||
var left | ||
var right | ||
var leftMatch | ||
var rightMatch | ||
var start | ||
var pos | ||
var end | ||
var replace | ||
var startIndex | ||
var nextSibling | ||
var nextNextSibling | ||
var possibleEmoji | ||
var maxSiblingIndex | ||
var loopIndex | ||
var lastSibling | ||
var lastSiblingIndex | ||
@@ -66,33 +66,33 @@ if (child.type === 'WordNode') { | ||
if (own.call(unicodes, value)) { | ||
node = {type: EMOTICON_NODE, value: value}; | ||
node = {type: EMOTICON_NODE, value: value} | ||
if (child.position) { | ||
node.position = child.position; | ||
node.position = child.position | ||
} | ||
siblings[index] = node; | ||
siblings[index] = node | ||
} else { | ||
/* ❤️ — Sometimes a unicode emoji is split in two. Remove the last | ||
* and add its value to the first. */ | ||
node = siblings[index - 1]; | ||
node = siblings[index - 1] | ||
if (node && own.call(unicodes, toString(node) + value)) { | ||
node.type = EMOTICON_NODE; | ||
node.value = toString(node) + value; | ||
node.type = EMOTICON_NODE | ||
node.value = toString(node) + value | ||
if (child.position && node.position) { | ||
node.position.end = child.position.end; | ||
node.position.end = child.position.end | ||
} | ||
siblings.splice(index, 1); | ||
siblings.splice(index, 1) | ||
return index; | ||
return index | ||
} | ||
} | ||
} else if (own.call(unicodes, value)) { | ||
child.type = EMOTICON_NODE; | ||
startIndex = index + 1; | ||
nextSibling = siblings[startIndex]; | ||
child.type = EMOTICON_NODE | ||
startIndex = index + 1 | ||
nextSibling = siblings[startIndex] | ||
if (!nextSibling) { | ||
return; | ||
return | ||
} | ||
@@ -103,22 +103,22 @@ | ||
if (!isVarianceSelector(nextSibling)) { | ||
return; | ||
return | ||
} | ||
possibleEmoji = value + toString(nextSibling); | ||
maxSiblingIndex = siblings.length; | ||
loopIndex = startIndex + 1; | ||
possibleEmoji = value + toString(nextSibling) | ||
maxSiblingIndex = siblings.length | ||
loopIndex = startIndex + 1 | ||
while ( | ||
loopIndex < maxSiblingIndex && | ||
(loopIndex - startIndex) < 5 && | ||
loopIndex - startIndex < 5 && | ||
siblings[loopIndex].type !== 'WordNode' | ||
) { | ||
possibleEmoji += toString(siblings[loopIndex]); | ||
loopIndex++; | ||
possibleEmoji += toString(siblings[loopIndex]) | ||
loopIndex++ | ||
} | ||
lastSibling = siblings[loopIndex]; | ||
lastSibling = siblings[loopIndex] | ||
if (lastSibling && lastSibling.type === 'WordNode') { | ||
possibleEmoji += toString(lastSibling); | ||
possibleEmoji += toString(lastSibling) | ||
} | ||
@@ -128,49 +128,49 @@ | ||
if (own.call(unicodes, possibleEmoji)) { | ||
child.value = possibleEmoji; | ||
child.value = possibleEmoji | ||
if (child.position && lastSibling.position) { | ||
child.position.end = lastSibling.position.end; | ||
child.position.end = lastSibling.position.end | ||
} | ||
siblings.splice(index + 1, loopIndex - index); | ||
siblings.splice(index + 1, loopIndex - index) | ||
return index + 1; | ||
return index + 1 | ||
} | ||
/* 👨❤️💋👨 — combined emoji. */ | ||
/* 👨❤️💋👨 — combined emoji. */ | ||
} else if (nextSibling.type === 'SymbolNode') { | ||
possibleEmoji = value + toString(nextSibling); | ||
maxSiblingIndex = siblings.length; | ||
loopIndex = startIndex + 1; | ||
possibleEmoji = value + toString(nextSibling) | ||
maxSiblingIndex = siblings.length | ||
loopIndex = startIndex + 1 | ||
while ( | ||
loopIndex < maxSiblingIndex && (loopIndex - startIndex) < 5 && | ||
( | ||
siblings[loopIndex].type === 'SymbolNode' || | ||
(siblings[loopIndex].type === 'WordNode' && isVarianceSelector(siblings[loopIndex])) | ||
) | ||
loopIndex < maxSiblingIndex && | ||
loopIndex - startIndex < 5 && | ||
(siblings[loopIndex].type === 'SymbolNode' || | ||
(siblings[loopIndex].type === 'WordNode' && | ||
isVarianceSelector(siblings[loopIndex]))) | ||
) { | ||
possibleEmoji += toString(siblings[loopIndex]); | ||
loopIndex++; | ||
possibleEmoji += toString(siblings[loopIndex]) | ||
loopIndex++ | ||
} | ||
if (own.call(unicodes, possibleEmoji)) { | ||
child.value = possibleEmoji; | ||
lastSiblingIndex = loopIndex - 1; | ||
lastSibling = siblings[lastSiblingIndex]; | ||
child.value = possibleEmoji | ||
lastSiblingIndex = loopIndex - 1 | ||
lastSibling = siblings[lastSiblingIndex] | ||
if (child.position && lastSibling.position) { | ||
child.position.end = lastSibling.position.end; | ||
child.position.end = lastSibling.position.end | ||
} | ||
siblings.splice(index + 1, lastSiblingIndex - index); | ||
siblings.splice(index + 1, lastSiblingIndex - index) | ||
return index + 1; | ||
return index + 1 | ||
} | ||
} | ||
/* 🤽♀ — Combined emoji starting in a symbol. */ | ||
/* 🤽♀ — Combined emoji starting in a symbol. */ | ||
} else if (child.type === 'SymbolNode') { | ||
nextSibling = siblings[index + 1]; | ||
nextNextSibling = siblings[index + 2]; | ||
nextSibling = siblings[index + 1] | ||
nextNextSibling = siblings[index + 2] | ||
if (!nextSibling || !nextNextSibling) { | ||
return; | ||
return | ||
} | ||
@@ -180,34 +180,35 @@ | ||
(nextSibling.type === 'SymbolNode' || nextSibling.type === 'WordNode') && | ||
nextNextSibling && nextNextSibling.type === 'SymbolNode' | ||
nextNextSibling && | ||
nextNextSibling.type === 'SymbolNode' | ||
) { | ||
possibleEmoji = value + toString(nextSibling) + toString(nextNextSibling); | ||
possibleEmoji = value + toString(nextSibling) + toString(nextNextSibling) | ||
if (own.call(unicodes, possibleEmoji)) { | ||
child.type = EMOTICON_NODE; | ||
child.value = possibleEmoji; | ||
child.type = EMOTICON_NODE | ||
child.value = possibleEmoji | ||
if (child.position && nextNextSibling.position) { | ||
child.position.end = nextNextSibling.position.end; | ||
child.position.end = nextNextSibling.position.end | ||
} | ||
siblings.splice(index + 1, 2); | ||
siblings.splice(index + 1, 2) | ||
return index + 1; | ||
return index + 1 | ||
} | ||
} | ||
/* :+1: — Gemoji shortcodes. */ | ||
/* :+1: — Gemoji shortcodes. */ | ||
} else if (value.charAt(0) === ':') { | ||
nodes = []; | ||
siblingIndex = index; | ||
subvalue = value; | ||
left = null; | ||
right = null; | ||
leftMatch = null; | ||
rightMatch = null; | ||
nodes = [] | ||
siblingIndex = index | ||
subvalue = value | ||
left = null | ||
right = null | ||
leftMatch = null | ||
rightMatch = null | ||
if (subvalue.length === 1) { | ||
rightMatch = child; | ||
rightMatch = child | ||
} else { | ||
end = child.position && child.position.end; | ||
start = end && child.position.start; | ||
end = child.position && child.position.end | ||
start = end && child.position.start | ||
pos = end && { | ||
@@ -217,3 +218,3 @@ line: start.line, | ||
offset: start.offset + 1 | ||
}; | ||
} | ||
@@ -223,3 +224,3 @@ rightMatch = { | ||
value: ':' | ||
}; | ||
} | ||
@@ -229,7 +230,7 @@ right = { | ||
value: subvalue.slice(1) | ||
}; | ||
} | ||
if (end) { | ||
rightMatch.position = {start: start, end: pos}; | ||
right.position = {start: pos, end: end}; | ||
rightMatch.position = {start: start, end: pos} | ||
right.position = {start: pos, end: end} | ||
} | ||
@@ -239,23 +240,23 @@ } | ||
while (siblingIndex--) { | ||
if ((index - siblingIndex) > MAX_GEMOJI_PART_COUNT) { | ||
return; | ||
if (index - siblingIndex > MAX_GEMOJI_PART_COUNT) { | ||
return | ||
} | ||
node = siblings[siblingIndex]; | ||
node = siblings[siblingIndex] | ||
subvalue = toString(node); | ||
subvalue = toString(node) | ||
if (subvalue.charAt(subvalue.length - 1) === ':') { | ||
leftMatch = node; | ||
break; | ||
leftMatch = node | ||
break | ||
} | ||
if (node.children) { | ||
nodes = nodes.concat(node.children.concat().reverse()); | ||
nodes = nodes.concat(node.children.concat().reverse()) | ||
} else { | ||
nodes.push(node); | ||
nodes.push(node) | ||
} | ||
if (siblingIndex === 0) { | ||
return; | ||
return | ||
} | ||
@@ -265,10 +266,10 @@ } | ||
if (!leftMatch) { | ||
return; | ||
return | ||
} | ||
subvalue = toString(leftMatch); | ||
subvalue = toString(leftMatch) | ||
if (subvalue.length !== 1) { | ||
end = leftMatch.position && leftMatch.position.end; | ||
start = end && leftMatch.position.start; | ||
end = leftMatch.position && leftMatch.position.end | ||
start = end && leftMatch.position.start | ||
pos = end && { | ||
@@ -278,3 +279,3 @@ line: end.line, | ||
offset: end.offset - 1 | ||
}; | ||
} | ||
@@ -284,3 +285,3 @@ left = { | ||
value: subvalue.slice(0, -1) | ||
}; | ||
} | ||
@@ -290,48 +291,45 @@ leftMatch = { | ||
value: ':' | ||
}; | ||
} | ||
if (end) { | ||
left.position = {start: start, end: pos}; | ||
leftMatch.position = {start: pos, end: end}; | ||
left.position = {start: start, end: pos} | ||
leftMatch.position = {start: pos, end: end} | ||
} | ||
} | ||
nodes.push(leftMatch); | ||
nodes.reverse().push(rightMatch); | ||
nodes.push(leftMatch) | ||
nodes.reverse().push(rightMatch) | ||
value = toString(nodes); | ||
value = toString(nodes) | ||
if (shortcodes.indexOf(value) === -1) { | ||
return; | ||
return | ||
} | ||
replace = [ | ||
siblingIndex, | ||
index - siblingIndex + 1 | ||
]; | ||
replace = [siblingIndex, index - siblingIndex + 1] | ||
if (left) { | ||
replace.push(left); | ||
replace.push(left) | ||
} | ||
child.type = EMOTICON_NODE; | ||
child.value = value; | ||
child.type = EMOTICON_NODE | ||
child.value = value | ||
if (child.position && leftMatch.position) { | ||
child.position.start = leftMatch.position.start; | ||
child.position.start = leftMatch.position.start | ||
} | ||
if (child.position && rightMatch.position) { | ||
child.position.end = rightMatch.position.end; | ||
child.position.end = rightMatch.position.end | ||
} | ||
replace.push(child); | ||
replace.push(child) | ||
if (right) { | ||
replace.push(right); | ||
replace.push(right) | ||
} | ||
[].splice.apply(siblings, replace); | ||
;[].splice.apply(siblings, replace) | ||
return siblingIndex + 3; | ||
return siblingIndex + 3 | ||
} | ||
@@ -341,4 +339,4 @@ } | ||
function isVarianceSelector(node) { | ||
var code = toString(node).charCodeAt(0); | ||
return code > 65023 && code < 65040; | ||
var code = toString(node).charCodeAt(0) | ||
return code > 65023 && code < 65040 | ||
} |
{ | ||
"name": "nlcst-emoji-modifier", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Emoji in NLCST", | ||
@@ -27,3 +27,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"browserify": "^14.0.0", | ||
"browserify": "^16.0.0", | ||
"esmangle": "^1.0.0", | ||
@@ -34,17 +34,17 @@ "is-hidden": "^1.1.0", | ||
"parse-english": "^4.1.0", | ||
"remark-cli": "^4.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"prettier": "^1.12.1", | ||
"remark-cli": "^5.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.6.2", | ||
"unist-util-remove-position": "^1.1.0", | ||
"xo": "^0.18.2" | ||
"xo": "^0.20.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . --quiet --frail --output", | ||
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix", | ||
"build-bundle": "browserify index.js --bare -s nlcstEmojiModifier > nlcst-emoji-modifier.js", | ||
"build-mangle": "esmangle < nlcst-emoji-modifier.js > nlcst-emoji-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" | ||
}, | ||
@@ -57,8 +57,19 @@ "nyc": { | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"complexity": "off", | ||
"guard-for-in": "off" | ||
"guard-for-in": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off", | ||
"object-shorthand": "off", | ||
"complexity": "off" | ||
}, | ||
@@ -65,0 +76,0 @@ "ignore": [ |
@@ -19,9 +19,9 @@ # nlcst-emoji-modifier [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
```javascript | ||
var modifier = require('nlcst-emoji-modifier'); | ||
var inspect = require('unist-util-inspect'); | ||
var english = require('parse-english')(); | ||
var modifier = require('nlcst-emoji-modifier') | ||
var inspect = require('unist-util-inspect') | ||
var english = require('parse-english')() | ||
english.useFirst('tokenizeSentence', modifier); | ||
english.useFirst('tokenizeSentence', modifier) | ||
console.log(inspect(english.parse('It\'s raining :cat:s and :dog:s.'))); | ||
console.log(inspect(english.parse('It’s raining :cat:s and :dog:s.'))) | ||
``` | ||
@@ -37,3 +37,3 @@ | ||
│ ├─ TextNode: "It" | ||
│ ├─ PunctuationNode: "'" | ||
│ ├─ PunctuationNode: "’" | ||
│ └─ TextNode: "s" | ||
@@ -67,2 +67,10 @@ ├─ WhiteSpaceNode: " " | ||
## Contribute | ||
See [`contributing.md` in `syntax-tree/nlcst`][contributing] for ways to get | ||
started. | ||
This organisation has a [Code of Conduct][coc]. By interacting with this | ||
repository, organisation, or community you agree to abide by its terms. | ||
## License | ||
@@ -91,1 +99,5 @@ | ||
[paragraph]: https://github.com/syntax-tree/nlcst#paragraph | ||
[contributing]: https://github.com/syntax-tree/nlcst/blob/master/contributing.md | ||
[coc]: https://github.com/syntax-tree/nlcst/blob/master/code-of-conduct.md |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13707
100
12
264
1