@onereach/billing-shared 31.3.2-beta.12665.0 → 31.4.1-beta.12685.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 (77) hide show
  1. package/dist/cjs/enums/common.js +1 -0
  2. package/dist/cjs/enums/common.js.map +1 -1
  3. package/dist/cjs/enums/index.js +0 -1
  4. package/dist/cjs/enums/index.js.map +1 -1
  5. package/dist/cjs/types/index.js +1 -1
  6. package/dist/cjs/types/index.js.map +1 -1
  7. package/dist/cjs/types/{usage-compaction.js → invoice-generation.js} +1 -1
  8. package/dist/cjs/types/invoice-generation.js.map +1 -0
  9. package/dist/cjs/utils/index.js +0 -2
  10. package/dist/cjs/utils/index.js.map +1 -1
  11. package/dist/esm/enums/common.js +1 -0
  12. package/dist/esm/enums/common.js.map +1 -1
  13. package/dist/esm/enums/index.js +0 -1
  14. package/dist/esm/enums/index.js.map +1 -1
  15. package/dist/esm/types/index.js +1 -1
  16. package/dist/esm/types/index.js.map +1 -1
  17. package/dist/esm/types/invoice-generation.js +2 -0
  18. package/dist/esm/types/invoice-generation.js.map +1 -0
  19. package/dist/esm/utils/index.js +0 -2
  20. package/dist/esm/utils/index.js.map +1 -1
  21. package/dist/types/enums/common.d.ts +1 -0
  22. package/dist/types/enums/common.d.ts.map +1 -1
  23. package/dist/types/enums/index.d.ts +0 -1
  24. package/dist/types/enums/index.d.ts.map +1 -1
  25. package/dist/types/types/index.d.ts +1 -1
  26. package/dist/types/types/index.d.ts.map +1 -1
  27. package/dist/types/types/invoice-generation.d.ts +17 -0
  28. package/dist/types/types/invoice-generation.d.ts.map +1 -0
  29. package/dist/types/utils/index.d.ts +0 -2
  30. package/dist/types/utils/index.d.ts.map +1 -1
  31. package/package.json +1 -1
  32. package/src/enums/common.ts +1 -0
  33. package/src/enums/index.ts +0 -1
  34. package/src/types/index.ts +1 -1
  35. package/src/types/invoice-generation.ts +19 -0
  36. package/src/utils/index.ts +0 -2
  37. package/dist/cjs/enums/usage-compaction.js +0 -35
  38. package/dist/cjs/enums/usage-compaction.js.map +0 -1
  39. package/dist/cjs/types/usage-compaction.js.map +0 -1
  40. package/dist/cjs/utils/concurrency-limiter.js +0 -88
  41. package/dist/cjs/utils/concurrency-limiter.js.map +0 -1
  42. package/dist/cjs/utils/exact-numeric.constants.js +0 -9
  43. package/dist/cjs/utils/exact-numeric.constants.js.map +0 -1
  44. package/dist/cjs/utils/exact-numeric.js +0 -164
  45. package/dist/cjs/utils/exact-numeric.js.map +0 -1
  46. package/dist/cjs/utils/exact-numeric.types.js +0 -3
  47. package/dist/cjs/utils/exact-numeric.types.js.map +0 -1
  48. package/dist/esm/enums/usage-compaction.js +0 -32
  49. package/dist/esm/enums/usage-compaction.js.map +0 -1
  50. package/dist/esm/types/usage-compaction.js +0 -2
  51. package/dist/esm/types/usage-compaction.js.map +0 -1
  52. package/dist/esm/utils/concurrency-limiter.js +0 -31
  53. package/dist/esm/utils/concurrency-limiter.js.map +0 -1
  54. package/dist/esm/utils/exact-numeric.constants.js +0 -6
  55. package/dist/esm/utils/exact-numeric.constants.js.map +0 -1
  56. package/dist/esm/utils/exact-numeric.js +0 -158
  57. package/dist/esm/utils/exact-numeric.js.map +0 -1
  58. package/dist/esm/utils/exact-numeric.types.js +0 -2
  59. package/dist/esm/utils/exact-numeric.types.js.map +0 -1
  60. package/dist/types/enums/usage-compaction.d.ts +0 -28
  61. package/dist/types/enums/usage-compaction.d.ts.map +0 -1
  62. package/dist/types/types/usage-compaction.d.ts +0 -14
  63. package/dist/types/types/usage-compaction.d.ts.map +0 -1
  64. package/dist/types/utils/concurrency-limiter.d.ts +0 -2
  65. package/dist/types/utils/concurrency-limiter.d.ts.map +0 -1
  66. package/dist/types/utils/exact-numeric.constants.d.ts +0 -4
  67. package/dist/types/utils/exact-numeric.constants.d.ts.map +0 -1
  68. package/dist/types/utils/exact-numeric.d.ts +0 -6
  69. package/dist/types/utils/exact-numeric.d.ts.map +0 -1
  70. package/dist/types/utils/exact-numeric.types.d.ts +0 -6
  71. package/dist/types/utils/exact-numeric.types.d.ts.map +0 -1
  72. package/src/enums/usage-compaction.ts +0 -30
  73. package/src/types/usage-compaction.ts +0 -16
  74. package/src/utils/concurrency-limiter.ts +0 -32
  75. package/src/utils/exact-numeric.constants.ts +0 -5
  76. package/src/utils/exact-numeric.ts +0 -199
  77. package/src/utils/exact-numeric.types.ts +0 -5
@@ -1,199 +0,0 @@
1
- import type { ExactNumeric } from '../types/usage-compaction';
2
-
3
- import {
4
- EXACT_NUMERIC_MAX_FRACTIONAL_DIGITS,
5
- EXACT_NUMERIC_MAX_INPUT_LENGTH,
6
- EXACT_NUMERIC_MAX_INTEGER_DIGITS,
7
- } from './exact-numeric.constants';
8
- import type { ParsedExactNumeric } from './exact-numeric.types';
9
-
10
- export function toExactNumeric(value: number | string): ExactNumeric {
11
- if (typeof value === 'number' && !Number.isFinite(value)) {
12
- throw new Error('Exact numeric value must be finite');
13
- }
14
-
15
- const input = String(value).trim();
16
- if (!input || input.length > EXACT_NUMERIC_MAX_INPUT_LENGTH) {
17
- throw new Error('Exact numeric value is invalid');
18
- }
19
-
20
- const match = /^([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(input);
21
- if (!match || (!match[2] && !match[3])) {
22
- throw new Error('Exact numeric value is invalid');
23
- }
24
-
25
- const sign = match[1] === '-' ? '-' : '';
26
- const integerPart = match[2] ?? '';
27
- const fractionalPart = match[3] ?? '';
28
- const digits = `${integerPart}${fractionalPart}`;
29
-
30
- if (/^0+$/.test(digits)) {
31
- return '0';
32
- }
33
-
34
- const exponentText = match[4] ?? '0';
35
- const normalizedExponentText = exponentText.replace(/^([+-]?)0+(?=\d)/, '$1');
36
- if (normalizedExponentText.replace(/^[+-]/, '').length > 6) {
37
- throw new Error('Exact numeric value exceeds PostgreSQL NUMERIC limits');
38
- }
39
- const exponent = Number(normalizedExponentText);
40
- const maximumShift = EXACT_NUMERIC_MAX_INTEGER_DIGITS
41
- + EXACT_NUMERIC_MAX_FRACTIONAL_DIGITS;
42
- if (exponent > maximumShift || exponent < -maximumShift) {
43
- throw new Error('Exact numeric value exceeds PostgreSQL NUMERIC limits');
44
- }
45
-
46
- const decimalIndex = integerPart.length + exponent;
47
- let expanded: string;
48
-
49
- if (decimalIndex <= 0) {
50
- expanded = `0.${'0'.repeat(-decimalIndex)}${digits}`;
51
- } else if (decimalIndex >= digits.length) {
52
- expanded = `${digits}${'0'.repeat(decimalIndex - digits.length)}`;
53
- } else {
54
- expanded = `${digits.slice(0, decimalIndex)}.${digits.slice(decimalIndex)}`;
55
- }
56
-
57
- const [expandedIntegerPart, expandedFractionalPart = ''] = expanded.split('.');
58
- const normalizedIntegerPart = expandedIntegerPart.replace(/^0+(?=\d)/, '');
59
- const normalizedFractionalPart = expandedFractionalPart.replace(/0+$/, '');
60
-
61
- if (normalizedIntegerPart.length > EXACT_NUMERIC_MAX_INTEGER_DIGITS
62
- || normalizedFractionalPart.length > EXACT_NUMERIC_MAX_FRACTIONAL_DIGITS) {
63
- throw new Error('Exact numeric value exceeds PostgreSQL NUMERIC limits');
64
- }
65
-
66
- const normalized = normalizedFractionalPart
67
- ? `${normalizedIntegerPart}.${normalizedFractionalPart}`
68
- : normalizedIntegerPart;
69
-
70
- return `${sign}${normalized}`;
71
- }
72
-
73
- export function addExactNumeric(
74
- left: ExactNumeric,
75
- right: ExactNumeric,
76
- ): ExactNumeric {
77
- const leftValue = parseExactNumeric(left);
78
- const rightValue = parseExactNumeric(right);
79
- const scale = Math.max(leftValue.scale, rightValue.scale);
80
- const leftDigits = alignScale(leftValue, scale);
81
- const rightDigits = alignScale(rightValue, scale);
82
-
83
- if (leftValue.sign === 0) return formatExactNumeric(rightDigits, scale, rightValue.sign);
84
- if (rightValue.sign === 0) return formatExactNumeric(leftDigits, scale, leftValue.sign);
85
-
86
- if (leftValue.sign === rightValue.sign) {
87
- return formatExactNumeric(addUnsigned(leftDigits, rightDigits), scale, leftValue.sign);
88
- }
89
-
90
- const comparison = compareUnsigned(leftDigits, rightDigits);
91
- if (comparison === 0) return '0';
92
-
93
- return comparison > 0
94
- ? formatExactNumeric(subtractUnsigned(leftDigits, rightDigits), scale, leftValue.sign)
95
- : formatExactNumeric(subtractUnsigned(rightDigits, leftDigits), scale, rightValue.sign);
96
- }
97
-
98
- export function sumExactNumeric(values: readonly ExactNumeric[]): ExactNumeric {
99
- return values.reduce<ExactNumeric>(addExactNumeric, '0');
100
- }
101
-
102
- export function compareExactNumeric(left: ExactNumeric, right: ExactNumeric): number {
103
- const leftValue = parseExactNumeric(left);
104
- const rightValue = parseExactNumeric(right);
105
-
106
- if (leftValue.sign !== rightValue.sign) {
107
- return leftValue.sign < rightValue.sign ? -1 : 1;
108
- }
109
- if (leftValue.sign === 0) return 0;
110
-
111
- const scale = Math.max(leftValue.scale, rightValue.scale);
112
- const comparison = compareUnsigned(
113
- alignScale(leftValue, scale),
114
- alignScale(rightValue, scale),
115
- );
116
-
117
- return leftValue.sign === 1 ? comparison : -comparison;
118
- }
119
-
120
- function parseExactNumeric(value: ExactNumeric): ParsedExactNumeric {
121
- const canonical = toExactNumeric(value);
122
- const isNegative = canonical.startsWith('-');
123
- const unsigned = isNegative ? canonical.slice(1) : canonical;
124
- const [integerPart, fractionalPart = ''] = unsigned.split('.');
125
- const digits = `${integerPart}${fractionalPart}`.replace(/^0+/, '') || '0';
126
-
127
- return {
128
- digits,
129
- scale: fractionalPart.length,
130
- sign: digits === '0' ? 0 : isNegative ? -1 : 1,
131
- };
132
- }
133
-
134
- function alignScale(value: ParsedExactNumeric, scale: number): string {
135
- return `${value.digits}${'0'.repeat(scale - value.scale)}`;
136
- }
137
-
138
- function formatExactNumeric(digits: string, scale: number, sign: -1 | 0 | 1): ExactNumeric {
139
- const normalizedDigits = digits.replace(/^0+/, '') || '0';
140
- if (normalizedDigits === '0' || sign === 0) return '0';
141
-
142
- const signPrefix = sign === -1 ? '-' : '';
143
- if (scale === 0) return `${signPrefix}${normalizedDigits}`;
144
-
145
- const paddedDigits = normalizedDigits.padStart(scale + 1, '0');
146
- const decimalIndex = paddedDigits.length - scale;
147
-
148
- return toExactNumeric(
149
- `${signPrefix}${paddedDigits.slice(0, decimalIndex)}.${paddedDigits.slice(decimalIndex)}`,
150
- );
151
- }
152
-
153
- function compareUnsigned(left: string, right: string): number {
154
- const normalizedLeft = left.replace(/^0+/, '') || '0';
155
- const normalizedRight = right.replace(/^0+/, '') || '0';
156
-
157
- if (normalizedLeft.length !== normalizedRight.length) {
158
- return normalizedLeft.length < normalizedRight.length ? -1 : 1;
159
- }
160
- if (normalizedLeft === normalizedRight) return 0;
161
-
162
- return normalizedLeft < normalizedRight ? -1 : 1;
163
- }
164
-
165
- function addUnsigned(left: string, right: string): string {
166
- const length = Math.max(left.length, right.length);
167
- const leftDigits = left.padStart(length, '0');
168
- const rightDigits = right.padStart(length, '0');
169
- const result = new Array<string>(length + 1).fill('0');
170
- let carry = 0;
171
-
172
- for (let index = length - 1;index >= 0;index -= 1) {
173
- const sum = Number(leftDigits[index]) + Number(rightDigits[index]) + carry;
174
- result[index + 1] = String(sum % 10);
175
- carry = Math.floor(sum / 10);
176
- }
177
- result[0] = String(carry);
178
-
179
- return result.join('').replace(/^0+/, '') || '0';
180
- }
181
-
182
- function subtractUnsigned(left: string, right: string): string {
183
- const rightDigits = right.padStart(left.length, '0');
184
- const result = new Array<string>(left.length).fill('0');
185
- let borrow = 0;
186
-
187
- for (let index = left.length - 1;index >= 0;index -= 1) {
188
- let difference = Number(left[index]) - Number(rightDigits[index]) - borrow;
189
- if (difference < 0) {
190
- difference += 10;
191
- borrow = 1;
192
- } else {
193
- borrow = 0;
194
- }
195
- result[index] = String(difference);
196
- }
197
-
198
- return result.join('').replace(/^0+/, '') || '0';
199
- }
@@ -1,5 +0,0 @@
1
- export interface ParsedExactNumeric {
2
- readonly digits: string;
3
- readonly scale: number;
4
- readonly sign: -1 | 0 | 1;
5
- }