ts-number-lite 2.0.0 → 2.0.3
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 +45 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/number.d.ts +26 -0
- package/dist/number.js +51 -0
- package/package.json +6 -2
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# ts-number-lite
|
|
2
|
+
|
|
3
|
+
Lightweight, type-safe number utility library for web developers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ts-number-lite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Injectable } from '@angular/core';
|
|
15
|
+
import { NumberUtils } from 'ts-number-lite';
|
|
16
|
+
|
|
17
|
+
@Injectable({ providedIn: 'root' })
|
|
18
|
+
export class MyService {
|
|
19
|
+
constructor(private num: NumberUtils) { }
|
|
20
|
+
|
|
21
|
+
myMethod(): void {
|
|
22
|
+
this.num.formatCurrency(1234.56); // '$1,234.56'
|
|
23
|
+
this.num.formatBytes(1048576); // '1 MB'
|
|
24
|
+
this.num.formatPercent(0.756); // '75.6%'
|
|
25
|
+
this.num.clamp(150, 0, 100); // 100
|
|
26
|
+
this.num.round(3.14159, 2); // 3.14
|
|
27
|
+
this.num.randomInt(1, 10); // random int 1-10
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Methods
|
|
33
|
+
|
|
34
|
+
| Method | Description | Example |
|
|
35
|
+
|--------|-------------|---------|
|
|
36
|
+
| `formatCurrency` | Format as currency | `1234.56` → `'$1,234.56'` |
|
|
37
|
+
| `formatBytes` | Format as bytes | `1048576` → `'1 MB'` |
|
|
38
|
+
| `formatPercent` | Format as percent | `0.756` → `'75.6%'` |
|
|
39
|
+
| `clamp` | Clamp value to range | `150, 0, 100` → `100` |
|
|
40
|
+
| `round` | Round to decimals | `3.14159, 2` → `3.14` |
|
|
41
|
+
| `randomInt` | Random integer | `1, 10` → random 1-10 |
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './number';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './number';
|
package/dist/number.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class NumberUtils {
|
|
2
|
+
formatCurrency(value: number, currency?: string, locale?: string): string;
|
|
3
|
+
formatBytes(bytes: number, decimals?: number): string;
|
|
4
|
+
formatPercent(value: number, decimals?: number): string;
|
|
5
|
+
clamp(value: number, min: number, max: number): number;
|
|
6
|
+
round(value: number, decimals?: number): number;
|
|
7
|
+
randomInt(min: number, max: number): number;
|
|
8
|
+
randomFloat(min: number, max: number): number;
|
|
9
|
+
isEven(n: number): boolean;
|
|
10
|
+
isOdd(n: number): boolean;
|
|
11
|
+
isPositive(n: number): boolean;
|
|
12
|
+
isNegative(n: number): boolean;
|
|
13
|
+
isInteger(n: number): boolean;
|
|
14
|
+
isNaN(n: number): boolean;
|
|
15
|
+
isFinite(n: number): boolean;
|
|
16
|
+
abs(n: number): number;
|
|
17
|
+
floor(n: number): number;
|
|
18
|
+
ceil(n: number): number;
|
|
19
|
+
sqrt(n: number): number;
|
|
20
|
+
pow(n: number, exp: number): number;
|
|
21
|
+
sum(...nums: number[]): number;
|
|
22
|
+
avg(...nums: number[]): number;
|
|
23
|
+
min(...nums: number[]): number;
|
|
24
|
+
max(...nums: number[]): number;
|
|
25
|
+
range(start: number, end: number, step?: number): number[];
|
|
26
|
+
}
|
package/dist/number.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export class NumberUtils {
|
|
2
|
+
formatCurrency(value, currency = 'USD', locale = 'en-US') {
|
|
3
|
+
return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value);
|
|
4
|
+
}
|
|
5
|
+
formatBytes(bytes, decimals = 2) {
|
|
6
|
+
if (bytes === 0)
|
|
7
|
+
return '0 Bytes';
|
|
8
|
+
const k = 1024;
|
|
9
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
10
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
11
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
|
|
12
|
+
}
|
|
13
|
+
formatPercent(value, decimals = 2) {
|
|
14
|
+
return (value * 100).toFixed(decimals) + '%';
|
|
15
|
+
}
|
|
16
|
+
clamp(value, min, max) {
|
|
17
|
+
return Math.min(Math.max(value, min), max);
|
|
18
|
+
}
|
|
19
|
+
round(value, decimals = 0) {
|
|
20
|
+
const factor = Math.pow(10, decimals);
|
|
21
|
+
return Math.round(value * factor) / factor;
|
|
22
|
+
}
|
|
23
|
+
randomInt(min, max) {
|
|
24
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
25
|
+
}
|
|
26
|
+
randomFloat(min, max) {
|
|
27
|
+
return Math.random() * (max - min) + min;
|
|
28
|
+
}
|
|
29
|
+
isEven(n) { return n % 2 === 0; }
|
|
30
|
+
isOdd(n) { return n % 2 !== 0; }
|
|
31
|
+
isPositive(n) { return n > 0; }
|
|
32
|
+
isNegative(n) { return n < 0; }
|
|
33
|
+
isInteger(n) { return Number.isInteger(n); }
|
|
34
|
+
isNaN(n) { return Number.isNaN(n); }
|
|
35
|
+
isFinite(n) { return Number.isFinite(n); }
|
|
36
|
+
abs(n) { return Math.abs(n); }
|
|
37
|
+
floor(n) { return Math.floor(n); }
|
|
38
|
+
ceil(n) { return Math.ceil(n); }
|
|
39
|
+
sqrt(n) { return Math.sqrt(n); }
|
|
40
|
+
pow(n, exp) { return Math.pow(n, exp); }
|
|
41
|
+
sum(...nums) { return nums.reduce((a, b) => a + b, 0); }
|
|
42
|
+
avg(...nums) { return nums.length ? this.sum(...nums) / nums.length : 0; }
|
|
43
|
+
min(...nums) { return Math.min(...nums); }
|
|
44
|
+
max(...nums) { return Math.max(...nums); }
|
|
45
|
+
range(start, end, step = 1) {
|
|
46
|
+
const result = [];
|
|
47
|
+
for (let i = start; i < end; i += step)
|
|
48
|
+
result.push(i);
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-number-lite",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Lightweight, type-safe number utility library for web developers",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -26,5 +26,9 @@
|
|
|
26
26
|
"lite"
|
|
27
27
|
],
|
|
28
28
|
"author": "Gaurang Mody - G8X",
|
|
29
|
-
"license": "MIT"
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/gbmrocks/ts-utils-lite.git"
|
|
33
|
+
}
|
|
30
34
|
}
|