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

@ffz/icu-msgparser

v2.0.0

Published

A tiny, basic parser for ICU MessageFormat strings that generates a basic AST.

Downloads

241

Readme

@ffz/icu-msgparser

NPM Version Build Status Dependency Status Test Coverage

A simple JavaScript parser for the ICU Message Format.

  • Under 2 Kilobytes, Minified and Gzipped
  • Decently Fast
  • Only does what it needs to.

Include the Thing

import Parser from '@ffz/icu-msgparser';

Parse the Stuff...

const parser = new Parser(),
    ast = parser.parse(`Hello, <b>{name}</b>! You have {messages, plural,
    =0 {no messages}
    one {one message}
    other {# messages}
} and you're {completion, number, percentage} done.`);

ast === [
    "Hello, ",
    {
        n: "b",
        c: [
            {v: "name"}
        ]
    },
    "! You have ",
    {
        v: "messages",
        t: "plural",
        o: {
            "=0": ["no messages"],
            one: ["one message"],
            other: [
                {v: "messages", t: "number"},
                " messages"
            ]
        }
    },
    " and you're ",
    {
        v: "completion",
        t: "number",
        f: "percentage"
    },
    " done."
]

Make Mistakes?

> parser.parse('Hello, {name{!');
SyntaxError: expected , or } at position 12 but found {

Interpret the AST

// ASTs are a simple array of strings and placeholder objects.
type MessageAST = MessageNode[];
type MessageNode = string | MessagePlaceholder;

// Placeholder objects are either tags or variables.
type MessagePlaceholder = MessageTag | MessageVariable;


type MessageTag = {
    // The name of the tag. For `<link>` this would be `link`.
    n: string;

    // If the tag has children, the child AST will be stored in this
    // variable. Otherwise, it will be undefined.
    c?: MessageAST;
};


type MessageVariable = {
    // n is never defined on variables and can be used to discriminate
    // between variables and tags.
    n: undefined;

    // The name of the variable. For `{count,number}` this would be `count`.
    v: string;

    // The type of the variable, if a type is included. Given the example
    // `{count,number}` this would be `number`.
    t?: string;

    // The format of the variable, if a format is included. Given the example
    // `{count,number,::currency/USD}` this would be `::currency/USD`.
    // For subnumeric types, this will be the `offset:` if one was provided
    // and the type will be a number.
    f?: string | number;

    // For submessage types, this will contain all of the separate submessage
    // ASTs, with their rules as unprocessed strings.
    o?: MessageSubmessages;
}

type MessageSubmessages = {
    [rule: string]: MessageAST;
};

Strings are just strings.

Placeholders can be tags or variables.

All tags will have a n, or name, which tells the interpreter what tag handler to use for them. c, or children/content, is only included for a tag with contents.

All variables will have a v, or value, which tells the interpreter what value to use for them. t, or type, is only included for variables with types given. This would be plural, number, etc.

f is the optionally provided format. For plural, selectordinal, as well as any custom types that use offset numbers, f will be the offset number if provided.

Finally, o is an object with containing all the parsed sub-messages for a variables with sub-messages.

Know the API

const parser = new Parser(/* options: */ {
    // Symbols (Single Character Only)
    OPEN: '{',
    CLOSE: '}',
    SEP: ',',
    ESCAPE: "'",
    SUB_VAR: '#',
    TAG_OPEN: '<',
    TAG_CLOSE: '>',
    TAG_CLOSING: '/',

    // Offset
    OFFSET: 'offset:',

    // Types that support offset:
    subnumeric_types: ['plural', 'selectordinal'],

    // Types that support sub-messages:
    submessage_types: ['plural', 'selectordinal', 'select'],

    // Config Flags
    allowTags: true,
    requireOther: true,
    // or
    requireOther: ['select']
});

const ast = parser.parse(message);

Tags can be completely disabled by setting allowTags to false.

By default, types with submessages are required to have an other type. You can set requireOther to false to disable this behavior entirely, or set it to an array of specific types that should require other while all other types should not.

Make Sure It Still Works

Run tests using npm test.

Contribute the Stuff

Please submit all issues and pull requests to the FrankerFaceZ/icu-msgparser repository.