{"id":18376874,"url":"https://github.com/bbc/unicode-bidirectional","last_synced_at":"2025-06-11T04:42:56.087Z","repository":{"id":57189721,"uuid":"68601623","full_name":"bbc/unicode-bidirectional","owner":"bbc","description":"A Javascript implementation of the Unicode 9.0.0 Bidirectional Algorithm","archived":false,"fork":false,"pushed_at":"2019-04-03T04:38:19.000Z","size":1785,"stargazers_count":48,"open_issues_count":4,"forks_count":11,"subscribers_count":43,"default_branch":"master","last_synced_at":"2025-05-07T17:05:51.078Z","etag":null,"topics":["dpub","frontend","innovation","library","unicode","unicode-bidirectional-algorithm"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bbc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-19T12:02:20.000Z","updated_at":"2025-02-20T06:21:22.000Z","dependencies_parsed_at":"2022-09-15T06:51:48.751Z","dependency_job_id":null,"html_url":"https://github.com/bbc/unicode-bidirectional","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Funicode-bidirectional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Funicode-bidirectional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Funicode-bidirectional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Funicode-bidirectional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bbc","download_url":"https://codeload.github.com/bbc/unicode-bidirectional/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbc%2Funicode-bidirectional/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258783535,"owners_count":22757278,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dpub","frontend","innovation","library","unicode","unicode-bidirectional-algorithm"],"created_at":"2024-11-06T00:25:13.746Z","updated_at":"2025-06-11T04:42:56.060Z","avatar_url":"https://github.com/bbc.png","language":"JavaScript","readme":"# unicode-bidirectional\n\n[![Code Climate](https://codeclimate.com/github/bbc/unicode-bidirectional/badges/gpa.svg)](https://codeclimate.com/github/bbc/unicode-bidirectional)\n[![Test Coverage](https://codeclimate.com/github/bbc/unicode-bidirectional/badges/coverage.svg)](https://codeclimate.com/github/bbc/unicode-bidirectional/coverage)\n[![Build Status](https://travis-ci.org/bbc/unicode-bidirectional.svg?branch=master)](https://travis-ci.org/bbc/unicode-bidirectional)     \n*A Javascript implementation of the [Unicode 9.0.0 Bidirectional Algorithm](http://www.unicode.org/reports/tr9/)*\n\nThis is an implementation of the Unicode Bidirectional Algorithm (UAX #9) that\nworks in both Browser and Node.js environments. The implementation is conformant as per definition [UAX#9-C1](http://www.unicode.org/reports/tr9/#C1).\n\n## Installation\n```\nnpm install unicode-bidirectional --save\n```\n\n\n## Usage\n\nunicode-bidirectional is declared as a *Universal Module* ([UMD](https://github.com/umdjs/umd)),\nmeaning it can be used with all conventional Javascript module systems:\n\n#### 1. ES6 [→](https://github.com/bbc/unicode-bidirectional/blob/master/example/example1.js)\n\n```javascript\nimport { resolve, reorder } from 'unicode-bidirectional';\n\nconst codepoints = [0x28, 0x29, 0x2A, 0x05D0, 0x05D1, 0x05D2]\nconst levels = resolve(codepoints, 0);  // [0, 0, 0, 1, 1, 1]\nconst reordering = reorder(codepoints, levels); // [0x28, 0x29, 0x2A, 0x05D2, 0x05D1, 0x05D0]\n```\n\n#### 2. CommonJS [→](https://github.com/bbc/unicode-bidirectional/blob/master/example/example2.js)\n\n```javascript\nvar UnicodeBidirectional = require('unicode-bidirectional/dist/unicode.bidirectional');\nvar resolve = UnicodeBidirectional.resolve;\nvar reorder = UnicodeBidirectional.reorder;\n\nvar codepoints = [0x28, 0x29, 0x2A, 0x05D0, 0x05D1, 0x05D2]\nvar levels = resolve(codepoints, 0);  // [0, 0, 0, 1, 1, 1]\nvar reordering = reorder(codepoints, levels); // [0x28, 0x29, 0x2A, 0x05D2, 0x05D1, 0x05D0]\n```\n\n#### 3. RequireJS [→](https://github.com/bbc/unicode-bidirectional/blob/master/example/example3.html)\n\n```javascript\nrequire(['UnicodeBidirectional'], function (UnicodeBidirectional) {\n  var resolve = UnicodeBidirectional.resolve;\n  var reorder = UnicodeBidirectional.reorder;\n\n  var codepoints = [0x28, 0x29, 0x2A, 0x05D0, 0x05D1, 0x05D2]\n  var levels = resolve(codepoints, 0);  // [0, 0, 0, 1, 1, 1]\n  var reordering = reorder(codepoints, levels); // [0x28, 0x29, 0x2A, 0x05D2, 0x05D1, 0x05D0]\n});\n```\n\n\n#### 4. HTML5 `\u003cscript\u003e` tag [→](https://github.com/bbc/unicode-bidirectional/blob/master/example/example4.html)\n```html\n\u003cscript src=\"unicode.bidirectional.js\" /\u003e \u003c!-- exposes window.UnicodeBidirectional --\u003e\n```\n\n```javascript\nvar resolve = UnicodeBidirectional.resolve;\nvar reorder = UnicodeBidirectional.reorder;\n\nvar codepoints = [0x28, 0x29, 0x2A, 0x05D0, 0x05D1, 0x05D2]\nvar levels = resolve(codepoints, 0);  // [0, 0, 0, 1, 1, 1]\nvar reordering = reorder(codepoints, levels); // [0x28, 0x29, 0x2A, 0x05D2, 0x05D1, 0x05D0]\n```\n\nYou can download `unicode.bidirectional.js` from [Releases](https://github.com/bbc/unicode-bidirectional/releases).\nUsing this file with a `\u003cscript\u003e` tag will\nexpose `UnicodeBidirectional` as global variable on the `window` object.\n\n\n## API\n\n\n\n\n\n### `resolve(codepoints, paragraphlevel[, automaticLevel = false])`\nReturns the resolved levels associated to each codepoint in **`codepoints`**\u003csup\u003e[1]\u003c/sup\u003e.\nThis levels array determines: (i) the relative nesting of LTR and RTL characters, and \nhence (ii) how characters should be reversed when displayed on the screen.\n\nThe input codepoints are assumed to be all be in one paragraph that has a base direction of **`paragraphLevel`** –\nthis is a Number that is either 0 or 1 and represents whether the paragraph is *left-to-right* (0) or *right-to-left* (1).\n**`automaticLevel`** is an optional Boolean flag that when present and set to true, \ncauses this function to ignore the **`paragraphlevel`** argument and instead attempt to deduce the paragraph level from the codepoints. \u003csup\u003e[2]\u003c/sup\u003e    \nNeither of the two input arrays are mutated.\n\n### `reorder(codepoints, levels)`\nReturns the codepoints in **`codepoints`** reordered (i.e. permuted) according the `levels` array. \u003csup\u003e[3]\u003c/sup\u003e    \nNeither of the two input arrays are mutated.\n\n### `reorderPermutation(levels[, IGNORE_INVISIBLE = false])`\nReturns the reordering that **`levels`** represents as an permutation array.\nWhen this array has an element at index i with value j, it denotes that the codepoint \nprevious positioned at index i is now positioned at index j. \u003csup\u003e[4]\u003c/sup\u003e     \nThe input array is not mutated. The `IGNORE_INVISIBLE` parameter controls whether or not\ninvisible characters (characters with a level of 'x' \u003csup\u003e[5]\u003c/sup\u003e)\nare to be included in the permutation array. \nBy default, they *are* included in the permutation (they are *not* ignored, hence `IGNORE_INVISIBLE` is *false*).\n\n### `mirror(codepoints, levels)`\nReplaces each codepoint in `codepoints` with its mirrored glyph according to rule [L4](http://unicode.org/reports/tr9/#L4)\nand the `levels` array.    \nNeither of the two input arrays are mutated.\n\n### `constants`\nAn object containing metadata used by the bidirectional algorithm. This object includes the following keys:\n- `mirrorMap`: a map mapping a codepoint to its mirrored counterpart, e.g. looking up \"\u003c\" gives \"\u003e\". If a codepoint does not\nhave a mirrored counterpart, then there is no key-value pair in the map and so a lookup will give *undefined*. \u003csup\u003e[6]\u003c/sup\u003e\n- `oppositeBracket`: a map mapping a codepoint to its bracket pair counterpart, e.g. looking up \"(\" gives \")\". If a codepoint does not\nhave a bracket pair counterpart, then there is no key-value pair in the map and so a lookup will give *undefined*. \u003csup\u003e[7]\u003c/sup\u003e\n- `openingBrackets`: a set containing all brackets that are opening brackets. \u003csup\u003e[7]\u003c/sup\u003e\n- `closingBrackets`: a set containing all brackets that are closing brackets. \u003csup\u003e[7]\u003c/sup\u003e\n\n**Additional Notes:**\n\n\u003e For all the above functions, codepoints are represented by an Array of Numbers \nwhere each Number denotes the Unicode codepoint of the character, that \nis an integer between 0x0 and 0x10FFFF inclusive. levels are represented by an Array of \nNumbers where Number is an integer between 0 and 127 inclusive. One or more entries of levels \nmay be the string 'x'. This denotes a character that does not have a level \u003csup\u003e[5]\u003c/sup\u003e.\n\n\n\u003e **[1]**: Codepoints are automatically converted to [NFC normal form](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) if they are not already in that form.    \n**[2]**: This function deduces the paragraph level according to:  [UAX#P1](http://unicode.org/reports/tr9/#P1), [UAX#P2](http://unicode.org/reports/tr9/#P2) and [UAX#P3](http://unicode.org/reports/tr9/#P3).     \n**[3]**: This is an implementation of [UAX#9-L2](http://unicode.org/reports/tr9/#L2).      \n**[4]**: More formally known as the *one-line notation* for permutations. [See Wikipedia](https://en.wikipedia.org/wiki/Permutation#Definition_and_notations).     \n**[5]**: Some characters have a level of x – the levels array has a string 'x' instead of a number.\nThis is expected behaviour. The reason is because the Unicode Bidirectional algorithm (by rule [X9](http://unicode.org/reports/tr9/#X9).) will not assign a level to certain invisible characters / control characters. \nThey are basically completely ignored by the algorithm. \nThey are invisible and so have no impact on the visual RTL/LTR ordering of characters. \nMost of the invisible characters that fall into this category are in this [list](https://www.compart.com/en/unicode/bidiclass/BN).        \n**[6]**: This is taken from [BidiMirroring.txt](http://www.unicode.org/Public/9.0.0/ucd/BidiMirroring.txt).    \n**[7]**: This is taken from [BidiBrackets.txt](http://www.unicode.org/Public/9.0.0/ucd/BidiBrackets.txt).\n\n\n## Polyfills\n\nunicode-bidirectional uses the following ECMAScript 2015 (ES5) features that are not fully supported by Internet Explorer and older versions of other browsers:\n- [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Browser_compatibility), [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Browser_compatibility)\n- [`String.prototype.normalize`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/normalize#Browser_compatibility)\n\nIf you are targeting these browsers, you'll need to add one or more Polyfill libraries to fill in these features \n(for example, [es6-shim](https://github.com/paulmillr/es6-shim) and [unorm](https://github.com/walling/unorm)).\n\n## More Info\n\nFor other Javascript Unicode Implementations see:\n- [devongovett/grapheme-breaker](https://github.com/devongovett/grapheme-breaker) – Unicode Grapheme Cluster Breaking Algorithm (UAX #29) \n- [devongovett/linebreak](https://github.com/devongovett/linebreak) – Unicode Line Breaking Algorithm (UAX #14)\n\n## License\nMIT.    \nCopyright (c) 2017 British Broadcasting Corporation\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbc%2Funicode-bidirectional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbbc%2Funicode-bidirectional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbc%2Funicode-bidirectional/lists"}