tiny-essentials 1.21.1 → 1.21.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/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyNeedBar.min.js +1 -1
- package/dist/v1/TinySimpleDice.min.js +1 -0
- package/dist/v1/basics/index.cjs +1 -0
- package/dist/v1/basics/index.d.mts +2 -1
- package/dist/v1/basics/index.mjs +2 -2
- package/dist/v1/build/TinySimpleDice.cjs +7 -0
- package/dist/v1/build/TinySimpleDice.d.mts +3 -0
- package/dist/v1/build/TinySimpleDice.mjs +2 -0
- package/dist/v1/index.cjs +3 -0
- package/dist/v1/index.d.mts +3 -1
- package/dist/v1/index.mjs +3 -2
- package/dist/v1/libs/TinyNeedBar.cjs +83 -10
- package/dist/v1/libs/TinyNeedBar.d.mts +17 -1
- package/dist/v1/libs/TinyNeedBar.mjs +79 -8
- package/dist/v1/libs/TinySimpleDice.cjs +91 -0
- package/dist/v1/libs/TinySimpleDice.d.mts +57 -0
- package/dist/v1/libs/TinySimpleDice.mjs +84 -0
- package/docs/v1/README.md +1 -0
- package/docs/v1/libs/TinyNeedBar.md +16 -1
- package/docs/v1/libs/TinySimpleDice.md +146 -0
- package/package.json +1 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TinySimpleDice
|
|
3
|
+
*
|
|
4
|
+
* A lightweight, flexible dice rolling utility for generating random numbers.
|
|
5
|
+
* You can configure the dice to allow zero, set a maximum value, and even roll
|
|
6
|
+
* values suitable for indexing arrays or Sets.
|
|
7
|
+
*/
|
|
8
|
+
class TinySimpleDice {
|
|
9
|
+
/**
|
|
10
|
+
* Rolls a dice specifically for choosing an array or Set index.
|
|
11
|
+
* @param {any[]|Set<any>} arr - The array or Set to get a random index from.
|
|
12
|
+
* @returns {number} - Valid index for the array or Set.
|
|
13
|
+
* @throws {TypeError} If the input is not an array or Set.
|
|
14
|
+
*/
|
|
15
|
+
static rollArrayIndex(arr) {
|
|
16
|
+
const isArray = Array.isArray(arr);
|
|
17
|
+
const isSet = arr instanceof Set;
|
|
18
|
+
if (!isArray && !isSet)
|
|
19
|
+
throw new TypeError('rollArrayIndex expects an array or Set.');
|
|
20
|
+
return Math.floor(Math.random() * (isArray ? arr.length : arr.size));
|
|
21
|
+
}
|
|
22
|
+
/** @type {number} */
|
|
23
|
+
#maxValue;
|
|
24
|
+
/** @type {boolean} */
|
|
25
|
+
#allowZero;
|
|
26
|
+
/**
|
|
27
|
+
* Maximum value the dice can roll.
|
|
28
|
+
* @type {number}
|
|
29
|
+
*/
|
|
30
|
+
get maxValue() {
|
|
31
|
+
return this.#maxValue;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Set the maximum value the dice can roll.
|
|
35
|
+
* @param {number} value - New maximum value (must be a non-negative integer)
|
|
36
|
+
* @throws {TypeError} If value is not a non-negative integer
|
|
37
|
+
*/
|
|
38
|
+
set maxValue(value) {
|
|
39
|
+
if (!Number.isInteger(value) || value < 0)
|
|
40
|
+
throw new TypeError('maxValue must be an integer greater than -1.');
|
|
41
|
+
this.#maxValue = value;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Whether 0 is allowed as a result.
|
|
45
|
+
* @type {boolean}
|
|
46
|
+
*/
|
|
47
|
+
get allowZero() {
|
|
48
|
+
return this.#allowZero;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Set whether 0 is allowed as a result.
|
|
52
|
+
* @param {boolean} value - true to allow 0, false to disallow
|
|
53
|
+
* @throws {TypeError} If value is not a boolean
|
|
54
|
+
*/
|
|
55
|
+
set allowZero(value) {
|
|
56
|
+
if (typeof value !== 'boolean')
|
|
57
|
+
throw new TypeError('allowZero must be a boolean.');
|
|
58
|
+
this.#allowZero = value;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new TinySimpleDice instance.
|
|
62
|
+
* @param {Object} options - Configuration options for the dice.
|
|
63
|
+
* @param {number} options.maxValue - Maximum value the dice can roll.
|
|
64
|
+
* @param {boolean} [options.allowZero=true] - Whether 0 is allowed as a result.
|
|
65
|
+
* @throws {TypeError} If maxValue is not a non-negative integer or allowZero is not boolean.
|
|
66
|
+
*/
|
|
67
|
+
constructor({ maxValue, allowZero = true }) {
|
|
68
|
+
if (typeof allowZero !== 'boolean')
|
|
69
|
+
throw new TypeError('allowZero must be an boolean.');
|
|
70
|
+
if (!Number.isInteger(maxValue) || maxValue < 0)
|
|
71
|
+
throw new TypeError('maxValue must be an integer greater than -1.');
|
|
72
|
+
this.#maxValue = maxValue;
|
|
73
|
+
this.#allowZero = allowZero;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Rolls the dice according to the configuration.
|
|
77
|
+
* @returns {number} - Random number according to the dice configuration.
|
|
78
|
+
*/
|
|
79
|
+
roll() {
|
|
80
|
+
const min = this.#allowZero ? 0 : 1;
|
|
81
|
+
return Math.floor(Math.random() * (this.#maxValue - min + 1)) + min;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export default TinySimpleDice;
|
package/docs/v1/README.md
CHANGED
|
@@ -56,6 +56,7 @@ This folder contains the core scripts we have worked on so far. Each file is a m
|
|
|
56
56
|
- 🤝 **[TinyInventoryTrader](./libs/TinyInventoryTrader.md)** — A trading helper for safely transferring items between two inventories with support for strict mode, slot targeting, and batch operations.
|
|
57
57
|
- 🌐 **[TinyI18](./libs/TinyI18.md)** — A flexible i18n manager supporting local and file modes, regex-based keys, function-based entries, string interpolation, and safe helper functions for advanced rendering.
|
|
58
58
|
- 🎮 **[TinyNeedBar](./libs/TinyNeedBar.md)** — A versatile "need bar" system for simulating decay over time with multiple configurable factors, serialization, cloning, and full control over clamped and infinite values.
|
|
59
|
+
- 🎲 **[TinySimpleDice](./libs/TinySimpleDice.md)** — A lightweight and flexible dice rolling utility with configurable maximum values, zero allowance, and array/Set index rolling support.
|
|
59
60
|
|
|
60
61
|
### 3. **`fileManager/`**
|
|
61
62
|
* 📁 **[Main](./fileManager/main.md)** — A Node.js file/directory utility module with support for JSON, backups, renaming, size analysis, and more.
|
|
@@ -132,7 +132,7 @@ bar.setFactor("hunger", 2, 1.2);
|
|
|
132
132
|
|
|
133
133
|
---
|
|
134
134
|
|
|
135
|
-
#### `removeFactor(key: string)` ❌
|
|
135
|
+
#### `removeFactor(key: string) : boolean` ❌
|
|
136
136
|
|
|
137
137
|
Remove a factor by key.
|
|
138
138
|
|
|
@@ -150,6 +150,21 @@ Execute one tick of decay using all active factors.
|
|
|
150
150
|
const result = bar.tick();
|
|
151
151
|
console.log(result.remainingValue);
|
|
152
152
|
```
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
#### `tickWithTempFactor(tempFactor): TickResult` ⏱️
|
|
156
|
+
|
|
157
|
+
Executes one tick using a **temporary factor** applied only for this tick, in addition to the regular factors.
|
|
158
|
+
|
|
159
|
+
**Use case:** Useful for testing temporary boosts or penalties without modifying the permanent factors.
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
#### `tickSingleFactor(factor): TickResult` ⏱️
|
|
164
|
+
|
|
165
|
+
Executes one tick using **only the specified factor**, ignoring all other factors.
|
|
166
|
+
|
|
167
|
+
**Use case:** Perfect for controlled testing of a single effect or for one-off calculations.
|
|
153
168
|
|
|
154
169
|
---
|
|
155
170
|
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# 🎲 TinySimpleDice
|
|
2
|
+
|
|
3
|
+
A **lightweight, flexible dice rolling utility** for generating random numbers.
|
|
4
|
+
You can configure the dice to **allow zero**, set a **maximum value**, and even roll values suitable for **indexing arrays or Sets**.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## ⚙️ Class: `TinySimpleDice`
|
|
9
|
+
|
|
10
|
+
### Properties
|
|
11
|
+
|
|
12
|
+
| Property | Type | Description |
|
|
13
|
+
| ----------- | ------- | --------------------------------- |
|
|
14
|
+
| `maxValue` | number | Maximum value the dice can roll. |
|
|
15
|
+
| `allowZero` | boolean | Whether 0 is allowed as a result. |
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
### Constructor
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
new TinySimpleDice({ maxValue, allowZero = true });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Parameters**
|
|
26
|
+
|
|
27
|
+
| Name | Type | Description |
|
|
28
|
+
| ----------- | ------- | ------------------------------------------------------- |
|
|
29
|
+
| `maxValue` | number | Maximum value the dice can roll (non-negative integer). |
|
|
30
|
+
| `allowZero` | boolean | Optional. If `true`, 0 is allowed; default is `true`. |
|
|
31
|
+
|
|
32
|
+
**Throws**
|
|
33
|
+
|
|
34
|
+
* `TypeError` if `maxValue` is not a non-negative integer.
|
|
35
|
+
* `TypeError` if `allowZero` is not a boolean.
|
|
36
|
+
|
|
37
|
+
**Example**
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
const dice = new TinySimpleDice({ maxValue: 6, allowZero: false });
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
### Getters & Setters
|
|
46
|
+
|
|
47
|
+
#### `maxValue`
|
|
48
|
+
|
|
49
|
+
* **Getter**: Returns the maximum value of the dice.
|
|
50
|
+
* **Setter**: Sets a new maximum value (must be a non-negative integer).
|
|
51
|
+
* **Throws**: `TypeError` if invalid.
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
console.log(dice.maxValue); // 6
|
|
55
|
+
dice.maxValue = 10; // Update maximum value
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### `allowZero`
|
|
59
|
+
|
|
60
|
+
* **Getter**: Returns whether 0 is allowed.
|
|
61
|
+
* **Setter**: Sets whether 0 is allowed.
|
|
62
|
+
* **Throws**: `TypeError` if not a boolean.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
console.log(dice.allowZero); // false
|
|
66
|
+
dice.allowZero = true; // Allow zero
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
### Methods
|
|
72
|
+
|
|
73
|
+
#### `roll()`
|
|
74
|
+
|
|
75
|
+
Rolls the dice according to the configuration.
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
const result = dice.roll();
|
|
79
|
+
console.log(result); // Random number between 1 and 6 (or 0 if allowZero)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**Returns:** `number` — the rolled value.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
#### `static rollArrayIndex(arr)`
|
|
87
|
+
|
|
88
|
+
Rolls a value suitable for indexing an **array** or **Set**.
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
const fruits = ['apple', 'banana', 'cherry'];
|
|
92
|
+
const index = TinySimpleDice.rollArrayIndex(fruits);
|
|
93
|
+
console.log(fruits[index]); // Random fruit
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**Parameters:**
|
|
97
|
+
|
|
98
|
+
| Name | Type | Description |
|
|
99
|
+
| ---- | ----------------- | -------------------------- |
|
|
100
|
+
| arr | array or Set<any> | The array or Set to index. |
|
|
101
|
+
|
|
102
|
+
**Returns:** `number` — a valid index.
|
|
103
|
+
|
|
104
|
+
**Throws:** `TypeError` if input is not an array or Set.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## 📝 Examples
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
// Create dice
|
|
112
|
+
const dice = new TinySimpleDice({ maxValue: 12, allowZero: true });
|
|
113
|
+
|
|
114
|
+
// Roll dice
|
|
115
|
+
console.log(dice.roll()); // e.g., 0–12
|
|
116
|
+
|
|
117
|
+
// Update dice configuration
|
|
118
|
+
dice.maxValue = 20;
|
|
119
|
+
dice.allowZero = false;
|
|
120
|
+
|
|
121
|
+
// Roll for an array index
|
|
122
|
+
const colors = ['red', 'green', 'blue', 'yellow'];
|
|
123
|
+
const idx = TinySimpleDice.rollArrayIndex(colors);
|
|
124
|
+
console.log(colors[idx]); // Random color
|
|
125
|
+
|
|
126
|
+
// Roll for a Set
|
|
127
|
+
const mySet = new Set([10, 20, 30]);
|
|
128
|
+
console.log(TinySimpleDice.rollArrayIndex(mySet)); // 0–2 index
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 🎯 Features
|
|
134
|
+
|
|
135
|
+
* ✅ Configurable maximum value
|
|
136
|
+
* ✅ Option to allow or disallow zero
|
|
137
|
+
* ✅ Static helper to roll indices for arrays or Sets
|
|
138
|
+
* ✅ Lightweight and easy to use
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 💡 Notes
|
|
143
|
+
|
|
144
|
+
* The `rollArrayIndex` method always returns a **valid index**, regardless of the collection type.
|
|
145
|
+
* Set results are converted to an array internally when accessing values.
|
|
146
|
+
* Getters and setters validate values to ensure safe usage.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.21.
|
|
3
|
+
"version": "1.21.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",
|