@parsrun/core 0.1.29 → 0.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/decimal.d.ts CHANGED
@@ -1,11 +1,36 @@
1
1
  /**
2
2
  * @parsrun/core - Decimal Utilities
3
- * Precise decimal calculations for financial and quantity operations
4
- * Edge-compatible - no external dependencies
3
+ * Precise decimal calculations for financial and quantity operations.
4
+ * Edge-compatible - no external dependencies.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { Decimal, decimal } from '@parsrun/core';
9
+ *
10
+ * // Create decimals
11
+ * const price = new Decimal('19.99');
12
+ * const quantity = decimal(3);
13
+ *
14
+ * // Arithmetic operations (chainable)
15
+ * const total = price.mul(quantity).round(2);
16
+ * console.log(total.toString()); // '59.97'
17
+ *
18
+ * // Static helpers
19
+ * const sum = Decimal.sum(['10.50', '20.25', '15.75']);
20
+ * const avg = Decimal.avg([100, 200, 300]);
21
+ * ```
5
22
  */
6
23
  /**
7
- * Decimal class for precise arithmetic
8
- * Uses string-based arithmetic to avoid floating point issues
24
+ * Decimal class for precise arithmetic operations.
25
+ * Uses string-based representation internally to avoid floating point issues.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const a = new Decimal('0.1');
30
+ * const b = new Decimal('0.2');
31
+ * const c = a.add(b);
32
+ * console.log(c.toString()); // '0.3' (not 0.30000000000000004)
33
+ * ```
9
34
  */
10
35
  declare class Decimal {
11
36
  private value;
@@ -13,160 +38,250 @@ declare class Decimal {
13
38
  private normalizeNumber;
14
39
  private normalizeString;
15
40
  /**
16
- * Add two decimals
41
+ * Add another value to this decimal.
42
+ * @param other - Value to add
43
+ * @returns A new Decimal with the result
17
44
  */
18
45
  add(other: number | string | Decimal): Decimal;
19
46
  /**
20
- * Subtract
47
+ * Subtract a value from this decimal.
48
+ * @param other - Value to subtract
49
+ * @returns A new Decimal with the result
21
50
  */
22
51
  sub(other: number | string | Decimal): Decimal;
23
52
  /**
24
- * Multiply
53
+ * Multiply this decimal by another value.
54
+ * @param other - Value to multiply by
55
+ * @returns A new Decimal with the result
25
56
  */
26
57
  mul(other: number | string | Decimal): Decimal;
27
58
  /**
28
- * Divide
59
+ * Divide this decimal by another value.
60
+ * @param other - Value to divide by
61
+ * @returns A new Decimal with the result
62
+ * @throws Error if dividing by zero
29
63
  */
30
64
  div(other: number | string | Decimal): Decimal;
31
65
  /**
32
- * Modulo
66
+ * Get the modulo (remainder) of dividing this decimal by another value.
67
+ * @param other - Value to divide by
68
+ * @returns A new Decimal with the remainder
33
69
  */
34
70
  mod(other: number | string | Decimal): Decimal;
35
71
  /**
36
- * Power
72
+ * Raise this decimal to a power.
73
+ * @param exp - The exponent
74
+ * @returns A new Decimal with the result
37
75
  */
38
76
  pow(exp: number): Decimal;
39
77
  /**
40
- * Square root
78
+ * Calculate the square root of this decimal.
79
+ * @returns A new Decimal with the square root
80
+ * @throws Error if the value is negative
41
81
  */
42
82
  sqrt(): Decimal;
43
83
  /**
44
- * Absolute value
84
+ * Get the absolute value of this decimal.
85
+ * @returns A new Decimal with the absolute value
45
86
  */
46
87
  abs(): Decimal;
47
88
  /**
48
- * Negate
89
+ * Negate this decimal (multiply by -1).
90
+ * @returns A new Decimal with the negated value
49
91
  */
50
92
  neg(): Decimal;
51
93
  /**
52
- * Round to decimal places
94
+ * Round to the specified number of decimal places using standard rounding.
95
+ * @param decimals - Number of decimal places (default: 0)
96
+ * @returns A new Decimal with the rounded value
53
97
  */
54
98
  round(decimals?: number): Decimal;
55
99
  /**
56
- * Floor to decimal places
100
+ * Round down to the specified number of decimal places.
101
+ * @param decimals - Number of decimal places (default: 0)
102
+ * @returns A new Decimal with the floored value
57
103
  */
58
104
  floor(decimals?: number): Decimal;
59
105
  /**
60
- * Ceiling to decimal places
106
+ * Round up to the specified number of decimal places.
107
+ * @param decimals - Number of decimal places (default: 0)
108
+ * @returns A new Decimal with the ceiled value
61
109
  */
62
110
  ceil(decimals?: number): Decimal;
63
111
  /**
64
- * Compare: returns -1, 0, or 1
112
+ * Compare this decimal to another value.
113
+ * @param other - Value to compare against
114
+ * @returns -1 if less, 0 if equal, 1 if greater
65
115
  */
66
116
  cmp(other: number | string | Decimal): -1 | 0 | 1;
67
117
  /**
68
- * Equality check
118
+ * Check if this decimal equals another value.
119
+ * @param other - Value to compare against
120
+ * @returns True if values are equal
69
121
  */
70
122
  eq(other: number | string | Decimal): boolean;
71
123
  /**
72
- * Greater than
124
+ * Check if this decimal is greater than another value.
125
+ * @param other - Value to compare against
126
+ * @returns True if this is greater
73
127
  */
74
128
  gt(other: number | string | Decimal): boolean;
75
129
  /**
76
- * Greater than or equal
130
+ * Check if this decimal is greater than or equal to another value.
131
+ * @param other - Value to compare against
132
+ * @returns True if this is greater or equal
77
133
  */
78
134
  gte(other: number | string | Decimal): boolean;
79
135
  /**
80
- * Less than
136
+ * Check if this decimal is less than another value.
137
+ * @param other - Value to compare against
138
+ * @returns True if this is less
81
139
  */
82
140
  lt(other: number | string | Decimal): boolean;
83
141
  /**
84
- * Less than or equal
142
+ * Check if this decimal is less than or equal to another value.
143
+ * @param other - Value to compare against
144
+ * @returns True if this is less or equal
85
145
  */
86
146
  lte(other: number | string | Decimal): boolean;
87
147
  /**
88
- * Check if zero
148
+ * Check if this decimal is exactly zero.
149
+ * @returns True if value is zero
89
150
  */
90
151
  isZero(): boolean;
91
152
  /**
92
- * Check if positive
153
+ * Check if this decimal is positive (greater than zero).
154
+ * @returns True if value is positive
93
155
  */
94
156
  isPositive(): boolean;
95
157
  /**
96
- * Check if negative
158
+ * Check if this decimal is negative (less than zero).
159
+ * @returns True if value is negative
97
160
  */
98
161
  isNegative(): boolean;
99
162
  /**
100
- * Convert to number
163
+ * Convert this decimal to a JavaScript number.
164
+ * @returns The numeric value
101
165
  */
102
166
  toNumber(): number;
103
167
  /**
104
- * Convert to string
168
+ * Convert this decimal to its string representation.
169
+ * @returns The string value
105
170
  */
106
171
  toString(): string;
107
172
  /**
108
- * Format with fixed decimal places
173
+ * Format this decimal with a fixed number of decimal places.
174
+ * @param decimals - Number of decimal places (default: 2)
175
+ * @returns Formatted string
109
176
  */
110
177
  toFixed(decimals?: number): string;
111
178
  /**
112
- * Convert to JSON (string representation)
179
+ * Convert to JSON (returns string representation for serialization).
180
+ * @returns The string value for JSON serialization
113
181
  */
114
182
  toJSON(): string;
115
183
  /**
116
- * Static: Create from value
184
+ * Create a Decimal from a value (alias for constructor).
185
+ * @param value - The value to convert
186
+ * @returns A new Decimal instance
117
187
  */
118
188
  static from(value: number | string | Decimal): Decimal;
119
189
  /**
120
- * Static: Sum array of values
190
+ * Calculate the sum of an array of values.
191
+ * @param values - Array of values to sum
192
+ * @returns A new Decimal with the sum
121
193
  */
122
194
  static sum(values: (number | string | Decimal)[]): Decimal;
123
195
  /**
124
- * Static: Average of array
196
+ * Calculate the average of an array of values.
197
+ * @param values - Array of values to average
198
+ * @returns A new Decimal with the average (0 if empty array)
125
199
  */
126
200
  static avg(values: (number | string | Decimal)[]): Decimal;
127
201
  /**
128
- * Static: Min of array
202
+ * Find the minimum value from the provided values.
203
+ * @param values - Values to compare
204
+ * @returns A new Decimal with the minimum value
205
+ * @throws Error if no values provided
129
206
  */
130
207
  static min(...values: (number | string | Decimal)[]): Decimal;
131
208
  /**
132
- * Static: Max of array
209
+ * Find the maximum value from the provided values.
210
+ * @param values - Values to compare
211
+ * @returns A new Decimal with the maximum value
212
+ * @throws Error if no values provided
133
213
  */
134
214
  static max(...values: (number | string | Decimal)[]): Decimal;
135
215
  }
136
216
  /**
137
- * Decimal utilities for database operations
217
+ * Utility functions for working with decimals in database operations.
218
+ * Provides helpers for converting between JavaScript numbers and database decimal strings.
219
+ *
220
+ * @example
221
+ * ```typescript
222
+ * // Convert numeric fields before database insert
223
+ * const data = DecimalUtils.prepareForDatabase(
224
+ * { price: 19.99, quantity: 5 },
225
+ * ['price']
226
+ * );
227
+ *
228
+ * // Format for display
229
+ * DecimalUtils.formatCurrency('19.99', { currency: 'USD' }); // '$19.99'
230
+ * ```
138
231
  */
139
232
  declare const DecimalUtils: {
140
233
  /**
141
- * Convert number to database decimal string
234
+ * Convert a number to a database-safe decimal string.
235
+ * @param value - The value to convert
236
+ * @returns The decimal string or null if value is null/undefined
142
237
  */
143
238
  toDecimalString(value: number | string | null | undefined): string | null;
144
239
  /**
145
- * Convert database decimal string to number
240
+ * Convert a database decimal string to a JavaScript number.
241
+ * @param value - The decimal string from database
242
+ * @returns The numeric value (0 if null/undefined/empty)
146
243
  */
147
244
  fromDecimalString(value: string | null | undefined): number;
148
245
  /**
149
- * Perform precise decimal multiplication
246
+ * Multiply two values with precise decimal arithmetic.
247
+ * @param a - First value
248
+ * @param b - Second value
249
+ * @returns The product as a string
150
250
  */
151
251
  multiply(a: number | string, b: number | string): string;
152
252
  /**
153
- * Perform precise decimal addition
253
+ * Add two values with precise decimal arithmetic.
254
+ * @param a - First value
255
+ * @param b - Second value
256
+ * @returns The sum as a string
154
257
  */
155
258
  add(a: number | string, b: number | string): string;
156
259
  /**
157
- * Perform precise decimal subtraction
260
+ * Subtract two values with precise decimal arithmetic.
261
+ * @param a - Value to subtract from
262
+ * @param b - Value to subtract
263
+ * @returns The difference as a string
158
264
  */
159
265
  subtract(a: number | string, b: number | string): string;
160
266
  /**
161
- * Perform precise decimal division
267
+ * Divide two values with precise decimal arithmetic.
268
+ * @param a - Dividend
269
+ * @param b - Divisor
270
+ * @returns The quotient as a string
162
271
  */
163
272
  divide(a: number | string, b: number | string): string;
164
273
  /**
165
- * Format decimal for display
274
+ * Format a decimal value for display with fixed decimal places.
275
+ * @param value - The value to format
276
+ * @param decimalPlaces - Number of decimal places (default: 2)
277
+ * @returns Formatted string
166
278
  */
167
279
  format(value: string | number, decimalPlaces?: number): string;
168
280
  /**
169
- * Format as currency
281
+ * Format a value as currency using Intl.NumberFormat.
282
+ * @param value - The value to format
283
+ * @param options - Currency formatting options
284
+ * @returns Formatted currency string
170
285
  */
171
286
  formatCurrency(value: string | number, options?: {
172
287
  currency?: string;
@@ -174,16 +289,31 @@ declare const DecimalUtils: {
174
289
  decimals?: number;
175
290
  }): string;
176
291
  /**
177
- * Convert object with decimal fields for database insert/update
292
+ * Prepare an object for database insert/update by converting numeric fields to decimal strings.
293
+ * @param data - The object to prepare
294
+ * @param decimalFields - Array of field names that should be converted to decimal strings
295
+ * @returns A new object with specified fields converted to decimal strings
178
296
  */
179
297
  prepareForDatabase<T extends Record<string, unknown>>(data: T, decimalFields: string[]): T;
180
298
  /**
181
- * Convert object with decimal fields from database
299
+ * Parse an object from database by converting decimal string fields to JavaScript numbers.
300
+ * @param data - The object from database
301
+ * @param decimalFields - Array of field names that should be converted from decimal strings
302
+ * @returns A new object with specified fields converted to numbers
182
303
  */
183
304
  parseFromDatabase<T extends Record<string, unknown>>(data: T, decimalFields: string[]): T;
184
305
  };
185
306
  /**
186
- * Shorthand for creating Decimal
307
+ * Shorthand function for creating a Decimal instance.
308
+ *
309
+ * @param value - The value to convert to a Decimal
310
+ * @returns A new Decimal instance
311
+ *
312
+ * @example
313
+ * ```typescript
314
+ * const price = decimal('19.99');
315
+ * const total = decimal(100).mul(price);
316
+ * ```
187
317
  */
188
318
  declare function decimal(value: number | string): Decimal;
189
319