No 3rd party dependencies. All dependencies and devDependencies, checked recursively, are Codsen packages.
Permalink to InstallationInstallation
Permalink to Quick TakeQuick Take
Permalink to ExamplesExamples
- Preserve combining marks while simplifying punctuation when requested
- Normalise common typographic characters
- Normalise an HTML-encoded typographic character
- Convert non-breaking spaces
- Decode repeatedly encoded HTML
Idea
Convert typographically-correct characters (like curly quotes or m-dashes) to their basic counterparts (like apostrophes or hyphens).
It’s the opposite of detergent and string-apostrophes.
Use it to simplify supported punctuation in text, including image alt values before they are encoded for HTML. Other characters, such as accented letters and backticks, remain unchanged.
API — unfancy()
The main function unfancy() is imported like this:
Pass a string and an optional options object:
The function returns a string. Omitting the options preserves the existing conversions.
| Option | Type | Default | Description |
|---|---|---|---|
preserveCombiningMarksType: Boolean Default: false | |||
preserveCombiningMarks | Boolean | false | Preserve U+0312–U+0315 combining marks instead of replacing them with apostrophes. Other typography conversions and recursive character-reference decoding still apply. |
unfancy("…q\u0313r—");
// → "...q'r-"
unfancy("…q\u0313r—", { preserveCombiningMarks: true });
// → "...q\u0313r-"
The option preserves these marks as written; unfancy() does not apply Unicode normalization. extract-search-index enables it alongside NFC normalization to keep canonically equivalent words together.
API — version
You can import version:
Example — normalize text
Character references are decoded repeatedly before typography is simplified. Every mapped character is converted, including quotes and apostrophes after an ellipsis expands to three periods:
import { unfancy } from "string-unfancy";
unfancy("…“alpha” …beta’s");
// → `..."alpha" ...beta's`
unfancy("…“alpha”");
// → `..."alpha"`
After decoding reaches a fixed point, one replacement pass handles the selected punctuation. Repeated typography does not require rebuilding the entire string for each replacement. Pass the complete text, including repeated or encoded punctuation; the same conversions apply throughout it.
Pass the text to normalize. When inserting the result into HTML, encode it for its destination attribute or text context. Extract an image’s alt value before normalizing it; unfancy() does not parse or repair the surrounding HTML attribute syntax.
Can you use lodash.deburr instead?
No. It won’t even convert a single m-dash! It’s a different tool for a different purpose.