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

icu-validator

v0.1.2

Published

Check if a text conforms to ICU format - supports individual string, JSON files or folders contaning JSON files

Downloads

1,829

Readme

icu-validator

Validate a data source if all strings in the data source conforms to ICU standard. Supports -

  • Individual text
  • Object of key value pair where value corresponds to texts
  • A JSON file path
  • A directory path contaning JSON files

Usage

Install using yarn -

yarn add icu-validator -D

or using npm

npm install icu-validator --save-dev

Import or require the module

// Using ESM
import { validate, validateFile, validateDirectory } from 'icu-validator';

// Using CJS
const { validate, validateFile, validateDirectory } = require('icu-validator');

Basic use case - validating a string

const { validate } = require('icu-validator');

const validationResult = validate('Hello {{name}}, how are you?');
/**
 * {
 *  "__icu_validator_error":true,
 *  "result": {
 *    "errorMessage":"MALFORMED_ARGUMENT",
 *    "originalMessage":"Hello {{name}}, how are you?",
 *    "location": {
 *      "start": { "offset":6,"line":1,"column":7 },
 *      "end": { "offset":7,"line":1,"column":8 }
 *    }
 *  }
 * }
 **/

const validationResult = validate('Hello {name}, how are you?');
/**
 * { "__icu_validator_error": false }
 **/

You can pretty print the errors by passing a flag prettyPrint: true

const validationResult = validate('Hello {{name}}, how are you?', { prettyPrint: true });

Validating an object of key value pairs

const obj = {
  "welcome": "Hello {name}, how are you?",
  "examples": {
    "pluralization": "Hi {name}, you have {{numProducts, plural, =0 {no items} =1 {one item} other {# items}} in cart"
  }
};

validate(obj, { prettyPrint: true });

/**
Invalid ICU string :- Hi {name}, you have {{numProducts, plural, =0 {no items} =1 {one item} other {# items}} in cart
Object path :- examples.pluralization
Error :- MALFORMED_ARGUMENT
Location :- {"start":{"offset":20,"line":1,"column":21},"end":{"offset":21,"line":1,"column":22}}
**/

Validating a JSON file or a directory containing many JSON files

Validate method does accept a file or directory path as first argument. But one very important thing to keep in mind - the validate method is completely sync, so all file & directory read operations will be sync. It is recommended to use dedicated validateFile and validateDirectory async methods.

validateFile(filePath).then((result) => {
  console.log(JSON.stringify(result));
});

validateDirectory(directoryPath).then((result) => {
  console.log(JSON.stringify(result));
});

Try prettyPrint: true if you want to show the errors on screen

Options

validate, validateFile and validateDirectory take a 2nd argument - options to customize the output format and validation rules/

validate(source: string | object, options: object)
validateFile(filePath: string, options: object)
validateDirectory(directoryPath: string, options: object)

Options include -

prettyPrint - default (false)

Print the validation result and errors in console.

verbose - default (false)

Provide complete execution logs when printing the results.

ignoreTransTag - default (false)

If you are using react-i18next with ICU, you will encounter situations where you need to externalize HTML elements. For react-i18next, the recommended method is Trans component. The externalized string resembles a format - Please click <0>here</0>. These are usually termed as invalid strings. Use this option if you want to ignore such tags from validation rule.

parseOptions

Under the hood, icu-validator uses formatjs parser. You can customize the validation rules by customizing the parser -

ignoreTag

Whether to treat HTML/XML tags as string literal

requiresOtherClause

Should select, selectordinal, and plural arguments always include the other case clause

shouldParseSkeletons

Whether to parse number/datetime skeleton into Intl.NumberFormatOptions and Intl.DateTimeFormatOptions, respectively.

captureLocation

Capture location info in AST

locale

Instance of Intl.Locale to resolve locale-dependent skeleton