Skip to content

Commit d93a898

Browse files
authored
🤖 Merge PR DefinitelyTyped#73481 Add the number-abbreviate npm package by @altie122
1 parent 907274e commit d93a898

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

types/number-abbreviate/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts

types/number-abbreviate/index.d.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Represents an instance of the NumberAbbreviate abbreviator.
3+
* Created by `new NumberAbbreviate(units)` or internally by the direct function call.
4+
*/
5+
interface AbbreviatorInstance {
6+
/**
7+
* Units used in the abbreviations
8+
*
9+
* @default ['k', 'm', 'b', 't']
10+
*/
11+
units: string[];
12+
/**
13+
* Abbreviates a number into a more readable format (e.g., 1000000 -> 1M).
14+
*
15+
* @param value number to be abbreviated. Non-numeric strings will result in `NaN`
16+
* @param decimalPlaces number of decimal places to include (default: 0).
17+
* @returns the abbreviated string or `NaN`
18+
*/
19+
abbreviate(value: number | string, decimalPlaces?: number): string;
20+
/**
21+
* Internal helper function for the abbreviation logic.
22+
*
23+
* @internal
24+
* @param number number to be abbreviated
25+
* @param decPlaces number of decimal places to include.
26+
* @returns the abbreviated string fragment without sign.
27+
*/
28+
_abbreviate(number: number, decPlaces: number): string;
29+
}
30+
31+
/**
32+
* A utility for abbreviating numbers into more readable formats (e.g., 1000000 becomes 1m).
33+
* Can be used as a direct function call for quick abbreviation or instantiated
34+
* as a class for custom unit configurations.
35+
*
36+
* @example
37+
* // Direct function call:
38+
* import abbreviate from "number-abbreviate";
39+
* console.log(abbreviate(1234567, 1)); // Outputs: "1.2m"
40+
*
41+
* @example
42+
* // Using as a constructor for custom units:
43+
* import NumberAbbreviateClass from "number-abbreviate";
44+
* const customAbbrev = new NumberAbbreviateClass(['k', 'M', 'B', 'T']);
45+
* console.log(customAbbrev.abbreviate(5000000000, 2)); // Outputs: "5.00B"
46+
*/
47+
interface NumberAbbreviateFunction {
48+
/**
49+
* Creates a new instance of the NumberAbbreviate utility with custom unit abbreviations.
50+
* @param units Optional. An array of custom unit strings (default: ['k', 'm', 'b', 't']).
51+
*/
52+
new(units?: string[]): AbbreviatorInstance;
53+
/**
54+
* Abbreviates a number into a more readable format.
55+
* @param value number or string to abbreviate. Non-numeric strings will result in `NaN`
56+
* @param decimalPlaces number of decimal places to include (default: `0`).
57+
* @param units an array of custom unit strings for this specific call, used in order. (default: `['k', 'm', 'b', 't']`)
58+
* @returns the abbreviated string or `NaN`
59+
*/
60+
(value: number | string, decimalPlaces?: number, units?: string[]): string;
61+
}
62+
63+
declare const NumberAbbreviate: NumberAbbreviateFunction; // <--- CHANGE HERE: Added 'declare'
64+
export = NumberAbbreviate;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import NumberAbbreviate from "number-abbreviate";
2+
3+
const num: number = 12345;
4+
const strNum: string = "98765";
5+
const nonNumericStr: string = "hello";
6+
7+
// Test the direct function call (shorthand mode)
8+
const res1: string = NumberAbbreviate(1000);
9+
const res2: string = NumberAbbreviate(1111, 0);
10+
const res3: string = NumberAbbreviate(1111, 1);
11+
const res4: string = NumberAbbreviate(1111, 2);
12+
const res5: string = NumberAbbreviate(num);
13+
const res6: string = NumberAbbreviate(strNum, 1);
14+
const res7: string = NumberAbbreviate(-12345);
15+
const res8: string = NumberAbbreviate(123456789, 2, ["k", "M", "B"]);
16+
17+
// @ts-expect-error
18+
NumberAbbreviate("invalid", true, ["a"]);
19+
20+
// Test the class/constructor mode
21+
const numAbbr1 = new NumberAbbreviate();
22+
const res9: string = numAbbr1.abbreviate(12, 1);
23+
const res10: string = numAbbr1.abbreviate(0, 2);
24+
const res11: string = numAbbr1.abbreviate(1234, 0);
25+
const res12: string = numAbbr1.abbreviate(34567, 2);
26+
const res13: string = numAbbr1.abbreviate(918395, 1);
27+
const res14: string = numAbbr1.abbreviate(2134124, 2);
28+
const res15: string = numAbbr1.abbreviate(47475782130, 2);
29+
const res16: string = numAbbr1.abbreviate(-1234, 0);
30+
const res17: string = numAbbr1.abbreviate(-918395, 1);
31+
const res18: string = numAbbr1.abbreviate(-47475782130, 2);
32+
const res19: string = numAbbr1.abbreviate(num, 0);
33+
const res20: string = numAbbr1.abbreviate(strNum);
34+
35+
// @ts-expect-error
36+
numAbbr1.abbreviate("invalid", true);
37+
38+
// Test constructor with custom units
39+
const customUnits = ["A", "B", "C"];
40+
const numAbbr2 = new NumberAbbreviate(customUnits);
41+
const res21: string = numAbbr2.abbreviate(1000, 0);
42+
const res22: string = numAbbr2.abbreviate(1000000, 1);
43+
const res23: string = numAbbr2.abbreviate(1000000000, 2);
44+
const res24: string = numAbbr2.abbreviate(num, 1);
45+
const res25: string = numAbbr2.abbreviate(strNum, 2);
46+
47+
// Test instance properties
48+
const unitsProp1: string[] = numAbbr1.units;
49+
const unitsProp2: string[] = numAbbr2.units;
50+
51+
// Verify `_abbreviate` type (internal, but we typed it)
52+
const internalAbbrResult: string = numAbbr1._abbreviate(12345, 0);
53+
// @ts-expect-error
54+
numAbbr1._abbreviate(123, "not_a_number");
55+
56+
// Test handling of non-numeric strings resulting in NaN (runtime)
57+
const nanResult1: string = NumberAbbreviate(nonNumericStr);
58+
const nanResult2: string = numAbbr1.abbreviate(nonNumericStr);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/number-abbreviate",
4+
"version": "2.0.9999",
5+
"projects": [
6+
"https://github.com/domharrington/js-number-abbreviate#readme"
7+
],
8+
"devDependencies": {
9+
"@types/number-abbreviate": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "Alton Rose",
14+
"githubUsername": "altie122"
15+
}
16+
]
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"number-abbreviate-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)