tiny-essentials 1.21.0 → 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.
@@ -0,0 +1,241 @@
1
+ # 🛠️ TinyNeedBar Documentation
2
+
3
+ `TinyNeedBar` is a utility class for simulating a **"need bar" system**.
4
+ It allows tracking of values that decrease over time, with configurable decay factors.
5
+
6
+ ---
7
+
8
+ ## 📦 Types
9
+
10
+ ### `TickResult` 🔄
11
+
12
+ Represents the result of one tick of decay.
13
+
14
+ | Property | Type | Description |
15
+ | ------------------- | -------- | ----------------------------------------- |
16
+ | `prevValue` | `number` | Infinite value before applying decay. |
17
+ | `removedTotal` | `number` | Total amount removed this tick. |
18
+ | `removedPercent` | `number` | Percentage of max removed this tick. |
19
+ | `currentPercent` | `number` | Current percentage relative to max. |
20
+ | `remainingValue` | `number` | Current clamped value (≥ 0). |
21
+ | `infiniteRemaining` | `number` | Current infinite value (can be negative). |
22
+
23
+ ---
24
+
25
+ ### `BarFactor` ⚡
26
+
27
+ Represents a decay factor applied to the need bar.
28
+
29
+ | Property | Type | Description |
30
+ | ------------ | -------- | --------------------------------- |
31
+ | `amount` | `number` | Base reduction value per tick. |
32
+ | `multiplier` | `number` | Multiplier applied to the amount. |
33
+
34
+ ---
35
+
36
+ ### `SerializedData` 💾
37
+
38
+ Represents the serialized state of a `TinyNeedBar` instance.
39
+ Can be used to export/import or clone bars.
40
+
41
+ | Property | Type | Description |
42
+ | --------------- | --------------------------- | ------------------------------------------- |
43
+ | `maxValue` | `number` | Maximum value of the bar at serialization. |
44
+ | `currentValue` | `number` | Current clamped value (≥ 0). |
45
+ | `infiniteValue` | `number` | Infinite value (can be negative). |
46
+ | `factors` | `Record<string, BarFactor>` | Active decay factors indexed by their keys. |
47
+
48
+ ---
49
+
50
+ ## 🏗️ Class: `TinyNeedBar`
51
+
52
+ ### Overview
53
+
54
+ * The bar decreases over time according to **defined factors**.
55
+ * Each factor has an `amount` and a `multiplier`.
56
+ * Tracks two values:
57
+
58
+ * `currentValue` → cannot go below zero.
59
+ * `infiniteValue` → can decrease infinitely into negative numbers.
60
+
61
+ ---
62
+
63
+ ### Constructor
64
+
65
+ ```ts
66
+ constructor(maxValue = 100, baseDecay = 1, baseDecayMulti = 1)
67
+ ```
68
+
69
+ * `maxValue` – Maximum bar value (default `100`)
70
+ * `baseDecay` – Base decay per tick (default `1`)
71
+ * `baseDecayMulti` – Multiplier for base decay (default `1`)
72
+
73
+ **Example:**
74
+
75
+ ```js
76
+ const bar = new TinyNeedBar(100, 2, 1.5);
77
+ ```
78
+
79
+ ---
80
+
81
+ ### Getters
82
+
83
+ | Getter | Returns | Description |
84
+ | ---------------- | --------------------------- | ----------------------------------------- |
85
+ | `factors` | `Record<string, BarFactor>` | Snapshot of all active factors. |
86
+ | `currentPercent` | `number` | Current percentage relative to max. |
87
+ | `maxValue` | `number` | Maximum possible value of the bar. |
88
+ | `currentValue` | `number` | Current clamped value (≥ 0). |
89
+ | `infiniteValue` | `number` | Current infinite value (can go negative). |
90
+
91
+ ---
92
+
93
+ ### Setters
94
+
95
+ | Setter | Parameters | Description |
96
+ | --------------- | --------------- | -------------------------------------------------- |
97
+ | `maxValue` | `value: number` | Update max value, clamps `currentValue` if needed. |
98
+ | `infiniteValue` | `value: number` | Update infinite value, auto-adjust `currentValue`. |
99
+
100
+ ---
101
+
102
+ ### Methods
103
+
104
+ #### `getFactor(key: string): BarFactor` 🔑
105
+
106
+ Get a specific factor by key.
107
+ Throws an error if the factor does not exist.
108
+
109
+ ```js
110
+ bar.getFactor("main");
111
+ ```
112
+
113
+ ---
114
+
115
+ #### `hasFactor(key: string): boolean` ✅
116
+
117
+ Check if a factor exists by key.
118
+
119
+ ```js
120
+ bar.hasFactor("main"); // true or false
121
+ ```
122
+
123
+ ---
124
+
125
+ #### `setFactor(key: string, amount: number, multiplier = 1)` ⚡
126
+
127
+ Add or update a decay factor.
128
+
129
+ ```js
130
+ bar.setFactor("hunger", 2, 1.2);
131
+ ```
132
+
133
+ ---
134
+
135
+ #### `removeFactor(key: string) : boolean` ❌
136
+
137
+ Remove a factor by key.
138
+
139
+ ```js
140
+ bar.removeFactor("hunger");
141
+ ```
142
+
143
+ ---
144
+
145
+ #### `tick(): TickResult` ⏱️
146
+
147
+ Execute one tick of decay using all active factors.
148
+
149
+ ```js
150
+ const result = bar.tick();
151
+ console.log(result.remainingValue);
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.
168
+
169
+ ---
170
+
171
+ #### `toJSON(): SerializedData` 💾
172
+
173
+ Serialize the current state to a JSON-compatible object.
174
+
175
+ ```js
176
+ const data = bar.toJSON();
177
+ ```
178
+
179
+ ---
180
+
181
+ #### `static fromJSON(data: SerializedData): TinyNeedBar` 📤
182
+
183
+ Restore a `TinyNeedBar` from serialized data.
184
+
185
+ ```js
186
+ const newBar = TinyNeedBar.fromJSON(data);
187
+ ```
188
+
189
+ ---
190
+
191
+ #### `clone(): TinyNeedBar` 🧬
192
+
193
+ Deep clone the bar.
194
+
195
+ ```js
196
+ const cloned = bar.clone();
197
+ ```
198
+
199
+ ---
200
+
201
+ #### `clearFactors()` 🧹
202
+
203
+ Remove all factors.
204
+
205
+ ```js
206
+ bar.clearFactors();
207
+ ```
208
+
209
+ ---
210
+
211
+ ### ✅ Example Usage
212
+
213
+ ```js
214
+ const bar = new TinyNeedBar(100, 2, 1.5);
215
+
216
+ // Add extra factors
217
+ bar.setFactor("energy", 1, 1.2);
218
+ bar.setFactor("fun", 0.5, 1);
219
+
220
+ // Tick simulation
221
+ setInterval(() => {
222
+ const result = bar.tick();
223
+ console.log(`Value: ${result.remainingValue} (${result.currentPercent}%)`);
224
+ }, 1000);
225
+
226
+ // Export / Import
227
+ const json = bar.toJSON();
228
+ const restored = TinyNeedBar.fromJSON(json);
229
+
230
+ // Clone
231
+ const clone = bar.clone();
232
+ ```
233
+
234
+ ---
235
+
236
+ ### 🎨 Notes
237
+
238
+ * Designed for simulation & testing.
239
+ * Can handle multiple independent decay factors.
240
+ * `infiniteValue` is useful for simulations that allow negative overflow.
241
+ * Fully serializable for state saving/loading.
@@ -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.0",
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",