Skip to content

sergeysolovev/hyphenated

Repository files navigation

Hyphenated

CircleCI npm version

Better hyphenation for JavaScript. Why?

  • Precise: uses Franklin Liang’s algorithm under the hood.
  • Versatile: hyphenates entire text and not just single words.
  • Multiple languages: can be installed independently for efficient code splitting.
  • Flexible licensing: each language package has a separate license which is MIT where it’s possible.

View demo →

Quickstart

Install the main package which already includes hyphenation patterns for American English:

npm install hyphenated

Hyphenate text:

import { hyphenated } from 'hyphenated';

const textWithSoftHyphens = hyphenated(
  'Self-evident. Evident to one’s self and to nobody else.'
);

// To display soft hyphen (\u00AD) positions in console:
console.log(textWithSoftHyphens.replace(/\u00AD/g, '~'));

This should output:

Self-ev~i~dent. Ev~i~dent to one’s self and to no~body else.

Here soft hyphens are replaced with ~ only for demonstration purposes.

Browser example

Although soft hyphens are invisible, they tell the browser where to put visible hyphens when flowing text.

loadable-components

import { hyphenated } from 'hyphenated';

const paragraph = document.createElement('p');
const textWithSoftHyphens = hyphenated(
  'Self-evident. Evident to one’s self and to nobody else.'
);

paragraph.innerText = textWithSoftHyphens;
document.body.appendChild(paragraph);

Text in another language

To hyphenate text in a language other than American English, first install an appropriate language package:

npm install hyphenated-fr

Pass it as an option to hyphenated:

import fr from 'hyphenated-fr';
import { hyphenated } from 'hyphenated';

const textWithSoftHyphens = hyphenated(
  "Je suis l'itinéraire donné par Pierre, un ami français.",
  { language: fr }
);

// To display soft hyphen (\u00AD) positions in console:
console.log(textWithSoftHyphens.replace(/\u00AD/g, '~'));

This should output:

Je suis l'iti~né~raire don~né par Pierre, un ami fran~çais.

Supported languages

American English is a default language for hyphenated. It’s not necessary to install it separately.

language package license
American English (default) hyphenated-en-us MIT
British English hyphenated-en-gb MIT
German hyphenated-de MIT
French hyphenated-fr MIT
Russian hyphenated-ru LPPL
Spanish hyphenated-es MIT
Polish hyphenated-pl MIT
Dutch @digitalartlab/hyphenated-nl LPPL

License

Hyphenated is primarily distributed under the terms of the MIT license. It includes packages with hyphenation patterns written by third parties. These packages carry their own copyright notices and license terms.

See LICENSE for details.