@kikiutils/shared 15.0.3 → 15.1.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/README.md +137 -67
- package/dist/buffer.d.ts.map +1 -1
- package/dist/buffer.js.map +1 -1
- package/dist/classes/path.d.ts +8 -0
- package/dist/classes/path.d.ts.map +1 -1
- package/dist/classes/path.js +5 -0
- package/dist/classes/path.js.map +1 -1
- package/dist/classes/precision-number.d.ts +215 -11
- package/dist/classes/precision-number.d.ts.map +1 -1
- package/dist/classes/precision-number.js +268 -25
- package/dist/classes/precision-number.js.map +1 -1
- package/dist/classes/ssh-client.js.map +1 -1
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js.map +1 -1
- package/dist/consola.d.ts.map +1 -1
- package/dist/consola.js.map +1 -1
- package/dist/crypto-hash.d.ts +7 -7
- package/dist/crypto-hash.d.ts.map +1 -1
- package/dist/crypto-hash.js +7 -7
- package/dist/crypto-hash.js.map +1 -1
- package/dist/datetime.d.ts.map +1 -1
- package/dist/datetime.js.map +1 -1
- package/dist/element-plus.d.ts.map +1 -1
- package/dist/element-plus.js.map +1 -1
- package/dist/enum.d.ts.map +1 -1
- package/dist/enum.js.map +1 -1
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js.map +1 -1
- package/dist/event-awaiter.d.ts.map +1 -1
- package/dist/event-awaiter.js.map +1 -1
- package/dist/general.d.ts.map +1 -1
- package/dist/general.js.map +1 -1
- package/dist/hash.d.ts +3 -3
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js +3 -3
- package/dist/hash.js.map +1 -1
- package/dist/math.d.ts.map +1 -1
- package/dist/math.js.map +1 -1
- package/dist/number.d.ts.map +1 -1
- package/dist/number.js.map +1 -1
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js.map +1 -1
- package/dist/pino.d.ts.map +1 -1
- package/dist/pino.js.map +1 -1
- package/dist/random.d.ts.map +1 -1
- package/dist/random.js.map +1 -1
- package/dist/storages/lru/keyed-store.d.ts +3 -3
- package/dist/storages/lru/keyed-store.js +3 -3
- package/dist/storages/lru/keyed-store.js.map +1 -1
- package/dist/storages/redis/keyed-store.d.ts +1 -1
- package/dist/storages/redis/keyed-store.js +1 -1
- package/dist/storages/redis/keyed-store.js.map +1 -1
- package/dist/storages/redis/msgpack.d.ts.map +1 -1
- package/dist/storages/redis/msgpack.js.map +1 -1
- package/dist/storages/redis/types.d.ts.map +1 -1
- package/dist/string.d.ts +12 -6
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +9 -6
- package/dist/string.js.map +1 -1
- package/dist/time.d.ts.map +1 -1
- package/dist/time.js.map +1 -1
- package/dist/types/filtered-key-path.d.ts.map +1 -1
- package/dist/types/index.d.ts +30 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/url.d.ts +27 -3
- package/dist/url.d.ts.map +1 -1
- package/dist/url.js +37 -4
- package/dist/url.js.map +1 -1
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js.map +1 -1
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js.map +1 -1
- package/package.json +12 -21
- package/src/classes/path.ts +9 -0
- package/src/classes/precision-number.ts +300 -25
- package/src/crypto-hash.ts +7 -7
- package/src/hash.ts +3 -3
- package/src/storages/lru/keyed-store.ts +3 -3
- package/src/storages/redis/keyed-store.ts +1 -1
- package/src/string.ts +12 -6
- package/src/types/index.ts +39 -0
- package/src/url.ts +39 -7
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
import { Decimal } from 'decimal.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Values accepted by `PrecisionNumber` constructors and arithmetic methods.
|
|
5
|
+
*/
|
|
3
6
|
export type PrecisionNumberValue = Decimal.Value | PrecisionNumber | { toString: () => string };
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
|
-
*
|
|
9
|
+
* Chainable fixed-decimal wrapper around `decimal.js`.
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
11
|
+
* `PrecisionNumber` stores values as `Decimal`, rounds every mutating operation to
|
|
12
|
+
* the configured decimal places, and exposes both mutable and immutable arithmetic
|
|
13
|
+
* methods. Mutable methods (`plus`, `minus`, `times`, `dividedBy`, `modulo`,
|
|
14
|
+
* `pow`, `clamp`, `ceil`, `floor`, `negate`, `absoluteValue`) update the current
|
|
15
|
+
* instance and return `this`; immutable methods prefixed with `to` return a new
|
|
16
|
+
* `PrecisionNumber` with the same precision settings.
|
|
12
17
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { PrecisionNumber } from '@kikiutils/shared/classes/precision-number';
|
|
15
21
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
22
|
+
* const value = new PrecisionNumber('1.239', 2).plus('2.111');
|
|
23
|
+
* value.toString(); // '3.35'
|
|
24
|
+
* value.toMinus('1').toString(); // '2.35' (original value remains '3.35')
|
|
25
|
+
* ```
|
|
20
26
|
*/
|
|
21
27
|
export class PrecisionNumber {
|
|
22
28
|
// Private properties
|
|
@@ -24,6 +30,13 @@ export class PrecisionNumber {
|
|
|
24
30
|
readonly #rounding: Decimal.Rounding;
|
|
25
31
|
#decimal: Decimal;
|
|
26
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Creates a fixed-decimal number.
|
|
35
|
+
*
|
|
36
|
+
* @param {PrecisionNumberValue} [value] - Initial value (default: `'0'`)
|
|
37
|
+
* @param {number} [decimalPlaces] - Decimal places retained after operations (default: `2`)
|
|
38
|
+
* @param {Decimal.Rounding} [rounding] - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`)
|
|
39
|
+
*/
|
|
27
40
|
constructor(
|
|
28
41
|
value: PrecisionNumberValue = '0',
|
|
29
42
|
decimalPlaces: number = 2,
|
|
@@ -35,10 +48,19 @@ export class PrecisionNumber {
|
|
|
35
48
|
}
|
|
36
49
|
|
|
37
50
|
// Symbols
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns the formatted value when inspected by Node.js utilities.
|
|
54
|
+
*/
|
|
38
55
|
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
39
56
|
return this.value;
|
|
40
57
|
}
|
|
41
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Converts to a number for numeric coercion and to the fixed string otherwise.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} hint - JavaScript primitive-conversion hint
|
|
63
|
+
*/
|
|
42
64
|
[Symbol.toPrimitive](hint: string) {
|
|
43
65
|
if (hint === 'number') return this.#decimal.toNumber();
|
|
44
66
|
return this.value;
|
|
@@ -49,12 +71,42 @@ export class PrecisionNumber {
|
|
|
49
71
|
return decimal.toDecimalPlaces(this.#decimalPlaces, this.#rounding);
|
|
50
72
|
}
|
|
51
73
|
|
|
74
|
+
#valueToString(value: PrecisionNumberValue) {
|
|
75
|
+
return value.toString().trim();
|
|
76
|
+
}
|
|
77
|
+
|
|
52
78
|
// Public getters
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Decimal places retained by mutating operations and default formatting.
|
|
82
|
+
*/
|
|
83
|
+
get decimalPlaces() {
|
|
84
|
+
return this.#decimalPlaces;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Decimal.js rounding mode used by mutating operations and default formatting.
|
|
89
|
+
*/
|
|
90
|
+
get rounding() {
|
|
91
|
+
return this.#rounding;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Fixed-decimal string using the instance precision and rounding mode.
|
|
96
|
+
*/
|
|
53
97
|
get value() {
|
|
54
98
|
return this.#decimal.toFixed(this.#decimalPlaces, this.#rounding);
|
|
55
99
|
}
|
|
56
100
|
|
|
57
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Formats a value with decimal.js without creating a reusable instance.
|
|
103
|
+
*
|
|
104
|
+
* @param {PrecisionNumberValue} value - Value to format
|
|
105
|
+
* @param {number} [decimalPlaces] - Decimal places to output (default: `2`)
|
|
106
|
+
* @param {Decimal.Rounding} [rounding] - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`)
|
|
107
|
+
*
|
|
108
|
+
* @returns {string} Fixed-decimal string
|
|
109
|
+
*/
|
|
58
110
|
static toFixed(
|
|
59
111
|
value: PrecisionNumberValue,
|
|
60
112
|
decimalPlaces: number = 2,
|
|
@@ -64,117 +116,340 @@ export class PrecisionNumber {
|
|
|
64
116
|
}
|
|
65
117
|
|
|
66
118
|
// Public methods
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Replaces the current value with its absolute value.
|
|
122
|
+
*
|
|
123
|
+
* @returns {this} The current instance for chaining
|
|
124
|
+
*/
|
|
67
125
|
absoluteValue() {
|
|
68
126
|
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.absoluteValue());
|
|
69
127
|
return this;
|
|
70
128
|
}
|
|
71
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Rounds the current value up to the nearest integer in place.
|
|
132
|
+
*
|
|
133
|
+
* The stored value still uses this instance's configured decimal places for output.
|
|
134
|
+
*
|
|
135
|
+
* @returns {this} The current instance for chaining
|
|
136
|
+
*/
|
|
137
|
+
ceil() {
|
|
138
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.ceil());
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Restricts the current value to the inclusive range `[min, max]` in place.
|
|
144
|
+
*
|
|
145
|
+
* @param {PrecisionNumberValue} min - Lower bound
|
|
146
|
+
* @param {PrecisionNumberValue} max - Upper bound
|
|
147
|
+
*
|
|
148
|
+
* @returns {this} The current instance for chaining
|
|
149
|
+
*
|
|
150
|
+
* @throws {Error} If `min` is greater than `max`
|
|
151
|
+
*/
|
|
152
|
+
clamp(min: PrecisionNumberValue, max: PrecisionNumberValue) {
|
|
153
|
+
const minDecimal = new Decimal(this.#valueToString(min));
|
|
154
|
+
const maxDecimal = new Decimal(this.#valueToString(max));
|
|
155
|
+
if (minDecimal.gt(maxDecimal)) throw new Error('Invalid clamp range: min cannot be greater than max');
|
|
156
|
+
this.#decimal = this.#decimalToFixedDecimal(Decimal.min(Decimal.max(this.#decimal, minDecimal), maxDecimal));
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Returns a new instance with the same value, decimal places, and rounding mode.
|
|
162
|
+
*/
|
|
163
|
+
clone() {
|
|
164
|
+
return new PrecisionNumber(this.#decimal, this.#decimalPlaces, this.#rounding);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Divides the current value by another value in place.
|
|
169
|
+
*
|
|
170
|
+
* @param {PrecisionNumberValue} value - Divisor
|
|
171
|
+
*
|
|
172
|
+
* @returns {this} The current instance for chaining
|
|
173
|
+
*/
|
|
72
174
|
dividedBy(value: PrecisionNumberValue) {
|
|
73
|
-
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(value
|
|
175
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(this.#valueToString(value)));
|
|
74
176
|
return this;
|
|
75
177
|
}
|
|
76
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Checks whether the current value equals another value.
|
|
181
|
+
*
|
|
182
|
+
* @param {PrecisionNumberValue} value - Value to compare
|
|
183
|
+
*
|
|
184
|
+
* @returns {boolean} `true` when both numeric values are equal
|
|
185
|
+
*/
|
|
77
186
|
equals(value: PrecisionNumberValue) {
|
|
78
|
-
return this.#decimal.equals(value
|
|
187
|
+
return this.#decimal.equals(this.#valueToString(value));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Rounds the current value down to the nearest integer in place.
|
|
192
|
+
*
|
|
193
|
+
* The stored value still uses this instance's configured decimal places for output.
|
|
194
|
+
*
|
|
195
|
+
* @returns {this} The current instance for chaining
|
|
196
|
+
*/
|
|
197
|
+
floor() {
|
|
198
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.floor());
|
|
199
|
+
return this;
|
|
79
200
|
}
|
|
80
201
|
|
|
202
|
+
/**
|
|
203
|
+
* Checks whether the current value is greater than another value.
|
|
204
|
+
*/
|
|
81
205
|
gt(value: PrecisionNumberValue) {
|
|
82
|
-
return this.#decimal.gt(value
|
|
206
|
+
return this.#decimal.gt(this.#valueToString(value));
|
|
83
207
|
}
|
|
84
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Checks whether the current value is greater than or equal to another value.
|
|
211
|
+
*/
|
|
85
212
|
gte(value: PrecisionNumberValue) {
|
|
86
|
-
return this.#decimal.gte(value
|
|
213
|
+
return this.#decimal.gte(this.#valueToString(value));
|
|
87
214
|
}
|
|
88
215
|
|
|
216
|
+
/**
|
|
217
|
+
* Checks whether the current value is finite.
|
|
218
|
+
*/
|
|
89
219
|
isFinite() {
|
|
90
220
|
return this.#decimal.isFinite();
|
|
91
221
|
}
|
|
92
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Checks whether the current value is an integer.
|
|
225
|
+
*/
|
|
93
226
|
isInteger() {
|
|
94
227
|
return this.#decimal.isInteger();
|
|
95
228
|
}
|
|
96
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Checks whether the current value is `NaN`.
|
|
232
|
+
*/
|
|
97
233
|
isNaN() {
|
|
98
234
|
return this.#decimal.isNaN();
|
|
99
235
|
}
|
|
100
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Checks whether the current value is negative.
|
|
239
|
+
*/
|
|
101
240
|
isNegative() {
|
|
102
241
|
return this.#decimal.isNegative();
|
|
103
242
|
}
|
|
104
243
|
|
|
244
|
+
/**
|
|
245
|
+
* Checks whether the current value is positive.
|
|
246
|
+
*/
|
|
105
247
|
isPositive() {
|
|
106
248
|
return this.#decimal.isPositive();
|
|
107
249
|
}
|
|
108
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Checks whether the current value is zero.
|
|
253
|
+
*/
|
|
109
254
|
isZero() {
|
|
110
255
|
return this.#decimal.isZero();
|
|
111
256
|
}
|
|
112
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Checks whether the current value is less than another value.
|
|
260
|
+
*/
|
|
113
261
|
lt(value: PrecisionNumberValue) {
|
|
114
|
-
return this.#decimal.lt(value
|
|
262
|
+
return this.#decimal.lt(this.#valueToString(value));
|
|
115
263
|
}
|
|
116
264
|
|
|
265
|
+
/**
|
|
266
|
+
* Checks whether the current value is less than or equal to another value.
|
|
267
|
+
*/
|
|
117
268
|
lte(value: PrecisionNumberValue) {
|
|
118
|
-
return this.#decimal.lte(value
|
|
269
|
+
return this.#decimal.lte(this.#valueToString(value));
|
|
119
270
|
}
|
|
120
271
|
|
|
272
|
+
/**
|
|
273
|
+
* Subtracts another value from the current value in place.
|
|
274
|
+
*/
|
|
121
275
|
minus(value: PrecisionNumberValue) {
|
|
122
|
-
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.minus(value
|
|
276
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.minus(this.#valueToString(value)));
|
|
277
|
+
return this;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Replaces the current value with the remainder after division by another value.
|
|
282
|
+
*
|
|
283
|
+
* @param {PrecisionNumberValue} value - Divisor used to compute the remainder
|
|
284
|
+
*
|
|
285
|
+
* @returns {this} The current instance for chaining
|
|
286
|
+
*/
|
|
287
|
+
modulo(value: PrecisionNumberValue) {
|
|
288
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.modulo(this.#valueToString(value)));
|
|
123
289
|
return this;
|
|
124
290
|
}
|
|
125
291
|
|
|
292
|
+
/**
|
|
293
|
+
* Negates the current value in place.
|
|
294
|
+
*/
|
|
126
295
|
negate() {
|
|
127
296
|
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.negated());
|
|
128
297
|
return this;
|
|
129
298
|
}
|
|
130
299
|
|
|
300
|
+
/**
|
|
301
|
+
* Adds another value to the current value in place.
|
|
302
|
+
*/
|
|
131
303
|
plus(value: PrecisionNumberValue) {
|
|
132
|
-
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.plus(value
|
|
304
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.plus(this.#valueToString(value)));
|
|
305
|
+
return this;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Raises the current value to an exponent in place.
|
|
310
|
+
*
|
|
311
|
+
* @param {PrecisionNumberValue} value - Exponent
|
|
312
|
+
*
|
|
313
|
+
* @returns {this} The current instance for chaining
|
|
314
|
+
*/
|
|
315
|
+
pow(value: PrecisionNumberValue) {
|
|
316
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.pow(this.#valueToString(value)));
|
|
133
317
|
return this;
|
|
134
318
|
}
|
|
135
319
|
|
|
320
|
+
/**
|
|
321
|
+
* Multiplies the current value by another value in place.
|
|
322
|
+
*/
|
|
136
323
|
times(value: PrecisionNumberValue) {
|
|
137
|
-
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.times(value
|
|
324
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.times(this.#valueToString(value)));
|
|
138
325
|
return this;
|
|
139
326
|
}
|
|
140
327
|
|
|
328
|
+
/**
|
|
329
|
+
* Returns a new instance with the absolute value.
|
|
330
|
+
*/
|
|
141
331
|
toAbsoluteValue() {
|
|
142
332
|
return new PrecisionNumber(this.#decimal.absoluteValue(), this.#decimalPlaces, this.#rounding);
|
|
143
333
|
}
|
|
144
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Returns a new instance rounded up to the nearest integer.
|
|
337
|
+
*/
|
|
338
|
+
toCeil() {
|
|
339
|
+
return new PrecisionNumber(this.#decimal.ceil(), this.#decimalPlaces, this.#rounding);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Returns a new instance restricted to the inclusive range `[min, max]`.
|
|
344
|
+
*
|
|
345
|
+
* @param {PrecisionNumberValue} min - Lower bound
|
|
346
|
+
* @param {PrecisionNumberValue} max - Upper bound
|
|
347
|
+
*
|
|
348
|
+
* @throws {Error} If `min` is greater than `max`
|
|
349
|
+
*/
|
|
350
|
+
toClamped(min: PrecisionNumberValue, max: PrecisionNumberValue) {
|
|
351
|
+
return this.clone().clamp(min, max);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Returns a new instance divided by another value.
|
|
356
|
+
*/
|
|
145
357
|
toDividedBy(value: PrecisionNumberValue) {
|
|
146
358
|
return new PrecisionNumber(
|
|
147
|
-
this.#decimal.dividedBy(value
|
|
359
|
+
this.#decimal.dividedBy(this.#valueToString(value)),
|
|
148
360
|
this.#decimalPlaces,
|
|
149
361
|
this.#rounding,
|
|
150
362
|
);
|
|
151
363
|
}
|
|
152
364
|
|
|
365
|
+
/**
|
|
366
|
+
* Returns a new instance rounded down to the nearest integer.
|
|
367
|
+
*/
|
|
368
|
+
toFloor() {
|
|
369
|
+
return new PrecisionNumber(this.#decimal.floor(), this.#decimalPlaces, this.#rounding);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Serializes to the fixed-decimal string.
|
|
374
|
+
*/
|
|
153
375
|
toJSON() {
|
|
154
376
|
return this.value;
|
|
155
377
|
}
|
|
156
378
|
|
|
379
|
+
/**
|
|
380
|
+
* Returns a new instance with another value subtracted.
|
|
381
|
+
*/
|
|
157
382
|
toMinus(value: PrecisionNumberValue) {
|
|
158
|
-
return new PrecisionNumber(
|
|
383
|
+
return new PrecisionNumber(
|
|
384
|
+
this.#decimal.minus(this.#valueToString(value)),
|
|
385
|
+
this.#decimalPlaces,
|
|
386
|
+
this.#rounding,
|
|
387
|
+
);
|
|
159
388
|
}
|
|
160
389
|
|
|
390
|
+
/**
|
|
391
|
+
* Returns a new instance with the remainder after division by another value.
|
|
392
|
+
*/
|
|
393
|
+
toModulo(value: PrecisionNumberValue) {
|
|
394
|
+
return new PrecisionNumber(
|
|
395
|
+
this.#decimal.modulo(this.#valueToString(value)),
|
|
396
|
+
this.#decimalPlaces,
|
|
397
|
+
this.#rounding,
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Returns a new instance with the value negated.
|
|
403
|
+
*/
|
|
161
404
|
toNegated() {
|
|
162
405
|
return new PrecisionNumber(this.#decimal.negated(), this.#decimalPlaces, this.#rounding);
|
|
163
406
|
}
|
|
164
407
|
|
|
408
|
+
/**
|
|
409
|
+
* Returns a new instance with another value added.
|
|
410
|
+
*/
|
|
165
411
|
toPlus(value: PrecisionNumberValue) {
|
|
166
|
-
return new PrecisionNumber(this.#decimal.plus(value
|
|
412
|
+
return new PrecisionNumber(this.#decimal.plus(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Returns a new instance raised to an exponent.
|
|
417
|
+
*/
|
|
418
|
+
toPow(value: PrecisionNumberValue) {
|
|
419
|
+
return new PrecisionNumber(this.#decimal.pow(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
|
|
167
420
|
}
|
|
168
421
|
|
|
422
|
+
/**
|
|
423
|
+
* Converts the current value to a JavaScript number.
|
|
424
|
+
*
|
|
425
|
+
* Use `toString`/`value` when preserving decimal precision is more important than native-number ergonomics.
|
|
426
|
+
*/
|
|
427
|
+
toNumber() {
|
|
428
|
+
return this.#decimal.toNumber();
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Converts the current value to its fixed-decimal string.
|
|
433
|
+
*/
|
|
169
434
|
toString() {
|
|
170
435
|
return this.value;
|
|
171
436
|
}
|
|
172
437
|
|
|
438
|
+
/**
|
|
439
|
+
* Formats the current value with custom precision and rounding for this call only.
|
|
440
|
+
*/
|
|
173
441
|
toFixed(decimalPlaces: number = this.#decimalPlaces, rounding: Decimal.Rounding = this.#rounding) {
|
|
174
442
|
return this.#decimal.toFixed(decimalPlaces, rounding);
|
|
175
443
|
}
|
|
176
444
|
|
|
445
|
+
/**
|
|
446
|
+
* Returns a new instance multiplied by another value.
|
|
447
|
+
*/
|
|
177
448
|
toTimes(value: PrecisionNumberValue) {
|
|
178
|
-
return new PrecisionNumber(
|
|
449
|
+
return new PrecisionNumber(
|
|
450
|
+
this.#decimal.times(this.#valueToString(value)),
|
|
451
|
+
this.#decimalPlaces,
|
|
452
|
+
this.#rounding,
|
|
453
|
+
);
|
|
179
454
|
}
|
|
180
455
|
}
|
package/src/crypto-hash.ts
CHANGED
|
@@ -16,7 +16,7 @@ import type {
|
|
|
16
16
|
* ```typescript
|
|
17
17
|
* import { cryptoMd5 } from '@kikiutils/shared/crypto-hash';
|
|
18
18
|
*
|
|
19
|
-
* console.log(cryptoMd5('test')); //
|
|
19
|
+
* console.log(cryptoMd5('test')); // 098f6bcd4621d373cade4e832627b4f6
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
22
|
export function cryptoMd5(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {
|
|
@@ -53,7 +53,7 @@ export function cryptoMd5ToBuffer(data: BinaryLike) {
|
|
|
53
53
|
* ```typescript
|
|
54
54
|
* import { cryptoSha3224 } from '@kikiutils/shared/crypto-hash';
|
|
55
55
|
*
|
|
56
|
-
* console.log(cryptoSha3224('test')); //
|
|
56
|
+
* console.log(cryptoSha3224('test')); // 3797bf0afbbfca4a7bbba7602a2b552746876517a7f9b7ce2db0ae7b
|
|
57
57
|
* ```
|
|
58
58
|
*/
|
|
59
59
|
export function cryptoSha3224(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {
|
|
@@ -71,7 +71,7 @@ export function cryptoSha3224(data: BinaryLike, outputEncoding: BinaryToTextEnco
|
|
|
71
71
|
* ```typescript
|
|
72
72
|
* import { cryptoSha3224ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
73
73
|
*
|
|
74
|
-
* console.log(cryptoSha3224ToBuffer('test')); // <Buffer
|
|
74
|
+
* console.log(cryptoSha3224ToBuffer('test')); // <Buffer 37 97 bf 0a ...>
|
|
75
75
|
* ```
|
|
76
76
|
*/
|
|
77
77
|
export function cryptoSha3224ToBuffer(data: BinaryLike) {
|
|
@@ -127,7 +127,7 @@ export function cryptoSha3256ToBuffer(data: BinaryLike) {
|
|
|
127
127
|
* ```typescript
|
|
128
128
|
* import { cryptoSha3384 } from '@kikiutils/shared/crypto-hash';
|
|
129
129
|
*
|
|
130
|
-
* console.log(cryptoSha3384('test')); //
|
|
130
|
+
* console.log(cryptoSha3384('test')); // e516dabb23b6e30026863543282780a3ae0dccf05551cf0295178d7f...
|
|
131
131
|
* ```
|
|
132
132
|
*/
|
|
133
133
|
export function cryptoSha3384(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {
|
|
@@ -145,7 +145,7 @@ export function cryptoSha3384(data: BinaryLike, outputEncoding: BinaryToTextEnco
|
|
|
145
145
|
* ```typescript
|
|
146
146
|
* import { cryptoSha3384ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
147
147
|
*
|
|
148
|
-
* console.log(cryptoSha3384ToBuffer('test')); // <Buffer
|
|
148
|
+
* console.log(cryptoSha3384ToBuffer('test')); // <Buffer e5 16 da bb ...>
|
|
149
149
|
* ```
|
|
150
150
|
*/
|
|
151
151
|
export function cryptoSha3384ToBuffer(data: BinaryLike) {
|
|
@@ -164,7 +164,7 @@ export function cryptoSha3384ToBuffer(data: BinaryLike) {
|
|
|
164
164
|
* ```typescript
|
|
165
165
|
* import { cryptoSha3512 } from '@kikiutils/shared/crypto-hash';
|
|
166
166
|
*
|
|
167
|
-
* console.log(cryptoSha3512('test')); //
|
|
167
|
+
* console.log(cryptoSha3512('test')); // 9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7...
|
|
168
168
|
* ```
|
|
169
169
|
*/
|
|
170
170
|
export function cryptoSha3512(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {
|
|
@@ -182,7 +182,7 @@ export function cryptoSha3512(data: BinaryLike, outputEncoding: BinaryToTextEnco
|
|
|
182
182
|
* ```typescript
|
|
183
183
|
* import { cryptoSha3512ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
184
184
|
*
|
|
185
|
-
* console.log(cryptoSha3512ToBuffer('test')); // <Buffer
|
|
185
|
+
* console.log(cryptoSha3512ToBuffer('test')); // <Buffer 9e ce 08 6e ...>
|
|
186
186
|
* ```
|
|
187
187
|
*/
|
|
188
188
|
export function cryptoSha3512ToBuffer(data: BinaryLike) {
|
package/src/hash.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
* ```typescript
|
|
21
21
|
* import { sha3224 } from '@kikiutils/shared/hash';
|
|
22
22
|
*
|
|
23
|
-
* console.log(sha3224('test')); //
|
|
23
|
+
* console.log(sha3224('test')); // 3797bf0afbbfca4a7bbba7602a2b552746876517a7f9b7ce2db0ae7b
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
26
|
export function sha3224(data: string | Uint8Array) {
|
|
@@ -56,7 +56,7 @@ export function sha3256(data: string | Uint8Array) {
|
|
|
56
56
|
* ```typescript
|
|
57
57
|
* import { sha3384 } from '@kikiutils/shared/hash';
|
|
58
58
|
*
|
|
59
|
-
* console.log(sha3384('test')); //
|
|
59
|
+
* console.log(sha3384('test')); // e516dabb23b6e30026863543282780a3ae0dccf05551cf0295178d7f...
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
62
|
export function sha3384(data: string | Uint8Array) {
|
|
@@ -74,7 +74,7 @@ export function sha3384(data: string | Uint8Array) {
|
|
|
74
74
|
* ```typescript
|
|
75
75
|
* import { sha3512 } from '@kikiutils/shared/hash';
|
|
76
76
|
*
|
|
77
|
-
* console.log(sha3512('test')); //
|
|
77
|
+
* console.log(sha3512('test')); // 9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7...
|
|
78
78
|
* ```
|
|
79
79
|
*/
|
|
80
80
|
export function sha3512(data: string | Uint8Array) {
|
|
@@ -9,7 +9,7 @@ import type { LRUCache } from 'lru-cache';
|
|
|
9
9
|
*
|
|
10
10
|
* @returns {(keyFn: (...args: P) => string) => Readonly<{
|
|
11
11
|
* getItem, getItemTtl, hasItem, removeItem, resolveKey, setItem
|
|
12
|
-
* }>} A
|
|
12
|
+
* }>} A factory that accepts a key resolver and returns a frozen keyed cache facade
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* ```typescript
|
|
@@ -18,8 +18,8 @@ import type { LRUCache } from 'lru-cache';
|
|
|
18
18
|
*
|
|
19
19
|
* const lru = new LRUCache({ max: 100 });
|
|
20
20
|
* const keyedStore = createLruKeyedStore(lru)((userId: string) => `user:${userId}`);
|
|
21
|
-
*
|
|
22
|
-
* const user =
|
|
21
|
+
* keyedStore.setItem({ name: 'Alice' }, 'user-123');
|
|
22
|
+
* const user = keyedStore.getItem('user-123'); // { name: 'Alice' }
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
|
25
25
|
export function createLruKeyedStore<D = unknown>(lruInstance: LRUCache<any, any, any>) {
|
|
@@ -11,7 +11,7 @@ import type { RedisLikeStorage } from './types';
|
|
|
11
11
|
*
|
|
12
12
|
* @returns {(keyFn: (...args: P) => string) => Readonly<{
|
|
13
13
|
* getItem, getItemTtl, hasItem, removeItem, resolveKey, setItem, setItemWithTtl
|
|
14
|
-
* }>} A
|
|
14
|
+
* }>} A factory that accepts a key resolver and returns a frozen Redis keyed-store facade
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```typescript
|
package/src/string.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Character-set presets supported by `randomString`.
|
|
3
|
+
*/
|
|
1
4
|
export type RandomStringMode =
|
|
2
5
|
| 'alphabetic'
|
|
3
6
|
| 'alphanumeric'
|
|
@@ -21,10 +24,13 @@ const CHARSETS: Record<RandomStringMode, string> = {
|
|
|
21
24
|
};
|
|
22
25
|
|
|
23
26
|
/**
|
|
24
|
-
* Generates a random string of a given length using a
|
|
27
|
+
* Generates a random string of a given length using a predefined character set.
|
|
28
|
+
*
|
|
29
|
+
* Uses `Math.random`, so this helper is suitable for display tokens, placeholders,
|
|
30
|
+
* and test data, but not for passwords, API keys, or other cryptographic secrets.
|
|
25
31
|
*
|
|
26
|
-
* @param {number} length - The length of the string to generate
|
|
27
|
-
* @param {RandomStringMode} [mode] - The character set to use
|
|
32
|
+
* @param {number} length - The positive integer length of the string to generate
|
|
33
|
+
* @param {RandomStringMode} [mode] - The character set to use (default: `'alphabetic'`)
|
|
28
34
|
*
|
|
29
35
|
* @returns {string} The generated random string
|
|
30
36
|
*
|
|
@@ -34,9 +40,9 @@ const CHARSETS: Record<RandomStringMode, string> = {
|
|
|
34
40
|
* ```typescript
|
|
35
41
|
* import { randomString } from '@kikiutils/shared/string';
|
|
36
42
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
43
|
+
* randomString(8); // e.g. 'aZbXwTyQ'
|
|
44
|
+
* randomString(6, 'numeric'); // e.g. '402398'
|
|
45
|
+
* randomString(10, 'alphanumeric'); // e.g. 'a9Z4pQ8xY2'
|
|
40
46
|
* ```
|
|
41
47
|
*/
|
|
42
48
|
export function randomString(length: number, mode: RandomStringMode = 'alphabetic') {
|
package/src/types/index.ts
CHANGED
|
@@ -6,13 +6,52 @@ import type {
|
|
|
6
6
|
|
|
7
7
|
export type { FilteredKeyPath } from './filtered-key-path';
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* A generic string-keyed object for utility APIs that intentionally accept any value shape.
|
|
11
|
+
*/
|
|
9
12
|
export type AnyRecord = Record<string, any>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Binary-like inputs accepted by `toBuffer` across browser and Node.js runtimes.
|
|
16
|
+
*/
|
|
10
17
|
export type BinaryInput = ArrayBuffer | Blob | Buffer | File | NodeBlob | NodeFile | Uint8Array;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Boolean values represented either as booleans or common string literals.
|
|
21
|
+
*/
|
|
11
22
|
export type Booleanish = 'false' | 'true' | boolean;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Accepts either a full object or a partial patch of that object.
|
|
26
|
+
*/
|
|
12
27
|
export type MaybePartial<T> = Partial<T> | T;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Accepts either a mutable value or a readonly view of that value.
|
|
31
|
+
*/
|
|
13
32
|
export type MaybeReadonly<T> = Readonly<T> | T;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Explicitly nullable value helper.
|
|
36
|
+
*/
|
|
14
37
|
export type Nullable<T> = null | T;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Number-like value represented as a number or a numeric string.
|
|
41
|
+
*/
|
|
15
42
|
export type Numberish = number | string;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A `Record` where every key is optional.
|
|
46
|
+
*/
|
|
16
47
|
export type PartialRecord<K extends keyof any, T> = Partial<Record<K, T>>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A readonly `PartialRecord`.
|
|
51
|
+
*/
|
|
17
52
|
export type ReadonlyPartialRecord<K extends keyof any, T> = Readonly<PartialRecord<K, T>>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A readonly `Record`.
|
|
56
|
+
*/
|
|
18
57
|
export type ReadonlyRecord<K extends keyof any, T> = Readonly<Record<K, T>>;
|