Hysen Labs
Open-source project
bbc/unicode-bidirectional avatar
bbc

unicode-bidirectional

A Javascript implementation of the Unicode 9.0.0 Bidirectional Algorithm

50 stars11 forksJavaScriptMIT
01
DEEP OPEN-SOURCE ANALYSIS

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.

02
DEEP OPEN-SOURCE ANALYSIS

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.

03
DEEP OPEN-SOURCE ANALYSIS

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.

04
DEEP OPEN-SOURCE ANALYSIS

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.

05
DEEP OPEN-SOURCE ANALYSIS

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.

06
DEEP OPEN-SOURCE ANALYSIS

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.

07
DEEP OPEN-SOURCE ANALYSIS

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.

08
DEEP OPEN-SOURCE ANALYSIS

Official sources

09
Community notes

Community notes