Socket
Book a DemoInstallSign in
Socket

itemizer

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

itemizer

Segment a JS string into script, emoji, and bidi parts

1.0.6
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

itemizer

Dice up JS strings by script, emoji and bidi direction. Hand ported from Pango with the help of SheenBidi and emoji-segmenter, both of which are compiled and distributed in WebAssembly form.

A useful text-stack component if you want to do your own font selection and shaping in JS, or if you need more power to customize the display of different scripts or emojis in a browser.

API

The module itself is a promise since WASM loads asynchronously:

const {emoji, script, bidi} = await require('itemizer');

Each of the values on the resolved object have the same API but iterate different things. The emoji iterator stops at boundaries between text and emoji sequences:

const str = 'I 👏🏼 proper i18n support';
let last = 0;
for (const {i, isEmoji} of emoji(str)) {
  console.log(str.slice(last, i)); // logs 3 times
  last = i;
}

The bidi iterator has the same API:

const str = 'Latin is common ពួកគេទាំងអស់   ';

let last = 0;
for (const {i, dir} of bidi(str, 0 /* base level (ex. 1 for RTL) */)) {
  console.log(str.slice(last, i)); // logs 2 times
  last = i;
}

and so does the script iterator's API:

const str = 'Latin is common ពួកគេទាំងអស់   ';

for (const {i, script} of script(str)) {
  console.log(script); // logs 2 times
}

Bigger example

Check out the repo and run

$ node test.js

Building the WebAssembly locally

  • Get ragel from your package manager
  • Get WASI (sysroot version)
  • Get LLVM Clang 9+ from your package manager
  • Open the makefile and set RL, WASI_SYSROOT CXX according to steps 1, 2, 3
  • make

FAQs

Package last updated on 12 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.