transliterate-generator
v1.4.1
Published
`transliterate-generator` helps you build deterministic transliteration functions out of simple mapping tables. It ships with ready-made Cyrillic ↔ Latin maps and exposes a generator that can produce every transliteration variant permitted by those maps.
Readme
transliterate-generator
transliterate-generator helps you build deterministic transliteration functions out of simple mapping tables. It ships with ready-made Cyrillic ↔ Latin maps and exposes a generator that can produce every transliteration variant permitted by those maps.
Installation
npm install transliterate-generatorUsage
import transliterateGenerator, { maps } from 'transliterate-generator';
const transliterate = transliterateGenerator(maps.cyrillicToLatin, Infinity);
transliterate('привет');
// [ 'privjet', 'privyet', 'privet' ]ℹ️ Pass a sufficiently large
maxTranslitsvalue (for unlimited output useInfinity). When the limit is omitted or reached the generator stops expanding new variants and simply appends the original characters.
When several replacements are defined for the same symbol sequence, every combination is produced:
const transliterate = transliterateGenerator(maps.cyrillicToLatin, Infinity);
transliterate('ёж');
// [ 'jozh', 'yozh' ]Limiting the output keeps the early alternatives and leaves the rest of the word untouched.
const transliterate = transliterateGenerator(maps.latinToCyrillic, 5);
transliterate('schastie');
// [ 'зкхastie', 'скхastie', 'зчastie', 'счastie', 'шastie', 'щastie' ]Custom maps
A transliteration map is a simple dictionary where keys are source sequences and values are arrays of replacement strings:
import transliterateGenerator from 'transliterate-generator';
const pigLatinMap = {
hello: ['ellohay'],
world: ['orldway'],
};
const pigLatin = transliterateGenerator(pigLatinMap, Infinity);
pigLatin('helloworld');
// [ 'ellohayorldway' ]You can mix single characters and longer sequences in the same map. The generator automatically checks every substring up to the longest key in the map.
API
transliterateGenerator(map, maxTranslits?)
| Parameter | Type | Description |
|-----------|------|-------------|
| map | TranslateMap | Dictionary of source substrings to replacement arrays. |
| maxTranslits | number \| null | Optional upper bound on the number of transliterations that will be expanded. Use Infinity for unrestricted output or leave null (default) to disable expansions entirely. |
Returns a function (input: string) => string[] that produces every transliteration permitted by the map while respecting the limit.
Built-in maps
Import the maps helper to access the bundled transliteration tables:
maps.cyrillicToLatinmaps.latinToCyrillic
These maps are defined in src/alphabetMaps and cover common one-to-many letter replacements.
Development
npm install # install dependencies
npm run build # build the library with Rollup
npm test # run Jest against the built bundle (requires npm run build first)
npm run lint # run ESLint on the TypeScript sources
npm run format # verify code formatting with PrettierLicense
MIT © Volodin Sergei
