|
| 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); |
0 commit comments