tiny-essentials 1.9.0 → 1.9.1
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/dist/TinyEssentials.js +152 -28
- package/dist/TinyEssentials.min.js +1 -1
- package/dist/TinyLevelUp.js +125 -15
- package/dist/TinyLevelUp.min.js +1 -1
- package/dist/TinyPromiseQueue.js +27 -13
- package/dist/TinyPromiseQueue.min.js +1 -1
- package/dist/legacy/libs/userLevel.cjs +125 -15
- package/dist/legacy/libs/userLevel.d.mts +86 -59
- package/dist/legacy/libs/userLevel.mjs +123 -15
- package/dist/v1/libs/TinyPromiseQueue.cjs +27 -13
- package/dist/v1/libs/TinyPromiseQueue.d.mts +38 -0
- package/dist/v1/libs/TinyPromiseQueue.mjs +26 -13
- package/docs/libs/TinyLevelUp.md +208 -68
- package/package.json +2 -1
package/docs/libs/TinyLevelUp.md
CHANGED
|
@@ -1,108 +1,248 @@
|
|
|
1
|
+
# 📚 TinyLevelUp Documentation
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
A lightweight XP-based leveling system for managing user experience and levels. Great for games, communities, or reward-based applications! 🎮✨
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 📐 Type Definitions
|
|
8
|
+
|
|
9
|
+
### `UserEditor` ✍️
|
|
5
10
|
|
|
6
|
-
|
|
11
|
+
Represents the structure of a user object used in the leveling system.
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
```ts
|
|
14
|
+
{
|
|
15
|
+
exp: number; // Current experience points of the user
|
|
16
|
+
level: number; // Current level of the user
|
|
17
|
+
totalExp: number; // Total accumulated experience
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
10
22
|
|
|
11
|
-
|
|
12
|
-
- `expLevel` (`number`): The base experience required to level up for each level. 📈
|
|
23
|
+
## 🏗️ Class: `TinyLevelUp`
|
|
13
24
|
|
|
14
|
-
|
|
25
|
+
Handles experience logic, leveling up/down, validation, and XP generation.
|
|
15
26
|
|
|
16
|
-
|
|
17
|
-
Validates and adjusts the user's level based on their current experience. If the user's experience is above or below the required threshold, their level is adjusted accordingly. ⚖️
|
|
27
|
+
### 🆕 Constructor
|
|
18
28
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
29
|
+
```ts
|
|
30
|
+
new TinyLevelUp(giveExp: number, expLevel: number)
|
|
31
|
+
```
|
|
22
32
|
|
|
23
|
-
|
|
24
|
-
|
|
33
|
+
* `giveExp`: Base XP value for random XP generation 🎲
|
|
34
|
+
* `expLevel`: Base XP required per level up 🔺
|
|
25
35
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
- **Returns**: `number` - The total experience of the user. 🔢
|
|
36
|
+
---
|
|
29
37
|
|
|
30
|
-
|
|
31
|
-
Generates random experience points based on a configured multiplier. 🎲
|
|
38
|
+
## 🧰 Methods
|
|
32
39
|
|
|
33
|
-
|
|
40
|
+
### ➕ `createUser()`
|
|
34
41
|
|
|
35
|
-
|
|
42
|
+
Creates a fresh user at level 1 with no XP.
|
|
36
43
|
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
```ts
|
|
45
|
+
createUser(): UserEditor
|
|
46
|
+
```
|
|
39
47
|
|
|
40
|
-
|
|
48
|
+
---
|
|
41
49
|
|
|
42
|
-
|
|
50
|
+
### 🔎 `validateUser(user)`
|
|
43
51
|
|
|
44
|
-
|
|
45
|
-
An alias for `progress`. Returns the experience points required to reach the next level. ⏩
|
|
52
|
+
Throws an error if the user object is malformed or contains invalid values.
|
|
46
53
|
|
|
47
|
-
|
|
54
|
+
```ts
|
|
55
|
+
validateUser(user: UserEditor): void
|
|
56
|
+
```
|
|
48
57
|
|
|
49
|
-
|
|
58
|
+
🚨 Throws if:
|
|
50
59
|
|
|
51
|
-
|
|
52
|
-
Sets the user's experience value and adjusts their level if necessary. 📝
|
|
60
|
+
* `exp`, `level`, or `totalExp` are not valid numbers.
|
|
53
61
|
|
|
54
|
-
|
|
55
|
-
- `value` (`number`): The new experience value to set for the user. 💡
|
|
62
|
+
---
|
|
56
63
|
|
|
57
|
-
|
|
64
|
+
### ✅ `isValidUser(user)`
|
|
58
65
|
|
|
59
|
-
|
|
60
|
-
Adds experience to the user and adjusts their level if necessary. Experience can be added with or without a multiplier. ➕
|
|
66
|
+
Checks if the given user object is valid (without throwing errors).
|
|
61
67
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
- `multi` (`number`): Multiplier for experience generation. Default is `1`. 💥
|
|
68
|
+
```ts
|
|
69
|
+
isValidUser(user: UserEditor): boolean
|
|
70
|
+
```
|
|
66
71
|
|
|
67
|
-
|
|
72
|
+
---
|
|
68
73
|
|
|
69
|
-
|
|
70
|
-
Removes experience from the user and adjusts their level if necessary. Experience can be removed with or without a multiplier. ➖
|
|
74
|
+
### 🎁 `getGiveExpBase()`
|
|
71
75
|
|
|
72
|
-
|
|
73
|
-
- `extraExp` (`number`): Additional experience to be removed. 💣
|
|
74
|
-
- `type` (`'add' | 'extra'`): Type of experience removal. `'add'` removes random experience, while `'extra'` removes specified experience. 🔧
|
|
75
|
-
- `multi` (`number`): Multiplier for experience generation. Default is `1`. 💥
|
|
76
|
+
Returns the base XP value used for generating XP.
|
|
76
77
|
|
|
77
|
-
|
|
78
|
+
```ts
|
|
79
|
+
getGiveExpBase(): number
|
|
80
|
+
```
|
|
78
81
|
|
|
79
|
-
|
|
82
|
+
---
|
|
80
83
|
|
|
81
|
-
|
|
82
|
-
Represents the structure of a user object after level validation. 🧑💻
|
|
84
|
+
### 🧮 `getExpLevelBase()`
|
|
83
85
|
|
|
84
|
-
|
|
85
|
-
- `level` (`number`): The user's current level. 🏆
|
|
86
|
-
- `totalExp` (`number`): The user's total experience. 📊
|
|
86
|
+
Returns the base XP required per level.
|
|
87
87
|
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
```ts
|
|
89
|
+
getExpLevelBase(): number
|
|
90
|
+
```
|
|
90
91
|
|
|
91
|
-
|
|
92
|
-
- `level` (`number`): The user's current level. 🏆
|
|
93
|
-
- `totalExp` (`any`): The user's total experience (can be calculated using `getTotalExp`). 📊
|
|
92
|
+
---
|
|
94
93
|
|
|
95
|
-
###
|
|
94
|
+
### ⚖️ `expValidator(user)`
|
|
96
95
|
|
|
97
|
-
|
|
98
|
-
const user = { exp: 50, level: 1, totalExp: 50 };
|
|
99
|
-
const levelUpSystem = new TinyLevelUp(100, 1000);
|
|
96
|
+
Validates and adjusts user level based on current XP.
|
|
100
97
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
console.log(user); // { exp: 112, level: 1, totalExp: 112 }
|
|
98
|
+
```ts
|
|
99
|
+
expValidator(user: UserEditor): UserEditor
|
|
104
100
|
```
|
|
105
101
|
|
|
102
|
+
* Levels up/down the user if needed.
|
|
103
|
+
* Applies "extra" XP appropriately.
|
|
104
|
+
|
|
106
105
|
---
|
|
107
106
|
|
|
108
|
-
|
|
107
|
+
### 📊 `getTotalExp(user)`
|
|
108
|
+
|
|
109
|
+
Calculates total accumulated XP (including current level and progress).
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
getTotalExp(user: UserEditor): number
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
### 🎲 `expGenerator(multi = 1)`
|
|
118
|
+
|
|
119
|
+
Generates a random XP value based on the multiplier.
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
expGenerator(multi?: number): number
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
### ⏩ `progress(user)`
|
|
128
|
+
|
|
129
|
+
Returns XP required to reach the next level.
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
progress(user: UserEditor): number
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
### 📈 `getProgress(user)`
|
|
138
|
+
|
|
139
|
+
Alias for `progress()`. Returns XP needed to reach the next level.
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
getProgress(user: UserEditor): number
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### ✍️ `set(user, value)`
|
|
148
|
+
|
|
149
|
+
Sets the current XP for the user and updates their level if necessary.
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
set(user: UserEditor, value: number): UserEditor
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### ⬆️ `give(user, extraExp?, type?, multi?)`
|
|
158
|
+
|
|
159
|
+
Adds experience to the user and auto-levels if needed.
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
give(
|
|
163
|
+
user: UserEditor,
|
|
164
|
+
extraExp?: number,
|
|
165
|
+
type?: 'add' | 'extra',
|
|
166
|
+
multi?: number
|
|
167
|
+
): UserEditor
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
* `type = 'add'`: Adds generated XP + extra
|
|
171
|
+
* `type = 'extra'`: Adds only extra
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
### ⬇️ `remove(user, extraExp?, type?, multi?)`
|
|
176
|
+
|
|
177
|
+
Removes experience from the user and updates their level if necessary.
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
remove(
|
|
181
|
+
user: UserEditor,
|
|
182
|
+
extraExp?: number,
|
|
183
|
+
type?: 'add' | 'extra',
|
|
184
|
+
multi?: number
|
|
185
|
+
): UserEditor
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
* `type = 'add'`: Removes generated XP + extra
|
|
189
|
+
* `type = 'extra'`: Removes only extra
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
### 🔍 `getMissingExp(user)`
|
|
194
|
+
|
|
195
|
+
**Description:**
|
|
196
|
+
Calculates how much experience is missing for the user to reach the next level.
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
**Parameters:**
|
|
201
|
+
|
|
202
|
+
* `user` (UserEditor) — The user object containing experience and level properties.
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
**Returns:**
|
|
207
|
+
|
|
208
|
+
* `number` — The amount of experience points still needed to level up.
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
**Throws:**
|
|
213
|
+
|
|
214
|
+
* `Error` — If any property (`exp`, `level`, or `totalExp`) in the `user` object is not a valid number. ⚠️
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
**Example usage:**
|
|
219
|
+
|
|
220
|
+
```js
|
|
221
|
+
const missing = tinyLevelUp.getMissingExp(user);
|
|
222
|
+
console.log(`You need ${missing} more XP to level up! 🚀`);
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## 🌟 Example Usage
|
|
228
|
+
|
|
229
|
+
```js
|
|
230
|
+
import TinyLevelUp from './TinyLevelUp.js';
|
|
231
|
+
|
|
232
|
+
const levelSystem = new TinyLevelUp(15, 10);
|
|
233
|
+
const user = levelSystem.createUser();
|
|
234
|
+
|
|
235
|
+
levelSystem.give(user); // Adds random XP
|
|
236
|
+
console.log(user);
|
|
237
|
+
|
|
238
|
+
levelSystem.set(user, 50); // Manually set XP
|
|
239
|
+
console.log(user.level);
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## 💬 Notes
|
|
245
|
+
|
|
246
|
+
* All methods throw if invalid values are passed ❗
|
|
247
|
+
* You can use `isValidUser()` for safe checks without exceptions 🛡️
|
|
248
|
+
* This system is deterministic and extendable!
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.1",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"test:mjs:objtype": "node test/index.mjs objType",
|
|
12
12
|
"test:mjs:jsoncolor": "node test/index.mjs colorStringify",
|
|
13
13
|
"test:mjs:ratelimit": "node test/index.mjs rateLimit",
|
|
14
|
+
"test:mjs:levelup": "node test/index.mjs levelUp",
|
|
14
15
|
"fix:prettier": "npm run fix:prettier:src && npm run fix:prettier:test && npm run fix:prettier:rollup.config && npm run fix:prettier:webpack.config",
|
|
15
16
|
"fix:prettier:src": "prettier --write ./src/*",
|
|
16
17
|
"fix:prettier:test": "prettier --write ./test/*",
|