@rbxts/abbreviate 3.0.3 → 3.0.5
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 +56 -98
- package/package.json +39 -40
- package/src/{commify.lua → commify.luau} +1 -1
- package/src/index.d.ts +21 -4
- package/src/{init.lua → init.luau} +51 -51
- package/src/{numberToString.lua → numberToString.luau} +18 -14
- package/src/{numbersToSortedString.lua → numbersToSortedString.luau} +1 -1
- package/src/{setSetting.lua → setSetting.luau} +1 -1
- package/src/{stringToNumber.lua → stringToNumber.luau} +1 -1
package/README.md
CHANGED
|
@@ -1,98 +1,56 @@
|
|
|
1
|
-
# @rbxts/abbreviate
|
|
2
|
-
|
|
3
|
-
[](https://npmjs.org/package/@rbxts/abbreviate)
|
|
4
|
-
|
|
5
|
-
## Installation:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
abbreviator
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
print(abbreviator.stringToNumber(
|
|
19
|
-
print(abbreviator.stringToNumber(
|
|
20
|
-
|
|
21
|
-
print(abbreviator.
|
|
22
|
-
|
|
23
|
-
print(abbreviator.numberToString(
|
|
24
|
-
print(abbreviator.numberToString(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
-
|
|
55
|
-
-
|
|
56
|
-
-
|
|
57
|
-
|
|
58
|
-
### 2.7.3
|
|
59
|
-
- Updated `README.md` to use `createAbbreviator()`
|
|
60
|
-
|
|
61
|
-
### 2.7.2
|
|
62
|
-
- Refactored internal code to use less memory
|
|
63
|
-
|
|
64
|
-
### 2.7.1
|
|
65
|
-
- Fixed a case where numbers under 1000 would have their decimal places stripped (e.g. `1.05` would become `1`, `0.5` would become `0`)
|
|
66
|
-
- Added unit tests
|
|
67
|
-
- Made function calls safer (it now validates the type of the data you pass)
|
|
68
|
-
|
|
69
|
-
### 2.7.0
|
|
70
|
-
- Changed the method used in `numberToString` to be similar to [Zombie Strike's](https://github.com/Kampfkarren/zombie-strike/blob/master/src/shared/ReplicatedStorage/Core/EnglishNumbers.lua) for more accurate rounding.
|
|
71
|
-
|
|
72
|
-
### 2.6.3
|
|
73
|
-
- Fixed numbers under 1000 not being added to the return result in `numbersToSortedString`.
|
|
74
|
-
|
|
75
|
-
### 2.6.2
|
|
76
|
-
- Removed prints in `numbersToSortedString`.
|
|
77
|
-
|
|
78
|
-
### 2.6.1
|
|
79
|
-
- Updated README.md.
|
|
80
|
-
|
|
81
|
-
### 2.6.0
|
|
82
|
-
- Created `numbersToSortedString`.
|
|
83
|
-
|
|
84
|
-
### 2.5.0
|
|
85
|
-
- Fixed a bug with `commify` function erroring.
|
|
86
|
-
|
|
87
|
-
### 2.4.0
|
|
88
|
-
- Added `commify(num: number): string` to convert a string into a comma separated value.
|
|
89
|
-
|
|
90
|
-
### 2.3.4
|
|
91
|
-
- Fixed `stringToNumber` returning `void` instead of `number`.
|
|
92
|
-
### 2.3.3
|
|
93
|
-
- Fixed numbers under 1000 not being decimal placed correctly when numberToString is called.
|
|
94
|
-
|
|
95
|
-
## Credits
|
|
96
|
-
[Kampfkarren](http://github.com/Kampfkarren/) - `numberToString` method
|
|
97
|
-
[Corecii](https://github.com/Corecii) - Help with `numbersToSortedString`
|
|
98
|
-
[Scyfren](https://github.com/Scyfren) - Help with `stripTrailingZeroes`
|
|
1
|
+
# @rbxts/abbreviate
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.org/package/@rbxts/abbreviate)
|
|
4
|
+
|
|
5
|
+
## Installation:
|
|
6
|
+
|
|
7
|
+
`npm i @rbxts/abbreviate`
|
|
8
|
+
|
|
9
|
+
## Example Usage
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import Abbreviator from "@rbxts/abbreviate";
|
|
13
|
+
|
|
14
|
+
const abbreviator = new Abbreviator();
|
|
15
|
+
abbreviator.setSetting("suffixTable", ["k", "m", "b"]);
|
|
16
|
+
abbreviator.setSetting("decimalPlaces", 2);
|
|
17
|
+
|
|
18
|
+
print(abbreviator.stringToNumber("500")); // 500
|
|
19
|
+
print(abbreviator.stringToNumber("5k")); // 5000
|
|
20
|
+
print(abbreviator.stringToNumber("5m")); // 5000000
|
|
21
|
+
print(abbreviator.stringToNumber("1.23456m")); // 1234560
|
|
22
|
+
|
|
23
|
+
print(abbreviator.numberToString(999)); // 999
|
|
24
|
+
print(abbreviator.numberToString(1000)); // 1.00k
|
|
25
|
+
print(abbreviator.numberToString(1000000)); // 1.00m
|
|
26
|
+
print(abbreviator.numberToString(1234567)); // 1.23m
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Settings
|
|
30
|
+
|
|
31
|
+
The possible settings you can set are the following:
|
|
32
|
+
| Setting Name | Setting Value Type | Setting Description | Setting Default | Setting Example
|
|
33
|
+
|---|---|---|---|---|
|
|
34
|
+
| suffixTable | Array\<string\> | Sets the suffix table to be used when using `numberToString` | [here](https://github.com/OverHash/Roblox-TS-Libraries/blob/master/abbreviate/init.lua#L1-L52) | <pre lang="ts">["k", "m", "b"] |
|
|
35
|
+
| decimalPlaces | number | Sets the amount of decimal places a number may have when using `numberToString` | 2 | 4 |
|
|
36
|
+
| stripTrailingZeroes | boolean | Removes any extra zeroes after a decimal place that are dangling after `numberToString` calls. E.g. `"52506.004"` => `"5.2506k"` | false | `true`
|
|
37
|
+
|
|
38
|
+
## Why make `Abbreviator` a class?
|
|
39
|
+
|
|
40
|
+
You may want multiple abbreviators throughout your game with different settings, i.e. one module may want only 2 d.p. while another may want 0 d.p
|
|
41
|
+
To solve this, abbreviate requires you to construct a new abbreviator.
|
|
42
|
+
The settings of this abbreviator is independent of other abbreviators.
|
|
43
|
+
|
|
44
|
+
## Changelog
|
|
45
|
+
|
|
46
|
+
See [CHANGELOG.md](https://github.com/OverHash/Roblox-TS-Libraries/blob/master/abbreviate/CHANGELOG.md)
|
|
47
|
+
|
|
48
|
+
## Contributing
|
|
49
|
+
|
|
50
|
+
See [CONTRIBUTING.md](https://github.com/OverHash/Roblox-TS-Libraries/blob/master/abbreviate/CONTRIBUTING.md)
|
|
51
|
+
|
|
52
|
+
## Credits
|
|
53
|
+
|
|
54
|
+
- [Kampfkarren](http://github.com/Kampfkarren/) - `numberToString` method
|
|
55
|
+
- [Corecii](https://github.com/Corecii) - Help with `numbersToSortedString`
|
|
56
|
+
- [Scyfren](https://github.com/Scyfren) - Help with `stripTrailingZeroes`
|
package/package.json
CHANGED
|
@@ -1,40 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@rbxts/abbreviate",
|
|
3
|
+
"version": "3.0.5",
|
|
4
|
+
"description": "A lightweight number abbreviator",
|
|
5
|
+
"main": "src/init.lua",
|
|
6
|
+
"types": "src/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/OverHash/Roblox-TS-Libraries/tree/master/abbreviate"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"abbreviate",
|
|
16
|
+
"roblox",
|
|
17
|
+
"roblox-ts",
|
|
18
|
+
"roblox-typescript",
|
|
19
|
+
"number"
|
|
20
|
+
],
|
|
21
|
+
"author": "OverHash",
|
|
22
|
+
"license": "mpl-2.0",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/OverHash/Roblox-TS-Libraries/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/OverHash/Roblox-TS-Libraries/blob/master/abbreviate/README.md",
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"src/init.luau",
|
|
32
|
+
"src/index.d.ts",
|
|
33
|
+
"src/commify.luau",
|
|
34
|
+
"src/numbersToSortedString.luau",
|
|
35
|
+
"src/numberToString.luau",
|
|
36
|
+
"src/setSetting.luau",
|
|
37
|
+
"src/stringToNumber.luau"
|
|
38
|
+
]
|
|
39
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ declare class Abbreviator {
|
|
|
13
13
|
* @example
|
|
14
14
|
* 10 => "10"
|
|
15
15
|
* 10.1234 => "10.1234"
|
|
16
|
-
*
|
|
16
|
+
* 1400 => "1,400"
|
|
17
17
|
* 123456789 => "123,456,789"
|
|
18
18
|
* 1234567890 => "1,234,567,890"
|
|
19
19
|
* @param number The number to comma separate
|
|
@@ -21,14 +21,31 @@ declare class Abbreviator {
|
|
|
21
21
|
commify(number: number): string;
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* Converts a number
|
|
24
|
+
* Converts a number into an abbreviated string using the configured suffix table.
|
|
25
|
+
*
|
|
26
|
+
* Defaulting to `roundDown = true` avoids overstating balances in UI (e.g. 1.499k won't display as 1.50k),
|
|
27
|
+
* which helps prevent players thinking they can afford a purchase when they cannot.
|
|
28
|
+
*
|
|
25
29
|
* @param number The number to convert into a abbreviated string
|
|
26
|
-
* @param roundDown If the abbreviation should round up or down. @default
|
|
30
|
+
* @param roundDown If the abbreviation should round up or down. @default true.
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const abbreviator = new Abbreviator();
|
|
34
|
+
*
|
|
35
|
+
* abbreviator.numberToString(1000); // "1.00k"
|
|
36
|
+
*
|
|
37
|
+
* abbreviator.numberToString(1005); // "1.00k" // default will round down
|
|
38
|
+
* abbreviator.numberToString(1005, false); // "1.01k"
|
|
39
|
+
*
|
|
40
|
+
* abbreviator.numberToString(999995, false); // "1.00M"
|
|
41
|
+
* ```
|
|
27
42
|
*/
|
|
28
43
|
numberToString(number: number, roundDown?: boolean): string;
|
|
29
44
|
|
|
30
45
|
/**
|
|
31
|
-
* Converts an array of numbers into sorted strings with a suffix as defined in the prefix table
|
|
46
|
+
* Converts an array of numbers into sorted strings with a suffix as defined in the prefix table.
|
|
47
|
+
*
|
|
48
|
+
* This is often useful when using default Roblox leaderboards, as they otherwise won't sort correctly.
|
|
32
49
|
* @param numbers The numbers to abbreviate and sort correctly
|
|
33
50
|
* @example
|
|
34
51
|
* ```ts
|
|
@@ -1,54 +1,54 @@
|
|
|
1
1
|
local DEFAULT_SUFFIX_TABLE = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
2
|
+
"k",
|
|
3
|
+
"M",
|
|
4
|
+
"B",
|
|
5
|
+
"T",
|
|
6
|
+
"Qd",
|
|
7
|
+
"Qn",
|
|
8
|
+
"Sx",
|
|
9
|
+
"Sp",
|
|
10
|
+
"O",
|
|
11
|
+
"N",
|
|
12
|
+
"De",
|
|
13
|
+
"Ud",
|
|
14
|
+
"DD",
|
|
15
|
+
"tdD",
|
|
16
|
+
"QnD",
|
|
17
|
+
"SxD",
|
|
18
|
+
"SpD",
|
|
19
|
+
"OcD",
|
|
20
|
+
"NvD",
|
|
21
|
+
"VgN",
|
|
22
|
+
"UvG",
|
|
23
|
+
"DvG",
|
|
24
|
+
"TvG",
|
|
25
|
+
"QtV",
|
|
26
|
+
"QnV",
|
|
27
|
+
"SeV",
|
|
28
|
+
"SpG",
|
|
29
|
+
"OvG",
|
|
30
|
+
"NvG",
|
|
31
|
+
"TgN",
|
|
32
|
+
"UtG",
|
|
33
|
+
"DtG",
|
|
34
|
+
"TsTg",
|
|
35
|
+
"QtTg",
|
|
36
|
+
"QnTg",
|
|
37
|
+
"SsTg",
|
|
38
|
+
"SpTg",
|
|
39
|
+
"OcTg",
|
|
40
|
+
"NoTg",
|
|
41
|
+
"QdDr",
|
|
42
|
+
"UnAg",
|
|
43
|
+
"DuAg",
|
|
44
|
+
"TeAg",
|
|
45
|
+
"QdAg",
|
|
46
|
+
"QnAG",
|
|
47
|
+
"SxAg",
|
|
48
|
+
"SpAg",
|
|
49
|
+
"OcAg",
|
|
50
|
+
"NvAg",
|
|
51
|
+
"CT",
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
local setSetting = require(script.setSetting).setSetting
|
|
@@ -70,5 +70,5 @@ return {
|
|
|
70
70
|
stringToNumber = stringToNumber,
|
|
71
71
|
commify = commify,
|
|
72
72
|
}
|
|
73
|
-
end
|
|
73
|
+
end,
|
|
74
74
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
local function round(number, decimalPlaces, roundDown)
|
|
2
|
-
number = number * 10^decimalPlaces
|
|
2
|
+
number = number * 10 ^ decimalPlaces
|
|
3
3
|
if roundDown then
|
|
4
|
-
return math.floor(number) / 10^decimalPlaces
|
|
4
|
+
return math.floor(number) / 10 ^ decimalPlaces
|
|
5
5
|
else
|
|
6
|
-
number = tonumber(
|
|
7
|
-
-- cast to string and back to number to
|
|
6
|
+
number = tonumber(string.format("%.14g", number))
|
|
7
|
+
-- cast to string and back to number to give some epsilon for floating point numbers
|
|
8
8
|
--[[ e.g.:
|
|
9
9
|
local number = 1005 / 10^3 * 10^2 + 0.5 -- 101
|
|
10
10
|
print(number, math.floor(number))
|
|
@@ -15,13 +15,13 @@ local function round(number, decimalPlaces, roundDown)
|
|
|
15
15
|
print(number, math.floor(number))
|
|
16
16
|
> 101 101
|
|
17
17
|
]]
|
|
18
|
-
return math.floor(number + 0.5) / 10^decimalPlaces
|
|
18
|
+
return math.floor(number + 0.5) / 10 ^ decimalPlaces
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
function numberToString(self, number, roundDown)
|
|
22
|
+
local function numberToString(self, number, roundDown)
|
|
23
23
|
if type(number) ~= "number" then
|
|
24
|
-
error(
|
|
24
|
+
error(`numberToString invalid parameter #1, expected number, got "${type(number)}"`, 2)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
if roundDown == nil then
|
|
@@ -30,11 +30,15 @@ function numberToString(self, number, roundDown)
|
|
|
30
30
|
|
|
31
31
|
if number < 1000 and number > -1000 then
|
|
32
32
|
-- special case: we must manually abbreviate numbers between -1000 and 1000
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
local rounded = round(number, self._decimalPlaces, roundDown)
|
|
34
|
+
if math.abs(rounded) < 1000 then
|
|
35
|
+
if not self._stripTrailingZeroes then
|
|
36
|
+
return ("%." .. self._decimalPlaces .. "f"):format(rounded)
|
|
37
|
+
else
|
|
38
|
+
return tostring(rounded)
|
|
39
|
+
end
|
|
35
40
|
else
|
|
36
|
-
number =
|
|
37
|
-
return tostring(number)
|
|
41
|
+
number = rounded
|
|
38
42
|
end
|
|
39
43
|
end
|
|
40
44
|
|
|
@@ -48,13 +52,13 @@ function numberToString(self, number, roundDown)
|
|
|
48
52
|
if size <= number then
|
|
49
53
|
number = round(number / size, self._decimalPlaces, roundDown)
|
|
50
54
|
|
|
51
|
-
if number == 1000 and index < #self._suffixTable
|
|
55
|
+
if number == 1000 and index < #self._suffixTable then
|
|
52
56
|
number = 1
|
|
53
|
-
unit = self._suffixTable[index
|
|
57
|
+
unit = self._suffixTable[index + 1]
|
|
54
58
|
end
|
|
55
59
|
|
|
56
60
|
if not self._stripTrailingZeroes then
|
|
57
|
-
number = ("%."..self._decimalPlaces.."f"):format(number)
|
|
61
|
+
number = ("%." .. self._decimalPlaces .. "f"):format(number)
|
|
58
62
|
end
|
|
59
63
|
|
|
60
64
|
number = number .. unit
|
|
@@ -46,7 +46,7 @@ local function round(number, decimalPlaces, roundDown)
|
|
|
46
46
|
end
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
-
function numbersToSortedString(self, numbers)
|
|
49
|
+
local function numbersToSortedString(self, numbers)
|
|
50
50
|
if not (numbers and type(numbers) == 'table') then
|
|
51
51
|
error('numbersToSortedString had invalid parameters.\nP1 - numbers: Array<number>', 2)
|
|
52
52
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function setSetting(self, settingName, settingValue)
|
|
1
|
+
local function setSetting(self, settingName, settingValue)
|
|
2
2
|
if not (settingName and settingValue ~= nil and type(settingName) == 'string') then
|
|
3
3
|
error('setSetting had invalid parameters.\nP1 - settingName: string\nP2 - settingValue: unknown', 2)
|
|
4
4
|
end
|