{"id":11065,"library":"hyphen","title":"Text Hyphenation Library","description":"The `hyphen` library provides robust text hyphenation capabilities in JavaScript, based on Franklin M. Liang's widely adopted hyphenation algorithm. It leverages pre-compiled hyphenation patterns sourced from ctan.org for various languages. Currently stable at version 1.14.1, the package is primarily feature-driven with an irregular release cadence. Key differentiators include its extensive language support via separate pattern imports (e.g., `hyphen/en`, `hyphen/de`), automatic skipping of HTML tags during hyphenation, and the provision of both asynchronous (`hyphenate`) and synchronous (`hyphenateSync`) functions to suit different application contexts. Users can configure hyphenation with options for exceptions, the soft hyphen character, and minimum word length for processing.","status":"active","version":"1.14.1","language":"javascript","source_language":"en","source_url":"https://github.com/ytiurin/hyphen","tags":["javascript","hyphen","hyphenate","hyphenation","hyphenator"],"install":[{"cmd":"npm install hyphen","lang":"bash","label":"npm"},{"cmd":"yarn add hyphen","lang":"bash","label":"yarn"},{"cmd":"pnpm add hyphen","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Hyphenation functions are imported from language-specific paths (e.g., 'hyphen/en', 'hyphen/de'). CommonJS `require` is not directly supported; use dynamic import or an ESM-aware bundler.","wrong":"const { hyphenate } = require('hyphen/en');","symbol":"hyphenate","correct":"import { hyphenate } from 'hyphen/en';"},{"note":"Use `hyphenateSync` for synchronous operations. The default `hyphenate` function is asynchronous.","wrong":"import { hyphenate } from 'hyphen/de'; // for sync","symbol":"hyphenateSync","correct":"import { hyphenateSync } from 'hyphen/de';"},{"note":"Language modules export named functions (`hyphenate`, `hyphenateSync`), not a default export. Use `* as` for grouping or named imports directly.","wrong":"import hyphenEn from 'hyphen/en';","symbol":"* as hyphenEn","correct":"import * as hyphenEn from 'hyphen/en';"}],"quickstart":{"code":"import { hyphenate } from \"hyphen/en\";\n\n(async () => {\n  const text = \"A certain king had a beautiful garden that needed careful tending and regular watering.\";\n\n  // Example with custom options\n  const options = {\n    exceptions: [\"beautiful\"], // Don't hyphenate 'beautiful'\n    hyphenChar: '·', // Use a middle dot instead of soft hyphen\n    minWordLength: 6 // Only hyphenate words 6 characters or longer\n  };\n\n  const result = await hyphenate(text, options);\n  console.log(result);\n  // Expected output (approx): \"A cer·tain king had a beautiful gar·den that need·ed care·ful tend·ing and reg·u·lar wa·ter·ing.\"\n\n  // Using the synchronous version for a different language\n  const { hyphenateSync } = await import('hyphen/de');\n  const germanText = \"Ein gewisser König hatte einen wunderschönen Garten, der sorgfältige Pflege und regelmäßiges Gießen benötigte.\";\n  const germanResult = hyphenateSync(germanText);\n  console.log(germanResult);\n  // Expected output (approx): \"Ein ge·wis·ser Kö·nig hat·te einen wun·der·schö·nen Gar·ten, der sorg·fäl·ti·ge Pfle·ge und re·gel·mä·ßi·ges Gie·ßen be·nö·tig·te.\"\n})();","lang":"typescript","description":"Demonstrates asynchronous and synchronous hyphenation for English and German text, including custom options like exceptions and hyphenation character."},"warnings":[{"fix":"Always import and use the correct function for your desired behavior: `import { hyphenate } from 'hyphen/en'` for async, and `import { hyphenateSync } from 'hyphen/en'` for sync.","message":"The library exports distinct asynchronous (`hyphenate`) and synchronous (`hyphenateSync`) functions. Mixing them up or attempting to `await` the synchronous version will not work as expected.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure you import from the correct language-specific module, e.g., `import { hyphenate } from 'hyphen/en';`.","message":"Hyphenation patterns are loaded per language via specific import paths (e.g., `hyphen/en`, `hyphen/de`). Importing directly from `hyphen` (e.g., `import { hyphenate } from 'hyphen'`) will not provide the hyphenation function and will likely result in an undefined or module not found error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pass an `options` object with `hyphenChar` set to your desired character, e.g., `{ hyphenChar: '-' }` or `{ hyphenChar: '·' }`.","message":"The default hyphenation character (`hyphenChar`) is `\\u00AD` (soft hyphen), which is often invisible but indicates a potential line break. If you need a visible hyphen or a different character, you must explicitly configure it.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure HTML input is well-formed. For very complex or untrusted HTML, consider pre-processing the text to extract plain text segments, hyphenate them, and then re-insert into the HTML structure, or use a dedicated HTML parsing library in conjunction.","message":"When hyphenating HTML, the library attempts to skip HTML tags. However, malformed HTML or extremely complex structures might not be parsed correctly, potentially leading to hyphenation within tag attributes or unexpected places.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change the import path to a language-specific module, e.g., `import { hyphenate } from 'hyphen/en';`. If using CommonJS, ensure your environment supports dynamic `import()` or switch to an ESM-aware setup.","cause":"Attempting to import `hyphenate` from the root `hyphen` package instead of a language-specific module, or trying to use a `require` statement in an ESM-only context.","error":"TypeError: hyphenate is not a function"},{"fix":"Run `npm install hyphen` or `yarn add hyphen`. Ensure your bundler (Webpack, Rollup, etc.) is configured to handle ES Modules and module resolution correctly. Check for typos in the import path.","cause":"The package `hyphen` or its language-specific modules are not correctly installed or the build environment does not resolve ESM paths correctly.","error":"Module not found: Can't resolve 'hyphen/en'"},{"fix":"Verify the `exceptions` array contains the correct words to exclude. Adjust `minWordLength` to control the shortest words hyphenated. Ensure the correct language module (e.g., `hyphen/de` for German) is imported for the given text.","cause":"Misconfiguration of `exceptions`, `minWordLength`, or an incorrect language module being used.","error":"Words are not hyphenated as expected, or too many words are hyphenated."}],"ecosystem":"npm"}