js-abbreviation-number
Advanced tools
Comparing version 0.2.3 to 1.0.0
@@ -26,2 +26,2 @@ module.exports = { | ||
} | ||
}; | ||
}; |
43
index.ts
@@ -0,28 +1,27 @@ | ||
export function abbreviateNumber( | ||
num: number, | ||
digit: number = 1, | ||
symbols: Array<string> = ["", "K", "M", "G", "T", "P", "E"], | ||
): string { | ||
// handle negatives | ||
const sign = Math.sign(num) >= 0; | ||
num = Math.abs(num); | ||
// what tier? (determines SI symbol) | ||
const tier = (Math.log10(num) / 3) | 0; | ||
export function abbreviateNumber(num: number, digit: number = 1, symbols: Array<string> = ["", "K", "M", "G", "T", "P", "E"]): string { | ||
// if zero, we don't need a suffix | ||
if (tier == 0) return num.toString(); | ||
// handle negatives | ||
const sign = Math.sign(num) >= 0; | ||
num = Math.abs(num); | ||
// get suffix and determine scale | ||
const suffix = symbols[tier]; | ||
if (!suffix) throw new RangeError(); | ||
// what tier? (determines SI symbol) | ||
const tier = Math.log10(num) / 3 | 0; | ||
const scale = Math.pow(10, tier * 3); | ||
// if zero, we don't need a suffix | ||
if(tier == 0) return num.toString(); | ||
// scale the number | ||
const scaled = num / scale; | ||
// get suffix and determine scale | ||
const suffix = symbols[tier]; | ||
if(!suffix) | ||
throw new RangeError(); | ||
const scale = Math.pow(10, tier * 3); | ||
// scale the number | ||
const scaled = num / scale; | ||
// format number and add suffix | ||
return (!sign?"-":"") + scaled.toFixed(digit) + suffix; | ||
} | ||
// format number and add suffix | ||
return (!sign ? "-" : "") + scaled.toFixed(digit) + suffix; | ||
} |
{ | ||
"name": "js-abbreviation-number", | ||
"version": "0.2.3", | ||
"version": "1.0.0", | ||
"description": "Abbreviate numbers in javascript", | ||
@@ -10,3 +10,5 @@ "main": "dist/index.js", | ||
"build": "tsc", | ||
"lint": "./node_modules/.bin/eslint *.ts" | ||
"lint": "./node_modules/.bin/eslint *.ts", | ||
"prettier-write": "./node_modules/.bin/prettier --write \"*.ts\"", | ||
"prettier-check": "./node_modules/.bin/prettier --check \"*.ts\"" | ||
}, | ||
@@ -37,2 +39,3 @@ "repository": { | ||
"jest": "^25.1.0", | ||
"prettier": "1.19.1", | ||
"ts-jest": "^22.4.1", | ||
@@ -39,0 +42,0 @@ "typescript": "^3.7.5" |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
52788
14
138
0
9