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

@betty-blocks/number-formatter

v2.1.0

Published

number formatter based on the syntax of [numeral.js](http://numeraljs.com/)

Downloads

796

Readme

Number Formatter

number formatter based on the syntax of numeral.js

The library works like:

  • parse user format
  • create config from parsed format
  • format using the generated config

The parser is generated from a pegjs grammar.

Commands

After changing the grammar you need to regenerate the parser the grammar.pegjs:

yarn generate

Run tests:

yarn test

Run formatter (prettier):

yarn fmt

Getting started

import { createConfig, format } from "@betty-blocks/number-formatter";

const myFormat = "0_0,00";

// create a config from your format
const { config } = createConfig(myFormat);

const myNumber = 123123.123;

// prints 123_123,12
console.log(format(myNumber, config));

other example:

import { createConfig, format } from "@betty-blocks/number-formatter";

const formatNumber = (number: number, formatConfig: string): string => {
  let { config, error } = createConfig(formatConfig);
  if (error) {
    return error;
  }
  return format(number, config);
};
import { createConfig, format } from "@betty-blocks/number-formatter";
import { Config } from "@betty-blocks/number-formatter/src/types";

const DEFAULT_FORMAT = "0.00";

const formatNumbers = (numbers: number[], formatConfig: string): string[] => {
  let {config, error} = createConfig(formatConfig);
  if (error) {
    {config} = createConfig(DEFAULT_FORMAT)
  }

  return numbers.map((number) => format(number, config));
};

Features

decimal/fractional seperator

Will use the first symbol that is encountered current allowed symbols are one of: , . _ :

| format | input | result | | ------ | --------- | ------- | | 0.0 | 123.123 | 123.1 | | 0.000 | 123.12313 | 123.123 | | 0,0 | 123.123 | 123,1 |

thousand seperator

Will use the first occurrence of a symbol between [] or if two decimal seperators are encountered.

| format | input | result | | -------- | ---------- | ---------- | | 0,0.0 | 123123.123 | 123,123.1 | | 0[]0,00 | 123123.123 | 123123,12 | | 0[]0. | 123123.123 | 123123 | | 0_0:00 | 123123.123 | 123_123:12 |

force sign

| format | input | result | | ------ | ------- | ------ | | +0.0 | 23.123 | +23.1 | | +0.0 | -23.123 | -23.1 |

left padding

Adds zeroes to the left

| format | input | result | | ------- | ------ | ------- | | 0.0 | 23.123 | 23.1 | | 00000.0 | 23.123 | 00023.1 | | 00000. | 23.123 | 00023 |

optional fractional digits

Allow flexible numbers behind the decimal seperator.

| format | input | result | | ---------- | ----------- | ------ | | 0.0[0000] | 23.123 | 23.123 | | 0.0[0000] | 23 | 23.0 | | 0.0[00] | 23.123 | 23.123 | | 0.0[00] | 23.12346789 | 23.123 | | 0.0[00] | 23.1 | 23.1 |

functions

ordinal

Or postition, rank ect. Postfixes 1, 2 and 3 with st, nd and rd other numbers get the th postfix

| format | input | result | | ------ | ------ | ------ | | 0o | 23.123 | 23th | | 0o | 1 | 1st |

average

if a number is less than 1000 get no postfix if a number is between 1000 and 999999 gets a k postfix if a number is greater than 1000000 gets a m postfix

| format | input | result | | ------ | ------- | ------ | | 0.0a | 1230000 | 1.2m | | 0. a | 1234 | 1 k | | 0. a | -104321 | -104 k | | 0. a | 1234 | 1 k | | 0.a | -104321 | -104k |

currency

Any text that is encountered will be used as a currency symbol

| format | input | result | | ----------------- | ------ | ----------------- | | $ 0. | 123.23 | $ 123 | | 0. $ | 123.23 | 123 $ | | €0,00 | 123.23 | €123,23 | | "MyCurrency" 0.00 | 123.23 | MyCurrency 123.23 |