js-format-kit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/index.js +16 -0
- package/package.json +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# js-format-kit
|
|
2
|
+
|
|
3
|
+
Tiny zero-dependency helpers for formatting numbers, bytes and percentages.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
```
|
|
7
|
+
npm install js-format-kit
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
```js
|
|
12
|
+
const { thousands, bytes, percent } = require('js-format-kit');
|
|
13
|
+
|
|
14
|
+
thousands(1234567); // "1,234,567"
|
|
15
|
+
bytes(1048576); // "1.0 MB"
|
|
16
|
+
percent(3, 8); // "37.5%"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## API
|
|
20
|
+
- `thousands(n)` — group digits with commas
|
|
21
|
+
- `bytes(n)` — human-readable byte size
|
|
22
|
+
- `percent(part, whole, digits=1)` — formatted percentage
|
|
23
|
+
|
|
24
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Small formatting helpers, no dependencies.
|
|
3
|
+
function thousands(n) {
|
|
4
|
+
return String(n).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
5
|
+
}
|
|
6
|
+
function bytes(n) {
|
|
7
|
+
const u = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
8
|
+
let i = 0;
|
|
9
|
+
while (n >= 1024 && i < u.length - 1) { n /= 1024; i++; }
|
|
10
|
+
return n.toFixed(i ? 1 : 0) + ' ' + u[i];
|
|
11
|
+
}
|
|
12
|
+
function percent(part, whole, digits = 1) {
|
|
13
|
+
if (!whole) return '0%';
|
|
14
|
+
return ((part / whole) * 100).toFixed(digits) + '%';
|
|
15
|
+
}
|
|
16
|
+
module.exports = { thousands, bytes, percent };
|
package/package.json
ADDED