Socket
Socket
Sign inDemoInstall

@betty-blocks/number-formatter

Package Overview
Dependencies
0
Maintainers
34
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @betty-blocks/number-formatter

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


Version published
Weekly downloads
139
decreased by-18.24%
Maintainers
34
Install size
112 kB
Created
Weekly downloads
 

Readme

Source

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: , . _ :

formatinputresult
0.0123.123123.1
0.000123.12313123.123
0,0123.123123,1

thousand seperator

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

formatinputresult
0,0.0123123.123123,123.1
0[]0,00123123.123123123,12
0[]0.123123.123123123
0_0:00123123.123123_123:12

force sign

formatinputresult
+0.023.123+23.1
+0.0-23.123-23.1

left padding

Adds zeroes to the left

formatinputresult
0.023.12323.1
00000.023.12300023.1
00000.23.12300023

optional fractional digits

Allow flexible numbers behind the decimal seperator.

formatinputresult
0.0[0000]23.12323.123
0.0[0000]2323.0
0.0[00]23.12323.123
0.0[00]23.1234678923.123
0.0[00]23.123.1

functions

ordinal

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

formatinputresult
0o23.12323th
0o11st
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

formatinputresult
0.0a12300001.2m
0. a12341 k
0. a-104321-104 k
0. a12341 k
0.a-104321-104k

currency

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

formatinputresult
$ 0.123.23$ 123
0. $123.23123 $
€0,00123.23€123,23
"MyCurrency" 0.00123.23MyCurrency 123.23

FAQs

Last updated on 28 May 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc