textr
textr is a tiny and extendable framework designed for composing text transformation functions. Its core idea revolves around enabling developers to build modular typography tools by chaining small, single-responsibility text processing modules. The package encourages creating specific transformers for tasks like smart quotes, ellipses, or em-dashes, rather than a monolithic typographic engine. The current stable version is 0.3.0, released in 2015. Given its age and lack of updates, it's considered an abandoned project, meaning no new features or maintenance fixes are expected. Its key differentiator was its pluggable architecture for highly customized text processing workflows, allowing users to combine various `typographic-*` packages available on npm.
Common errors
-
TypeError: textr is not a function
cause Attempting to instantiate `textr` using `new textr()` or incorrect CommonJS import syntax for a default export, or treating `require('textr')` as a constructor.fixThe `textr` package exports a function directly. Use `const textr = require('textr');` and then call `textr()` to create an instance, e.g., `const tf = textr();`. -
TypeError: tf.use is not a function
cause Calling `.use()` on the raw `textr` function instead of an instantiated transformer object.fixFirst create a transformer instance using `const tf = textr();`, then call `tf.use(plugin);`.
Warnings
- gotcha The `textr` package has not been updated since April 2015 (version 0.3.0). This means it likely lacks support for modern JavaScript features (like native ESM) and may have unpatched security vulnerabilities or compatibility issues with newer Node.js versions or ecosystems. Consider alternatives for active projects.
- breaking In version 0.3.0, the `tf.exec` method's signature changed to accept `options` as a second argument. If you were calling `tf.exec(text)` and relying on `options` being passed implicitly or not existing, your code might break or behave unexpectedly.
- gotcha As an older CommonJS-era library, `textr` does not natively support ES Modules (`import`). While Node.js can sometimes interop, direct `require()` is the only reliable way to use it. Attempting `import textr from 'textr'` in an ESM context may lead to unexpected behavior or errors in bundlers or native ESM environments.
Install
-
npm install textr -
yarn add textr -
pnpm add textr
Imports
- textr
import textr from 'textr';
const textr = require('textr'); - tf.use
textr.use(pluginFunction);
tf.use(pluginFunction);
- String.prototype methods as transforms
tf.use(String.prototype.trim);
Quickstart
const textr = require('textr');
const ellipses = require('typographic-ellipses');
const spaces = require('typographic-single-spaces');
const quotes = require('typographic-quotes');
// Create a new text transformer instance, optionally with default options
const tf = textr({ locale: 'ru' })
.use(ellipses) // Add a plugin for ellipses
.use(spaces) // Add a plugin for single spaces
.use(quotes) // Add a plugin for smart quotes
.use(String.prototype.trim); // Use a native String prototype method
// Process text using the configured transformer
const input = 'Hello "world"...\n This is some text with bad spacing and unsmart quotes. ';
const output = tf(input);
console.log(output); // Expected: Hello «world»… This is some text with bad spacing and unsmart quotes.