unicode-bidirectional
A Javascript implementation of the Unicode 9.0.0 Bidirectional Algorithm
unicode-bidirectional: bidi handling for browsers and Node
A UAX 9 implementation of the Unicode bidirectional algorithm, shipped as a UMD module, with resolve, reorder, and mirror functions that never mutate their inputs.
What it covers
This is an implementation of the Unicode Bidirectional Algorithm, UAX 9, that runs in both browser and Node.js, and it claims conformance per definition UAX 9-C1. The repository description anchors it to Unicode 9.0.0. For code that must render mixed left-to-right and right-to-left text correctly, that is the algorithm underneath the layout.
Loading it
The package is declared as a universal module, so it plugs into the usual JavaScript module systems. A prebuilt unicode.bidirectional.js file is downloadable from Releases, and loading that with a script tag exposes UnicodeBidirectional as a global on window. Both the module path and the plain script path are covered.
The API
resolve takes codepoints and a paragraph level, 0 for LTR and 1 for RTL, and returns the resolved levels for each codepoint; those levels decide the nesting of LTR and RTL characters and how they get reversed on screen. An optional automaticLevel flag tells it to ignore the paragraph argument and deduce the level from the codepoints. reorder permutes codepoints according to a levels array, reorderPermutation returns that permutation as an array with an option to drop invisible characters, and mirror swaps codepoints for their mirrored glyphs per rule L4.
Data and constants
Codepoints are arrays of numbers up to 0x10FFFF, and levels are numbers from 0 to 127, with the string x marking characters the algorithm does not assign a level to, mostly invisible control characters. The constants object carries a mirror map and bracket maps and sets. Codepoints are normalized to NFC automatically, and no input array is ever mutated.
Older browsers
The library relies on ECMAScript 2015 Map and Set, which older Internet Explorer and legacy browsers do not fully support, so targeting those means adding polyfills such as es6-shim and unorm. For related work the README links devongovett's grapheme-breaker for UAX 29 and linebreak for UAX 14.
Editorial conclusion
The library covers the core bidi functions plus polyfill notes for older browsers, and it points to companion Unicode implementations for grapheme breaking and line breaking.
Community notes