tiny-essentials 1.9.0 → 1.9.2
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/TinyBasicsEs.js +155 -98
- package/dist/TinyBasicsEs.min.js +1 -1
- package/dist/TinyEssentials.js +307 -126
- 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/firebase/mySQL.cjs +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/basics/objFilter.cjs +155 -98
- package/dist/v1/basics/objFilter.d.mts +16 -26
- package/dist/v1/basics/objFilter.mjs +146 -77
- 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/basics/objFilter.md +7 -0
- package/docs/libs/TinyLevelUp.md +208 -68
- package/package.json +2 -1
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {Object} QueuedTask
|
|
5
|
+
* @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
|
|
6
|
+
* @property {(value: any) => any} resolve - The resolve function from the Promise.
|
|
7
|
+
* @property {(reason?: any) => any} reject - The reject function from the Promise.
|
|
8
|
+
* @property {string|undefined} [id] - Optional identifier for the task.
|
|
9
|
+
* @property {string|null|undefined} [marker] - Optional marker for the task.
|
|
10
|
+
* @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
|
|
11
|
+
*/
|
|
12
|
+
|
|
3
13
|
/**
|
|
4
14
|
* A queue system for managing and executing asynchronous tasks sequentially, one at a time.
|
|
5
15
|
*
|
|
@@ -9,20 +19,10 @@
|
|
|
9
19
|
* @class
|
|
10
20
|
*/
|
|
11
21
|
class TinyPromiseQueue {
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {Object} QueuedTask
|
|
14
|
-
* @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
|
|
15
|
-
* @property {(value: any) => any} resolve - The resolve function from the Promise.
|
|
16
|
-
* @property {(reason?: any) => any} reject - The reject function from the Promise.
|
|
17
|
-
* @property {string|undefined} [id] - Optional identifier for the task.
|
|
18
|
-
* @property {string|null|undefined} [marker] - Optional marker for the task.
|
|
19
|
-
* @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/** @type {Array<QueuedTask>} */
|
|
22
|
+
/** @type {QueuedTask[]} */
|
|
23
23
|
#queue = [];
|
|
24
24
|
#running = false;
|
|
25
|
-
/** @type {Record<string,
|
|
25
|
+
/** @type {Record<string, ReturnType<typeof setTimeout>>} */
|
|
26
26
|
#timeouts = {};
|
|
27
27
|
/** @type {Set<string>} */
|
|
28
28
|
#blacklist = new Set();
|
|
@@ -185,8 +185,13 @@ class TinyPromiseQueue {
|
|
|
185
185
|
* @param {(...args: any[]) => Promise<any>|Promise<any>} task A function that returns a Promise.
|
|
186
186
|
* @param {string} [id] Optional ID to identify the task in the queue.
|
|
187
187
|
* @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
|
|
188
|
+
* @throws {Error} Throws if param is invalid.
|
|
188
189
|
*/
|
|
189
190
|
async enqueuePoint(task, id) {
|
|
191
|
+
if (typeof task !== 'function')
|
|
192
|
+
return Promise.reject(new Error('Task must be a function returning a Promise.'));
|
|
193
|
+
if (typeof id !== 'undefined' && typeof id !== 'string')
|
|
194
|
+
throw new Error('The "id" parameter must be a string.');
|
|
190
195
|
if (!this.#running) return task();
|
|
191
196
|
return new Promise((resolve, reject) => {
|
|
192
197
|
this.#queue.push({ marker: 'POINT_MARKER', task, resolve, reject, id });
|
|
@@ -205,8 +210,16 @@ class TinyPromiseQueue {
|
|
|
205
210
|
* @param {number|null} [delay] Optional delay (in ms) before the task is executed.
|
|
206
211
|
* @param {string} [id] Optional ID to identify the task in the queue.
|
|
207
212
|
* @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
|
|
213
|
+
* @throws {Error} Throws if param is invalid.
|
|
208
214
|
*/
|
|
209
215
|
enqueue(task, delay, id) {
|
|
216
|
+
if (typeof task !== 'function')
|
|
217
|
+
return Promise.reject(new Error('Task must be a function returning a Promise.'));
|
|
218
|
+
if (typeof delay !== 'undefined' && (typeof delay !== 'number' || delay < 0))
|
|
219
|
+
return Promise.reject(new Error('Delay must be a positive number or undefined.'));
|
|
220
|
+
if (typeof id !== 'undefined' && typeof id !== 'string')
|
|
221
|
+
throw new Error('The "id" parameter must be a string.');
|
|
222
|
+
|
|
210
223
|
return new Promise((resolve, reject) => {
|
|
211
224
|
this.#queue.push({ task, resolve, reject, id, delay });
|
|
212
225
|
this.#processQueue();
|
|
@@ -219,9 +232,10 @@ class TinyPromiseQueue {
|
|
|
219
232
|
*
|
|
220
233
|
* @param {string} id The ID of the task to cancel.
|
|
221
234
|
* @returns {boolean} True if a delay was cancelled and the task was removed.
|
|
235
|
+
* @throws {Error} Throws if `id` is not a string.
|
|
222
236
|
*/
|
|
223
237
|
cancelTask(id) {
|
|
224
|
-
if (
|
|
238
|
+
if (typeof id !== 'string') throw new Error('The "id" parameter must be a string.');
|
|
225
239
|
let cancelled = false;
|
|
226
240
|
|
|
227
241
|
if (id in this.#timeouts) {
|
|
@@ -1,4 +1,39 @@
|
|
|
1
1
|
export default TinyPromiseQueue;
|
|
2
|
+
export type QueuedTask = {
|
|
3
|
+
/**
|
|
4
|
+
* - The async task to execute.
|
|
5
|
+
*/
|
|
6
|
+
task: (...args: any[]) => Promise<any> | Promise<any>;
|
|
7
|
+
/**
|
|
8
|
+
* - The resolve function from the Promise.
|
|
9
|
+
*/
|
|
10
|
+
resolve: (value: any) => any;
|
|
11
|
+
/**
|
|
12
|
+
* - The reject function from the Promise.
|
|
13
|
+
*/
|
|
14
|
+
reject: (reason?: any) => any;
|
|
15
|
+
/**
|
|
16
|
+
* - Optional identifier for the task.
|
|
17
|
+
*/
|
|
18
|
+
id?: string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* - Optional marker for the task.
|
|
21
|
+
*/
|
|
22
|
+
marker?: string | null | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* - Optional delay (in ms) before the task is executed.
|
|
25
|
+
*/
|
|
26
|
+
delay?: number | null | undefined;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* @typedef {Object} QueuedTask
|
|
30
|
+
* @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
|
|
31
|
+
* @property {(value: any) => any} resolve - The resolve function from the Promise.
|
|
32
|
+
* @property {(reason?: any) => any} reject - The reject function from the Promise.
|
|
33
|
+
* @property {string|undefined} [id] - Optional identifier for the task.
|
|
34
|
+
* @property {string|null|undefined} [marker] - Optional marker for the task.
|
|
35
|
+
* @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
|
|
36
|
+
*/
|
|
2
37
|
/**
|
|
3
38
|
* A queue system for managing and executing asynchronous tasks sequentially, one at a time.
|
|
4
39
|
*
|
|
@@ -44,6 +79,7 @@ declare class TinyPromiseQueue {
|
|
|
44
79
|
* @param {(...args: any[]) => Promise<any>|Promise<any>} task A function that returns a Promise.
|
|
45
80
|
* @param {string} [id] Optional ID to identify the task in the queue.
|
|
46
81
|
* @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
|
|
82
|
+
* @throws {Error} Throws if param is invalid.
|
|
47
83
|
*/
|
|
48
84
|
enqueuePoint(task: (...args: any[]) => Promise<any> | Promise<any>, id?: string): Promise<any>;
|
|
49
85
|
/**
|
|
@@ -57,6 +93,7 @@ declare class TinyPromiseQueue {
|
|
|
57
93
|
* @param {number|null} [delay] Optional delay (in ms) before the task is executed.
|
|
58
94
|
* @param {string} [id] Optional ID to identify the task in the queue.
|
|
59
95
|
* @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
|
|
96
|
+
* @throws {Error} Throws if param is invalid.
|
|
60
97
|
*/
|
|
61
98
|
enqueue(task: (...args: any[]) => Promise<any> | Promise<any>, delay?: number | null, id?: string): Promise<any>;
|
|
62
99
|
/**
|
|
@@ -65,6 +102,7 @@ declare class TinyPromiseQueue {
|
|
|
65
102
|
*
|
|
66
103
|
* @param {string} id The ID of the task to cancel.
|
|
67
104
|
* @returns {boolean} True if a delay was cancelled and the task was removed.
|
|
105
|
+
* @throws {Error} Throws if `id` is not a string.
|
|
68
106
|
*/
|
|
69
107
|
cancelTask(id: string): boolean;
|
|
70
108
|
#private;
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} QueuedTask
|
|
3
|
+
* @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
|
|
4
|
+
* @property {(value: any) => any} resolve - The resolve function from the Promise.
|
|
5
|
+
* @property {(reason?: any) => any} reject - The reject function from the Promise.
|
|
6
|
+
* @property {string|undefined} [id] - Optional identifier for the task.
|
|
7
|
+
* @property {string|null|undefined} [marker] - Optional marker for the task.
|
|
8
|
+
* @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
|
|
9
|
+
*/
|
|
1
10
|
/**
|
|
2
11
|
* A queue system for managing and executing asynchronous tasks sequentially, one at a time.
|
|
3
12
|
*
|
|
@@ -7,19 +16,10 @@
|
|
|
7
16
|
* @class
|
|
8
17
|
*/
|
|
9
18
|
class TinyPromiseQueue {
|
|
10
|
-
/**
|
|
11
|
-
* @typedef {Object} QueuedTask
|
|
12
|
-
* @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute.
|
|
13
|
-
* @property {(value: any) => any} resolve - The resolve function from the Promise.
|
|
14
|
-
* @property {(reason?: any) => any} reject - The reject function from the Promise.
|
|
15
|
-
* @property {string|undefined} [id] - Optional identifier for the task.
|
|
16
|
-
* @property {string|null|undefined} [marker] - Optional marker for the task.
|
|
17
|
-
* @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed.
|
|
18
|
-
*/
|
|
19
|
-
/** @type {Array<QueuedTask>} */
|
|
19
|
+
/** @type {QueuedTask[]} */
|
|
20
20
|
#queue = [];
|
|
21
21
|
#running = false;
|
|
22
|
-
/** @type {Record<string,
|
|
22
|
+
/** @type {Record<string, ReturnType<typeof setTimeout>>} */
|
|
23
23
|
#timeouts = {};
|
|
24
24
|
/** @type {Set<string>} */
|
|
25
25
|
#blacklist = new Set();
|
|
@@ -165,8 +165,13 @@ class TinyPromiseQueue {
|
|
|
165
165
|
* @param {(...args: any[]) => Promise<any>|Promise<any>} task A function that returns a Promise.
|
|
166
166
|
* @param {string} [id] Optional ID to identify the task in the queue.
|
|
167
167
|
* @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
|
|
168
|
+
* @throws {Error} Throws if param is invalid.
|
|
168
169
|
*/
|
|
169
170
|
async enqueuePoint(task, id) {
|
|
171
|
+
if (typeof task !== 'function')
|
|
172
|
+
return Promise.reject(new Error('Task must be a function returning a Promise.'));
|
|
173
|
+
if (typeof id !== 'undefined' && typeof id !== 'string')
|
|
174
|
+
throw new Error('The "id" parameter must be a string.');
|
|
170
175
|
if (!this.#running)
|
|
171
176
|
return task();
|
|
172
177
|
return new Promise((resolve, reject) => {
|
|
@@ -185,8 +190,15 @@ class TinyPromiseQueue {
|
|
|
185
190
|
* @param {number|null} [delay] Optional delay (in ms) before the task is executed.
|
|
186
191
|
* @param {string} [id] Optional ID to identify the task in the queue.
|
|
187
192
|
* @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed.
|
|
193
|
+
* @throws {Error} Throws if param is invalid.
|
|
188
194
|
*/
|
|
189
195
|
enqueue(task, delay, id) {
|
|
196
|
+
if (typeof task !== 'function')
|
|
197
|
+
return Promise.reject(new Error('Task must be a function returning a Promise.'));
|
|
198
|
+
if (typeof delay !== 'undefined' && (typeof delay !== 'number' || delay < 0))
|
|
199
|
+
return Promise.reject(new Error('Delay must be a positive number or undefined.'));
|
|
200
|
+
if (typeof id !== 'undefined' && typeof id !== 'string')
|
|
201
|
+
throw new Error('The "id" parameter must be a string.');
|
|
190
202
|
return new Promise((resolve, reject) => {
|
|
191
203
|
this.#queue.push({ task, resolve, reject, id, delay });
|
|
192
204
|
this.#processQueue();
|
|
@@ -198,10 +210,11 @@ class TinyPromiseQueue {
|
|
|
198
210
|
*
|
|
199
211
|
* @param {string} id The ID of the task to cancel.
|
|
200
212
|
* @returns {boolean} True if a delay was cancelled and the task was removed.
|
|
213
|
+
* @throws {Error} Throws if `id` is not a string.
|
|
201
214
|
*/
|
|
202
215
|
cancelTask(id) {
|
|
203
|
-
if (
|
|
204
|
-
|
|
216
|
+
if (typeof id !== 'string')
|
|
217
|
+
throw new Error('The "id" parameter must be a string.');
|
|
205
218
|
let cancelled = false;
|
|
206
219
|
if (id in this.#timeouts) {
|
|
207
220
|
clearTimeout(this.#timeouts[id]);
|
package/docs/basics/objFilter.md
CHANGED
|
@@ -83,6 +83,13 @@ extendObjType({
|
|
|
83
83
|
});
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
+
```js
|
|
87
|
+
extendObjType([
|
|
88
|
+
[ 'alpha', val => typeof val === 'string' ],
|
|
89
|
+
[ 'beta', val => Array.isArray(val) ]
|
|
90
|
+
]);
|
|
91
|
+
```
|
|
92
|
+
|
|
86
93
|
This will insert `customElement` before the built-in `object` type unless a position is specified.
|
|
87
94
|
|
|
88
95
|
---
|
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.2",
|
|
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/*",
|