Coder Social home page Coder Social logo

itemizer's Introduction

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

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

itemizer's People

Contributors

chearon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.