2.1.0 • Published 4 years ago

@betty-blocks/number-formatter v2.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

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
2.1.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago