nlcst-emoji-modifier
Advanced tools
Comparing version 5.0.0 to 5.1.0
/** | ||
* Merge emoji (π) and Gemoji (GitHub emoji, :+1:). | ||
* | ||
* @param {Parent} node | ||
* @param {Sentence} node | ||
*/ | ||
export function emojiModifier(node: Parent): import('unist').Parent | ||
export type Node = import('unist').Node | ||
export type Parent = import('unist').Parent | ||
export function emojiModifier(node: Sentence): import('nlcst').Sentence | ||
export type UnistParent = import('unist').Parent | ||
export type Point = import('unist').Point | ||
export type Sentence = import('nlcst').Sentence | ||
export type ParagraphContent = import('nlcst').ParagraphContent | ||
export type SentenceContent = import('nlcst').SentenceContent | ||
export type WordContent = import('nlcst').WordContent | ||
export type DeepSentenceContent = SentenceContent | WordContent | ||
export type DeepSentenceContentParent = Extract< | ||
ParagraphContent | DeepSentenceContent, | ||
UnistParent | ||
> | ||
export type DeepSentenceContentLeaf = Exclude<DeepSentenceContent, UnistParent> | ||
export type Word = import('nlcst').Word | ||
export type Emoticon = import('./complex-types').Emoticon | ||
export type FindMatch = { | ||
@@ -14,5 +25,1 @@ start: number | ||
} | ||
export type ChangeResult = { | ||
nodes: Array<Node> | ||
end: number | ||
} |
183
index.js
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('unist').Parent} UnistParent | ||
* @typedef {import('unist').Point} Point | ||
* @typedef {import('nlcst').Sentence} Sentence | ||
* @typedef {import('nlcst').ParagraphContent} ParagraphContent | ||
* @typedef {import('nlcst').SentenceContent} SentenceContent | ||
* @typedef {import('nlcst').WordContent} WordContent | ||
* @typedef {SentenceContent|WordContent} DeepSentenceContent | ||
* @typedef {Extract<ParagraphContent|DeepSentenceContent, UnistParent>} DeepSentenceContentParent | ||
* @typedef {Exclude<DeepSentenceContent, UnistParent>} DeepSentenceContentLeaf | ||
* @typedef {import('nlcst').Word} Word | ||
* | ||
* @typedef {import('./complex-types').Emoticon} Emoticon | ||
* | ||
* @typedef {Object} FindMatch | ||
* @property {number} start | ||
* @property {number} end | ||
* | ||
* @typedef {Object} ChangeResult | ||
* @property {Array.<Node>} nodes | ||
* @property {number} end | ||
*/ | ||
@@ -22,4 +27,3 @@ | ||
var own = {}.hasOwnProperty | ||
var push = [].push | ||
const own = {}.hasOwnProperty | ||
@@ -29,3 +33,3 @@ /** | ||
* | ||
* @param {Parent} node | ||
* @param {Sentence} node | ||
*/ | ||
@@ -37,5 +41,9 @@ export function emojiModifier(node) { | ||
// @ts-expect-error: assume content model matches (no sentences in sentences). | ||
node.children = changeParent(node, findEmoji(node), 0).nodes | ||
visit(node, 'EmoticonNode', removeMatch) | ||
visit(node, 'EmoticonNode', (node) => { | ||
// @ts-expect-error: custom. | ||
delete node._match | ||
}) | ||
@@ -46,27 +54,24 @@ return node | ||
/** | ||
* @param {Parent} node | ||
* @param {Array.<FindMatch>} matches | ||
* @param {DeepSentenceContentParent} node | ||
* @param {FindMatch[]} matches | ||
* @param {number} start | ||
* @returns {{end: number, nodes: DeepSentenceContent[]}} | ||
*/ | ||
function changeParent(node, matches, start) { | ||
var children = node.children | ||
var end = start | ||
var index = -1 | ||
/** @type {Array.<Node>} */ | ||
var nodes = [] | ||
/** @type {Array.<Node>} */ | ||
var merged = [] | ||
/** @type {ChangeResult} */ | ||
var result | ||
/** @type {Node} */ | ||
var child | ||
/** @type {Node} */ | ||
var previous | ||
let end = start | ||
let index = -1 | ||
/** @type {DeepSentenceContent[]} */ | ||
const nodes = [] | ||
/** @type {DeepSentenceContent[]} */ | ||
const merged = [] | ||
/** @type {DeepSentenceContent|undefined} */ | ||
let previous | ||
while (++index < children.length) { | ||
result = children[index].children | ||
? // @ts-ignore Looks like a parent. | ||
changeParent(children[index], matches, end) | ||
: changeLeaf(children[index], matches, end) | ||
push.apply(nodes, result.nodes) | ||
while (++index < node.children.length) { | ||
const child = node.children[index] | ||
const result = | ||
'children' in child | ||
? changeParent(child, matches, end) | ||
: changeLeaf(child, matches, end) | ||
nodes.push(...result.nodes) | ||
end = result.end | ||
@@ -78,27 +83,36 @@ } | ||
while (++index < nodes.length) { | ||
if (nodes[index].type === 'EmoticonNode') { | ||
if (previous && previous._match === nodes[index]._match) { | ||
// @ts-ignore Both literals. | ||
previous.value += nodes[index].value | ||
const child = nodes[index] | ||
if (child.type === 'EmoticonNode') { | ||
if ( | ||
previous && | ||
previous.type === 'EmoticonNode' && | ||
// @ts-expect-error: custom. | ||
previous._match === child._match | ||
) { | ||
previous.value += child.value | ||
if (!generated(previous)) { | ||
previous.position.end = pointEnd(nodes[index]) | ||
// @ts-expect-error: defined. | ||
previous.position.end = pointEnd(child) | ||
} | ||
} else { | ||
previous = nodes[index] | ||
merged.push(nodes[index]) | ||
previous = child | ||
merged.push(child) | ||
} | ||
} else { | ||
previous = null | ||
previous = undefined | ||
if (node.type === 'WordNode') { | ||
child = {type: node.type, children: [nodes[index]]} | ||
/** @type {typeof node} */ | ||
// @ts-expect-error: assume content model matches (no words in words). | ||
const replacement = {type: node.type, children: [child]} | ||
if (!generated(nodes[index])) { | ||
child.position = position(nodes[index]) | ||
if (!generated(child)) { | ||
replacement.position = position(child) | ||
} | ||
merged.push(replacement) | ||
} else { | ||
merged.push(child) | ||
} else { | ||
merged.push(nodes[index]) | ||
} | ||
@@ -112,29 +126,24 @@ } | ||
/** | ||
* @param {Node} node | ||
* @param {Array.<FindMatch>} matches | ||
* @param {DeepSentenceContentLeaf} node | ||
* @param {FindMatch[]} matches | ||
* @param {number} start | ||
* @returns {ChangeResult} | ||
* @returns {{end: number, nodes: DeepSentenceContent[]}} | ||
*/ | ||
function changeLeaf(node, matches, start) { | ||
var value = toString(node) | ||
var point = generated(node) ? null : pointStart(node) | ||
var end = start + value.length | ||
var index = -1 | ||
var textEnd = 0 | ||
/** @type {Array.<Node>} */ | ||
var nodes = [] | ||
/** @type {number} */ | ||
var emojiEnd | ||
/** @type {Node} */ | ||
var child | ||
/** @type {FindMatch} */ | ||
var match | ||
const value = toString(node) | ||
const point = generated(node) ? undefined : pointStart(node) | ||
const end = start + value.length | ||
let index = -1 | ||
let textEnd = 0 | ||
/** @type {DeepSentenceContentLeaf[]} */ | ||
const nodes = [] | ||
while (++index < matches.length) { | ||
match = matches[index] | ||
emojiEnd = match.end - start + 1 | ||
const match = matches[index] | ||
let emojiEnd = match.end - start + 1 | ||
if (match.start - start < value.length && emojiEnd > 0) { | ||
if (match.start - start > textEnd) { | ||
child = { | ||
/** @type {typeof node} */ | ||
const child = { | ||
type: node.type, | ||
@@ -159,8 +168,11 @@ value: value.slice(textEnd, match.start - start) | ||
child = { | ||
/** @type {Emoticon} */ | ||
const child = { | ||
type: 'EmoticonNode', | ||
value: value.slice(textEnd, emojiEnd), | ||
_match: match | ||
value: value.slice(textEnd, emojiEnd) | ||
} | ||
// @ts-expect-error: removed later. | ||
child._match = match | ||
if (point) { | ||
@@ -179,5 +191,6 @@ child.position = { | ||
if (textEnd < value.length) { | ||
child = {type: node.type, value: value.slice(textEnd)} | ||
/** @type {typeof node} */ | ||
const child = {type: node.type, value: value.slice(textEnd)} | ||
if (point) { | ||
if (point && node.position) { | ||
child.position = {start: shift(point, textEnd), end: node.position.end} | ||
@@ -193,19 +206,18 @@ } | ||
/** | ||
* @param {Node} node | ||
* @param {Sentence} node | ||
* @returns {FindMatch[]} | ||
*/ | ||
function findEmoji(node) { | ||
var emojiExpression = emojiRegex() | ||
/** @type {Array.<FindMatch>} */ | ||
var matches = [] | ||
var value = toString(node) | ||
var start = value.indexOf(':') | ||
var end = start === -1 ? -1 : value.indexOf(':', start + 1) | ||
/** @type {string} */ | ||
var slice | ||
/** @type {RegExpExecArray} */ | ||
var match | ||
const emojiExpression = emojiRegex() | ||
/** @type {FindMatch[]} */ | ||
const matches = [] | ||
const value = toString(node) | ||
let start = value.indexOf(':') | ||
let end = start === -1 ? -1 : value.indexOf(':', start + 1) | ||
/** @type {RegExpExecArray|null} */ | ||
let match | ||
// Get Gemoji shortcodes. | ||
while (end !== -1) { | ||
slice = value.slice(start + 1, end) | ||
const slice = value.slice(start + 1, end) | ||
@@ -227,3 +239,3 @@ if (own.call(nameToEmoji, slice)) { | ||
if (value.charCodeAt(end + 1) === 0xfe0f) { | ||
if (value.charCodeAt(end + 1) === 0xfe_0f) { | ||
end++ | ||
@@ -241,9 +253,2 @@ } | ||
/** | ||
* @param {Node} node | ||
*/ | ||
function removeMatch(node) { | ||
delete node._match | ||
} | ||
/** | ||
* @param {FindMatch} a | ||
@@ -266,4 +271,4 @@ * @param {FindMatch} b | ||
column: point.column + offset, | ||
offset: point.offset + offset | ||
offset: (point.offset || 0) + offset | ||
} | ||
} |
{ | ||
"name": "nlcst-emoji-modifier", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "nlcst utility to support emoji", | ||
@@ -31,2 +31,3 @@ "license": "MIT", | ||
"files": [ | ||
"complex-types.d.ts", | ||
"index.d.ts", | ||
@@ -36,2 +37,3 @@ "index.js" | ||
"dependencies": { | ||
"@types/nlcst": "^1.0.0", | ||
"emoji-regex": "^9.0.0", | ||
@@ -42,3 +44,3 @@ "gemoji": "^7.0.0", | ||
"unist-util-position": "^4.0.0", | ||
"unist-util-visit": "^3.0.0" | ||
"unist-util-visit": "^4.0.0" | ||
}, | ||
@@ -51,4 +53,4 @@ "devDependencies": { | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
@@ -60,7 +62,7 @@ "tape": "^5.0.0", | ||
"unist-util-remove-position": "^4.0.0", | ||
"xo": "^0.38.0" | ||
"xo": "^0.44.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"{test/**,}*.d.ts\" && tsc && type-coverage", | ||
"build": "rimraf \"test/**/*.d.ts\" \"index.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . --write --loglevel warn && xo --fix", | ||
@@ -80,8 +82,3 @@ "test-api": "node test/index.js", | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"unicorn/no-array-for-each": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -88,0 +85,0 @@ "remarkConfig": { |
@@ -34,3 +34,3 @@ # nlcst-emoji-modifier | ||
var english = new ParseEnglish() | ||
const english = new ParseEnglish() | ||
english.useFirst('tokenizeSentence', emojiModifier) | ||
@@ -37,0 +37,0 @@ |
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
15334
6
254
0
7
+ Added@types/nlcst@^1.0.0
+ Addedunist-util-visit@4.1.2(transitive)
+ Addedunist-util-visit-parents@5.1.3(transitive)
- Removedunist-util-visit@3.1.0(transitive)
- Removedunist-util-visit-parents@4.1.1(transitive)
Updatedunist-util-visit@^4.0.0