npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@socheatsok78/intl-segmenter-polyfill

v1.2.0

Published

This repo builds .wasm module using icu4c for breaking text into words, so that we can polyfill [Intl Segmenter Proposal](https://github.com/tc39/proposal-intl-segmenter) with full compatibility, even on browsers that do not expose v8BreakIterator api.

Downloads

3

Readme

Intl Segmenter Polyfill

npm version Build WASM Test

Provides .wasm module built with icu4c for breaking text into words, so that we can polyfill Intl Segmenter Proposal with full compatibility, even on browsers that do not expose v8BreakIterator api.

By default it bundles only Thai language dictionary. Modify filters.json if you need to support other exotic languages.

Usage

npm install --save intl-segmenter-polyfill

Web – fetch

This is the most efficient way as you can lazily load the wasm module only when you need it and use instantiateStreaming for the best performance. Serve break_iterator.wasm as a static asset with application/wasm content-type and you are good to go.

index.js

import { createIntlSegmenterPolyfill } from 'intl-segmenter-polyfill'
;(async function () {
  const Segmenter = await createIntlSegmenterPolyfill(
    fetch('/path/to/break_iterator.wasm'),
  )

  const segmenter = new Segmenter('en', { granularity: 'word' })
  const segments = segmenter.segment('foo bar baz')
})()

Web – bundle with base64 encoded module

This is the simplest way to use the polyfill, at the cost of base64 encoded module – it's ~33% bigger and cannot be loaded on demand.

index.js

import { createIntlSegmenterPolyfill } from 'intl-segmenter-polyfill/bundled'
;(async function () {
  const Segmenter = await createIntlSegmenterPolyfill()
  const segmenter = new Segmenter('en', { granularity: 'word' })
  const segments = segmenter.segment('foo bar baz')
  console.log(segments)
})()

OR using plain old in html

<script src="bundled.js"></script>
<script>
  IntlSegmenterPolyfillBundled.createIntlSegmenterPolyfill().then(function (
    Segmenter,
  ) {
    const segmenter = new Segmenter('en', { granularity: 'word' })
    const segments = segmenter.segment('foo bar baz')
    console.log(segments)
  })
</script>

Web – Rollup / Webpack wasm loader

@rollup/plugin-wasm and webpack wasm-loader can be used with createIntlSegmenterPolyfillFromFactory

rollup.config.js

import commonjs from '@rollup/plugin-commonjs'
import { wasm } from '@rollup/plugin-wasm'

export default {
  input: 'index.js',
  output: {
    file: 'out.js',
    format: 'iife',
  },
  plugins: [commonjs(), wasm()],
}

index.js

import { createIntlSegmenterPolyfillFromFactory } from 'intl-segmenter-polyfill'
import break_iterator from 'intl-segmenter-polyfill/break_iterator.wasm'
;(async function () {
  const Segmenter = await createIntlSegmenterPolyfillFromFactory(break_iterator)

  const segmenter = new Segmenter('en', { granularity: 'word' })
  const segments = segmenter.segment('foo bar baz')
})()

Node

const {createIntlSegmenterPolyfill} = require('intl-segmenter-polyfill')
const fs = require('fs')

const wasmBuffer = fs.readFileSync('node_modules/intl-segmenter-polyfill/break_iterator.wasm')
let wasmBinary = new Uint8Array(wasmBuffer)

;(async () => {
  const Segmenter = await createIntlSegmenterPolyfill(wasmBinary);
  const segmenter = new Segmenter("en", { granularity: 'word' });
  const segments = segmenter.segment("foo bar baz");
)()

Supported browsers

Besides Chrome, Firefox and Safari with reasonable versions, it polyfills TextEncoder/TextDecoder to support Edge 18 (non-chromium).

Building

Running ./build.sh while having docker installed should output break_iterator.wasm ready to be used in Node, browsers or Wasmer without a lot of special treatment (see examples above or examples/).