tiny-essentials 1.21.0 → 1.21.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/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyNeedBar.min.js +1 -0
- package/dist/v1/basics/clock.cjs +127 -11
- package/dist/v1/basics/clock.d.mts +26 -0
- package/dist/v1/basics/clock.mjs +96 -1
- package/dist/v1/build/TinyNeedBar.cjs +7 -0
- package/dist/v1/build/TinyNeedBar.d.mts +3 -0
- package/dist/v1/build/TinyNeedBar.mjs +2 -0
- package/dist/v1/index.cjs +2 -0
- package/dist/v1/index.d.mts +2 -1
- package/dist/v1/index.mjs +2 -1
- package/dist/v1/libs/TinyNeedBar.cjs +272 -0
- package/dist/v1/libs/TinyNeedBar.d.mts +223 -0
- package/dist/v1/libs/TinyNeedBar.mjs +241 -0
- package/docs/v1/README.md +1 -0
- package/docs/v1/basics/clock.md +51 -11
- package/docs/v1/libs/TinyNeedBar.md +226 -0
- package/package.json +1 -1
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
export default TinyNeedBar;
|
|
2
|
+
export type TickResult = {
|
|
3
|
+
/**
|
|
4
|
+
* - Infinite value before applying decay.
|
|
5
|
+
*/
|
|
6
|
+
prevValue: number;
|
|
7
|
+
/**
|
|
8
|
+
* - Total amount removed this tick.
|
|
9
|
+
*/
|
|
10
|
+
removedTotal: number;
|
|
11
|
+
/**
|
|
12
|
+
* - Percentage of max removed this tick.
|
|
13
|
+
*/
|
|
14
|
+
removedPercent: number;
|
|
15
|
+
/**
|
|
16
|
+
* - Current percentage relative to max.
|
|
17
|
+
*/
|
|
18
|
+
currentPercent: number;
|
|
19
|
+
/**
|
|
20
|
+
* - Current clamped value (≥ 0).
|
|
21
|
+
*/
|
|
22
|
+
remainingValue: number;
|
|
23
|
+
/**
|
|
24
|
+
* - Current infinite value (can be negative).
|
|
25
|
+
*/
|
|
26
|
+
infiniteRemaining: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Represents a decay factor applied to the need bar.
|
|
30
|
+
*
|
|
31
|
+
* - `amount`: base value reduced per tick.
|
|
32
|
+
* - `multiplier`: multiplier applied to the amount.
|
|
33
|
+
*/
|
|
34
|
+
export type BarFactor = {
|
|
35
|
+
/**
|
|
36
|
+
* - Base reduction value per tick.
|
|
37
|
+
*/
|
|
38
|
+
amount: number;
|
|
39
|
+
/**
|
|
40
|
+
* - Multiplier applied to the amount.
|
|
41
|
+
*/
|
|
42
|
+
multiplier: number;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Represents the serialized state of a TinyNeedBar instance.
|
|
46
|
+
*
|
|
47
|
+
* This object is typically produced by {@link TinyNeedBar#toJSON} and
|
|
48
|
+
* can be used to recreate an instance via {@link TinyNeedBar.fromJSON}.
|
|
49
|
+
*/
|
|
50
|
+
export type SerializedData = {
|
|
51
|
+
/**
|
|
52
|
+
* - Maximum value of the bar at the moment of serialization.
|
|
53
|
+
*/
|
|
54
|
+
maxValue: number;
|
|
55
|
+
/**
|
|
56
|
+
* - Current clamped value (never below 0).
|
|
57
|
+
*/
|
|
58
|
+
currentValue: number;
|
|
59
|
+
/**
|
|
60
|
+
* - Infinite value (can go negative).
|
|
61
|
+
*/
|
|
62
|
+
infiniteValue: number;
|
|
63
|
+
/**
|
|
64
|
+
* - Active decay factors indexed by their keys.
|
|
65
|
+
*/
|
|
66
|
+
factors: Record<string, BarFactor>;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* @typedef {Object} TickResult
|
|
70
|
+
* @property {number} prevValue - Infinite value before applying decay.
|
|
71
|
+
* @property {number} removedTotal - Total amount removed this tick.
|
|
72
|
+
* @property {number} removedPercent - Percentage of max removed this tick.
|
|
73
|
+
* @property {number} currentPercent - Current percentage relative to max.
|
|
74
|
+
* @property {number} remainingValue - Current clamped value (≥ 0).
|
|
75
|
+
* @property {number} infiniteRemaining - Current infinite value (can be negative).
|
|
76
|
+
*/
|
|
77
|
+
/**
|
|
78
|
+
* Represents a decay factor applied to the need bar.
|
|
79
|
+
*
|
|
80
|
+
* - `amount`: base value reduced per tick.
|
|
81
|
+
* - `multiplier`: multiplier applied to the amount.
|
|
82
|
+
*
|
|
83
|
+
* @typedef {Object} BarFactor
|
|
84
|
+
* @property {number} amount - Base reduction value per tick.
|
|
85
|
+
* @property {number} multiplier - Multiplier applied to the amount.
|
|
86
|
+
*/
|
|
87
|
+
/**
|
|
88
|
+
* Represents the serialized state of a TinyNeedBar instance.
|
|
89
|
+
*
|
|
90
|
+
* This object is typically produced by {@link TinyNeedBar#toJSON} and
|
|
91
|
+
* can be used to recreate an instance via {@link TinyNeedBar.fromJSON}.
|
|
92
|
+
*
|
|
93
|
+
* @typedef {Object} SerializedData
|
|
94
|
+
* @property {number} maxValue - Maximum value of the bar at the moment of serialization.
|
|
95
|
+
* @property {number} currentValue - Current clamped value (never below 0).
|
|
96
|
+
* @property {number} infiniteValue - Infinite value (can go negative).
|
|
97
|
+
* @property {Record<string, BarFactor>} factors - Active decay factors indexed by their keys.
|
|
98
|
+
*/
|
|
99
|
+
/**
|
|
100
|
+
* A utility class to simulate a "need bar" system.
|
|
101
|
+
*
|
|
102
|
+
* The bar decreases over time according to defined factors (each with an amount and multiplier).
|
|
103
|
+
* - The **main factor** controls the base decay per tick.
|
|
104
|
+
* - Additional factors can be added dynamically.
|
|
105
|
+
*
|
|
106
|
+
* The system tracks two values:
|
|
107
|
+
* - `currentValue` → cannot go below zero.
|
|
108
|
+
* - `infiniteValue` → can decrease infinitely into negative numbers.
|
|
109
|
+
*/
|
|
110
|
+
declare class TinyNeedBar {
|
|
111
|
+
/**
|
|
112
|
+
* Restores a need bar from a serialized object.
|
|
113
|
+
* @param {SerializedData} data
|
|
114
|
+
* @returns {TinyNeedBar}
|
|
115
|
+
*/
|
|
116
|
+
static fromJSON(data: SerializedData): TinyNeedBar;
|
|
117
|
+
/**
|
|
118
|
+
* Creates a new need bar instance.
|
|
119
|
+
*
|
|
120
|
+
* @param {number} [maxValue=100] - Maximum value of the bar.
|
|
121
|
+
* @param {number} [baseDecay=1] - Base amount reduced each tick.
|
|
122
|
+
* @param {number} [baseDecayMulti=1] - Multiplier applied to the base decay.
|
|
123
|
+
*/
|
|
124
|
+
constructor(maxValue?: number, baseDecay?: number, baseDecayMulti?: number);
|
|
125
|
+
/**
|
|
126
|
+
* Returns a snapshot of all currently active factors.
|
|
127
|
+
* Each factor is returned as a plain object to prevent direct mutation of the internal map.
|
|
128
|
+
*
|
|
129
|
+
* @returns {Record<string, BarFactor>} A record of all factors indexed by their key.
|
|
130
|
+
*/
|
|
131
|
+
get factors(): Record<string, BarFactor>;
|
|
132
|
+
/**
|
|
133
|
+
* Returns the current percentage of the bar relative to the maximum value.
|
|
134
|
+
*
|
|
135
|
+
* @returns {number} Percentage from `0` to `100`.
|
|
136
|
+
*/
|
|
137
|
+
get currentPercent(): number;
|
|
138
|
+
/**
|
|
139
|
+
* Updates the maximum possible value of the bar.
|
|
140
|
+
* Ensures `currentValue` never exceeds the new maximum.
|
|
141
|
+
*
|
|
142
|
+
* @param {number} value - New maximum value.
|
|
143
|
+
*/
|
|
144
|
+
set maxValue(value: number);
|
|
145
|
+
/**
|
|
146
|
+
* Returns the maximum possible value of the bar.
|
|
147
|
+
*
|
|
148
|
+
* @returns {number} The maximum value.
|
|
149
|
+
*/
|
|
150
|
+
get maxValue(): number;
|
|
151
|
+
/**
|
|
152
|
+
* Returns the current clamped value of the bar.
|
|
153
|
+
* This value will never be below `0`.
|
|
154
|
+
*
|
|
155
|
+
* @returns {number} Current value (≥ 0).
|
|
156
|
+
*/
|
|
157
|
+
get currentValue(): number;
|
|
158
|
+
/**
|
|
159
|
+
* Updates the infinite value of the bar.
|
|
160
|
+
* Automatically recalculates the `currentValue` (never below 0).
|
|
161
|
+
*
|
|
162
|
+
* @param {number} value - New infinite value.
|
|
163
|
+
*/
|
|
164
|
+
set infiniteValue(value: number);
|
|
165
|
+
/**
|
|
166
|
+
* Returns the current infinite value of the bar.
|
|
167
|
+
* Unlike `currentValue`, this one can go below zero.
|
|
168
|
+
*
|
|
169
|
+
* @returns {number} Current infinite value.
|
|
170
|
+
*/
|
|
171
|
+
get infiniteValue(): number;
|
|
172
|
+
/**
|
|
173
|
+
* Retrieves a specific factor by its key.
|
|
174
|
+
*
|
|
175
|
+
* @param {string} key - The unique key of the factor.
|
|
176
|
+
* @returns {BarFactor} The requested factor object.
|
|
177
|
+
* @throws {Error} If the factor does not exist.
|
|
178
|
+
*/
|
|
179
|
+
getFactor(key: string): BarFactor;
|
|
180
|
+
/**
|
|
181
|
+
* Checks if a specific factor exists by key.
|
|
182
|
+
*
|
|
183
|
+
* @param {string} key - The factor key to check.
|
|
184
|
+
* @returns {boolean} `true` if the factor exists, otherwise `false`.
|
|
185
|
+
*/
|
|
186
|
+
hasFactor(key: string): boolean;
|
|
187
|
+
/**
|
|
188
|
+
* Defines or updates a decay factor.
|
|
189
|
+
*
|
|
190
|
+
* @param {string} key - Unique identifier for the factor.
|
|
191
|
+
* @param {number} amount - Amount reduced per tick.
|
|
192
|
+
* @param {number} [multiplier=1] - Multiplier applied to the amount.
|
|
193
|
+
*/
|
|
194
|
+
setFactor(key: string, amount: number, multiplier?: number): void;
|
|
195
|
+
/**
|
|
196
|
+
* Removes a decay factor by its key.
|
|
197
|
+
*
|
|
198
|
+
* @param {string} key - The factor key to remove.
|
|
199
|
+
*/
|
|
200
|
+
removeFactor(key: string): void;
|
|
201
|
+
/**
|
|
202
|
+
* Executes one tick of decay, applying all active factors.
|
|
203
|
+
*
|
|
204
|
+
* @returns {TickResult}
|
|
205
|
+
*/
|
|
206
|
+
tick(): TickResult;
|
|
207
|
+
/**
|
|
208
|
+
* Serializes the current state of the need bar.
|
|
209
|
+
* @returns {SerializedData}
|
|
210
|
+
*/
|
|
211
|
+
toJSON(): SerializedData;
|
|
212
|
+
/**
|
|
213
|
+
* Creates a deep clone of this need bar.
|
|
214
|
+
* @returns {TinyNeedBar}
|
|
215
|
+
*/
|
|
216
|
+
clone(): TinyNeedBar;
|
|
217
|
+
/**
|
|
218
|
+
* Clear the factors map, clearing all factor data.
|
|
219
|
+
*/
|
|
220
|
+
clearFactors(): void;
|
|
221
|
+
#private;
|
|
222
|
+
}
|
|
223
|
+
//# sourceMappingURL=TinyNeedBar.d.mts.map
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} TickResult
|
|
3
|
+
* @property {number} prevValue - Infinite value before applying decay.
|
|
4
|
+
* @property {number} removedTotal - Total amount removed this tick.
|
|
5
|
+
* @property {number} removedPercent - Percentage of max removed this tick.
|
|
6
|
+
* @property {number} currentPercent - Current percentage relative to max.
|
|
7
|
+
* @property {number} remainingValue - Current clamped value (≥ 0).
|
|
8
|
+
* @property {number} infiniteRemaining - Current infinite value (can be negative).
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Represents a decay factor applied to the need bar.
|
|
12
|
+
*
|
|
13
|
+
* - `amount`: base value reduced per tick.
|
|
14
|
+
* - `multiplier`: multiplier applied to the amount.
|
|
15
|
+
*
|
|
16
|
+
* @typedef {Object} BarFactor
|
|
17
|
+
* @property {number} amount - Base reduction value per tick.
|
|
18
|
+
* @property {number} multiplier - Multiplier applied to the amount.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Represents the serialized state of a TinyNeedBar instance.
|
|
22
|
+
*
|
|
23
|
+
* This object is typically produced by {@link TinyNeedBar#toJSON} and
|
|
24
|
+
* can be used to recreate an instance via {@link TinyNeedBar.fromJSON}.
|
|
25
|
+
*
|
|
26
|
+
* @typedef {Object} SerializedData
|
|
27
|
+
* @property {number} maxValue - Maximum value of the bar at the moment of serialization.
|
|
28
|
+
* @property {number} currentValue - Current clamped value (never below 0).
|
|
29
|
+
* @property {number} infiniteValue - Infinite value (can go negative).
|
|
30
|
+
* @property {Record<string, BarFactor>} factors - Active decay factors indexed by their keys.
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* A utility class to simulate a "need bar" system.
|
|
34
|
+
*
|
|
35
|
+
* The bar decreases over time according to defined factors (each with an amount and multiplier).
|
|
36
|
+
* - The **main factor** controls the base decay per tick.
|
|
37
|
+
* - Additional factors can be added dynamically.
|
|
38
|
+
*
|
|
39
|
+
* The system tracks two values:
|
|
40
|
+
* - `currentValue` → cannot go below zero.
|
|
41
|
+
* - `infiniteValue` → can decrease infinitely into negative numbers.
|
|
42
|
+
*/
|
|
43
|
+
class TinyNeedBar {
|
|
44
|
+
/**
|
|
45
|
+
* Stores all factors that influence decay.
|
|
46
|
+
* Each entry contains an amount and a multiplier.
|
|
47
|
+
* @type {Map<string, BarFactor>}
|
|
48
|
+
*/
|
|
49
|
+
#factors = new Map();
|
|
50
|
+
/** Maximum value of the bar. @type {number} */
|
|
51
|
+
#maxValue;
|
|
52
|
+
/** Current clamped value of the bar (never below 0). @type {number} */
|
|
53
|
+
#currentValue;
|
|
54
|
+
/** Current "infinite" value of the bar (can go negative). @type {number} */
|
|
55
|
+
#infiniteValue;
|
|
56
|
+
/**
|
|
57
|
+
* Returns a snapshot of all currently active factors.
|
|
58
|
+
* Each factor is returned as a plain object to prevent direct mutation of the internal map.
|
|
59
|
+
*
|
|
60
|
+
* @returns {Record<string, BarFactor>} A record of all factors indexed by their key.
|
|
61
|
+
*/
|
|
62
|
+
get factors() {
|
|
63
|
+
/** @type {Record<string, BarFactor>} */
|
|
64
|
+
const factors = {};
|
|
65
|
+
for (let [name, factor] of this.#factors.entries()) {
|
|
66
|
+
factors[name] = { ...factor };
|
|
67
|
+
}
|
|
68
|
+
return factors;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns the current percentage of the bar relative to the maximum value.
|
|
72
|
+
*
|
|
73
|
+
* @returns {number} Percentage from `0` to `100`.
|
|
74
|
+
*/
|
|
75
|
+
get currentPercent() {
|
|
76
|
+
return (this.#currentValue / this.#maxValue) * 100;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Updates the maximum possible value of the bar.
|
|
80
|
+
* Ensures `currentValue` never exceeds the new maximum.
|
|
81
|
+
*
|
|
82
|
+
* @param {number} value - New maximum value.
|
|
83
|
+
*/
|
|
84
|
+
set maxValue(value) {
|
|
85
|
+
this.#maxValue = value;
|
|
86
|
+
this.#currentValue = Math.min(this.#currentValue, value);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns the maximum possible value of the bar.
|
|
90
|
+
*
|
|
91
|
+
* @returns {number} The maximum value.
|
|
92
|
+
*/
|
|
93
|
+
get maxValue() {
|
|
94
|
+
return this.#maxValue;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Returns the current clamped value of the bar.
|
|
98
|
+
* This value will never be below `0`.
|
|
99
|
+
*
|
|
100
|
+
* @returns {number} Current value (≥ 0).
|
|
101
|
+
*/
|
|
102
|
+
get currentValue() {
|
|
103
|
+
return this.#currentValue;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Updates the infinite value of the bar.
|
|
107
|
+
* Automatically recalculates the `currentValue` (never below 0).
|
|
108
|
+
*
|
|
109
|
+
* @param {number} value - New infinite value.
|
|
110
|
+
*/
|
|
111
|
+
set infiniteValue(value) {
|
|
112
|
+
this.#infiniteValue = value;
|
|
113
|
+
this.#currentValue = Math.max(0, value);
|
|
114
|
+
this.#currentValue = Math.min(this.#currentValue, this.#maxValue);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Returns the current infinite value of the bar.
|
|
118
|
+
* Unlike `currentValue`, this one can go below zero.
|
|
119
|
+
*
|
|
120
|
+
* @returns {number} Current infinite value.
|
|
121
|
+
*/
|
|
122
|
+
get infiniteValue() {
|
|
123
|
+
return this.#infiniteValue;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Creates a new need bar instance.
|
|
127
|
+
*
|
|
128
|
+
* @param {number} [maxValue=100] - Maximum value of the bar.
|
|
129
|
+
* @param {number} [baseDecay=1] - Base amount reduced each tick.
|
|
130
|
+
* @param {number} [baseDecayMulti=1] - Multiplier applied to the base decay.
|
|
131
|
+
*/
|
|
132
|
+
constructor(maxValue = 100, baseDecay = 1, baseDecayMulti = 1) {
|
|
133
|
+
this.#maxValue = maxValue;
|
|
134
|
+
this.setFactor('main', baseDecay, baseDecayMulti);
|
|
135
|
+
this.#currentValue = maxValue;
|
|
136
|
+
this.#infiniteValue = maxValue;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Retrieves a specific factor by its key.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} key - The unique key of the factor.
|
|
142
|
+
* @returns {BarFactor} The requested factor object.
|
|
143
|
+
* @throws {Error} If the factor does not exist.
|
|
144
|
+
*/
|
|
145
|
+
getFactor(key) {
|
|
146
|
+
const result = this.#factors.get(key);
|
|
147
|
+
if (!result)
|
|
148
|
+
throw new Error(`Factor with key "${key}" not found.`);
|
|
149
|
+
return { ...result };
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Checks if a specific factor exists by key.
|
|
153
|
+
*
|
|
154
|
+
* @param {string} key - The factor key to check.
|
|
155
|
+
* @returns {boolean} `true` if the factor exists, otherwise `false`.
|
|
156
|
+
*/
|
|
157
|
+
hasFactor(key) {
|
|
158
|
+
return this.#factors.has(key);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Defines or updates a decay factor.
|
|
162
|
+
*
|
|
163
|
+
* @param {string} key - Unique identifier for the factor.
|
|
164
|
+
* @param {number} amount - Amount reduced per tick.
|
|
165
|
+
* @param {number} [multiplier=1] - Multiplier applied to the amount.
|
|
166
|
+
*/
|
|
167
|
+
setFactor(key, amount, multiplier = 1) {
|
|
168
|
+
this.#factors.set(key, { amount, multiplier });
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Removes a decay factor by its key.
|
|
172
|
+
*
|
|
173
|
+
* @param {string} key - The factor key to remove.
|
|
174
|
+
*/
|
|
175
|
+
removeFactor(key) {
|
|
176
|
+
this.#factors.delete(key);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Executes one tick of decay, applying all active factors.
|
|
180
|
+
*
|
|
181
|
+
* @returns {TickResult}
|
|
182
|
+
*/
|
|
183
|
+
tick() {
|
|
184
|
+
let removedTotal = 0;
|
|
185
|
+
for (let [_, factor] of this.#factors.entries()) {
|
|
186
|
+
removedTotal += factor.amount * factor.multiplier;
|
|
187
|
+
}
|
|
188
|
+
const prevValue = this.#infiniteValue;
|
|
189
|
+
this.#infiniteValue -= removedTotal;
|
|
190
|
+
this.#currentValue = Math.max(0, this.#currentValue - removedTotal);
|
|
191
|
+
const removedPercent = (removedTotal / this.#maxValue) * 100;
|
|
192
|
+
return {
|
|
193
|
+
prevValue,
|
|
194
|
+
removedTotal,
|
|
195
|
+
removedPercent,
|
|
196
|
+
currentPercent: this.currentPercent,
|
|
197
|
+
remainingValue: this.#currentValue,
|
|
198
|
+
infiniteRemaining: this.#infiniteValue,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Serializes the current state of the need bar.
|
|
203
|
+
* @returns {SerializedData}
|
|
204
|
+
*/
|
|
205
|
+
toJSON() {
|
|
206
|
+
return {
|
|
207
|
+
maxValue: this.#maxValue,
|
|
208
|
+
currentValue: this.#currentValue,
|
|
209
|
+
infiniteValue: this.#infiniteValue,
|
|
210
|
+
factors: this.factors,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Restores a need bar from a serialized object.
|
|
215
|
+
* @param {SerializedData} data
|
|
216
|
+
* @returns {TinyNeedBar}
|
|
217
|
+
*/
|
|
218
|
+
static fromJSON(data) {
|
|
219
|
+
const bar = new TinyNeedBar(data.maxValue, 0, 0);
|
|
220
|
+
bar.infiniteValue = data.infiniteValue;
|
|
221
|
+
bar.#factors.clear();
|
|
222
|
+
for (const [key, factor] of Object.entries(data.factors)) {
|
|
223
|
+
bar.setFactor(key, factor.amount, factor.multiplier);
|
|
224
|
+
}
|
|
225
|
+
return bar;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Creates a deep clone of this need bar.
|
|
229
|
+
* @returns {TinyNeedBar}
|
|
230
|
+
*/
|
|
231
|
+
clone() {
|
|
232
|
+
return TinyNeedBar.fromJSON(this.toJSON());
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Clear the factors map, clearing all factor data.
|
|
236
|
+
*/
|
|
237
|
+
clearFactors() {
|
|
238
|
+
this.#factors.clear();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
export default TinyNeedBar;
|
package/docs/v1/README.md
CHANGED
|
@@ -55,6 +55,7 @@ This folder contains the core scripts we have worked on so far. Each file is a m
|
|
|
55
55
|
- 📦 **[TinyInventory](./libs/TinyInventory.md)** — A robust inventory management system with stack handling, slot management, special equipment slots, serialization, cloning, and item registry support.
|
|
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
|
+
- 🎮 **[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.
|
|
58
59
|
|
|
59
60
|
### 3. **`fileManager/`**
|
|
60
61
|
* 📁 **[Main](./fileManager/main.md)** — A Node.js file/directory utility module with support for JSON, backups, renaming, size analysis, and more.
|
package/docs/v1/basics/clock.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# ⏰ clock.mjs
|
|
2
2
|
|
|
3
|
-
A versatile time utility module for JavaScript that helps you calculate and format time durations with style. Whether you're building countdowns, timers, or just need a nice "HH
|
|
3
|
+
A versatile time utility module for JavaScript that helps you calculate and format time durations with style. Whether you're building countdowns, timers, or just need a nice "HH\:MM\:SS" string, this module has your back!
|
|
4
4
|
|
|
5
5
|
## ✨ Features
|
|
6
6
|
|
|
7
7
|
- 🔢 Calculate time differences in various units
|
|
8
8
|
- 🧭 Format durations into clean, readable timer strings
|
|
9
|
-
- 🧱 Support for years, months, days, hours, minutes, and
|
|
9
|
+
- 🧱 Support for years, months, days, hours, minutes, seconds **and milliseconds**
|
|
10
10
|
- 🎛️ Customizable output with template-based formatting
|
|
11
11
|
- 🛠️ Built-in presets for classic timer formats
|
|
12
12
|
- 🪶 Zero dependencies, lightweight and modular (ESM-ready!)
|
|
@@ -19,11 +19,11 @@ A versatile time utility module for JavaScript that helps you calculate and form
|
|
|
19
19
|
|
|
20
20
|
Calculates how much time remains (or has passed) between now and a given time.
|
|
21
21
|
|
|
22
|
-
| Param
|
|
23
|
-
|
|
24
|
-
| `timeData`
|
|
25
|
-
| `durationType
|
|
26
|
-
| `now`
|
|
22
|
+
| Param | Type | Description |
|
|
23
|
+
| -------------- | -------- | ------------------------------------------------------------------- |
|
|
24
|
+
| `timeData` | `Date` | The target time |
|
|
25
|
+
| `durationType` | `string` | Format to return (`asMilliseconds`, `asSeconds`, `asMinutes`, etc.) |
|
|
26
|
+
| `now` | `Date` | Optional custom "now". Defaults to current time. |
|
|
27
27
|
|
|
28
28
|
**Returns:** `number|null` — The duration in the chosen unit.
|
|
29
29
|
|
|
@@ -33,11 +33,11 @@ Calculates how much time remains (or has passed) between now and a given time.
|
|
|
33
33
|
|
|
34
34
|
Turns seconds into a custom timer string with fine-grained control.
|
|
35
35
|
|
|
36
|
-
| Param
|
|
37
|
-
|
|
36
|
+
| Param | Type | Description |
|
|
37
|
+
| -------------- | -------- | ------------------------------------------------------------------------------------------- |
|
|
38
38
|
| `totalSeconds` | `number` | The duration to format (in seconds) |
|
|
39
|
-
| `level`
|
|
40
|
-
| `format`
|
|
39
|
+
| `level` | `string` | Highest level to show: `'seconds'`, `'minutes'`, `'hours'`, `'days'`, `'months'`, `'years'` |
|
|
40
|
+
| `format` | `string` | Output string template. Supports placeholders like `{days}`, `{hours}`, `{time}`, `{total}` |
|
|
41
41
|
|
|
42
42
|
**Returns:** `string` — A formatted string with padded units.
|
|
43
43
|
|
|
@@ -63,6 +63,33 @@ formatDayTimer(190000); // "2d 04:46:40"
|
|
|
63
63
|
|
|
64
64
|
---
|
|
65
65
|
|
|
66
|
+
### 🔹 `breakdownDuration(totalMs, level = 'milliseconds')`
|
|
67
|
+
|
|
68
|
+
Breaks down a duration (in **milliseconds**) into its full components, returning an object instead of a string.
|
|
69
|
+
This is useful when you want numeric values to build your own custom formats.
|
|
70
|
+
|
|
71
|
+
| Param | Type | Description |
|
|
72
|
+
| --------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
|
|
73
|
+
| `totalMs` | `number` | The duration to format (in milliseconds) |
|
|
74
|
+
| `level` | `string` | Highest level to break down: `'milliseconds'`, `'seconds'`, `'minutes'`, `'hours'`, `'days'`, `'months'`, `'years'` |
|
|
75
|
+
|
|
76
|
+
**Returns:** `object` — An object with numeric values for all included units:
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{
|
|
80
|
+
"years": 0,
|
|
81
|
+
"months": 0,
|
|
82
|
+
"days": 11,
|
|
83
|
+
"hours": 10,
|
|
84
|
+
"minutes": 20,
|
|
85
|
+
"seconds": 54,
|
|
86
|
+
"milliseconds": 321,
|
|
87
|
+
"total": 987654321
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
66
93
|
## 🧪 Examples
|
|
67
94
|
|
|
68
95
|
```js
|
|
@@ -74,4 +101,17 @@ console.log(formatDayTimer(172800 + 3661)); // "2d 01:01:01"
|
|
|
74
101
|
|
|
75
102
|
const custom = formatCustomTimer(3600 * 26 + 61, 'days', '{days}d {hours}h {minutes}m {seconds}s');
|
|
76
103
|
console.log(custom); // "1d 2h 1m 1s"
|
|
104
|
+
|
|
105
|
+
const breakdown = breakdownDuration(987654321, 'years');
|
|
106
|
+
console.log(breakdown);
|
|
107
|
+
// {
|
|
108
|
+
// years: 0,
|
|
109
|
+
// months: 0,
|
|
110
|
+
// days: 11,
|
|
111
|
+
// hours: 10,
|
|
112
|
+
// minutes: 20,
|
|
113
|
+
// seconds: 54,
|
|
114
|
+
// milliseconds: 321,
|
|
115
|
+
// total: 987654321
|
|
116
|
+
// }
|
|
77
117
|
```
|