Gathering detailed insights and metrics for bidi-js
Gathering detailed insights and metrics for bidi-js
Gathering detailed insights and metrics for bidi-js
Gathering detailed insights and metrics for bidi-js
chromium-bidi
An implementation of the WebDriver BiDi protocol for Chromium implemented as a JavaScript layer translating between BiDi and CDP, running inside a Chrome tab.
marked-bidi
marked bidirectional text support
@react-pdf/textkit
An advanced text layout framework
bidi-css-js
Logical, flow-ralative conversion for CSS in JS objects
A pure JavaScript implementation of the Unicode Bidirectional Algorithm
npm install bidi-js
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
43 Stars
39 Commits
7 Forks
4 Watching
1 Branches
3 Contributors
Updated on 09 Oct 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-4.4%
179,222
Compared to previous day
Last week
3.3%
1,001,169
Compared to previous week
Last month
7.7%
4,166,950
Compared to previous month
Last year
514.9%
34,983,436
Compared to previous year
This is a pure JavaScript implementation of the Unicode Bidirectional Algorithm version 13.0.0. Its goals, in no particular order, are to be:
This implementation currently conforms to section UAX-C1 of the bidi spec, as verified by running all the provided conformance tests.
It has no external dependencies and therefore should run just fine in any relatively capable web browser, Node.js, etc. The provided distribution .js
files are valid ES5.
Install it from npm:
1npm install bidi-js
Import and initialize:
1import bidiFactory from 'bidi-js' 2// or: const bidiFactory = require('bidi-js') 3 4const bidi = bidiFactory()
The bidi-js
package's only export is a factory function which you must invoke to return a bidi
object; that object exposes the methods for bidi processing.
(Why a factory function? The main reason is to ensure the entire module's code is wrapped within a single self-contained function with no closure dependencies. This enables that function to be stringified and passed into a web worker, for example.)
Now that you have the bidi
object, you can:
1const embeddingLevels = bidi.getEmbeddingLevels(
2 text, //the input string containing mixed-direction text
3 explicitDirection //"ltr" or "rtl" if you don't want to auto-detect it
4)
5
6const { levels, paragraphs } = embeddingLevels
The result object embeddingLevels
will usually be passed to other functions described below. Its contents, should you need to inspect them individually, are:
levels
is a Uint8Array
holding the calculated bidi embedding levels for each character in the string. The most important thing to know about these levels is that any given character is in a right-to-left scope if its embedding level is an odd number, and left-to-right if it's an even number.
paragraphs
is an array of {start, end, level}
objects, one for each paragraph in the text (paragraphs are separated by explicit breaking characters, not soft line wrapping). The start
and end
indices are inclusive, and level
is the resolved base embedding level of that paragraph.
1const flips = bidi.getReorderSegments(
2 text, //the full input string
3 embeddingLevels //the full result object from getEmbeddingLevels
4)
5
6// Process all reversal sequences, in order:
7flips.forEach(range => {
8 const [start, end] = range
9 // Reverse this sequence of characters from start to end, inclusive
10 for (let i = start; i <= end; i++) {
11 //...
12 }
13})
Each "flip" is a range that should be reversed in place; they must all be applied in order.
Sometimes you don't want to process the whole string at once, but just a particular substring. A common example would be if you've applied line wrapping, in which case you need to process each line individually (in particular this does some special handling for trailing whitespace for each line). For this you can pass the extra start
and end
parameters:
1yourWrappedLines.forEach(([lineStart, lineEnd]) => {
2 const flips = bidi.getReorderSegments(
3 text,
4 embeddingLevels,
5 lineStart,
6 lineEnd //inclusive
7 )
8 // ...process flips for this line
9})
Some characters that resolve to right-to-left need to be swapped with their "mirrored" characters. Examples of this are opening/closing parentheses. You can determine all the characters that need to be mirrored like so:
1const mirrored = bidi.getMirroredCharactersMap( 2 text, 3 embeddingLevels 4)
This returns a Map
of numeric character indices to replacement characters.
You can also process just a substring with extra start
and end
parameters:
1const mirrored = bidi.getMirroredCharactersMap( 2 text, 3 embeddingLevels, 4 start, 5 end //inclusive 6)
If you'd rather process mirrored characters individually, you can use the single getMirroredCharacter
function, just make sure you only do it for right-to-left characters (those whose embedding level is an odd number.) It will return null
if the character doesn't support mirroring.
1const mirroredChar = (embeddingLevels.levels[charIndex] & 1) //odd number means RTL 2 ? bidi.getMirroredCharacter(text[charIndex]) 3 : null
This is used internally, but you can also ask for the "bidi character type" of any character, should you need it:
1const bidiType = bidi.getBidiCharTypeName(string[charIndex]) 2// e.g. "L", "R", "AL", "NSM", ...
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 5/25 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More