Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-abbreviation-number

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-abbreviation-number - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

_config.yml

7

.eslintrc.js

@@ -6,2 +6,4 @@ module.exports = {

es6: true,
node: true,
jest: true
},

@@ -11,7 +13,2 @@ extends: [

],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
process: true
},
parser: '@typescript-eslint/parser',

@@ -18,0 +15,0 @@ parserOptions: {

@@ -10,3 +10,3 @@ "use strict";

// what tier? (determines SI symbol)
var tier = Math.log10(num) / 3 | 0;
var tier = (Math.log10(num) / 3) | 0;
// if zero, we don't need a suffix

@@ -13,0 +13,0 @@ if (tier == 0)

@@ -0,6 +1,26 @@

interface Options {
padding?: boolean;
symbols?: string[];
}
const defaultOptions = {
padding: true,
symbols: ["", "K", "M", "G", "T", "P", "E"],
};
export function abbreviateNumber(
num: number,
digit: number = 1,
symbols: Array<string> = ["", "K", "M", "G", "T", "P", "E"],
options?: Options | Options["symbols"],
): string {
// Previous options style
if (Array.isArray(options)) {
options = { symbols: options };
}
const { symbols, padding }: Required<Options> = Object.assign(
{},
defaultOptions,
options,
);
// handle negatives

@@ -25,4 +45,9 @@ const sign = Math.sign(num) >= 0;

let rounded = scaled.toFixed(digit);
if (!padding) {
rounded = String(Number(rounded));
}
// format number and add suffix
return (!sign ? "-" : "") + scaled.toFixed(digit) + suffix;
return (!sign ? "-" : "") + rounded + suffix;
}
{
"name": "js-abbreviation-number",
"version": "1.0.0",
"version": "1.1.0",
"description": "Abbreviate numbers in javascript",

@@ -8,7 +8,8 @@ "main": "dist/index.js",

"scripts": {
"test": "jest --config=jest.config.json",
"test": "jest --config=jest.config.json --collectCoverage",
"build": "tsc",
"lint": "./node_modules/.bin/eslint *.ts",
"prettier-write": "./node_modules/.bin/prettier --write \"*.ts\"",
"prettier-check": "./node_modules/.bin/prettier --check \"*.ts\""
"prettier-check": "./node_modules/.bin/prettier --check \"*.ts\"",
"codecov": "./node_modules/.bin/codecov -t 6fc5b4e4-4e80-42dd-b9ed-057a670de47a"
},

@@ -36,2 +37,3 @@ "repository": {

"@typescript-eslint/parser": "^2.18.0",
"codecov": "^3.6.5",
"eslint": "^6.7.2",

@@ -38,0 +40,0 @@ "eslint-plugin-import": "^2.19.1",

@@ -1,13 +0,18 @@

# js-abbreviation-number &middot; [![Circle CI](https://circleci.com/gh/moh3n9595/js-abbreviation-number/tree/master.svg?style=shield)](https://circleci.com/gh/moh3n9595/js-abbreviation-number) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/moh3n9595/js-abbreviation-number/blob/master/LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-orange.svg)](https://github.com/moh3n9595/js-abbreviation-number/compare)
<div align="center">
<p align="center">
<img src="./images/cover.png"/>
</p>
<h1 align="center">js-abbreviation-number</h1>
<p align="center">Abbreviate numbers in javascript</p>
![CI/CD](https://github.com/moh3n9595/js-abbreviation-number/workflows/Continuous%20Integration/badge.svg)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/moh3n9595/js-abbreviation-number/blob/master/LICENSE)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-orange.svg)](https://github.com/moh3n9595/js-abbreviation-number/compare)
[![codecov](https://codecov.io/gh/moh3n9595/js-abbreviation-number/branch/master/graph/badge.svg)](https://codecov.io/gh/moh3n9595/js-abbreviation-number) [![CodeFactor](https://www.codefactor.io/repository/github/moh3n9595/js-abbreviation-number/badge/master)](https://www.codefactor.io/repository/github/moh3n9595/js-abbreviation-number/overview/master)
[![NPM](https://img.shields.io/npm/v/js-abbreviation-number.svg)](https://www.npmjs.com/package/js-abbreviation-number)
[![GitHub contributors](https://img.shields.io/github/contributors/moh3n9595/js-abbreviation-number.svg)](https://GitHub.com/moh3n9595/js-abbreviation-number/contributors/)
</div>
<hr />
<p align="center">
<img src="./images/cover.png">
<br>
<p align="center">
Abbreviate numbers in javascript
</p>
</p>
## Installation

@@ -29,13 +34,24 @@

const num = abbreviateNumber(1111, 2); // expected 1.11K
const num = abbreviateNumber(1100, 2); // expected 1.10K
const num = abbreviateNumber(1234.56, 1); // expected 1.2K
const num = abbreviateNumber(1100, 2, {padding: false}); // expected 1.1K
const num = abbreviateNumber(1234.56, 2); // expected 1.23K
const num = abbreviateNumber(1234, 1, {symbols: ['', 'kg']}); // expected 1.2kg
const num = abbreviateNumber(1234567, 1, {symbols: ['', 'kg']}); // expected 1234.5kg
```
`abbreviateNumber(num: number, digit?: number, symbols?: Array<string>): string`
The `symbols` is optional. It can be an array of units, defaulting to `["", "K", "M", "G", "T", "P", "E"]`.
`abbreviateNumber(num: number, digit?: number, options?: {symbols?: string[]; padding: boolean}): string`
`digit` is also optional. It defaults to 1.
`digit` is optional (unless you need to specify the `options` object). It defaults to 1.
The `options` object is optional too:
- `symbols` can be an array of units, defaulting to `["", "K", "M", "G", "T", "P", "E"]`.
- `padding` determines whether to keep the exact number of digits or to drop trailing zeroes.
## Contributing

@@ -42,0 +58,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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