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 🙏

© 2026 – Pkg Stats / Ryan Hefner

haicu

v0.1.0

Published

tiny ICU message parser

Readme

Introduction

This haicu package provides an ICU message parser you can use to translate messages for example in a multi-lingual web site.

You can use messages with arguments, plurals, HTML-like tags, etc.

I have been using for years tools like FormatJS and others but they all come with a bloated implementation that increases a lot the bundle of your webapp. Packages like react-intl, react-i18next, messageformat are great software and a good starting point if you want to learn the best practices. However the only core part that you actually need nowadays is an ICU message parser, everything else can be done with few custom lines of code tailored on your needs.

There are also other use cases where you may want something lighter, for instance if you want to translate a desktop app menu or an automatic email.

The goal of haicu is to have a tiny and minimal implementation (it is below 3kb when minified) of an ICU message parser as well as documented examples about how to implement translated content.

The package name comes from Haiku which is a type of short form poetry that originated in Japan.

For example

寺の鐘消ゆる 花の香は撞く 夕べかな

by Matsuo Bashō

The temple bell stops but the sound keeps coming out of the flowers.

Status and roadmap

Implementation of ICU message parser is complete. Even if haicu has still a 0.x version, API will probably not change. The package will reach a stable 1.0 version once there are enough tests and documentation. After 1.0 the API will be freezed and the package will be updated only for bug fixes.

Installation

With npm do

npm install haicu

or just copy the haicu.js code in your project.

Usage

The haicu package default export is an ICU message parser.

import haicu from 'haicu'

haicu('Hello {name}')
// [ 'Hello ', { arg: 'name' } ]

It returns an Abstract Syntax Tree (AST) of an ICU message where a node can be

  • NonEmptyString: e.g. Hello
  • MessageTag: an HTML-like tag
  • MessageArg: an argument like { arg: 'name' }

See types.ts for details.

Tags

HTML-like tags can be used inside messages.

haicu('<p>nested <b>tag</b></p>')
// [ { tag: 'p', ast: [ 'nested ', { tag: 'b', ast: [ 'tag' ] } ] } ]

Notice that tag arguments are not supported. In particular it is a best practice in translations to not hardcode links. For example

  • bad: Click <a href="http://example.com">here</a>
  • good: Click <a>here</a>

Just add the href at runtime, it could depend on the locale and it should not be hardcoded anyways. Remember you can use any name for your tags, so for example if you have two links this translation is fine

<link1>Learn more</link1> or <link2>download<link2>

Tags can be auto-closed, for example

haicu('auto-closed <br/> tag')
// [ 'auto-closed ', { tag: 'br', ast: [] }, ' tag' ]

Errors

The invalid message Hello {name produces this output

[ 'Hello ', { error: 'No closing bracket' } ]

but the haicu parser does not throw errors.

You should use validators to check your translations before deploying them.

Escape

The escape character is quote: '.

It can be used to escape:

  • brackets: This is '{escaped}'
  • tags or < (opening angular bracket):
    • escaped '<tag>'
    • Made with '<3
  • another quote: I''m fine
  • hashes: escaped '#' hash

Validators

To validate an ICU message node or AST, import a validator from haicu/validators.js.

For example, to test that all your messages parse correctly:

import haicu from 'haicu'
import { isMessageAST } from 'haicu/validators.js'

export function testTranslations (messages) {
  for (const message of messages) {
    const ast = haicu(message)
    if (isMessageAST(ast))
      continue
    const error = findError(ast)
    throw new Error(`Invalid ICU message ${message} with error '${error}'`)
  }
}

The haicu/validators.js exports these type-guards:

  • isMessageAST(arg: unknown): arg is MessageAST
  • isMessageArg(arg: unknown): arg is MessageArg
  • isMessageTag(arg: unknown): arg is MessageTag

And a findError(ast: MessageAST): string util.