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

text-formatjs

v1.0.4

Published

A javascript library for formatting locale-sensitive information such as dates, messages and numbers

Downloads

6

Readme

Text Format

npm Version build status

text-formatjs is a javascript library for formatting locale-sensitive information such as dates, messages and numbers.

text-formatjs defines a programming interface for formatting locale-sensitive objects into Strings (the format method) and for parsing Strings back into objects (the parse method). The library provides a set of javascript modules based on java classes defined in java.text and java.util packages.

Here we have the most important javascript modules to be used for formatting data:

  • locale: Represents a specific geografical, political or cultural region.
  • format: The base module for formatting locale-sensitive information.
  • number-format: The base module for formatting numbers.
  • decimal-format: Extends the number-format module for formatting decimal numbers.
  • choice-format: Allows you to attach a format to a range of numbers. It is generally used in a Message-format for handling plurals.
  • date-format: The base module for date/time formatting submodules which formats and parses dates or time in a language-independent manner.
  • simple-date-format: A date-format module for formatting and parsing dates in a locale-sensitive manner.
  • message-format: Provides a means to produce concatenated messages in a language-neutral way. To be used to construct messages displayed for end users.

There are also other modules provided to help the formats module to complete their functionalities:

  • resource-bundle: Implements the logic to return resource bundle files provided by the text-resources library.
  • currency: Represents a currency identified by its ISO 4217 currency code.
  • field-position: A simple module used by format and its submodules to identify fields in formatted output.
  • parse-position: A simple module used by format and its submodules to keep track of the current position during parsing.
  • date-format-symbols: A module for encapsulating localizable date-time formatting data, such as the names of the months, the names of the days of the week, and the time zone data.
  • decimal-format-symbols: Represents the set of symbols (such as the decimal separator, the grouping separator, and so on) needed by DecimalFormat to format numbers.

Installation

Loading text-formatjs in a browser

<!-- text-resources for default locale -->
<script src="path/to/text-resources/text-resources.js"></script>

<!-- text-resources for specific locale (Optional) -->
<script src="path/to/text-resources/text-resources-en.js"></script>
<script src="path/to/text-resources/text-resources-en-GB.js"></script>
<script src="path/to/text-resources/text-resources-en-US.js"></script>
<script src="path/to/text-resources/text-resources-fr.js"></script>
<script src="path/to/text-resources/text-resources-fr-FR.js"></script>

<!-- text-formatjs library -->
<script src="path/to/text-formatjs/text-format.js"></script>

Loading text-formatjs in Node.js

require("text-formatjs");

Note: This call will load the text resources for all the supported language of the current release of the text-resources library

Usage

After loading the text-formatjs library, a set of javascript objects are available in the global context.

Formatting numbers

Use the NumberFormat object method factory:

NumberFormat.getInstance().format(12436.736);       // -> '12,436.736'
NumberFormat.getCurrencyInstance().format(1500);    // -> '$1,500.00'
NumberFormat.getIntegerInstance().format(0.75);     // -> '1'
NumberFormat.getPercentInstance().format(0.6);      // -> '60%'

Create a new DecimalFormat object instance:

new DecimalFormat("#,##0.###").format(12.345678);   // -> '12.346'
new DecimalFormat("#,##0%").format(1/3);            // -> '33%'
new DecimalFormat("\u00A4#,##0.00;(\u00A4#,##0.00")
    .format(-35);                                   // -> '($35.00)'

Formatting dates and times

Use the DateFormat object factory:

DateFormat.getInstance().format(new Date());        // -> '2/3/17 4:55:20 PM'
DateFormat.getDateInstance().format(new Date());    // -> 'Feb 3, 2017'
DateFormat.getTimeInstance().format(new Date());    // -> '4:55:20 PM'
DateFormat.getDateTimeInstance()
    .format(new Date());                            // -> 'Feb 3, 2017 4:55:20 PM'

Create a new SimpleDateFormat object instance:

new SimpleDateFormat("M/d/yy").format(new Date());  // -> '2/3/17'
new SimpleDateFormat("EEEE, MMMM d, yyyy")
    .format(new Date());                            // -> 'Friday, February 3, 2017'
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
    .format(new Date());                            // -> '2017-02-03T17:09:08.779+01:00'

Formatting messages

Example 1

var planet = 7;
var event = "a disturbance in the Force";
MessageFormat.format(
    "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
    planet, new Date(), event);

// -> 'At 9:05:47 PM on Feb 3, 2017, there was a disturbance in the Force on planet 7.'

Example 2

var form = new MessageFormat( "The disk \"{1}\" contains {0}.", new Locale( "en", "US" ) );
var fileLimits = [ 0, 1, 2 ];
var fileParts = [ "no files", "one file", "{0,number} files" ];
var fileForm = new ChoiceFormat( fileLimits, fileParts );
var fileCounts = [ 0, 1, 2, 3, 1273 ];
form.setFormatByArgumentIndex( 0, fileForm );
fileCounts.forEach( function( fileCount ) {
    form.format( [ fileCount, "MyDisk" ] );
} );

// -> 'The disk "MyDisk" contains no files.'
// -> 'The disk "MyDisk" contains one file.'
// -> 'The disk "MyDisk" contains 2 files.'
// -> 'The disk "MyDisk" contains 3 files.'
// -> 'The disk "MyDisk" contains 1,273 files.'

Learn more

For further information about how to use the text-formatjs library, see

License

Copyright (c) 2016 Yannick Ebongue

Released under the MIT License (see LICENSE.txt)