@rbxts/abbreviate 3.0.4 → 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 CHANGED
@@ -1,98 +1,56 @@
1
- # @rbxts/abbreviate
2
-
3
- [![NPM](https://nodei.co/npm/@rbxts/abbreviate.png)](https://npmjs.org/package/@rbxts/abbreviate)
4
-
5
- ## Installation:
6
- ```npm i @rbxts/abbreviate```
7
-
8
- ## Example Usage
9
- ```typescript
10
- import Abbreviator from "@rbxts/abbreviate";
11
-
12
- const abbreviator = new Abbreviator();
13
- abbreviator.setSetting('suffixTable', ['k', 'm', 'b']);
14
- abbreviator.setSetting('decimalPlaces', 2);
15
-
16
- print(abbreviator.stringToNumber('500')) // 500
17
- print(abbreviator.stringToNumber('5k')) // 5000
18
- print(abbreviator.stringToNumber('5m')) // 5000000
19
- print(abbreviator.stringToNumber('1.23456m')) // 1234560
20
-
21
- print(abbreviator.numberToString(999)) // 999
22
- print(abbreviator.numberToString(1000)) // 1.00k
23
- print(abbreviator.numberToString(1000000)) // 1.00m
24
- print(abbreviator.numberToString(1234567)) // 1.23m
25
- ```
26
-
27
- ## Settings
28
- The possible settings you can set are the following:
29
- | Setting Name | Setting Value Type | Setting Description | Setting Default | Setting Example
30
- |---|---|---|---|---|
31
- | 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"] |
32
- | decimalPlaces | number | Sets the amount of decimal places a number may have when using `numberToString` | 2 | 4 |
33
- | stripTrailingZeroes | boolean | Removes any extra zeroes after a decimal place that are dangling after `numberToString` calls. E.g. `"52506.004"` => `"5.2506k"` | false | `true`
34
-
35
- ## Why make `Abbreviator` a class?
36
- 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
37
- To solve this, abbreviate requires you to construct a new abbreviator.
38
- The settings of this abbreviator is independent of other abbreviators.
39
-
40
- ## Changelog
41
-
42
- ### 3.0.3
43
- - Fixed `stripTrailingZeroes` incorrectly showing as `removeDanglingZeroes` in `README.md`
44
-
45
- ### 3.0.2
46
- - Added setting default and setting example for `stripTrailingZeroes`
47
-
48
- ### 3.0.1
49
- - Fixed `README.md` example being outdated
50
- - Added `Scyfren` to contributors
51
-
52
- ### 3.0.0
53
- - Changed the method to create an abbreviator from `createAbbreviator()` to `abbreviator.new()` (`new Abbreviator()` in TypeScript)
54
- - Added option `stripTrailingZeroes`
55
- - Fixed some bugs regarding settings changes
56
- - Slight speed improvements
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
+ [![NPM](https://nodei.co/npm/@rbxts/abbreviate.png)](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,39 +1,39 @@
1
- {
2
- "name": "@rbxts/abbreviate",
3
- "version": "3.0.4",
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.lua",
32
- "src/index.d.ts",
33
- "src/commify.lua",
34
- "src/numbersToSortedString.lua",
35
- "src/numberToString.lua",
36
- "src/setSetting.lua",
37
- "src/stringToNumber.lua"
38
- ]
39
- }
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
+ }
File without changes
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
- * 1000 => "1,400"
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 of full length into a string with a suffix as defined in the prefix table
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 down.
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
- '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'
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
  }
@@ -21,7 +21,7 @@ end
21
21
 
22
22
  local function numberToString(self, number, roundDown)
23
23
  if type(number) ~= "number" then
24
- error('numberToString invalid parameter #1, expected number, got "nil"', 2)
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 @@ local 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
- if not self._stripTrailingZeroes then
34
- return ("%." .. self._decimalPlaces .. "f"):format(number)
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 = round(number, self._decimalPlaces, roundDown)
37
- return tostring(number)
41
+ number = rounded
38
42
  end
39
43
  end
40
44
 
@@ -48,9 +52,9 @@ local 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[index] then
55
+ if number == 1000 and index < #self._suffixTable then
52
56
  number = 1
53
- unit = self._suffixTable[index][index + 1]
57
+ unit = self._suffixTable[index + 1]
54
58
  end
55
59
 
56
60
  if not self._stripTrailingZeroes then
File without changes
File without changes