@kikiutils/shared 18.0.1 → 18.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.
Files changed (156) hide show
  1. package/dist/buffer.d.ts +19 -21
  2. package/dist/buffer.d.ts.map +1 -1
  3. package/dist/buffer.js +18 -19
  4. package/dist/buffer.js.map +1 -1
  5. package/dist/classes/enhanced-date.d.ts +425 -0
  6. package/dist/classes/enhanced-date.d.ts.map +1 -0
  7. package/dist/classes/enhanced-date.js +591 -0
  8. package/dist/classes/enhanced-date.js.map +1 -0
  9. package/dist/classes/path.d.ts +211 -48
  10. package/dist/classes/path.d.ts.map +1 -1
  11. package/dist/classes/path.js +181 -52
  12. package/dist/classes/path.js.map +1 -1
  13. package/dist/classes/precision-number.d.ts +44 -39
  14. package/dist/classes/precision-number.d.ts.map +1 -1
  15. package/dist/classes/precision-number.js +51 -45
  16. package/dist/classes/precision-number.js.map +1 -1
  17. package/dist/classes/ssh-client.d.ts +3 -4
  18. package/dist/classes/ssh-client.d.ts.map +1 -1
  19. package/dist/classes/ssh-client.js +2 -2
  20. package/dist/classes/ssh-client.js.map +1 -1
  21. package/dist/clipboard.d.ts +25 -26
  22. package/dist/clipboard.d.ts.map +1 -1
  23. package/dist/clipboard.js +23 -23
  24. package/dist/clipboard.js.map +1 -1
  25. package/dist/consola.d.ts +12 -15
  26. package/dist/consola.d.ts.map +1 -1
  27. package/dist/consola.js +10 -12
  28. package/dist/consola.js.map +1 -1
  29. package/dist/crypto-hash.d.ts +65 -56
  30. package/dist/crypto-hash.d.ts.map +1 -1
  31. package/dist/crypto-hash.js +55 -45
  32. package/dist/crypto-hash.js.map +1 -1
  33. package/dist/datetime.d.ts +41 -53
  34. package/dist/datetime.d.ts.map +1 -1
  35. package/dist/datetime.js +53 -66
  36. package/dist/datetime.js.map +1 -1
  37. package/dist/element-plus.d.ts +13 -14
  38. package/dist/element-plus.d.ts.map +1 -1
  39. package/dist/element-plus.js +11 -11
  40. package/dist/element-plus.js.map +1 -1
  41. package/dist/enum.d.ts +39 -36
  42. package/dist/enum.d.ts.map +1 -1
  43. package/dist/enum.js +36 -32
  44. package/dist/enum.js.map +1 -1
  45. package/dist/env.d.ts +22 -15
  46. package/dist/env.d.ts.map +1 -1
  47. package/dist/env.js +20 -12
  48. package/dist/env.js.map +1 -1
  49. package/dist/event-awaiter.d.ts +47 -52
  50. package/dist/event-awaiter.d.ts.map +1 -1
  51. package/dist/event-awaiter.js +46 -50
  52. package/dist/event-awaiter.js.map +1 -1
  53. package/dist/general.d.ts +22 -17
  54. package/dist/general.d.ts.map +1 -1
  55. package/dist/general.js.map +1 -1
  56. package/dist/hash.d.ts +24 -21
  57. package/dist/hash.d.ts.map +1 -1
  58. package/dist/hash.js +20 -16
  59. package/dist/hash.js.map +1 -1
  60. package/dist/math.d.ts +20 -19
  61. package/dist/math.d.ts.map +1 -1
  62. package/dist/math.js +14 -12
  63. package/dist/math.js.map +1 -1
  64. package/dist/number.d.ts +10 -9
  65. package/dist/number.d.ts.map +1 -1
  66. package/dist/number.js +9 -7
  67. package/dist/number.js.map +1 -1
  68. package/dist/object.d.ts +22 -10
  69. package/dist/object.d.ts.map +1 -1
  70. package/dist/object.js +21 -8
  71. package/dist/object.js.map +1 -1
  72. package/dist/pino.d.ts +12 -15
  73. package/dist/pino.d.ts.map +1 -1
  74. package/dist/pino.js +10 -15
  75. package/dist/pino.js.map +1 -1
  76. package/dist/random.d.ts +17 -15
  77. package/dist/random.d.ts.map +1 -1
  78. package/dist/random.js +16 -13
  79. package/dist/random.js.map +1 -1
  80. package/dist/redis/script-runner.d.ts +57 -0
  81. package/dist/redis/script-runner.d.ts.map +1 -0
  82. package/dist/redis/script-runner.js +46 -0
  83. package/dist/redis/script-runner.js.map +1 -0
  84. package/dist/storages/lru/keyed-store.d.ts +18 -14
  85. package/dist/storages/lru/keyed-store.d.ts.map +1 -1
  86. package/dist/storages/lru/keyed-store.js +17 -12
  87. package/dist/storages/lru/keyed-store.js.map +1 -1
  88. package/dist/storages/redis/keyed-store.d.ts +11 -21
  89. package/dist/storages/redis/keyed-store.d.ts.map +1 -1
  90. package/dist/storages/redis/keyed-store.js +10 -19
  91. package/dist/storages/redis/keyed-store.js.map +1 -1
  92. package/dist/storages/redis/msgpack.d.ts +33 -13
  93. package/dist/storages/redis/msgpack.d.ts.map +1 -1
  94. package/dist/storages/redis/msgpack.js +32 -11
  95. package/dist/storages/redis/msgpack.js.map +1 -1
  96. package/dist/storages/redis/types.d.ts +34 -17
  97. package/dist/storages/redis/types.d.ts.map +1 -1
  98. package/dist/string.d.ts +12 -11
  99. package/dist/string.d.ts.map +1 -1
  100. package/dist/string.js +20 -15
  101. package/dist/string.js.map +1 -1
  102. package/dist/time.d.ts +26 -21
  103. package/dist/time.d.ts.map +1 -1
  104. package/dist/time.js +24 -18
  105. package/dist/time.js.map +1 -1
  106. package/dist/types/filtered-key-path.d.ts +4 -15
  107. package/dist/types/filtered-key-path.d.ts.map +1 -1
  108. package/dist/types/index.d.ts +10 -10
  109. package/dist/types/index.d.ts.map +1 -1
  110. package/dist/types/vue.d.ts +7 -7
  111. package/dist/types/vue.d.ts.map +1 -1
  112. package/dist/url.d.ts +15 -14
  113. package/dist/url.d.ts.map +1 -1
  114. package/dist/url.js +12 -10
  115. package/dist/url.js.map +1 -1
  116. package/dist/vue.d.ts +19 -21
  117. package/dist/vue.d.ts.map +1 -1
  118. package/dist/vue.js +15 -16
  119. package/dist/vue.js.map +1 -1
  120. package/dist/web.d.ts +14 -11
  121. package/dist/web.d.ts.map +1 -1
  122. package/dist/web.js +12 -8
  123. package/dist/web.js.map +1 -1
  124. package/package.json +20 -20
  125. package/src/buffer.ts +18 -19
  126. package/src/classes/enhanced-date.ts +665 -0
  127. package/src/classes/path.ts +232 -91
  128. package/src/classes/precision-number.ts +61 -53
  129. package/src/classes/ssh-client.ts +8 -5
  130. package/src/clipboard.ts +23 -23
  131. package/src/consola.ts +10 -12
  132. package/src/crypto-hash.ts +55 -45
  133. package/src/datetime.ts +65 -85
  134. package/src/element-plus.ts +11 -11
  135. package/src/enum.ts +36 -32
  136. package/src/env.ts +20 -12
  137. package/src/event-awaiter.ts +49 -50
  138. package/src/general.ts +20 -14
  139. package/src/hash.ts +20 -16
  140. package/src/math.ts +18 -16
  141. package/src/number.ts +9 -7
  142. package/src/object.ts +21 -8
  143. package/src/pino.ts +11 -15
  144. package/src/random.ts +16 -13
  145. package/src/redis/script-runner.ts +107 -0
  146. package/src/storages/lru/keyed-store.ts +17 -12
  147. package/src/storages/redis/keyed-store.ts +10 -19
  148. package/src/storages/redis/msgpack.ts +32 -11
  149. package/src/storages/redis/types.ts +32 -14
  150. package/src/string.ts +10 -8
  151. package/src/time.ts +24 -18
  152. package/src/types/filtered-key-path.ts +9 -14
  153. package/src/types/vue.ts +6 -5
  154. package/src/url.ts +12 -10
  155. package/src/vue.ts +15 -16
  156. package/src/web.ts +12 -8
@@ -1,22 +1,25 @@
1
1
  import { Decimal } from "decimal.js";
2
2
  //#region src/classes/precision-number.ts
3
3
  /**
4
- * Chainable fixed-decimal wrapper around `decimal.js`.
4
+ * A fixed-decimal number with mutable and immutable arithmetic operations.
5
5
  *
6
+ * @remarks
6
7
  * `PrecisionNumber` stores values as `Decimal`, rounds every mutating operation to
7
8
  * the configured decimal places, and exposes both mutable and immutable arithmetic
8
9
  * methods. Mutable methods (`plus`, `minus`, `times`, `dividedBy`, `modulo`,
9
10
  * `pow`, `clamp`, `ceil`, `floor`, `negate`, `absoluteValue`) update the current
10
11
  * instance and return `this`; immutable methods prefixed with `to` return a new
11
- * `PrecisionNumber` with the same precision settings.
12
+ * `PrecisionNumber` with the same precision settings, leaving this instance unchanged.
13
+ * Decimal.js exceptions propagate unchanged; non-finite arithmetic results are not converted into exceptions.
12
14
  *
13
15
  * @example
14
- * ```typescript
16
+ *
17
+ * ```ts
15
18
  * import { PrecisionNumber } from '@kikiutils/shared/classes/precision-number';
16
19
  *
17
20
  * 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')
21
+ * value.toString(); // => '3.35'
22
+ * value.toMinus('1').toString(); // => '2.35' (original value remains '3.35')
20
23
  * ```
21
24
  */
22
25
  var PrecisionNumber = class PrecisionNumber {
@@ -26,30 +29,15 @@ var PrecisionNumber = class PrecisionNumber {
26
29
  /**
27
30
  * Creates a fixed-decimal number.
28
31
  *
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
+ * @param value - Initial value (default: `'0'`).
33
+ * @param decimalPlaces - Decimal places retained after operations (default: `2`).
34
+ * @param rounding - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`).
32
35
  */
33
36
  constructor(value = "0", decimalPlaces = 2, rounding = Decimal.ROUND_DOWN) {
34
37
  this.#decimalPlaces = decimalPlaces;
35
38
  this.#rounding = rounding;
36
39
  this.#decimal = this.#decimalToFixedDecimal(new Decimal(value.toString().trim()));
37
40
  }
38
- /**
39
- * Returns the formatted value when inspected by Node.js utilities.
40
- */
41
- [Symbol.for("nodejs.util.inspect.custom")]() {
42
- return this.value;
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
- */
49
- [Symbol.toPrimitive](hint) {
50
- if (hint === "number") return this.#decimal.toNumber();
51
- return this.value;
52
- }
53
41
  #decimalToFixedDecimal(decimal) {
54
42
  return decimal.toDecimalPlaces(this.#decimalPlaces, this.#rounding);
55
43
  }
@@ -77,19 +65,34 @@ var PrecisionNumber = class PrecisionNumber {
77
65
  /**
78
66
  * Formats a value with decimal.js without creating a reusable instance.
79
67
  *
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`)
68
+ * @param value - Value to format.
69
+ * @param decimalPlaces - Decimal places to output (default: `2`).
70
+ * @param rounding - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`).
83
71
  *
84
- * @returns {string} Fixed-decimal string
72
+ * @returns Fixed-decimal string.
85
73
  */
86
74
  static toFixed(value, decimalPlaces = 2, rounding = Decimal.ROUND_DOWN) {
87
75
  return new Decimal(value.toString().trim()).toFixed(decimalPlaces, rounding);
88
76
  }
89
77
  /**
78
+ * Returns the formatted value when inspected by Node.js utilities.
79
+ */
80
+ [Symbol.for("nodejs.util.inspect.custom")]() {
81
+ return this.value;
82
+ }
83
+ /**
84
+ * Converts to a number for numeric coercion and to the fixed string otherwise.
85
+ *
86
+ * @param hint - JavaScript primitive-conversion hint.
87
+ */
88
+ [Symbol.toPrimitive](hint) {
89
+ if (hint === "number") return this.#decimal.toNumber();
90
+ return this.value;
91
+ }
92
+ /**
90
93
  * Replaces the current value with its absolute value.
91
94
  *
92
- * @returns {this} The current instance for chaining
95
+ * @returns This instance for chaining.
93
96
  */
94
97
  absoluteValue() {
95
98
  this.#decimal = this.#decimalToFixedDecimal(this.#decimal.absoluteValue());
@@ -98,9 +101,10 @@ var PrecisionNumber = class PrecisionNumber {
98
101
  /**
99
102
  * Rounds the current value up to the nearest integer in place.
100
103
  *
104
+ * @remarks
101
105
  * The stored value still uses this instance's configured decimal places for output.
102
106
  *
103
- * @returns {this} The current instance for chaining
107
+ * @returns This instance for chaining.
104
108
  */
105
109
  ceil() {
106
110
  this.#decimal = this.#decimalToFixedDecimal(this.#decimal.ceil());
@@ -109,12 +113,12 @@ var PrecisionNumber = class PrecisionNumber {
109
113
  /**
110
114
  * Restricts the current value to the inclusive range `[min, max]` in place.
111
115
  *
112
- * @param {PrecisionNumberValue} min - Lower bound
113
- * @param {PrecisionNumberValue} max - Upper bound
116
+ * @param min - Lower bound.
117
+ * @param max - Upper bound.
114
118
  *
115
- * @returns {this} The current instance for chaining
119
+ * @returns This instance for chaining.
116
120
  *
117
- * @throws {Error} If `min` is greater than `max`
121
+ * @throws Error if `min` is greater than `max`.
118
122
  */
119
123
  clamp(min, max) {
120
124
  const minDecimal = new Decimal(this.#valueToString(min));
@@ -132,9 +136,9 @@ var PrecisionNumber = class PrecisionNumber {
132
136
  /**
133
137
  * Divides the current value by another value in place.
134
138
  *
135
- * @param {PrecisionNumberValue} value - Divisor
139
+ * @param value - Divisor.
136
140
  *
137
- * @returns {this} The current instance for chaining
141
+ * @returns This instance for chaining.
138
142
  */
139
143
  dividedBy(value) {
140
144
  this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(this.#valueToString(value)));
@@ -143,9 +147,9 @@ var PrecisionNumber = class PrecisionNumber {
143
147
  /**
144
148
  * Checks whether the current value equals another value.
145
149
  *
146
- * @param {PrecisionNumberValue} value - Value to compare
150
+ * @param value - Value to compare.
147
151
  *
148
- * @returns {boolean} `true` when both numeric values are equal
152
+ * @returns Whether both numeric values are equal.
149
153
  */
150
154
  equals(value) {
151
155
  return this.#decimal.equals(this.#valueToString(value));
@@ -153,9 +157,10 @@ var PrecisionNumber = class PrecisionNumber {
153
157
  /**
154
158
  * Rounds the current value down to the nearest integer in place.
155
159
  *
160
+ * @remarks
156
161
  * The stored value still uses this instance's configured decimal places for output.
157
162
  *
158
- * @returns {this} The current instance for chaining
163
+ * @returns This instance for chaining.
159
164
  */
160
165
  floor() {
161
166
  this.#decimal = this.#decimalToFixedDecimal(this.#decimal.floor());
@@ -231,9 +236,9 @@ var PrecisionNumber = class PrecisionNumber {
231
236
  /**
232
237
  * Replaces the current value with the remainder after division by another value.
233
238
  *
234
- * @param {PrecisionNumberValue} value - Divisor used to compute the remainder
239
+ * @param value - Divisor used to compute the remainder.
235
240
  *
236
- * @returns {this} The current instance for chaining
241
+ * @returns This instance for chaining.
237
242
  */
238
243
  modulo(value) {
239
244
  this.#decimal = this.#decimalToFixedDecimal(this.#decimal.modulo(this.#valueToString(value)));
@@ -256,9 +261,9 @@ var PrecisionNumber = class PrecisionNumber {
256
261
  /**
257
262
  * Raises the current value to an exponent in place.
258
263
  *
259
- * @param {PrecisionNumberValue} value - Exponent
264
+ * @param value - Exponent.
260
265
  *
261
- * @returns {this} The current instance for chaining
266
+ * @returns This instance for chaining.
262
267
  */
263
268
  pow(value) {
264
269
  this.#decimal = this.#decimalToFixedDecimal(this.#decimal.pow(this.#valueToString(value)));
@@ -286,10 +291,10 @@ var PrecisionNumber = class PrecisionNumber {
286
291
  /**
287
292
  * Returns a new instance restricted to the inclusive range `[min, max]`.
288
293
  *
289
- * @param {PrecisionNumberValue} min - Lower bound
290
- * @param {PrecisionNumberValue} max - Upper bound
294
+ * @param min - Lower bound.
295
+ * @param max - Upper bound.
291
296
  *
292
- * @throws {Error} If `min` is greater than `max`
297
+ * @throws Error if `min` is greater than `max`.
293
298
  */
294
299
  toClamped(min, max) {
295
300
  return this.clone().clamp(min, max);
@@ -345,6 +350,7 @@ var PrecisionNumber = class PrecisionNumber {
345
350
  /**
346
351
  * Converts the current value to a JavaScript number.
347
352
  *
353
+ * @remarks
348
354
  * Use `toString`/`value` when preserving decimal precision is more important than native-number ergonomics.
349
355
  */
350
356
  toNumber() {
@@ -1 +1 @@
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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gBACL,KAAKC,SACT;CACJ;;;;CAKA,UAAU;EACN,OAAO,IAAI,gBAAgB,KAAKC,SAAS,MAAM,GAAG,KAAKF,gBAAgB,KAAKC,SAAS;CACzF;;;;CAKA,SAAS;EACL,OAAO,KAAK;CAChB;;;;CAKA,QAAQ,OAA6B;EACjC,OAAO,IAAI,gBACP,KAAKC,SAAS,MAAM,KAAKE,eAAe,KAAK,CAAC,GAC9C,KAAKJ,gBACL,KAAKC,SACT;CACJ;;;;CAKA,SAAS,OAA6B;EAClC,OAAO,IAAI,gBACP,KAAKC,SAAS,OAAO,KAAKE,eAAe,KAAK,CAAC,GAC/C,KAAKJ,gBACL,KAAKC,SACT;CACJ;;;;CAKA,YAAY;EACR,OAAO,IAAI,gBAAgB,KAAKC,SAAS,QAAQ,GAAG,KAAKF,gBAAgB,KAAKC,SAAS;CAC3F;;;;CAKA,OAAO,OAA6B;EAChC,OAAO,IAAI,gBAAgB,KAAKC,SAAS,KAAK,KAAKE,eAAe,KAAK,CAAC,GAAG,KAAKJ,gBAAgB,KAAKC,SAAS;CAClH;;;;CAKA,MAAM,OAA6B;EAC/B,OAAO,IAAI,gBAAgB,KAAKC,SAAS,IAAI,KAAKE,eAAe,KAAK,CAAC,GAAG,KAAKJ,gBAAgB,KAAKC,SAAS;CACjH;;;;;;CAOA,WAAW;EACP,OAAO,KAAKC,SAAS,SAAS;CAClC;;;;CAKA,WAAW;EACP,OAAO,KAAK;CAChB;;;;CAKA,QAAQ,gBAAwB,KAAKF,gBAAgB,WAA6B,KAAKC,WAAW;EAC9F,OAAO,KAAKC,SAAS,QAAQ,eAAe,QAAQ;CACxD;;;;CAKA,QAAQ,OAA6B;EACjC,OAAO,IAAI,gBACP,KAAKA,SAAS,MAAM,KAAKE,eAAe,KAAK,CAAC,GAC9C,KAAKJ,gBACL,KAAKC,SACT;CACJ;AACJ"}
1
+ {"version":3,"file":"precision-number.js","names":[],"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 * A fixed-decimal number with mutable and immutable arithmetic operations.\n *\n * @remarks\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, leaving this instance unchanged.\n * Decimal.js exceptions propagate unchanged; non-finite arithmetic results are not converted into exceptions.\n *\n * @example\n *\n * ```ts\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 instance properties\n readonly #decimalPlaces: number;\n readonly #rounding: Decimal.Rounding;\n #decimal: Decimal;\n\n // Constructor\n\n /**\n * Creates a fixed-decimal number.\n *\n * @param value - Initial value (default: `'0'`).\n * @param decimalPlaces - Decimal places retained after operations (default: `2`).\n * @param 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 // Private instance 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 instance accessors\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 // Public static methods\n\n /**\n * Formats a value with decimal.js without creating a reusable instance.\n *\n * @param value - Value to format.\n * @param decimalPlaces - Decimal places to output (default: `2`).\n * @param rounding - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`).\n *\n * @returns 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 instance methods\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 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 /**\n * Replaces the current value with its absolute value.\n *\n * @returns This 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 * @remarks\n * The stored value still uses this instance's configured decimal places for output.\n *\n * @returns This 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 min - Lower bound.\n * @param max - Upper bound.\n *\n * @returns This 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 value - Divisor.\n *\n * @returns This 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 value - Value to compare.\n *\n * @returns Whether 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 * @remarks\n * The stored value still uses this instance's configured decimal places for output.\n *\n * @returns This 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 value - Divisor used to compute the remainder.\n *\n * @returns This 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 value - Exponent.\n *\n * @returns This 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 min - Lower bound.\n * @param 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 * @remarks\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":";;;;;;;;;;;;;;;;;;;;;;;;AA6BA,IAAa,kBAAb,MAAa,gBAAgB;CAEzB;CACA;CACA;;;;;;;;CAWA,YACI,QAA8B,KAC9B,gBAAwB,GACxB,WAA6B,QAAQ,YACvC;EACE,KAAK,iBAAiB;EACtB,KAAK,YAAY;EACjB,KAAK,WAAW,KAAK,uBAAuB,IAAI,QAAQ,MAAM,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC;CACpF;CAGA,uBAAuB,SAAkB;EACrC,OAAO,QAAQ,gBAAgB,KAAK,gBAAgB,KAAK,SAAS;CACtE;CAEA,eAAe,OAA6B;EACxC,OAAO,MAAM,SAAS,CAAC,CAAC,KAAK;CACjC;;;;CAOA,IAAI,gBAAgB;EAChB,OAAO,KAAK;CAChB;;;;CAKA,IAAI,WAAW;EACX,OAAO,KAAK;CAChB;;;;CAKA,IAAI,QAAQ;EACR,OAAO,KAAK,SAAS,QAAQ,KAAK,gBAAgB,KAAK,SAAS;CACpE;;;;;;;;;;CAaA,OAAO,QACH,OACA,gBAAwB,GACxB,WAA6B,QAAQ,YACvC;EACE,OAAO,IAAI,QAAQ,MAAM,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,eAAe,QAAQ;CAC/E;;;;CAOA,CAAC,OAAO,IAAI,4BAA4B,KAAK;EACzC,OAAO,KAAK;CAChB;;;;;;CAOA,CAAC,OAAO,aAAa,MAAc;EAC/B,IAAI,SAAS,UAAU,OAAO,KAAK,SAAS,SAAS;EACrD,OAAO,KAAK;CAChB;;;;;;CAOA,gBAAgB;EACZ,KAAK,WAAW,KAAK,uBAAuB,KAAK,SAAS,cAAc,CA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-13,20 +13,20 @@ declare const loggerLevelStringToConsolaLogLevelMap: {
13
13
  readonly verbose: 999;
14
14
  readonly warn: 1;
15
15
  };
16
- declare class SshClient {
16
+ export declare class SshClient {
17
17
  #private;
18
+ getDir: (localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions) => Promise<boolean>;
19
+ putDir: (localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions) => Promise<boolean>;
18
20
  constructor(host: string, username: string, password: string, port?: number, connectConfig?: Config);
19
21
  get nodeSsh(): NodeSSH;
20
22
  connect(): Promise<boolean>;
21
23
  disconnect(): boolean;
22
24
  execCommand(command: string, options?: SSHExecCommandOptions): Promise<void | import("node-ssh").SSHExecCommandResponse>;
23
25
  execCommandWithIo(command: string, options?: SSHExecCommandOptions): Promise<void | import("node-ssh").SSHExecCommandResponse>;
24
- getDir: (localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions) => Promise<boolean>;
25
26
  getDirectory(localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions): Promise<boolean>;
26
27
  getFile(localFile: PathLike, remoteFile: PathLike, givenSftp?: null | SFTPWrapper, transferOptions?: null | TransferOptions): Promise<boolean>;
27
28
  isConnected(): boolean;
28
29
  mkdir(path: PathLike): Promise<boolean>;
29
- putDir: (localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions) => Promise<boolean>;
30
30
  putDirectory(localDirectory: PathLike, remoteDirectory: PathLike, options?: SSHGetPutDirectoryOptions): Promise<boolean>;
31
31
  putFile(localFile: PathLike, remoteFile: PathLike, givenSftp?: null | SFTPWrapper, transferOptions?: null | TransferOptions): Promise<boolean>;
32
32
  putFiles(files: {
@@ -36,5 +36,4 @@ declare class SshClient {
36
36
  setLoggerLevel(level: keyof typeof loggerLevelStringToConsolaLogLevelMap): void;
37
37
  }
38
38
  //#endregion
39
- export { SshClient };
40
39
  //# sourceMappingURL=ssh-client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ssh-client.d.ts","names":[],"sources":["../../src/classes/ssh-client.ts"],"mappings":";;;;cAgBM;;;;;;;;;;;cAYO;;EAMG,YAAA,cAAc,kBAAkB,kBAAkB,eAAmB,gBAAgB;MAc7F,WAAO;EAIL,WAAO;EAUb;EAUA,YAAY,iBAAiB,UAAU,wBAAqB,kCAAA;EAI5D,kBAAkB,iBAAiB,UAAU,wBAAqB,kCAAA;EAWlE,SAAM,gBAE6B,UAAQ,iBAAmB,UAAQ,UAAY,8BAAyB;EAArG,aAAa,gBAAgB,UAAU,iBAAiB,UAAU,UAAU,4BAAyB;EASrG,QACF,WAAW,UACX,YAAY,UACZ,mBAAmB,aACnB,yBAAyB,kBAAe;EAW5C;EAIM,MAAM,MAAM,WAAQ;EAU1B,SAAM,gBAE6B,UAAQ,iBAAmB,UAAQ,UAAY,8BAAyB;EAArG,aAAa,gBAAgB,UAAU,iBAAiB,UAAU,UAAU,4BAAyB;EASrG,QACF,WAAW,UACX,YAAY,UACZ,mBAAmB,aACnB,yBAAyB,kBAAe;EAWtC,SAAS;IAAS,OAAO;IAAU,QAAQ;OAAc,UAAU,qBAAkB;EAe3F,eAAe,oBAAoB"}
1
+ {"version":3,"file":"ssh-client.d.ts","names":[],"sources":["../../src/classes/ssh-client.ts"],"mappings":";;;;cAgBM;;;;;;;;;;;qBAYO;;EAOT,SAAM,gBA2D6B,UAAQ,iBAAmB,UAAQ,UAAY,8BAAyB;EA1D3G,SAAM,gBAgG6B,UAAQ,iBAAmB,UAAQ,UAAY,8BAAyB;EA7F/F,YAAA,cAAc,kBAAkB,kBAAkB,eAAmB,gBAAgB;MAe7F,WAAO;EAKL,WAAO;EAUb;EAUA,YAAY,iBAAiB,UAAU,wBAAqB,kCAAA;EAI5D,kBAAkB,iBAAiB,UAAU,wBAAqB,kCAAA;EAW5D,aAAa,gBAAgB,UAAU,iBAAiB,UAAU,UAAU,4BAAyB;EASrG,QACF,WAAW,UACX,YAAY,UACZ,mBAAmB,aACnB,yBAAyB,kBAAe;EAW5C;EAIM,MAAM,MAAM,WAAQ;EAUpB,aAAa,gBAAgB,UAAU,iBAAiB,UAAU,UAAU,4BAAyB;EASrG,QACF,WAAW,UACX,YAAY,UACZ,mBAAmB,aACnB,yBAAyB,kBAAe;EAWtC,SAAS;IAAS,OAAO;IAAU,QAAQ;OAAc,UAAU,qBAAkB;EAe3F,eAAe,oBAAoB"}
@@ -16,6 +16,8 @@ var SshClient = class {
16
16
  #connectConfig;
17
17
  #logger;
18
18
  #nodeSsh;
19
+ getDir = this.getDirectory;
20
+ putDir = this.putDirectory;
19
21
  constructor(host, username, password, port = 22, connectConfig) {
20
22
  this.#connectConfig = {
21
23
  ...connectConfig,
@@ -59,7 +61,6 @@ var SshClient = class {
59
61
  onStdout: (data) => process.stdout.write(data.toString())
60
62
  });
61
63
  }
62
- getDir = this.getDirectory;
63
64
  async getDirectory(localDirectory, remoteDirectory, options) {
64
65
  try {
65
66
  return await this.#nodeSsh.getDirectory(localDirectory.toString(), remoteDirectory.toString(), options);
@@ -89,7 +90,6 @@ var SshClient = class {
89
90
  return false;
90
91
  }
91
92
  }
92
- putDir = this.putDirectory;
93
93
  async putDirectory(localDirectory, remoteDirectory, options) {
94
94
  try {
95
95
  return await this.#nodeSsh.putDirectory(localDirectory.toString(), remoteDirectory.toString(), options);
@@ -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 { 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?: null | SFTPWrapper,\n transferOptions?: null | 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?: null | SFTPWrapper,\n transferOptions?: null | 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":";;;AAgBA,MAAM,wCAAwC;CAC1C,OAAO;CACP,OAAO;CACP,OAAO;CACP,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,OAAO;CACP,SAAS;CACT,MAAM;AACV;AAEA,IAAa,YAAb,MAAuB;CACnB;CACA;CAEA;CAEA,YAAY,MAAc,UAAkB,UAAkB,OAAe,IAAI,eAAwB;EACrG,KAAKA,iBAAiB;GAClB,GAAG;GACH;GACA;GACA;GACA;EACJ;EAEA,KAAKC,UAAU,cAAc;EAC7B,KAAKC,WAAW,IAAI,QAAQ;EAC5B,IAAI,QAAQ,IAAI,aAAa,cAAc,KAAK,eAAe,OAAO;CAC1E;CAEA,IAAI,UAAU;EACV,OAAO,KAAKA;CAChB;CAEA,MAAM,UAAU;EACZ,IAAI;GACA,KAAKA,WAAW,MAAM,KAA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MAAM,aAAa,gBAA0B,iBAA2B,SAAqC;EACzG,IAAI;GACA,OAAO,MAAM,KAAKC,SAAS,aAAa,eAAe,SAAS,GAAG,gBAAgB,SAAS,GAAG,OAAO;EAC1G,SAAS,OAAO;GACZ,KAAKD,QAAQ,MAAM,KAAK;GACxB,OAAO;EACX;CACJ;CAEA,MAAM,QACF,WACA,YACA,WACA,iBACF;EACE,IAAI;GACA,MAAM,KAAKC,SAAS,QAAQ,UAAU,SAAS,GAAG,WAAW,SAAS,GAAG,WAAW,eAAe;GACnG,OAAO;EACX,SAAS,OAAO;GACZ,KAAKD,QAAQ,MAAM,KAAK;GACxB,OAAO;EACX;CACJ;CAEA,MAAM,SAAS,OAAgD,SAA8B;EACzF,IAAI;GACA,MAAM,iBAAiB,MAAM,KAAK,EAAE,OAAO,cAAc;IACrD,OAAO,MAAM,SAAS;IACtB,QAAQ,OAAO,SAAS;GAC5B,EAAE;GAEF,MAAM,KAAKC,SAAS,SAAS,gBAAgB,OAAO;GACpD,OAAO;EACX,SAAS,OAAO;GACZ,KAAKD,QAAQ,MAAM,KAAK;GACxB,OAAO;EACX;CACJ;CAEA,eAAe,OAA2D;EACtE,KAAKA,QAAQ,QAAQ,sCAAsC;CAC/D;AACJ"}
1
+ {"version":3,"file":"ssh-client.js","names":[],"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 { 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 // Private instance properties\n readonly #connectConfig: Config;\n readonly #logger: ConsolaInstance;\n #nodeSsh: NodeSSH;\n\n // Public instance properties\n getDir = this.getDirectory;\n putDir = this.putDirectory;\n\n // Constructor\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 // Public instance accessors\n get nodeSsh() {\n return this.#nodeSsh;\n }\n\n // Public instance methods\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 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?: null | SFTPWrapper,\n transferOptions?: null | 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 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?: null | SFTPWrapper,\n transferOptions?: null | 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":";;;AAgBA,MAAM,wCAAwC;CAC1C,OAAO;CACP,OAAO;CACP,OAAO;CACP,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,OAAO;CACP,SAAS;CACT,MAAM;AACV;AAEA,IAAa,YAAb,MAAuB;CAEnB;CACA;CACA;CAGA,SAAS,KAAK;CACd,SAAS,KAAK;CAGd,YAAY,MAAc,UAAkB,UAAkB,OAAe,IAAI,eAAwB;EACrG,KAAK,iBAAiB;GAClB,GAAG;GACH;GACA;GACA;GACA;EACJ;EAEA,KAAK,UAAU,cAAc;EAC7B,KAAK,WAAW,IAAI,QAAQ;EAC5B,IAAI,QAAQ,IAAI,aAAa,cAAc,KAAK,eAAe,OAAO;CAC1E;CAGA,IAAI,UAAU;EACV,OAAO,KAAK;CAChB;CAGA,MAAM,UAAU;EACZ,IAAI;GACA,KAAK,WAAW,MAAM,KAAK,SAAS,QAAQ,KAAK,cAAc;GAC/D,OA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SAAS,GAAG,gBAAgB,SAAS,GAAG,OAAO;EAC1G,SAAS,OAAO;GACZ,KAAK,QAAQ,MAAM,KAAK;GACxB,OAAO;EACX;CACJ;CAEA,MAAM,QACF,WACA,YACA,WACA,iBACF;EACE,IAAI;GACA,MAAM,KAAK,SAAS,QAAQ,UAAU,SAAS,GAAG,WAAW,SAAS,GAAG,WAAW,eAAe;GACnG,OAAO;EACX,SAAS,OAAO;GACZ,KAAK,QAAQ,MAAM,KAAK;GACxB,OAAO;EACX;CACJ;CAEA,MAAM,SAAS,OAAgD,SAA8B;EACzF,IAAI;GACA,MAAM,iBAAiB,MAAM,KAAK,EAAE,OAAO,cAAc;IACrD,OAAO,MAAM,SAAS;IACtB,QAAQ,OAAO,SAAS;GAC5B,EAAE;GAEF,MAAM,KAAK,SAAS,SAAS,gBAAgB,OAAO;GACpD,OAAO;EACX,SAAS,OAAO;GACZ,KAAK,QAAQ,MAAM,KAAK;GACxB,OAAO;EACX;CACJ;CAEA,eAAe,OAA2D;EACtE,KAAK,QAAQ,QAAQ,sCAAsC;CAC/D;AACJ"}
@@ -6,53 +6,53 @@ type CopyResult = {
6
6
  ok: true;
7
7
  };
8
8
  /**
9
- * Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
9
+ * Attempts to copy a blob to the browser clipboard.
10
10
  *
11
- * ⚠️ Usage Notes:
12
- * - Must be called in a **secure context** (HTTPS or localhost).
13
- * - Must be called **in response to a user interaction** (e.g. click, input).
14
- * - Not supported in Safari and some older browsers.
11
+ * @remarks
12
+ * Requires a browser environment with clipboard support, a secure context, and any user activation required
13
+ * by the browser. Clipboard API failures are returned as results rather than rethrown.
15
14
  *
16
- * @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content)
17
- * @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor
15
+ * @param blob - The blob whose MIME type identifies the clipboard format.
16
+ * @param options - Options passed to the `ClipboardItem` constructor.
18
17
  *
19
- * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
20
- * - `{ ok: true }` if the copy succeeded
21
- * - `{ ok: false, error }` if the copy failed, with the error included
18
+ * @returns A promise resolving after the copy attempt to `{ ok: true }` on success, or
19
+ * `{ ok: false, error }` with the failure reason.
22
20
  *
23
21
  * @example
24
- * ```typescript
22
+ *
23
+ * ```ts
25
24
  * import { copyBlobToClipboard } from '@kikiutils/shared/clipboard';
26
25
  *
26
+ * // Call from a user interaction in a supported browser.
27
27
  * const blob = new Blob(['Hello world'], { type: 'text/plain' });
28
28
  * const result = await copyBlobToClipboard(blob);
29
29
  * if (result.ok) {
30
- * console.log('Copied blob!');
30
+ * console.log('Copied!');
31
31
  * } else {
32
32
  * console.error('Copy failed:', result.error);
33
33
  * }
34
34
  * ```
35
35
  */
36
- declare function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult>;
36
+ export declare function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult>;
37
37
  /**
38
- * Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
38
+ * Attempts to copy text to the browser clipboard.
39
39
  *
40
- * ⚠️ Usage Notes:
41
- * - Must be called in a **secure context** (HTTPS or localhost).
42
- * - Must be called **in response to a user interaction** (e.g. click, input).
43
- * - Not supported in some older browsers (especially legacy Safari).
40
+ * @remarks
41
+ * Requires a browser environment with clipboard support, a secure context, and any user activation required
42
+ * by the browser. Clipboard API failures are returned as results rather than rethrown.
44
43
  *
45
- * @param {string} text - The string to be copied to the clipboard
44
+ * @param text - The text to copy.
46
45
  *
47
- * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
48
- * - `{ ok: true }` if the copy succeeded
49
- * - `{ ok: false, error }` if the copy failed, with the error included
46
+ * @returns A promise resolving after the copy attempt to `{ ok: true }` on success, or
47
+ * `{ ok: false, error }` with the failure reason.
50
48
  *
51
49
  * @example
52
- * ```typescript
50
+ *
51
+ * ```ts
53
52
  * import { copyTextToClipboard } from '@kikiutils/shared/clipboard';
54
53
  *
55
- * const result = await copyTextToClipboard('Hello!');
54
+ * // Call from a user interaction in a supported browser.
55
+ * const result = await copyTextToClipboard('Hello world');
56
56
  * if (result.ok) {
57
57
  * console.log('Copied!');
58
58
  * } else {
@@ -60,7 +60,6 @@ declare function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions)
60
60
  * }
61
61
  * ```
62
62
  */
63
- declare function copyTextToClipboard(text: string): Promise<CopyResult>;
63
+ export declare function copyTextToClipboard(text: string): Promise<CopyResult>;
64
64
  //#endregion
65
- export { copyBlobToClipboard, copyTextToClipboard };
66
65
  //# sourceMappingURL=clipboard.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"clipboard.d.ts","names":[],"sources":["../src/clipboard.ts"],"mappings":";KAAK;EACC;EAAgB;;EAChB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8BgB,oBAAoB,MAAM,MAAM,UAAU,uBAAuB,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8CzE,oBAAoB,eAAe,QAAQ"}
1
+ {"version":3,"file":"clipboard.d.ts","names":[],"sources":["../src/clipboard.ts"],"mappings":";KAAK;EACC;EAAgB;;EAChB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA8BgB,oBAAoB,MAAM,MAAM,UAAU,uBAAuB,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA8CzE,oBAAoB,eAAe,QAAQ"}
package/dist/clipboard.js CHANGED
@@ -1,27 +1,27 @@
1
1
  //#region src/clipboard.ts
2
2
  /**
3
- * Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
3
+ * Attempts to copy a blob to the browser clipboard.
4
4
  *
5
- * ⚠️ Usage Notes:
6
- * - Must be called in a **secure context** (HTTPS or localhost).
7
- * - Must be called **in response to a user interaction** (e.g. click, input).
8
- * - Not supported in Safari and some older browsers.
5
+ * @remarks
6
+ * Requires a browser environment with clipboard support, a secure context, and any user activation required
7
+ * by the browser. Clipboard API failures are returned as results rather than rethrown.
9
8
  *
10
- * @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content)
11
- * @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor
9
+ * @param blob - The blob whose MIME type identifies the clipboard format.
10
+ * @param options - Options passed to the `ClipboardItem` constructor.
12
11
  *
13
- * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
14
- * - `{ ok: true }` if the copy succeeded
15
- * - `{ ok: false, error }` if the copy failed, with the error included
12
+ * @returns A promise resolving after the copy attempt to `{ ok: true }` on success, or
13
+ * `{ ok: false, error }` with the failure reason.
16
14
  *
17
15
  * @example
18
- * ```typescript
16
+ *
17
+ * ```ts
19
18
  * import { copyBlobToClipboard } from '@kikiutils/shared/clipboard';
20
19
  *
20
+ * // Call from a user interaction in a supported browser.
21
21
  * const blob = new Blob(['Hello world'], { type: 'text/plain' });
22
22
  * const result = await copyBlobToClipboard(blob);
23
23
  * if (result.ok) {
24
- * console.log('Copied blob!');
24
+ * console.log('Copied!');
25
25
  * } else {
26
26
  * console.error('Copy failed:', result.error);
27
27
  * }
@@ -44,24 +44,24 @@ async function copyBlobToClipboard(blob, options) {
44
44
  }
45
45
  }
46
46
  /**
47
- * Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
47
+ * Attempts to copy text to the browser clipboard.
48
48
  *
49
- * ⚠️ Usage Notes:
50
- * - Must be called in a **secure context** (HTTPS or localhost).
51
- * - Must be called **in response to a user interaction** (e.g. click, input).
52
- * - Not supported in some older browsers (especially legacy Safari).
49
+ * @remarks
50
+ * Requires a browser environment with clipboard support, a secure context, and any user activation required
51
+ * by the browser. Clipboard API failures are returned as results rather than rethrown.
53
52
  *
54
- * @param {string} text - The string to be copied to the clipboard
53
+ * @param text - The text to copy.
55
54
  *
56
- * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
57
- * - `{ ok: true }` if the copy succeeded
58
- * - `{ ok: false, error }` if the copy failed, with the error included
55
+ * @returns A promise resolving after the copy attempt to `{ ok: true }` on success, or
56
+ * `{ ok: false, error }` with the failure reason.
59
57
  *
60
58
  * @example
61
- * ```typescript
59
+ *
60
+ * ```ts
62
61
  * import { copyTextToClipboard } from '@kikiutils/shared/clipboard';
63
62
  *
64
- * const result = await copyTextToClipboard('Hello!');
63
+ * // Call from a user interaction in a supported browser.
64
+ * const result = await copyTextToClipboard('Hello world');
65
65
  * if (result.ok) {
66
66
  * console.log('Copied!');
67
67
  * } else {
@@ -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;CACvG,IAAI,CAAC,UAAU,WAAW,OACtB,OAAO;EACH,uBAAO,IAAI,MAAM,kDAAkD;EACnE,IAAI;CACR;CAGJ,IAAI;EACA,MAAM,OAAO,IAAI,cAAc,GAAG,KAAK,OAAO,KAAK,GAAG,OAAO;EAC7D,MAAM,UAAU,UAAU,MAAM,CAAC,IAAI,CAAC;EACtC,OAAO,EAAE,IAAI,KAAK;CACtB,SAAS,OAAO;EACZ,OAAO;GACH;GACA,IAAI;EACR;CACJ;AACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,eAAsB,oBAAoB,MAAmC;CACzE,IAAI;EACA,MAAM,UAAU,UAAU,UAAU,IAAI;EACxC,OAAO,EAAE,IAAI,KAAK;CACtB,SAAS,OAAO;EACZ,OAAO;GACH;GACA,IAAI;EACR;CACJ;AACJ"}
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 to the browser clipboard.\n *\n * @remarks\n * Requires a browser environment with clipboard support, a secure context, and any user activation required\n * by the browser. Clipboard API failures are returned as results rather than rethrown.\n *\n * @param blob - The blob whose MIME type identifies the clipboard format.\n * @param options - Options passed to the `ClipboardItem` constructor.\n *\n * @returns A promise resolving after the copy attempt to `{ ok: true }` on success, or\n * `{ ok: false, error }` with the failure reason.\n *\n * @example\n *\n * ```ts\n * import { copyBlobToClipboard } from '@kikiutils/shared/clipboard';\n *\n * // Call from a user interaction in a supported browser.\n * const blob = new Blob(['Hello world'], { type: 'text/plain' });\n * const result = await copyBlobToClipboard(blob);\n * if (result.ok) {\n * console.log('Copied!');\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 text to the browser clipboard.\n *\n * @remarks\n * Requires a browser environment with clipboard support, a secure context, and any user activation required\n * by the browser. Clipboard API failures are returned as results rather than rethrown.\n *\n * @param text - The text to copy.\n *\n * @returns A promise resolving after the copy attempt to `{ ok: true }` on success, or\n * `{ ok: false, error }` with the failure reason.\n *\n * @example\n *\n * ```ts\n * import { copyTextToClipboard } from '@kikiutils/shared/clipboard';\n *\n * // Call from a user interaction in a supported browser.\n * const result = await copyTextToClipboard('Hello world');\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,kDAAkD;EACnE,IAAI;CACR;CAGJ,IAAI;EACA,MAAM,OAAO,IAAI,cAAc,GAAG,KAAK,OAAO,KAAK,GAAG,OAAO;EAC7D,MAAM,UAAU,UAAU,MAAM,CAAC,IAAI,CAAC;EACtC,OAAO,EAAE,IAAI,KAAK;CACtB,SAAS,OAAO;EACZ,OAAO;GACH;GACA,IAAI;EACR;CACJ;AACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,eAAsB,oBAAoB,MAAmC;CACzE,IAAI;EACA,MAAM,UAAU,UAAU,UAAU,IAAI;EACxC,OAAO,EAAE,IAAI,KAAK;CACtB,SAAS,OAAO;EACZ,OAAO;GACH;GACA,IAAI;EACR;CACJ;AACJ"}