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

react-format-kit

v0.2.3

Published

A TypeScript-based React component for formatting various data types such as dates, numbers, currencies and units with support for multiple formats and localization.

Downloads

432

Readme

React Format Kit

A TypeScript-based React component for formatting various data types such as dates, numbers, currencies and units with support for multiple formats and localization. Powered by the native Intl API, this library simplifies the process of displaying localized data formats across your React applications.

Package: Go to npm

Features

  • 📅 Date Formatting: Supports multiple date formats (ISO, numeric, human-readable).
  • 💰 Number and Currency Formatting: Easily format numbers, percentages and currencies with localization support.
  • 📏 Unit Formatting: (TODO) Format units of measure such as meters, kilometers, and more.
  • 🌍 Localization: Full locale support using the Intl API for internationalized applications.
  • 🔧 TypeScript: Written in TypeScript with full type declarations, providing autocompletion and type safety.
  • 📊 Expandable: Designed to be expanded with additional formatting components in the future.

Installation

To install, simply run the following command:

npm i react-format-kit

Examples

Format Date

The FormatDate component returns a time tag with formatted date.
The date prop must be a string that Date.parse() can interpret or a Date object.
The format prop can be one of those values: iso, numeric, human or human-long.
If locale is not passed, navigator.language is used or, if it is not specified, the 'en-US' locale is used.

import React from 'react';
import { FormatDate } from 'react-format-kit';

const App = () => (
  <div>
    {/* Human-readable format: current date with current locale */}
    <FormatDate format="human" />

    {/* Numeric format: will render 10/09/1991 */}
    <FormatDate date="1991-10-09T10:00:00Z" format="numeric" locale="en-US" />

    {/* ISO format: will render "1991-10-09T10:00:00.000Z" */}
    <FormatDate date="1991-10-09T10:00:00Z" format="iso" />
  </div>
);

The FormatDate component accepts the following props:

| Prop | Type | Default | Description | |-----------|---------------|----------|------------| | date | Date or string | new Date() | The date to format. | | format | iso \ numeric \ human \ human-long | human| The format to display the date: ISO format, numeric, human-readable, or long human-readable format. | | locale | string | navigator.language OR 'en-US' | The locale to use for formatting the date (e.g., 'en-US', 'fr-FR'). |

Format Number

The FormatNumber component returns a span tag with formatted number.
The minimumFractionDigits and maximumFractionDigits are set to default value of 0.
If locale is not passed, navigator.language is used or, if it is not specified, the 'en-US' locale is used.

import React from 'react';
import { FormatNumber } from 'react-format-kit';

const App = () => (
  <div>
    {/* Will render 10,000 */}
    <FormatNumber value={10000} locale='en-US' />

    {/* Will render 10.000 */}
    <FormatNumber value={10000} locale='it-IT' />

    {/* Will render 10.000,98 */}
    <FormatNumber value={10000.98} minimumFractionDigits={1} maximumFractionDigits={2} locale='it-IT' />
  </div>
);

The FormatNumber component accepts the following props:

| Prop | Type | Default | Description | |-----------|---------------|----------|------------| | value | number | required | The number to format. | | minimumFractionDigits | number | 0 | The minimum fraction digits to display. | | maximumFractionDigits | number | 0 | The maximum fraction digits to display. | | locale | string | navigator.language OR 'en-US' | The locale to use for formatting the date (e.g., 'en-US', 'fr-FR'). |

Format Currency

The FormatCurrency component returns a span tag with formatted number.
The currency will be to default USD if not specified. Must be a valid currency in ISO 4217 list.
The minimumFractionDigits and maximumFractionDigits are set to default value of 0.
If locale is not passed, navigator.language is used or, if it is not specified, the 'en-US' locale is used.

import React from 'react';
import { FormatCurrency } from 'react-format-kit';

const App = () => (
  <div>
    {/* Will render €10,000 */}
    <FormatCurrency value={10000} currency='EUR' locale='en-US' />

    {/* Will render 10.000 € */}
    <FormatCurrency value={10000} currency='EUR' />
  </div>
);

The FormatCurrency component accepts the following props:

| Prop | Type | Default | Description | |-----------|---------------|----------|------------| | value | number | required | The number to format. | | currency | string | USD | Currency | | minimumFractionDigits | number | 0 | The minimum fraction digits to display. | | maximumFractionDigits | number | 0 | The maximum fraction digits to display. | | locale | string | navigator.language OR 'en-US' | The locale to use for formatting the date (e.g., 'en-US', 'fr-FR'). |

Format Percentage

The FormatPercentage component returns a span tag with formatted number.
The value must be a number that needs to be multiplied by 100 (e.g. 0.75 instead of 75).
The minimumFractionDigits and maximumFractionDigits are set to default value of 0.
If locale is not passed, navigator.language is used or, if it is not specified, the 'en-US' locale is used.

import React from 'react';
import { FormatPercentage } from 'react-format-kit';

const App = () => (
  <div>
    {/* Will render 2.275% */}
    <FormatPercentage value={22.75} />

    {/* Will render 2,275% */}
    <FormatPercentage value={22.75} locale='en-US' />
  </div>
);

The FormatPercentage component accepts the following props:

| Prop | Type | Default | Description | |-----------|---------------|----------|------------| | value | number | required | The number to format. | | minimumFractionDigits | number | 0 | The minimum fraction digits to display. | | maximumFractionDigits | number | 0 | The maximum fraction digits to display. | | locale | string | navigator.language OR 'en-US' | The locale to use for formatting the date (e.g., 'en-US', 'fr-FR'). |

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request on GitHub.