@kikiutils/shared 15.0.3 → 15.1.0
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.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.js.map +1 -1
- package/dist/consola.js.map +1 -1
- package/dist/crypto-hash.d.ts +7 -7
- package/dist/crypto-hash.js +7 -7
- package/dist/crypto-hash.js.map +1 -1
- package/dist/datetime.js.map +1 -1
- package/dist/element-plus.js.map +1 -1
- package/dist/enum.js.map +1 -1
- package/dist/env.js.map +1 -1
- package/dist/event-awaiter.js.map +1 -1
- package/dist/general.js.map +1 -1
- package/dist/hash.d.ts +3 -3
- package/dist/hash.js +3 -3
- package/dist/hash.js.map +1 -1
- package/dist/math.js.map +1 -1
- package/dist/number.js.map +1 -1
- package/dist/object.js.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.js.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.js.map +1 -1
- package/dist/types/index.d.ts +30 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/url.js.map +1 -1
- package/dist/vue.js.map +1 -1
- package/dist/web.js.map +1 -1
- package/package.json +8 -17
- 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
|
@@ -1,33 +1,51 @@
|
|
|
1
1
|
import { Decimal } from "decimal.js";
|
|
2
2
|
//#region src/classes/precision-number.ts
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Chainable fixed-decimal wrapper around `decimal.js`.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* `PrecisionNumber` stores values as `Decimal`, rounds every mutating operation to
|
|
7
|
+
* the configured decimal places, and exposes both mutable and immutable arithmetic
|
|
8
|
+
* methods. Mutable methods (`plus`, `minus`, `times`, `dividedBy`, `modulo`,
|
|
9
|
+
* `pow`, `clamp`, `ceil`, `floor`, `negate`, `absoluteValue`) update the current
|
|
10
|
+
* instance and return `this`; immutable methods prefixed with `to` return a new
|
|
11
|
+
* `PrecisionNumber` with the same precision settings.
|
|
10
12
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { PrecisionNumber } from '@kikiutils/shared/classes/precision-number';
|
|
13
16
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
17
|
+
* const value = new PrecisionNumber('1.239', 2).plus('2.111');
|
|
18
|
+
* value.toString(); // '3.35'
|
|
19
|
+
* value.toMinus('1').toString(); // '2.35' (original value remains '3.35')
|
|
20
|
+
* ```
|
|
18
21
|
*/
|
|
19
22
|
var PrecisionNumber = class PrecisionNumber {
|
|
20
23
|
#decimalPlaces;
|
|
21
24
|
#rounding;
|
|
22
25
|
#decimal;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a fixed-decimal number.
|
|
28
|
+
*
|
|
29
|
+
* @param {PrecisionNumberValue} [value] - Initial value (default: `'0'`)
|
|
30
|
+
* @param {number} [decimalPlaces] - Decimal places retained after operations (default: `2`)
|
|
31
|
+
* @param {Decimal.Rounding} [rounding] - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`)
|
|
32
|
+
*/
|
|
23
33
|
constructor(value = "0", decimalPlaces = 2, rounding = Decimal.ROUND_DOWN) {
|
|
24
34
|
this.#decimalPlaces = decimalPlaces;
|
|
25
35
|
this.#rounding = rounding;
|
|
26
36
|
this.#decimal = this.#decimalToFixedDecimal(new Decimal(value.toString().trim()));
|
|
27
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns the formatted value when inspected by Node.js utilities.
|
|
40
|
+
*/
|
|
28
41
|
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
29
42
|
return this.value;
|
|
30
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Converts to a number for numeric coercion and to the fixed string otherwise.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} hint - JavaScript primitive-conversion hint
|
|
48
|
+
*/
|
|
31
49
|
[Symbol.toPrimitive](hint) {
|
|
32
50
|
if (hint === "number") return this.#decimal.toNumber();
|
|
33
51
|
return this.value;
|
|
@@ -35,95 +53,320 @@ var PrecisionNumber = class PrecisionNumber {
|
|
|
35
53
|
#decimalToFixedDecimal(decimal) {
|
|
36
54
|
return decimal.toDecimalPlaces(this.#decimalPlaces, this.#rounding);
|
|
37
55
|
}
|
|
56
|
+
#valueToString(value) {
|
|
57
|
+
return value.toString().trim();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Decimal places retained by mutating operations and default formatting.
|
|
61
|
+
*/
|
|
62
|
+
get decimalPlaces() {
|
|
63
|
+
return this.#decimalPlaces;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Decimal.js rounding mode used by mutating operations and default formatting.
|
|
67
|
+
*/
|
|
68
|
+
get rounding() {
|
|
69
|
+
return this.#rounding;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Fixed-decimal string using the instance precision and rounding mode.
|
|
73
|
+
*/
|
|
38
74
|
get value() {
|
|
39
75
|
return this.#decimal.toFixed(this.#decimalPlaces, this.#rounding);
|
|
40
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Formats a value with decimal.js without creating a reusable instance.
|
|
79
|
+
*
|
|
80
|
+
* @param {PrecisionNumberValue} value - Value to format
|
|
81
|
+
* @param {number} [decimalPlaces] - Decimal places to output (default: `2`)
|
|
82
|
+
* @param {Decimal.Rounding} [rounding] - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`)
|
|
83
|
+
*
|
|
84
|
+
* @returns {string} Fixed-decimal string
|
|
85
|
+
*/
|
|
41
86
|
static toFixed(value, decimalPlaces = 2, rounding = Decimal.ROUND_DOWN) {
|
|
42
87
|
return new Decimal(value.toString().trim()).toFixed(decimalPlaces, rounding);
|
|
43
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Replaces the current value with its absolute value.
|
|
91
|
+
*
|
|
92
|
+
* @returns {this} The current instance for chaining
|
|
93
|
+
*/
|
|
44
94
|
absoluteValue() {
|
|
45
95
|
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.absoluteValue());
|
|
46
96
|
return this;
|
|
47
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Rounds the current value up to the nearest integer in place.
|
|
100
|
+
*
|
|
101
|
+
* The stored value still uses this instance's configured decimal places for output.
|
|
102
|
+
*
|
|
103
|
+
* @returns {this} The current instance for chaining
|
|
104
|
+
*/
|
|
105
|
+
ceil() {
|
|
106
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.ceil());
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Restricts the current value to the inclusive range `[min, max]` in place.
|
|
111
|
+
*
|
|
112
|
+
* @param {PrecisionNumberValue} min - Lower bound
|
|
113
|
+
* @param {PrecisionNumberValue} max - Upper bound
|
|
114
|
+
*
|
|
115
|
+
* @returns {this} The current instance for chaining
|
|
116
|
+
*
|
|
117
|
+
* @throws {Error} If `min` is greater than `max`
|
|
118
|
+
*/
|
|
119
|
+
clamp(min, max) {
|
|
120
|
+
const minDecimal = new Decimal(this.#valueToString(min));
|
|
121
|
+
const maxDecimal = new Decimal(this.#valueToString(max));
|
|
122
|
+
if (minDecimal.gt(maxDecimal)) throw new Error("Invalid clamp range: min cannot be greater than max");
|
|
123
|
+
this.#decimal = this.#decimalToFixedDecimal(Decimal.min(Decimal.max(this.#decimal, minDecimal), maxDecimal));
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returns a new instance with the same value, decimal places, and rounding mode.
|
|
128
|
+
*/
|
|
129
|
+
clone() {
|
|
130
|
+
return new PrecisionNumber(this.#decimal, this.#decimalPlaces, this.#rounding);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Divides the current value by another value in place.
|
|
134
|
+
*
|
|
135
|
+
* @param {PrecisionNumberValue} value - Divisor
|
|
136
|
+
*
|
|
137
|
+
* @returns {this} The current instance for chaining
|
|
138
|
+
*/
|
|
48
139
|
dividedBy(value) {
|
|
49
|
-
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(value
|
|
140
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(this.#valueToString(value)));
|
|
50
141
|
return this;
|
|
51
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Checks whether the current value equals another value.
|
|
145
|
+
*
|
|
146
|
+
* @param {PrecisionNumberValue} value - Value to compare
|
|
147
|
+
*
|
|
148
|
+
* @returns {boolean} `true` when both numeric values are equal
|
|
149
|
+
*/
|
|
52
150
|
equals(value) {
|
|
53
|
-
return this.#decimal.equals(value
|
|
151
|
+
return this.#decimal.equals(this.#valueToString(value));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Rounds the current value down to the nearest integer in place.
|
|
155
|
+
*
|
|
156
|
+
* The stored value still uses this instance's configured decimal places for output.
|
|
157
|
+
*
|
|
158
|
+
* @returns {this} The current instance for chaining
|
|
159
|
+
*/
|
|
160
|
+
floor() {
|
|
161
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.floor());
|
|
162
|
+
return this;
|
|
54
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Checks whether the current value is greater than another value.
|
|
166
|
+
*/
|
|
55
167
|
gt(value) {
|
|
56
|
-
return this.#decimal.gt(value
|
|
168
|
+
return this.#decimal.gt(this.#valueToString(value));
|
|
57
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Checks whether the current value is greater than or equal to another value.
|
|
172
|
+
*/
|
|
58
173
|
gte(value) {
|
|
59
|
-
return this.#decimal.gte(value
|
|
174
|
+
return this.#decimal.gte(this.#valueToString(value));
|
|
60
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Checks whether the current value is finite.
|
|
178
|
+
*/
|
|
61
179
|
isFinite() {
|
|
62
180
|
return this.#decimal.isFinite();
|
|
63
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Checks whether the current value is an integer.
|
|
184
|
+
*/
|
|
64
185
|
isInteger() {
|
|
65
186
|
return this.#decimal.isInteger();
|
|
66
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Checks whether the current value is `NaN`.
|
|
190
|
+
*/
|
|
67
191
|
isNaN() {
|
|
68
192
|
return this.#decimal.isNaN();
|
|
69
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Checks whether the current value is negative.
|
|
196
|
+
*/
|
|
70
197
|
isNegative() {
|
|
71
198
|
return this.#decimal.isNegative();
|
|
72
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Checks whether the current value is positive.
|
|
202
|
+
*/
|
|
73
203
|
isPositive() {
|
|
74
204
|
return this.#decimal.isPositive();
|
|
75
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Checks whether the current value is zero.
|
|
208
|
+
*/
|
|
76
209
|
isZero() {
|
|
77
210
|
return this.#decimal.isZero();
|
|
78
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* Checks whether the current value is less than another value.
|
|
214
|
+
*/
|
|
79
215
|
lt(value) {
|
|
80
|
-
return this.#decimal.lt(value
|
|
216
|
+
return this.#decimal.lt(this.#valueToString(value));
|
|
81
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Checks whether the current value is less than or equal to another value.
|
|
220
|
+
*/
|
|
82
221
|
lte(value) {
|
|
83
|
-
return this.#decimal.lte(value
|
|
222
|
+
return this.#decimal.lte(this.#valueToString(value));
|
|
84
223
|
}
|
|
224
|
+
/**
|
|
225
|
+
* Subtracts another value from the current value in place.
|
|
226
|
+
*/
|
|
85
227
|
minus(value) {
|
|
86
|
-
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.minus(value
|
|
228
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.minus(this.#valueToString(value)));
|
|
87
229
|
return this;
|
|
88
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Replaces the current value with the remainder after division by another value.
|
|
233
|
+
*
|
|
234
|
+
* @param {PrecisionNumberValue} value - Divisor used to compute the remainder
|
|
235
|
+
*
|
|
236
|
+
* @returns {this} The current instance for chaining
|
|
237
|
+
*/
|
|
238
|
+
modulo(value) {
|
|
239
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.modulo(this.#valueToString(value)));
|
|
240
|
+
return this;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Negates the current value in place.
|
|
244
|
+
*/
|
|
89
245
|
negate() {
|
|
90
246
|
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.negated());
|
|
91
247
|
return this;
|
|
92
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Adds another value to the current value in place.
|
|
251
|
+
*/
|
|
93
252
|
plus(value) {
|
|
94
|
-
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.plus(value
|
|
253
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.plus(this.#valueToString(value)));
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Raises the current value to an exponent in place.
|
|
258
|
+
*
|
|
259
|
+
* @param {PrecisionNumberValue} value - Exponent
|
|
260
|
+
*
|
|
261
|
+
* @returns {this} The current instance for chaining
|
|
262
|
+
*/
|
|
263
|
+
pow(value) {
|
|
264
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.pow(this.#valueToString(value)));
|
|
95
265
|
return this;
|
|
96
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Multiplies the current value by another value in place.
|
|
269
|
+
*/
|
|
97
270
|
times(value) {
|
|
98
|
-
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.times(value
|
|
271
|
+
this.#decimal = this.#decimalToFixedDecimal(this.#decimal.times(this.#valueToString(value)));
|
|
99
272
|
return this;
|
|
100
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Returns a new instance with the absolute value.
|
|
276
|
+
*/
|
|
101
277
|
toAbsoluteValue() {
|
|
102
278
|
return new PrecisionNumber(this.#decimal.absoluteValue(), this.#decimalPlaces, this.#rounding);
|
|
103
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Returns a new instance rounded up to the nearest integer.
|
|
282
|
+
*/
|
|
283
|
+
toCeil() {
|
|
284
|
+
return new PrecisionNumber(this.#decimal.ceil(), this.#decimalPlaces, this.#rounding);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Returns a new instance restricted to the inclusive range `[min, max]`.
|
|
288
|
+
*
|
|
289
|
+
* @param {PrecisionNumberValue} min - Lower bound
|
|
290
|
+
* @param {PrecisionNumberValue} max - Upper bound
|
|
291
|
+
*
|
|
292
|
+
* @throws {Error} If `min` is greater than `max`
|
|
293
|
+
*/
|
|
294
|
+
toClamped(min, max) {
|
|
295
|
+
return this.clone().clamp(min, max);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Returns a new instance divided by another value.
|
|
299
|
+
*/
|
|
104
300
|
toDividedBy(value) {
|
|
105
|
-
return new PrecisionNumber(this.#decimal.dividedBy(value
|
|
301
|
+
return new PrecisionNumber(this.#decimal.dividedBy(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
|
|
106
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Returns a new instance rounded down to the nearest integer.
|
|
305
|
+
*/
|
|
306
|
+
toFloor() {
|
|
307
|
+
return new PrecisionNumber(this.#decimal.floor(), this.#decimalPlaces, this.#rounding);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Serializes to the fixed-decimal string.
|
|
311
|
+
*/
|
|
107
312
|
toJSON() {
|
|
108
313
|
return this.value;
|
|
109
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Returns a new instance with another value subtracted.
|
|
317
|
+
*/
|
|
110
318
|
toMinus(value) {
|
|
111
|
-
return new PrecisionNumber(this.#decimal.minus(value
|
|
319
|
+
return new PrecisionNumber(this.#decimal.minus(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Returns a new instance with the remainder after division by another value.
|
|
323
|
+
*/
|
|
324
|
+
toModulo(value) {
|
|
325
|
+
return new PrecisionNumber(this.#decimal.modulo(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
|
|
112
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* Returns a new instance with the value negated.
|
|
329
|
+
*/
|
|
113
330
|
toNegated() {
|
|
114
331
|
return new PrecisionNumber(this.#decimal.negated(), this.#decimalPlaces, this.#rounding);
|
|
115
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Returns a new instance with another value added.
|
|
335
|
+
*/
|
|
116
336
|
toPlus(value) {
|
|
117
|
-
return new PrecisionNumber(this.#decimal.plus(value
|
|
118
|
-
}
|
|
337
|
+
return new PrecisionNumber(this.#decimal.plus(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Returns a new instance raised to an exponent.
|
|
341
|
+
*/
|
|
342
|
+
toPow(value) {
|
|
343
|
+
return new PrecisionNumber(this.#decimal.pow(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Converts the current value to a JavaScript number.
|
|
347
|
+
*
|
|
348
|
+
* Use `toString`/`value` when preserving decimal precision is more important than native-number ergonomics.
|
|
349
|
+
*/
|
|
350
|
+
toNumber() {
|
|
351
|
+
return this.#decimal.toNumber();
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Converts the current value to its fixed-decimal string.
|
|
355
|
+
*/
|
|
119
356
|
toString() {
|
|
120
357
|
return this.value;
|
|
121
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Formats the current value with custom precision and rounding for this call only.
|
|
361
|
+
*/
|
|
122
362
|
toFixed(decimalPlaces = this.#decimalPlaces, rounding = this.#rounding) {
|
|
123
363
|
return this.#decimal.toFixed(decimalPlaces, rounding);
|
|
124
364
|
}
|
|
365
|
+
/**
|
|
366
|
+
* Returns a new instance multiplied by another value.
|
|
367
|
+
*/
|
|
125
368
|
toTimes(value) {
|
|
126
|
-
return new PrecisionNumber(this.#decimal.times(value
|
|
369
|
+
return new PrecisionNumber(this.#decimal.times(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
|
|
127
370
|
}
|
|
128
371
|
};
|
|
129
372
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"precision-number.js","names":["#decimalPlaces","#rounding","#decimal","#decimalToFixedDecimal"],"sources":["../../src/classes/precision-number.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\n\nexport type PrecisionNumberValue = Decimal.Value | PrecisionNumber | { toString: () => string };\n\n/**\n * Class representing a precision number with configurable decimal places and rounding.\n *\n * This class leverages the Decimal.js library to ensure accurate arithmetic operations\n * with floating point numbers. It provides methods for various arithmetic operations\n * (addition, subtraction, multiplication, division) as well as comparison and utility\n * methods to check the state of the number (e.g., if it is finite, an integer, zero, etc.).\n *\n * The class also includes custom symbol methods to support Node.js inspection and primitive\n * type conversion.\n *\n * The class supports in-place modification methods that alter the current instance's value,\n * such as `plus`, `minus`, `times`, `dividedBy`, and others. Additionally, methods prefixed\n * with `to` (e.g., `toPlus`, `toMinus`) return a new instance of `PrecisionNumber` with the\n * modified value, leaving the original instance unchanged.\n */\nexport class PrecisionNumber {\n // Private properties\n readonly #decimalPlaces: number;\n readonly #rounding: Decimal.Rounding;\n #decimal: Decimal;\n\n constructor(\n value: PrecisionNumberValue = '0',\n decimalPlaces: number = 2,\n rounding: Decimal.Rounding = Decimal.ROUND_DOWN,\n ) {\n this.#decimalPlaces = decimalPlaces;\n this.#rounding = rounding;\n this.#decimal = this.#decimalToFixedDecimal(new Decimal(value.toString().trim()));\n }\n\n // Symbols\n [Symbol.for('nodejs.util.inspect.custom')]() {\n return this.value;\n }\n\n [Symbol.toPrimitive](hint: string) {\n if (hint === 'number') return this.#decimal.toNumber();\n return this.value;\n }\n\n // Private methods\n #decimalToFixedDecimal(decimal: Decimal) {\n return decimal.toDecimalPlaces(this.#decimalPlaces, this.#rounding);\n }\n\n // Public getters\n get value() {\n return this.#decimal.toFixed(this.#decimalPlaces, this.#rounding);\n }\n\n // Static methods\n static toFixed(\n value: PrecisionNumberValue,\n decimalPlaces: number = 2,\n rounding: Decimal.Rounding = Decimal.ROUND_DOWN,\n ) {\n return new Decimal(value.toString().trim()).toFixed(decimalPlaces, rounding);\n }\n\n // Public methods\n absoluteValue() {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.absoluteValue());\n return this;\n }\n\n dividedBy(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(value.toString().trim()));\n return this;\n }\n\n equals(value: PrecisionNumberValue) {\n return this.#decimal.equals(value.toString().trim());\n }\n\n gt(value: PrecisionNumberValue) {\n return this.#decimal.gt(value.toString().trim());\n }\n\n gte(value: PrecisionNumberValue) {\n return this.#decimal.gte(value.toString().trim());\n }\n\n isFinite() {\n return this.#decimal.isFinite();\n }\n\n isInteger() {\n return this.#decimal.isInteger();\n }\n\n isNaN() {\n return this.#decimal.isNaN();\n }\n\n isNegative() {\n return this.#decimal.isNegative();\n }\n\n isPositive() {\n return this.#decimal.isPositive();\n }\n\n isZero() {\n return this.#decimal.isZero();\n }\n\n lt(value: PrecisionNumberValue) {\n return this.#decimal.lt(value.toString().trim());\n }\n\n lte(value: PrecisionNumberValue) {\n return this.#decimal.lte(value.toString().trim());\n }\n\n minus(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.minus(value.toString().trim()));\n return this;\n }\n\n negate() {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.negated());\n return this;\n }\n\n plus(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.plus(value.toString().trim()));\n return this;\n }\n\n times(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.times(value.toString().trim()));\n return this;\n }\n\n toAbsoluteValue() {\n return new PrecisionNumber(this.#decimal.absoluteValue(), this.#decimalPlaces, this.#rounding);\n }\n\n toDividedBy(value: PrecisionNumberValue) {\n return new PrecisionNumber(\n this.#decimal.dividedBy(value.toString().trim()),\n this.#decimalPlaces,\n this.#rounding,\n );\n }\n\n toJSON() {\n return this.value;\n }\n\n toMinus(value: PrecisionNumberValue) {\n return new PrecisionNumber(this.#decimal.minus(value.toString().trim()), this.#decimalPlaces, this.#rounding);\n }\n\n toNegated() {\n return new PrecisionNumber(this.#decimal.negated(), this.#decimalPlaces, this.#rounding);\n }\n\n toPlus(value: PrecisionNumberValue) {\n return new PrecisionNumber(this.#decimal.plus(value.toString().trim()), this.#decimalPlaces, this.#rounding);\n }\n\n toString() {\n return this.value;\n }\n\n toFixed(decimalPlaces: number = this.#decimalPlaces, rounding: Decimal.Rounding = this.#rounding) {\n return this.#decimal.toFixed(decimalPlaces, rounding);\n }\n\n toTimes(value: PrecisionNumberValue) {\n return new PrecisionNumber(this.#decimal.times(value.toString().trim()), this.#decimalPlaces, this.#rounding);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoBA,IAAa,kBAAb,MAAa,gBAAgB;CAEzB;CACA;CACA;CAEA,YACI,QAA8B,KAC9B,gBAAwB,GACxB,WAA6B,QAAQ,YACvC;AACE,QAAA,gBAAsB;AACtB,QAAA,WAAiB;AACjB,QAAA,UAAgB,MAAA,sBAA4B,IAAI,QAAQ,MAAM,UAAU,CAAC,MAAM,CAAC,CAAC;;CAIrF,CAAC,OAAO,IAAI,6BAA6B,IAAI;AACzC,SAAO,KAAK;;CAGhB,CAAC,OAAO,aAAa,MAAc;AAC/B,MAAI,SAAS,SAAU,QAAO,MAAA,QAAc,UAAU;AACtD,SAAO,KAAK;;CAIhB,uBAAuB,SAAkB;AACrC,SAAO,QAAQ,gBAAgB,MAAA,eAAqB,MAAA,SAAe;;CAIvE,IAAI,QAAQ;AACR,SAAO,MAAA,QAAc,QAAQ,MAAA,eAAqB,MAAA,SAAe;;CAIrE,OAAO,QACH,OACA,gBAAwB,GACxB,WAA6B,QAAQ,YACvC;AACE,SAAO,IAAI,QAAQ,MAAM,UAAU,CAAC,MAAM,CAAC,CAAC,QAAQ,eAAe,SAAS;;CAIhF,gBAAgB;AACZ,QAAA,UAAgB,MAAA,sBAA4B,MAAA,QAAc,eAAe,CAAC;AAC1E,SAAO;;CAGX,UAAU,OAA6B;AACnC,QAAA,UAAgB,MAAA,sBAA4B,MAAA,QAAc,UAAU,MAAM,UAAU,CAAC,MAAM,CAAC,CAAC;AAC7F,SAAO;;CAGX,OAAO,OAA6B;AAChC,SAAO,MAAA,QAAc,OAAO,MAAM,UAAU,CAAC,MAAM,CAAC;;CAGxD,GAAG,OAA6B;AAC5B,SAAO,MAAA,QAAc,GAA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K;;CAGhB,QAAQ,OAA6B;AACjC,SAAO,IAAI,gBAAgB,MAAA,QAAc,MAAM,MAAM,UAAU,CAAC,MAAM,CAAC,EAAE,MAAA,eAAqB,MAAA,SAAe;;CAGjH,YAAY;AACR,SAAO,IAAI,gBAAgB,MAAA,QAAc,SAAS,EAAE,MAAA,eAAqB,MAAA,SAAe;;CAG5F,OAAO,OAA6B;AAChC,SAAO,IAAI,gBAAgB,MAAA,QAAc,KAAK,MAAM,UAAU,CAAC,MAAM,CAAC,EAAE,MAAA,eAAqB,MAAA,SAAe;;CAGhH,WAAW;AACP,SAAO,KAAK;;CAGhB,QAAQ,gBAAwB,MAAA,eAAqB,WAA6B,MAAA,UAAgB;AAC9F,SAAO,MAAA,QAAc,QAAQ,eAAe,SAAS;;CAGzD,QAAQ,OAA6B;AACjC,SAAO,IAAI,gBAAgB,MAAA,QAAc,MAAM,MAAM,UAAU,CAAC,MAAM,CAAC,EAAE,MAAA,eAAqB,MAAA,SAAe"}
|
|
1
|
+
{"version":3,"file":"precision-number.js","names":["#decimalPlaces","#rounding","#decimal","#decimalToFixedDecimal","#valueToString"],"sources":["../../src/classes/precision-number.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\n\n/**\n * Values accepted by `PrecisionNumber` constructors and arithmetic methods.\n */\nexport type PrecisionNumberValue = Decimal.Value | PrecisionNumber | { toString: () => string };\n\n/**\n * Chainable fixed-decimal wrapper around `decimal.js`.\n *\n * `PrecisionNumber` stores values as `Decimal`, rounds every mutating operation to\n * the configured decimal places, and exposes both mutable and immutable arithmetic\n * methods. Mutable methods (`plus`, `minus`, `times`, `dividedBy`, `modulo`,\n * `pow`, `clamp`, `ceil`, `floor`, `negate`, `absoluteValue`) update the current\n * instance and return `this`; immutable methods prefixed with `to` return a new\n * `PrecisionNumber` with the same precision settings.\n *\n * @example\n * ```typescript\n * import { PrecisionNumber } from '@kikiutils/shared/classes/precision-number';\n *\n * const value = new PrecisionNumber('1.239', 2).plus('2.111');\n * value.toString(); // '3.35'\n * value.toMinus('1').toString(); // '2.35' (original value remains '3.35')\n * ```\n */\nexport class PrecisionNumber {\n // Private properties\n readonly #decimalPlaces: number;\n readonly #rounding: Decimal.Rounding;\n #decimal: Decimal;\n\n /**\n * Creates a fixed-decimal number.\n *\n * @param {PrecisionNumberValue} [value] - Initial value (default: `'0'`)\n * @param {number} [decimalPlaces] - Decimal places retained after operations (default: `2`)\n * @param {Decimal.Rounding} [rounding] - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`)\n */\n constructor(\n value: PrecisionNumberValue = '0',\n decimalPlaces: number = 2,\n rounding: Decimal.Rounding = Decimal.ROUND_DOWN,\n ) {\n this.#decimalPlaces = decimalPlaces;\n this.#rounding = rounding;\n this.#decimal = this.#decimalToFixedDecimal(new Decimal(value.toString().trim()));\n }\n\n // Symbols\n\n /**\n * Returns the formatted value when inspected by Node.js utilities.\n */\n [Symbol.for('nodejs.util.inspect.custom')]() {\n return this.value;\n }\n\n /**\n * Converts to a number for numeric coercion and to the fixed string otherwise.\n *\n * @param {string} hint - JavaScript primitive-conversion hint\n */\n [Symbol.toPrimitive](hint: string) {\n if (hint === 'number') return this.#decimal.toNumber();\n return this.value;\n }\n\n // Private methods\n #decimalToFixedDecimal(decimal: Decimal) {\n return decimal.toDecimalPlaces(this.#decimalPlaces, this.#rounding);\n }\n\n #valueToString(value: PrecisionNumberValue) {\n return value.toString().trim();\n }\n\n // Public getters\n\n /**\n * Decimal places retained by mutating operations and default formatting.\n */\n get decimalPlaces() {\n return this.#decimalPlaces;\n }\n\n /**\n * Decimal.js rounding mode used by mutating operations and default formatting.\n */\n get rounding() {\n return this.#rounding;\n }\n\n /**\n * Fixed-decimal string using the instance precision and rounding mode.\n */\n get value() {\n return this.#decimal.toFixed(this.#decimalPlaces, this.#rounding);\n }\n\n /**\n * Formats a value with decimal.js without creating a reusable instance.\n *\n * @param {PrecisionNumberValue} value - Value to format\n * @param {number} [decimalPlaces] - Decimal places to output (default: `2`)\n * @param {Decimal.Rounding} [rounding] - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`)\n *\n * @returns {string} Fixed-decimal string\n */\n static toFixed(\n value: PrecisionNumberValue,\n decimalPlaces: number = 2,\n rounding: Decimal.Rounding = Decimal.ROUND_DOWN,\n ) {\n return new Decimal(value.toString().trim()).toFixed(decimalPlaces, rounding);\n }\n\n // Public methods\n\n /**\n * Replaces the current value with its absolute value.\n *\n * @returns {this} The current instance for chaining\n */\n absoluteValue() {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.absoluteValue());\n return this;\n }\n\n /**\n * Rounds the current value up to the nearest integer in place.\n *\n * The stored value still uses this instance's configured decimal places for output.\n *\n * @returns {this} The current instance for chaining\n */\n ceil() {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.ceil());\n return this;\n }\n\n /**\n * Restricts the current value to the inclusive range `[min, max]` in place.\n *\n * @param {PrecisionNumberValue} min - Lower bound\n * @param {PrecisionNumberValue} max - Upper bound\n *\n * @returns {this} The current instance for chaining\n *\n * @throws {Error} If `min` is greater than `max`\n */\n clamp(min: PrecisionNumberValue, max: PrecisionNumberValue) {\n const minDecimal = new Decimal(this.#valueToString(min));\n const maxDecimal = new Decimal(this.#valueToString(max));\n if (minDecimal.gt(maxDecimal)) throw new Error('Invalid clamp range: min cannot be greater than max');\n this.#decimal = this.#decimalToFixedDecimal(Decimal.min(Decimal.max(this.#decimal, minDecimal), maxDecimal));\n return this;\n }\n\n /**\n * Returns a new instance with the same value, decimal places, and rounding mode.\n */\n clone() {\n return new PrecisionNumber(this.#decimal, this.#decimalPlaces, this.#rounding);\n }\n\n /**\n * Divides the current value by another value in place.\n *\n * @param {PrecisionNumberValue} value - Divisor\n *\n * @returns {this} The current instance for chaining\n */\n dividedBy(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(this.#valueToString(value)));\n return this;\n }\n\n /**\n * Checks whether the current value equals another value.\n *\n * @param {PrecisionNumberValue} value - Value to compare\n *\n * @returns {boolean} `true` when both numeric values are equal\n */\n equals(value: PrecisionNumberValue) {\n return this.#decimal.equals(this.#valueToString(value));\n }\n\n /**\n * Rounds the current value down to the nearest integer in place.\n *\n * The stored value still uses this instance's configured decimal places for output.\n *\n * @returns {this} The current instance for chaining\n */\n floor() {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.floor());\n return this;\n }\n\n /**\n * Checks whether the current value is greater than another value.\n */\n gt(value: PrecisionNumberValue) {\n return this.#decimal.gt(this.#valueToString(value));\n }\n\n /**\n * Checks whether the current value is greater than or equal to another value.\n */\n gte(value: PrecisionNumberValue) {\n return this.#decimal.gte(this.#valueToString(value));\n }\n\n /**\n * Checks whether the current value is finite.\n */\n isFinite() {\n return this.#decimal.isFinite();\n }\n\n /**\n * Checks whether the current value is an integer.\n */\n isInteger() {\n return this.#decimal.isInteger();\n }\n\n /**\n * Checks whether the current value is `NaN`.\n */\n isNaN() {\n return this.#decimal.isNaN();\n }\n\n /**\n * Checks whether the current value is negative.\n */\n isNegative() {\n return this.#decimal.isNegative();\n }\n\n /**\n * Checks whether the current value is positive.\n */\n isPositive() {\n return this.#decimal.isPositive();\n }\n\n /**\n * Checks whether the current value is zero.\n */\n isZero() {\n return this.#decimal.isZero();\n }\n\n /**\n * Checks whether the current value is less than another value.\n */\n lt(value: PrecisionNumberValue) {\n return this.#decimal.lt(this.#valueToString(value));\n }\n\n /**\n * Checks whether the current value is less than or equal to another value.\n */\n lte(value: PrecisionNumberValue) {\n return this.#decimal.lte(this.#valueToString(value));\n }\n\n /**\n * Subtracts another value from the current value in place.\n */\n minus(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.minus(this.#valueToString(value)));\n return this;\n }\n\n /**\n * Replaces the current value with the remainder after division by another value.\n *\n * @param {PrecisionNumberValue} value - Divisor used to compute the remainder\n *\n * @returns {this} The current instance for chaining\n */\n modulo(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.modulo(this.#valueToString(value)));\n return this;\n }\n\n /**\n * Negates the current value in place.\n */\n negate() {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.negated());\n return this;\n }\n\n /**\n * Adds another value to the current value in place.\n */\n plus(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.plus(this.#valueToString(value)));\n return this;\n }\n\n /**\n * Raises the current value to an exponent in place.\n *\n * @param {PrecisionNumberValue} value - Exponent\n *\n * @returns {this} The current instance for chaining\n */\n pow(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.pow(this.#valueToString(value)));\n return this;\n }\n\n /**\n * Multiplies the current value by another value in place.\n */\n times(value: PrecisionNumberValue) {\n this.#decimal = this.#decimalToFixedDecimal(this.#decimal.times(this.#valueToString(value)));\n return this;\n }\n\n /**\n * Returns a new instance with the absolute value.\n */\n toAbsoluteValue() {\n return new PrecisionNumber(this.#decimal.absoluteValue(), this.#decimalPlaces, this.#rounding);\n }\n\n /**\n * Returns a new instance rounded up to the nearest integer.\n */\n toCeil() {\n return new PrecisionNumber(this.#decimal.ceil(), this.#decimalPlaces, this.#rounding);\n }\n\n /**\n * Returns a new instance restricted to the inclusive range `[min, max]`.\n *\n * @param {PrecisionNumberValue} min - Lower bound\n * @param {PrecisionNumberValue} max - Upper bound\n *\n * @throws {Error} If `min` is greater than `max`\n */\n toClamped(min: PrecisionNumberValue, max: PrecisionNumberValue) {\n return this.clone().clamp(min, max);\n }\n\n /**\n * Returns a new instance divided by another value.\n */\n toDividedBy(value: PrecisionNumberValue) {\n return new PrecisionNumber(\n this.#decimal.dividedBy(this.#valueToString(value)),\n this.#decimalPlaces,\n this.#rounding,\n );\n }\n\n /**\n * Returns a new instance rounded down to the nearest integer.\n */\n toFloor() {\n return new PrecisionNumber(this.#decimal.floor(), this.#decimalPlaces, this.#rounding);\n }\n\n /**\n * Serializes to the fixed-decimal string.\n */\n toJSON() {\n return this.value;\n }\n\n /**\n * Returns a new instance with another value subtracted.\n */\n toMinus(value: PrecisionNumberValue) {\n return new PrecisionNumber(\n this.#decimal.minus(this.#valueToString(value)),\n this.#decimalPlaces,\n this.#rounding,\n );\n }\n\n /**\n * Returns a new instance with the remainder after division by another value.\n */\n toModulo(value: PrecisionNumberValue) {\n return new PrecisionNumber(\n this.#decimal.modulo(this.#valueToString(value)),\n this.#decimalPlaces,\n this.#rounding,\n );\n }\n\n /**\n * Returns a new instance with the value negated.\n */\n toNegated() {\n return new PrecisionNumber(this.#decimal.negated(), this.#decimalPlaces, this.#rounding);\n }\n\n /**\n * Returns a new instance with another value added.\n */\n toPlus(value: PrecisionNumberValue) {\n return new PrecisionNumber(this.#decimal.plus(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);\n }\n\n /**\n * Returns a new instance raised to an exponent.\n */\n toPow(value: PrecisionNumberValue) {\n return new PrecisionNumber(this.#decimal.pow(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);\n }\n\n /**\n * Converts the current value to a JavaScript number.\n *\n * Use `toString`/`value` when preserving decimal precision is more important than native-number ergonomics.\n */\n toNumber() {\n return this.#decimal.toNumber();\n }\n\n /**\n * Converts the current value to its fixed-decimal string.\n */\n toString() {\n return this.value;\n }\n\n /**\n * Formats the current value with custom precision and rounding for this call only.\n */\n toFixed(decimalPlaces: number = this.#decimalPlaces, rounding: Decimal.Rounding = this.#rounding) {\n return this.#decimal.toFixed(decimalPlaces, rounding);\n }\n\n /**\n * Returns a new instance multiplied by another value.\n */\n toTimes(value: PrecisionNumberValue) {\n return new PrecisionNumber(\n this.#decimal.times(this.#valueToString(value)),\n this.#decimalPlaces,\n this.#rounding,\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA0BA,IAAa,kBAAb,MAAa,gBAAgB;CAEzB;CACA;CACA;;;;;;;;CASA,YACI,QAA8B,KAC9B,gBAAwB,GACxB,WAA6B,QAAQ,YACvC;EACE,KAAKA,iBAAiB;EACtB,KAAKC,YAAY;E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AAO,KAAKE,eAAe,MAAM,CAAC,EAChD,KAAKJ,gBACL,KAAKC,UACR;;;;;CAML,YAAY;EACR,OAAO,IAAI,gBAAgB,KAAKC,SAAS,SAAS,EAAE,KAAKF,gBAAgB,KAAKC,UAAU;;;;;CAM5F,OAAO,OAA6B;EAChC,OAAO,IAAI,gBAAgB,KAAKC,SAAS,KAAK,KAAKE,eAAe,MAAM,CAAC,EAAE,KAAKJ,gBAAgB,KAAKC,UAAU;;;;;CAMnH,MAAM,OAA6B;EAC/B,OAAO,IAAI,gBAAgB,KAAKC,SAAS,IAAI,KAAKE,eAAe,MAAM,CAAC,EAAE,KAAKJ,gBAAgB,KAAKC,UAAU;;;;;;;CAQlH,WAAW;EACP,OAAO,KAAKC,SAAS,UAAU;;;;;CAMnC,WAAW;EACP,OAAO,KAAK;;;;;CAMhB,QAAQ,gBAAwB,KAAKF,gBAAgB,WAA6B,KAAKC,WAAW;EAC9F,OAAO,KAAKC,SAAS,QAAQ,eAAe,SAAS;;;;;CAMzD,QAAQ,OAA6B;EACjC,OAAO,IAAI,gBACP,KAAKA,SAAS,MAAM,KAAKE,eAAe,MAAM,CAAC,EAC/C,KAAKJ,gBACL,KAAKC,UACR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ssh-client.js","names":["#connectConfig","#logger","#nodeSsh"],"sources":["../../src/classes/ssh-client.ts"],"sourcesContent":["import { createConsola } from 'consola';\nimport type { ConsolaInstance } from 'consola';\nimport { NodeSSH } from 'node-ssh';\nimport type {\n Config,\n SSHExecCommandOptions,\n SSHGetPutDirectoryOptions,\n SSHPutFilesOptions,\n} from 'node-ssh';\nimport type {\n SFTPWrapper,\n TransferOptions,\n} from 'ssh2';\n\nimport type { Nullable } from '../types';\n\nimport type { PathLike } from './path';\n\nconst loggerLevelStringToConsolaLogLevelMap = {\n debug: 4,\n error: 0,\n fatal: 0,\n info: 3,\n normal: 2,\n silent: -999,\n trace: 5,\n verbose: 999,\n warn: 1,\n} as const;\n\nexport class SshClient {\n readonly #connectConfig: Config;\n readonly #logger: ConsolaInstance;\n\n #nodeSsh: NodeSSH;\n\n constructor(host: string, username: string, password: string, port: number = 22, connectConfig?: Config) {\n this.#connectConfig = {\n ...connectConfig,\n host,\n password,\n port,\n username,\n };\n\n this.#logger = createConsola();\n this.#nodeSsh = new NodeSSH();\n if (process.env.NODE_ENV === 'production') this.setLoggerLevel('error');\n }\n\n get nodeSsh() {\n return this.#nodeSsh;\n }\n\n async connect() {\n try {\n this.#nodeSsh = await this.#nodeSsh.connect(this.#connectConfig);\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n disconnect() {\n try {\n this.#nodeSsh.dispose();\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n execCommand(command: string, options?: SSHExecCommandOptions) {\n return this.#nodeSsh.execCommand(command, options).catch(() => {});\n }\n\n execCommandWithIo(command: string, options?: SSHExecCommandOptions) {\n return this.execCommand(\n command,\n {\n ...options,\n onStderr: (data) => process.stderr.write(data.toString()),\n onStdout: (data) => process.stdout.write(data.toString()),\n },\n );\n }\n\n getDir = this.getDirectory;\n\n async getDirectory(localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions) {\n try {\n return await this.#nodeSsh.getDirectory(localDirectory.toString(), remoteDirectory.toString(), options);\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n async getFile(\n localFile: PathLike,\n remoteFile: PathLike,\n givenSftp?: Nullable<SFTPWrapper>,\n transferOptions?: Nullable<TransferOptions>,\n ) {\n try {\n await this.#nodeSsh.getFile(localFile.toString(), remoteFile.toString(), givenSftp, transferOptions);\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n isConnected() {\n return this.#nodeSsh.isConnected();\n }\n\n async mkdir(path: PathLike) {\n try {\n await this.#nodeSsh.mkdir(path.toString());\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n putDir = this.putDirectory;\n\n async putDirectory(localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions) {\n try {\n return await this.#nodeSsh.putDirectory(localDirectory.toString(), remoteDirectory.toString(), options);\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n async putFile(\n localFile: PathLike,\n remoteFile: PathLike,\n givenSftp?: Nullable<SFTPWrapper>,\n transferOptions?: Nullable<TransferOptions>,\n ) {\n try {\n await this.#nodeSsh.putFile(localFile.toString(), remoteFile.toString(), givenSftp, transferOptions);\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n async putFiles(files: { local: PathLike; remote: PathLike }[], options?: SSHPutFilesOptions) {\n try {\n const convertedFiles = files.map(({ local, remote }) => ({\n local: local.toString(),\n remote: remote.toString(),\n }));\n\n await this.#nodeSsh.putFiles(convertedFiles, options);\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n setLoggerLevel(level: keyof typeof loggerLevelStringToConsolaLogLevelMap) {\n this.#logger.level = loggerLevelStringToConsolaLogLevelMap[level];\n }\n}\n"],"mappings":";;;AAkBA,MAAM,wCAAwC;CAC1C,OAAO;CACP,OAAO;CACP,OAAO;CACP,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,OAAO;CACP,SAAS;CACT,MAAM;CACT;AAED,IAAa,YAAb,MAAuB;CACnB;CACA;CAEA;CAEA,YAAY,MAAc,UAAkB,UAAkB,OAAe,IAAI,eAAwB;
|
|
1
|
+
{"version":3,"file":"ssh-client.js","names":["#connectConfig","#logger","#nodeSsh"],"sources":["../../src/classes/ssh-client.ts"],"sourcesContent":["import { createConsola } from 'consola';\nimport type { ConsolaInstance } from 'consola';\nimport { NodeSSH } from 'node-ssh';\nimport type {\n Config,\n SSHExecCommandOptions,\n SSHGetPutDirectoryOptions,\n SSHPutFilesOptions,\n} from 'node-ssh';\nimport type {\n SFTPWrapper,\n TransferOptions,\n} from 'ssh2';\n\nimport type { Nullable } from '../types';\n\nimport type { PathLike } from './path';\n\nconst loggerLevelStringToConsolaLogLevelMap = {\n debug: 4,\n error: 0,\n fatal: 0,\n info: 3,\n normal: 2,\n silent: -999,\n trace: 5,\n verbose: 999,\n warn: 1,\n} as const;\n\nexport class SshClient {\n readonly #connectConfig: Config;\n readonly #logger: ConsolaInstance;\n\n #nodeSsh: NodeSSH;\n\n constructor(host: string, username: string, password: string, port: number = 22, connectConfig?: Config) {\n this.#connectConfig = {\n ...connectConfig,\n host,\n password,\n port,\n username,\n };\n\n this.#logger = createConsola();\n this.#nodeSsh = new NodeSSH();\n if (process.env.NODE_ENV === 'production') this.setLoggerLevel('error');\n }\n\n get nodeSsh() {\n return this.#nodeSsh;\n }\n\n async connect() {\n try {\n this.#nodeSsh = await this.#nodeSsh.connect(this.#connectConfig);\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n disconnect() {\n try {\n this.#nodeSsh.dispose();\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n execCommand(command: string, options?: SSHExecCommandOptions) {\n return this.#nodeSsh.execCommand(command, options).catch(() => {});\n }\n\n execCommandWithIo(command: string, options?: SSHExecCommandOptions) {\n return this.execCommand(\n command,\n {\n ...options,\n onStderr: (data) => process.stderr.write(data.toString()),\n onStdout: (data) => process.stdout.write(data.toString()),\n },\n );\n }\n\n getDir = this.getDirectory;\n\n async getDirectory(localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions) {\n try {\n return await this.#nodeSsh.getDirectory(localDirectory.toString(), remoteDirectory.toString(), options);\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n async getFile(\n localFile: PathLike,\n remoteFile: PathLike,\n givenSftp?: Nullable<SFTPWrapper>,\n transferOptions?: Nullable<TransferOptions>,\n ) {\n try {\n await this.#nodeSsh.getFile(localFile.toString(), remoteFile.toString(), givenSftp, transferOptions);\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n isConnected() {\n return this.#nodeSsh.isConnected();\n }\n\n async mkdir(path: PathLike) {\n try {\n await this.#nodeSsh.mkdir(path.toString());\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n putDir = this.putDirectory;\n\n async putDirectory(localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions) {\n try {\n return await this.#nodeSsh.putDirectory(localDirectory.toString(), remoteDirectory.toString(), options);\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n async putFile(\n localFile: PathLike,\n remoteFile: PathLike,\n givenSftp?: Nullable<SFTPWrapper>,\n transferOptions?: Nullable<TransferOptions>,\n ) {\n try {\n await this.#nodeSsh.putFile(localFile.toString(), remoteFile.toString(), givenSftp, transferOptions);\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n async putFiles(files: { local: PathLike; remote: PathLike }[], options?: SSHPutFilesOptions) {\n try {\n const convertedFiles = files.map(({ local, remote }) => ({\n local: local.toString(),\n remote: remote.toString(),\n }));\n\n await this.#nodeSsh.putFiles(convertedFiles, options);\n return true;\n } catch (error) {\n this.#logger.error(error);\n return false;\n }\n }\n\n setLoggerLevel(level: keyof typeof loggerLevelStringToConsolaLogLevelMap) {\n this.#logger.level = loggerLevelStringToConsolaLogLevelMap[level];\n }\n}\n"],"mappings":";;;AAkBA,MAAM,wCAAwC;CAC1C,OAAO;CACP,OAAO;CACP,OAAO;CACP,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,OAAO;CACP,SAAS;CACT,MAAM;CACT;AAED,IAAa,YAAb,MAAuB;CACnB;CACA;CAEA;CAEA,YAAY,MAAc,UAAkB,UAAkB,OAAe,IAAI,eAAwB;EACrG,KAAKA,iBAAiB;GAClB,GAAG;GACH;GACA;GACA;GACA;GACH;EAED,KAAKC,UAAU,eAAe;EAC9B,KAAKC,WAAW,IAAI,SAAS;EAC7B,IAAI,QAAQ,IAAI,aAAa,cAAc,KAAK,eAAe,QAAQ;;CAG3E,IAAI,UAAU;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A0B,iBAA2B,SAAqC;EACzG,IAAI;GACA,OAAO,MAAM,KAAKC,SAAS,aAAa,eAAe,UAAU,EAAE,gBAAgB,UAAU,EAAE,QAAQ;WAClG,OAAO;GACZ,KAAKD,QAAQ,MAAM,MAAM;GACzB,OAAO;;;CAIf,MAAM,QACF,WACA,YACA,WACA,iBACF;EACE,IAAI;GACA,MAAM,KAAKC,SAAS,QAAQ,UAAU,UAAU,EAAE,WAAW,UAAU,EAAE,WAAW,gBAAgB;GACpG,OAAO;WACF,OAAO;GACZ,KAAKD,QAAQ,MAAM,MAAM;GACzB,OAAO;;;CAIf,MAAM,SAAS,OAAgD,SAA8B;EACzF,IAAI;GACA,MAAM,iBAAiB,MAAM,KAAK,EAAE,OAAO,cAAc;IACrD,OAAO,MAAM,UAAU;IACvB,QAAQ,OAAO,UAAU;IAC5B,EAAE;GAEH,MAAM,KAAKC,SAAS,SAAS,gBAAgB,QAAQ;GACrD,OAAO;WACF,OAAO;GACZ,KAAKD,QAAQ,MAAM,MAAM;GACzB,OAAO;;;CAIf,eAAe,OAA2D;EACtE,KAAKA,QAAQ,QAAQ,sCAAsC"}
|
package/dist/clipboard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clipboard.js","names":[],"sources":["../src/clipboard.ts"],"sourcesContent":["type CopyResult =\n | { error: unknown; ok: false }\n | { ok: true };\n\n/**\n * Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in Safari and some older browsers.\n *\n * @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content)\n * @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * import { copyBlobToClipboard } from '@kikiutils/shared/clipboard';\n *\n * const blob = new Blob(['Hello world'], { type: 'text/plain' });\n * const result = await copyBlobToClipboard(blob);\n * if (result.ok) {\n * console.log('Copied blob!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult> {\n if (!navigator.clipboard?.write) {\n return {\n error: new Error('Clipboard.write is not supported in this browser'),\n ok: false,\n };\n }\n\n try {\n const item = new ClipboardItem({ [blob.type]: blob }, options);\n await navigator.clipboard.write([item]);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n\n/**\n * Attempts to copy the given text to the user's clipboard using the modern Clipboard API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in some older browsers (especially legacy Safari).\n *\n * @param {string} text - The string to be copied to the clipboard\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * import { copyTextToClipboard } from '@kikiutils/shared/clipboard';\n *\n * const result = await copyTextToClipboard('Hello!');\n * if (result.ok) {\n * console.log('Copied!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyTextToClipboard(text: string): Promise<CopyResult> {\n try {\n await navigator.clipboard.writeText(text);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,eAAsB,oBAAoB,MAAY,SAAqD;
|
|
1
|
+
{"version":3,"file":"clipboard.js","names":[],"sources":["../src/clipboard.ts"],"sourcesContent":["type CopyResult =\n | { error: unknown; ok: false }\n | { ok: true };\n\n/**\n * Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in Safari and some older browsers.\n *\n * @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content)\n * @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * import { copyBlobToClipboard } from '@kikiutils/shared/clipboard';\n *\n * const blob = new Blob(['Hello world'], { type: 'text/plain' });\n * const result = await copyBlobToClipboard(blob);\n * if (result.ok) {\n * console.log('Copied blob!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult> {\n if (!navigator.clipboard?.write) {\n return {\n error: new Error('Clipboard.write is not supported in this browser'),\n ok: false,\n };\n }\n\n try {\n const item = new ClipboardItem({ [blob.type]: blob }, options);\n await navigator.clipboard.write([item]);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n\n/**\n * Attempts to copy the given text to the user's clipboard using the modern Clipboard API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in some older browsers (especially legacy Safari).\n *\n * @param {string} text - The string to be copied to the clipboard\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * import { copyTextToClipboard } from '@kikiutils/shared/clipboard';\n *\n * const result = await copyTextToClipboard('Hello!');\n * if (result.ok) {\n * console.log('Copied!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyTextToClipboard(text: string): Promise<CopyResult> {\n try {\n await navigator.clipboard.writeText(text);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,eAAsB,oBAAoB,MAAY,SAAqD;CACvG,IAAI,CAAC,UAAU,WAAW,OACtB,OAAO;EACH,uBAAO,IAAI,MAAM,mDAAmD;EACpE,IAAI;EACP;CAGL,IAAI;EACA,MAAM,OAAO,IAAI,cAAc,GAAG,KAAK,OAAO,MAAM,EAAE,QAAQ;EAC9D,MAAM,UAAU,UAAU,MAAM,CAAC,KAAK,CAAC;EACvC,OAAO,EAAE,IAAI,MAAM;UACd,OAAO;EACZ,OAAO;GACH;GACA,IAAI;GACP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BT,eAAsB,oBAAoB,MAAmC;CACzE,IAAI;EACA,MAAM,UAAU,UAAU,UAAU,KAAK;EACzC,OAAO,EAAE,IAAI,MAAM;UACd,OAAO;EACZ,OAAO;GACH;GACA,IAAI;GACP"}
|
package/dist/consola.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consola.js","names":[],"sources":["../src/consola.ts"],"sourcesContent":["import { createConsola } from 'consola';\n\n/**\n * A consola logger instance.\n *\n * The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.\n * If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,\n * the level will be set to `0`.\n *\n * To manually change the level, assign the desired level to `logger.level`.\n *\n * See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).\n *\n * @example\n * ```typescript\n * import { logger } from '@kikiutils/shared/consola';\n *\n * logger.info('test'); // ℹ test 3:56:30 AM\n *\n * // Manually change the level\n * logger.level = 3;\n * ```\n */\nexport const consolaLogger = createConsola();\nexport const logger = consolaLogger;\nif (process.env.CONSOLA_LOGGER_LEVEL !== undefined) consolaLogger.level = +process.env.CONSOLA_LOGGER_LEVEL;\nelse consolaLogger.level = process.env.NODE_ENV === 'production' ? 0 : consolaLogger.level;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAa,gBAAgB,eAAe;AAC5C,MAAa,SAAS;AACtB,IAAI,QAAQ,IAAI,yBAAyB,KAAA,
|
|
1
|
+
{"version":3,"file":"consola.js","names":[],"sources":["../src/consola.ts"],"sourcesContent":["import { createConsola } from 'consola';\n\n/**\n * A consola logger instance.\n *\n * The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.\n * If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,\n * the level will be set to `0`.\n *\n * To manually change the level, assign the desired level to `logger.level`.\n *\n * See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).\n *\n * @example\n * ```typescript\n * import { logger } from '@kikiutils/shared/consola';\n *\n * logger.info('test'); // ℹ test 3:56:30 AM\n *\n * // Manually change the level\n * logger.level = 3;\n * ```\n */\nexport const consolaLogger = createConsola();\nexport const logger = consolaLogger;\nif (process.env.CONSOLA_LOGGER_LEVEL !== undefined) consolaLogger.level = +process.env.CONSOLA_LOGGER_LEVEL;\nelse consolaLogger.level = process.env.NODE_ENV === 'production' ? 0 : consolaLogger.level;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAa,gBAAgB,eAAe;AAC5C,MAAa,SAAS;AACtB,IAAI,QAAQ,IAAI,yBAAyB,KAAA,GAAW,cAAc,QAAQ,CAAC,QAAQ,IAAI;KAClF,cAAc,QAAQ,QAAQ,IAAI,aAAa,eAAe,IAAI,cAAc"}
|
package/dist/crypto-hash.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { BinaryLike, BinaryToTextEncoding } from "node:crypto";
|
|
|
13
13
|
* ```typescript
|
|
14
14
|
* import { cryptoMd5 } from '@kikiutils/shared/crypto-hash';
|
|
15
15
|
*
|
|
16
|
-
* console.log(cryptoMd5('test')); //
|
|
16
|
+
* console.log(cryptoMd5('test')); // 098f6bcd4621d373cade4e832627b4f6
|
|
17
17
|
* ```
|
|
18
18
|
*/
|
|
19
19
|
declare function cryptoMd5(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
@@ -44,7 +44,7 @@ declare function cryptoMd5ToBuffer(data: BinaryLike): NonSharedBuffer;
|
|
|
44
44
|
* ```typescript
|
|
45
45
|
* import { cryptoSha3224 } from '@kikiutils/shared/crypto-hash';
|
|
46
46
|
*
|
|
47
|
-
* console.log(cryptoSha3224('test')); //
|
|
47
|
+
* console.log(cryptoSha3224('test')); // 3797bf0afbbfca4a7bbba7602a2b552746876517a7f9b7ce2db0ae7b
|
|
48
48
|
* ```
|
|
49
49
|
*/
|
|
50
50
|
declare function cryptoSha3224(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
@@ -59,7 +59,7 @@ declare function cryptoSha3224(data: BinaryLike, outputEncoding?: BinaryToTextEn
|
|
|
59
59
|
* ```typescript
|
|
60
60
|
* import { cryptoSha3224ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
61
61
|
*
|
|
62
|
-
* console.log(cryptoSha3224ToBuffer('test')); // <Buffer
|
|
62
|
+
* console.log(cryptoSha3224ToBuffer('test')); // <Buffer 37 97 bf 0a ...>
|
|
63
63
|
* ```
|
|
64
64
|
*/
|
|
65
65
|
declare function cryptoSha3224ToBuffer(data: BinaryLike): NonSharedBuffer;
|
|
@@ -106,7 +106,7 @@ declare function cryptoSha3256ToBuffer(data: BinaryLike): NonSharedBuffer;
|
|
|
106
106
|
* ```typescript
|
|
107
107
|
* import { cryptoSha3384 } from '@kikiutils/shared/crypto-hash';
|
|
108
108
|
*
|
|
109
|
-
* console.log(cryptoSha3384('test')); //
|
|
109
|
+
* console.log(cryptoSha3384('test')); // e516dabb23b6e30026863543282780a3ae0dccf05551cf0295178d7f...
|
|
110
110
|
* ```
|
|
111
111
|
*/
|
|
112
112
|
declare function cryptoSha3384(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
@@ -121,7 +121,7 @@ declare function cryptoSha3384(data: BinaryLike, outputEncoding?: BinaryToTextEn
|
|
|
121
121
|
* ```typescript
|
|
122
122
|
* import { cryptoSha3384ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
123
123
|
*
|
|
124
|
-
* console.log(cryptoSha3384ToBuffer('test')); // <Buffer
|
|
124
|
+
* console.log(cryptoSha3384ToBuffer('test')); // <Buffer e5 16 da bb ...>
|
|
125
125
|
* ```
|
|
126
126
|
*/
|
|
127
127
|
declare function cryptoSha3384ToBuffer(data: BinaryLike): NonSharedBuffer;
|
|
@@ -137,7 +137,7 @@ declare function cryptoSha3384ToBuffer(data: BinaryLike): NonSharedBuffer;
|
|
|
137
137
|
* ```typescript
|
|
138
138
|
* import { cryptoSha3512 } from '@kikiutils/shared/crypto-hash';
|
|
139
139
|
*
|
|
140
|
-
* console.log(cryptoSha3512('test')); //
|
|
140
|
+
* console.log(cryptoSha3512('test')); // 9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7...
|
|
141
141
|
* ```
|
|
142
142
|
*/
|
|
143
143
|
declare function cryptoSha3512(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
@@ -152,7 +152,7 @@ declare function cryptoSha3512(data: BinaryLike, outputEncoding?: BinaryToTextEn
|
|
|
152
152
|
* ```typescript
|
|
153
153
|
* import { cryptoSha3512ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
154
154
|
*
|
|
155
|
-
* console.log(cryptoSha3512ToBuffer('test')); // <Buffer
|
|
155
|
+
* console.log(cryptoSha3512ToBuffer('test')); // <Buffer 9e ce 08 6e ...>
|
|
156
156
|
* ```
|
|
157
157
|
*/
|
|
158
158
|
declare function cryptoSha3512ToBuffer(data: BinaryLike): NonSharedBuffer;
|
package/dist/crypto-hash.js
CHANGED
|
@@ -12,7 +12,7 @@ import { createHash } from "node:crypto";
|
|
|
12
12
|
* ```typescript
|
|
13
13
|
* import { cryptoMd5 } from '@kikiutils/shared/crypto-hash';
|
|
14
14
|
*
|
|
15
|
-
* console.log(cryptoMd5('test')); //
|
|
15
|
+
* console.log(cryptoMd5('test')); // 098f6bcd4621d373cade4e832627b4f6
|
|
16
16
|
* ```
|
|
17
17
|
*/
|
|
18
18
|
function cryptoMd5(data, outputEncoding = "hex") {
|
|
@@ -47,7 +47,7 @@ function cryptoMd5ToBuffer(data) {
|
|
|
47
47
|
* ```typescript
|
|
48
48
|
* import { cryptoSha3224 } from '@kikiutils/shared/crypto-hash';
|
|
49
49
|
*
|
|
50
|
-
* console.log(cryptoSha3224('test')); //
|
|
50
|
+
* console.log(cryptoSha3224('test')); // 3797bf0afbbfca4a7bbba7602a2b552746876517a7f9b7ce2db0ae7b
|
|
51
51
|
* ```
|
|
52
52
|
*/
|
|
53
53
|
function cryptoSha3224(data, outputEncoding = "hex") {
|
|
@@ -64,7 +64,7 @@ function cryptoSha3224(data, outputEncoding = "hex") {
|
|
|
64
64
|
* ```typescript
|
|
65
65
|
* import { cryptoSha3224ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
66
66
|
*
|
|
67
|
-
* console.log(cryptoSha3224ToBuffer('test')); // <Buffer
|
|
67
|
+
* console.log(cryptoSha3224ToBuffer('test')); // <Buffer 37 97 bf 0a ...>
|
|
68
68
|
* ```
|
|
69
69
|
*/
|
|
70
70
|
function cryptoSha3224ToBuffer(data) {
|
|
@@ -117,7 +117,7 @@ function cryptoSha3256ToBuffer(data) {
|
|
|
117
117
|
* ```typescript
|
|
118
118
|
* import { cryptoSha3384 } from '@kikiutils/shared/crypto-hash';
|
|
119
119
|
*
|
|
120
|
-
* console.log(cryptoSha3384('test')); //
|
|
120
|
+
* console.log(cryptoSha3384('test')); // e516dabb23b6e30026863543282780a3ae0dccf05551cf0295178d7f...
|
|
121
121
|
* ```
|
|
122
122
|
*/
|
|
123
123
|
function cryptoSha3384(data, outputEncoding = "hex") {
|
|
@@ -134,7 +134,7 @@ function cryptoSha3384(data, outputEncoding = "hex") {
|
|
|
134
134
|
* ```typescript
|
|
135
135
|
* import { cryptoSha3384ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
136
136
|
*
|
|
137
|
-
* console.log(cryptoSha3384ToBuffer('test')); // <Buffer
|
|
137
|
+
* console.log(cryptoSha3384ToBuffer('test')); // <Buffer e5 16 da bb ...>
|
|
138
138
|
* ```
|
|
139
139
|
*/
|
|
140
140
|
function cryptoSha3384ToBuffer(data) {
|
|
@@ -152,7 +152,7 @@ function cryptoSha3384ToBuffer(data) {
|
|
|
152
152
|
* ```typescript
|
|
153
153
|
* import { cryptoSha3512 } from '@kikiutils/shared/crypto-hash';
|
|
154
154
|
*
|
|
155
|
-
* console.log(cryptoSha3512('test')); //
|
|
155
|
+
* console.log(cryptoSha3512('test')); // 9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7...
|
|
156
156
|
* ```
|
|
157
157
|
*/
|
|
158
158
|
function cryptoSha3512(data, outputEncoding = "hex") {
|
|
@@ -169,7 +169,7 @@ function cryptoSha3512(data, outputEncoding = "hex") {
|
|
|
169
169
|
* ```typescript
|
|
170
170
|
* import { cryptoSha3512ToBuffer } from '@kikiutils/shared/crypto-hash';
|
|
171
171
|
*
|
|
172
|
-
* console.log(cryptoSha3512ToBuffer('test')); // <Buffer
|
|
172
|
+
* console.log(cryptoSha3512ToBuffer('test')); // <Buffer 9e ce 08 6e ...>
|
|
173
173
|
* ```
|
|
174
174
|
*/
|
|
175
175
|
function cryptoSha3512ToBuffer(data) {
|