Formatter Utilities module.
Common formatters that we use across different projects. Note that many methods in this module use methods from other modules.
Methods
(inner) separateThousands(value, delimiters) → {string}
Separate input from right to left in buckets of 3 digits using specified delimiters. Useful to format prices.
Examples
separateThousands(12000); //=>'12,000'
separateThousands('1250000'); //=> '1,250,000'
separateThousands(-1000); //=> '-1,000'
separateThousands(1000.111); //=> '1,000.111'
Note that this function won't work with non numerical values. So expect:
separateThousands("hola"); //> 'hola'
separateThousands("0.123456"); //=> '0.123456'
separateThousands("1000.1234567"); //=> '1,000.1234567'
separateThousands("-100.10000"); //=> '-100.10000'
delimiters = { thousands: '.', decimal: ',' }
separateThousands(12000, delimiters); //=>'12.000'
separateThousands('1250000', delimiters); //=> '1.250.000'
separateThousands(-1000, delimiters); //=> '-1.000'
separateThousands(1000.111, delimiters); //=> '1.000,111'
Parameters:
Name | Type | Description |
---|---|---|
value |
number | string | Input that will be separated with specified thousands delimiter every 3 elements |
delimiters |
object | Object with delimiter settings. Default to { thousands: ',', decimal: '.' } |
Returns:
- Type
- string
(inner) snakeToCamelCase(value, first_letter) → {string}
Run a pipeline of formatters to transform a string in snake_case style to camelCase
Example
snakeToCamelCase('snake_case_string'); //=> 'snakeCaseString'
snakeToCamelCase('snake_case_string', true); //=> 'SnakeCaseString'
Parameters:
Name | Type | Description |
---|---|---|
value |
string | String in snake case style to be transformed to camel case |
first_letter |
boolean | Boolean that indicates if the first letter should be capitalized |
Returns:
- Type
- string
(inner) toPercentage(value, decimal_points) → {string}
Run a pipeline of formatters to transform a ranged 0 to 1 number into a
readable percentage.
The output would look like XX.XX %
Example
toPercentage(0.67489); //=> '67.49 %'
toPercentage(0.67439, 1); //=> '67.4 %'
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
number | Floating precision number to transform into percentage |
|
decimal_points |
number |
2
|
Floating points to take in the final format |
Returns:
- Type
- string