New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

number-display

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

number-display - npm Package Compare versions

Comparing version 2.0.3 to 2.1.0

7

CHANGELOG.md

@@ -58,1 +58,8 @@ ## 1.0.0

- Fix decimal trailing zeros with units.
## 2.1.0
**2019-12-02**
- Add feature: roundingType. Now you can set the way to round the decimal in 'round', 'floor' or 'ceil', witch default to 'round'.
- When the length is too small, number-display will return the origin value as a string, instead of throwing an error.

40

index.js

@@ -0,1 +1,10 @@

const rounding = (intStr, decimalStr, decimalLength, type) => {
decimalLength = Math.max(decimalLength, 0);
const handler = type === 'ceil' ? Math.ceil : (type === 'floor' ? Math.floor : Math.round);
const value = parseFloat(`${intStr}.${decimalStr}`);
const rstValue = handler(+value + 'e' + decimalLength) / Math.pow(10, decimalLength);
const rstStrs = String(rstValue).split('.')
return [rstStrs[0] || '', rstStrs[1] || ''];
}
/**

@@ -9,2 +18,3 @@ * Create a display function with configs. The function returned converts the value.

* @param {boolean} [comma] show commas between digits in group of 3, if there are rooms; default true
* @param {string} [roundingType] Rounding type of decimals, enum in 'round', 'floor' or 'ceil'; default 'round'
*/

@@ -17,2 +27,3 @@ const createDisplay = ({

comma = true,
roundingType = 'round',
} = {}) => value => {

@@ -37,20 +48,16 @@ placeholder = placeholder.slice(0, length);

const negative = cells[1];
const int = cells[2] || '0';
let [int, deci] = rounding(cells[2] || '0', cells[4] || '', decimal, roundingType);
const localeInt = int.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
const deci = (cells[4] || '').slice(0, decimal);
let currentLen = negative.length + localeInt.length + 1 + deci.length;
let deciShow;
if (comma && currentLen <= length) {
deciShow = deci.replace(/0+$/, '');
return `${negative}${localeInt}${deciShow && '.'}${deciShow}`;
deci = deci.replace(/0+$/, '');
return `${negative}${localeInt}${deci && '.'}${deci}`;
}
let space = length - negative.length - int.length;
if (space >= 2) {
deciShow = deci.slice(0, space - 1).replace(/0+$/, '');
return `${negative}${int}${deciShow && '.'}${deciShow}`;
}
if (space >= 0) {
return `${negative}${int}`;
[int, deci] = rounding(int, deci, space - 1, roundingType);
deci = deci.replace(/0+$/, '');
return `${negative}${int}${deci && '.'}${deci}`;
}

@@ -65,14 +72,13 @@

space = length - negative.length - mainSection.length - 1;
if (space >= 2) {
const tailShow = tailSection.slice(0, space - 1).replace(/0+$/, '');
return `${negative}${mainSection}${tailShow && '.'}${tailShow}${unit}`;
}
if (space >= 0) {
return `${negative}${mainSection}${unit}`;
let [main, tail] = rounding(mainSection, tailSection, space - 1, roundingType);
tail = tail.replace(/0+$/, '');
return `${negative}${main}${tail && '.'}${tail}${unit}`;
}
}
throw new Error(`length: ${length} is too small`);
console.error(`number-display: length: ${length} is too small for ${value}`);
return value;
};
module.exports = createDisplay;
{
"name": "number-display",
"version": "2.0.3",
"version": "2.1.0",
"description": "Display number smartly within a certain length.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -12,3 +12,3 @@ # number-display

- result chart length will never overflow length
- result char length will never overflow length, unless the length setting is too small(< 5).
- replace null or wrong type inputs ( null/NaN/object ) to placeholder

@@ -19,2 +19,3 @@ - use locale string with commas ( 1,234,222 ) as possible ( configurable )

- directly return input text if allowed
- when omitting decimals, you can change the rounding type, default to 'round'
- no decimal trailing zeros

@@ -63,3 +64,3 @@

-123456789.123456789 => '-123.456M'
-123456789.123456789 => '-123.457M'
'123456' => '123,456'

@@ -120,2 +121,8 @@ -1.2345e+5 => '-123,450'

Whether the locale string has commas ( 1,234,222 ), if there are rooms.
Whether the locale string has commas ( 1,234,222 ), if there are rooms.
**roundingType**
( default: 'round' )
The rounding type when omitting decimals, enum in 'round', 'floor' or 'ceil'.
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc