NLCST Affix Emoticon Modifier
nlcst-affix-emoticon-modifier is a utility within the unified ecosystem designed to process Natural Language Concrete Syntax Tree (NLCST) nodes. Its primary function is to identify and re-position emoticons that appear at the beginning of a sentence, moving them to the end of the preceding sentence. This addresses common linguistic patterns where users place emoticons as a reaction to the previous thought, even after terminal punctuation. The current stable version is 3.0.0, which requires Node.js 16+. While a low-level utility, it forms a crucial part of more abstracted tools like `retext-emoji`. The project follows semver for releases, with significant breaking changes in major versions, particularly the move to ESM-only in v2 and further API adjustments in v3.
Common errors
-
ERR_REQUIRE_ESM (or similar 'require is not defined in ES module scope')
cause Attempting to use `require()` to import `nlcst-affix-emoticon-modifier`, which is an ESM-only package.fixUpdate your import statement to use ES module syntax: `import { affixEmoticonModifier } from 'nlcst-affix-emoticon-modifier'`. Ensure your project's `package.json` has `"type": "module"` or you are using `.mjs` file extensions. -
TypeError: affixEmoticonModifier is not a function
cause This error often occurs when running on an unsupported Node.js version (older than 16) or due to incorrect interoperability between CommonJS and ES Modules.fixEnsure your Node.js version is 16 or newer. Verify that your project is correctly configured for ES modules, using `import` statements and appropriate `package.json` settings. -
Property 'Emoticon' does not exist on type 'typeof import("nlcst-affix-emoticon-modifier")'cause Attempting to import the `Emoticon` type directly from `nlcst-affix-emoticon-modifier` in TypeScript after upgrading to v3.0.0.fixThe `Emoticon` type has been moved. Import it from `nlcst-emoticon-modifier` instead: `import type { Emoticon } from 'nlcst-emoticon-modifier'`.
Warnings
- breaking The package is now ESM-only (ECMAScript Modules). CommonJS `require()` is no longer supported.
- breaking This package now requires Node.js version 16 or higher to run.
- breaking The package now uses the `exports` field in its `package.json`, which affects module resolution and restricts direct deep imports or access to previously public internal paths.
- breaking The `Emoticon` TypeScript type has been removed from `nlcst-affix-emoticon-modifier`.
Install
-
npm install nlcst-affix-emoticon-modifier -
yarn add nlcst-affix-emoticon-modifier -
pnpm add nlcst-affix-emoticon-modifier
Imports
- affixEmoticonModifier
const affixEmoticonModifier = require('nlcst-affix-emoticon-modifier')import { affixEmoticonModifier } from 'nlcst-affix-emoticon-modifier' - Emoticon (type)
import type { Emoticon } from 'nlcst-affix-emoticon-modifier'import type { Emoticon } from 'nlcst-emoticon-modifier'
Quickstart
import {affixEmoticonModifier} from 'nlcst-affix-emoticon-modifier'
import {emoticonModifier} from 'nlcst-emoticon-modifier'
import {ParseEnglish} from 'parse-english'
import {inspect} from 'unist-util-inspect'
const parser = new ParseEnglish()
// Register emoticonModifier to tokenize emoticons into EmoticonNodes
parser.tokenizeSentencePlugins.unshift(emoticonModifier)
// Register affixEmoticonModifier to move these nodes after terminal punctuation
parser.tokenizeParagraphPlugins.unshift(affixEmoticonModifier)
const text = 'Hey. :) How is it going?'
const tree = parser.parse(text)
console.log(inspect(tree))
/* Expected output will show ' :)' moved into the first sentence. */