number-display
Advanced tools
Comparing version 2.3.0 to 2.3.1
## 2.3.0 | ||
**2020-06-02** | ||
- Make decimal point configurable. | ||
## 2.3.0 | ||
**2020-03-12** | ||
@@ -4,0 +10,0 @@ |
/** | ||
* Create a display function with configs. The function returned converts the value. | ||
* | ||
* @param length - (Default 9) Max display char length (better >= 5 to allow any number). | ||
* @param decimal - (Default equals length) Max decimal precision. | ||
* @param placeholder - (Default '') Result when neither number nor text. | ||
* @param allowText - (Default false) Allow text as results, if false text will convert to placeholder, text will be slice within length param. | ||
* @param {boolean} [separator] set separators between digits in group of 3, if there are rooms; default is ',' | ||
* @param {number} [length] max display char length (better >= 5 to allow any number); default 9 | ||
* @param {number} [decimal] max decimal precision; default equals length | ||
* @param {string} [placeholder] result when neither number nor text; default '' | ||
* @param {boolean} [allowText] allow text as results, if false text will convert to placeholder, text will be slice within length param; default false | ||
* @param {string} [separator] set separators between digits in group of 3, if there are rooms; default is ',' | ||
* @param {string} [decimalPoint] set decimal point; default is '.' | ||
* @param {string} [roundingType] rounding type of decimals, enum in 'round', 'floor' or 'ceil'; default 'round' | ||
@@ -20,2 +21,3 @@ * @param {Array<string>} [units] digital units, default is ['k', 'M', 'G', 'T', 'P'] | ||
separator, | ||
decimalPoint, | ||
roundingType, | ||
@@ -27,2 +29,3 @@ units, | ||
placeholder?: string, | ||
decimalPoint?: string, | ||
allowText?: boolean, | ||
@@ -29,0 +32,0 @@ separator?: string, |
@@ -21,5 +21,8 @@ const maxPrecision = 12; | ||
* @param {boolean} [allowText] allow text as results, if false text will convert to placeholder, text will be slice within length param; default false | ||
* @param {boolean} [separator] set separators between digits in group of 3, if there are rooms; default is ',' | ||
* @param {string} [separator] set separators between digits in group of 3, if there are rooms; default is ',' | ||
* @param {string} [decimalPoint] set decimal point; default is '.' | ||
* @param {string} [roundingType] rounding type of decimals, enum in 'round', 'floor' or 'ceil'; default 'round' | ||
* @param {Array<string>} [units] digital units, default is ['k', 'M', 'G', 'T', 'P'] | ||
* | ||
* @returns The display function. | ||
*/ | ||
@@ -32,2 +35,3 @@ const createDisplay = ({ | ||
separator = ',', | ||
decimalPoint = '.', | ||
roundingType = 'round', | ||
@@ -62,2 +66,5 @@ units = ['k', 'M', 'G', 'T', 'P'], | ||
const localeInt = int.replace(/\B(?=(\d{3})+(?!\d))/g, ','); | ||
separator = separator && separator.slice(0, 1); | ||
decimalPoint = decimalPoint && decimalPoint.slice(0, 1); | ||
@@ -67,3 +74,3 @@ let currentLen = negative.length + localeInt.length + 1 + deci.length; | ||
deci = deci.replace(/0+$/, ''); | ||
return `${negative}${localeInt.replace(/,/g, separator.slice(0, 1))}${deci && '.'}${deci}`; | ||
return `${negative}${localeInt.replace(/,/g, separator)}${deci && decimalPoint}${deci}`; | ||
} | ||
@@ -75,3 +82,3 @@ | ||
deci = deci.replace(/0+$/, ''); | ||
return `${negative}${int}${deci && '.'}${deci}`; | ||
return `${negative}${int}${deci && decimalPoint}${deci}`; | ||
} | ||
@@ -89,3 +96,3 @@ | ||
tail = tail.replace(/0+$/, ''); | ||
return `${negative}${main}${tail && '.'}${tail}${unit}`; | ||
return `${negative}${main}${tail && decimalPoint}${tail}${unit}`; | ||
} | ||
@@ -92,0 +99,0 @@ } |
{ | ||
"name": "number-display", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"description": "Display number smartly within a certain length.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,3 +0,1 @@ | ||
[中文](https://github.com/entronad/number-display/blob/master/README_CN.md) | ||
# number-display | ||
@@ -94,2 +92,6 @@ | ||
## Locale | ||
By setting the `seperator` and `decimalPoint` , numbers can be any locale form: '4.623.933,8'. | ||
## Configurations | ||
@@ -105,3 +107,3 @@ | ||
( default: equals to 'length' ) | ||
( default: equals to `length` ) | ||
@@ -128,2 +130,8 @@ The max decimal length. Note that this is only a constraint. The final precision will be calculated by length, and less than this param. This param is the same as 'length' by default, witch means no additional limit. There will be no decimal trailing zeros. | ||
**decimalPoint** | ||
( default: '.' ) | ||
Set the locale string decimal point. Only the first char is kept. | ||
**roundingType** | ||
@@ -143,4 +151,2 @@ | ||
[En](https://medium.com/front-end-weekly/displaying-numbers-in-frontend-2336323493c2) | ||
[中文](https://zhuanlan.zhihu.com/p/85536865) | ||
[Displaying Numbers in Frontend](https://medium.com/front-end-weekly/displaying-numbers-in-frontend-2336323493c2) |
12915
119
148