@obinexusmk2/hypernum 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +355 -256
  3. package/dist/index.cjs +4 -8
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +4 -7
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.umd.js +2 -2
  9. package/dist/index.umd.js.map +1 -1
  10. package/dist/types/{config → src/config}/config-loader.d.ts +0 -0
  11. package/dist/types/src/config/config-loader.d.ts.map +1 -0
  12. package/dist/types/{config → src/config}/config-parser.d.ts +0 -0
  13. package/dist/types/src/config/config-parser.d.ts.map +1 -0
  14. package/dist/types/{config → src/config}/config-resolver.d.ts +0 -0
  15. package/dist/types/src/config/config-resolver.d.ts.map +1 -0
  16. package/dist/types/{config → src/config}/config-source.d.ts +0 -0
  17. package/dist/types/src/config/config-source.d.ts.map +1 -0
  18. package/dist/types/{config → src/config}/index.d.ts +0 -0
  19. package/dist/types/src/config/index.d.ts.map +1 -0
  20. package/dist/types/{core → src/core}/common.d.ts +0 -0
  21. package/dist/types/src/core/common.d.ts.map +1 -0
  22. package/dist/types/{core → src/core}/config.d.ts +0 -0
  23. package/dist/types/src/core/config.d.ts.map +1 -0
  24. package/dist/types/{core → src/core}/constants.d.ts +0 -0
  25. package/dist/types/src/core/constants.d.ts.map +1 -0
  26. package/dist/types/{core → src/core}/errors.d.ts +0 -0
  27. package/dist/types/src/core/errors.d.ts.map +1 -0
  28. package/dist/types/{core → src/core}/hypernum.d.ts +0 -0
  29. package/dist/types/src/core/hypernum.d.ts.map +1 -0
  30. package/dist/types/{core → src/core}/index.d.ts +0 -0
  31. package/dist/types/src/core/index.d.ts.map +1 -0
  32. package/dist/types/{index.d.ts → src/index.d.ts} +1 -1
  33. package/dist/types/src/index.d.ts.map +1 -0
  34. package/dist/types/{operations → src/operations}/arithmetic.d.ts +0 -0
  35. package/dist/types/src/operations/arithmetic.d.ts.map +1 -0
  36. package/dist/types/{operations → src/operations}/bitwise.d.ts +0 -0
  37. package/dist/types/src/operations/bitwise.d.ts.map +1 -0
  38. package/dist/types/{operations → src/operations}/comparison.d.ts +0 -0
  39. package/dist/types/src/operations/comparison.d.ts.map +1 -0
  40. package/dist/types/{operations → src/operations}/conversion.d.ts +0 -0
  41. package/dist/types/src/operations/conversion.d.ts.map +1 -0
  42. package/dist/types/{operations → src/operations}/factorial.d.ts +0 -0
  43. package/dist/types/src/operations/factorial.d.ts.map +1 -0
  44. package/dist/types/{operations → src/operations}/index.d.ts +0 -0
  45. package/dist/types/src/operations/index.d.ts.map +1 -0
  46. package/dist/types/{operations → src/operations}/power.d.ts +0 -0
  47. package/dist/types/src/operations/power.d.ts.map +1 -0
  48. package/dist/types/{storage → src/storage}/Heap.d.ts +0 -0
  49. package/dist/types/src/storage/Heap.d.ts.map +1 -0
  50. package/dist/types/{storage → src/storage}/index.d.ts +0 -0
  51. package/dist/types/src/storage/index.d.ts.map +1 -0
  52. package/dist/types/{structures → src/structures}/ackermann.d.ts +0 -0
  53. package/dist/types/src/structures/ackermann.d.ts.map +1 -0
  54. package/dist/types/{structures → src/structures}/big-array.d.ts +0 -0
  55. package/dist/types/src/structures/big-array.d.ts.map +1 -0
  56. package/dist/types/{structures → src/structures}/index.d.ts +0 -0
  57. package/dist/types/src/structures/index.d.ts.map +1 -0
  58. package/dist/types/{structures → src/structures}/number-tree.d.ts +0 -0
  59. package/dist/types/src/structures/number-tree.d.ts.map +1 -0
  60. package/dist/types/{structures → src/structures}/power-tower.d.ts +0 -0
  61. package/dist/types/src/structures/power-tower.d.ts.map +1 -0
  62. package/dist/types/{utils → src/utils}/formatting.d.ts +0 -0
  63. package/dist/types/src/utils/formatting.d.ts.map +1 -0
  64. package/dist/types/{utils → src/utils}/index.d.ts +0 -0
  65. package/dist/types/src/utils/index.d.ts.map +1 -0
  66. package/dist/types/{utils → src/utils}/parser.d.ts +0 -0
  67. package/dist/types/src/utils/parser.d.ts.map +1 -0
  68. package/dist/types/{utils → src/utils}/precision.d.ts +0 -0
  69. package/dist/types/src/utils/precision.d.ts.map +1 -0
  70. package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
  71. package/dist/types/src/utils/validation.d.ts.map +1 -0
  72. package/package.json +169 -164
  73. package/rollup.config.js +163 -161
  74. package/src/cli/hypernum.js +272 -0
  75. package/src/config/config-loader.ts +0 -0
  76. package/src/config/config-parser.ts +160 -160
  77. package/src/config/config-resolver.ts +0 -0
  78. package/src/config/config-source.ts +0 -0
  79. package/src/config/index.ts +0 -0
  80. package/src/core/common.ts +184 -184
  81. package/src/core/config.ts +392 -392
  82. package/src/core/constants.ts +101 -101
  83. package/src/core/errors.ts +202 -202
  84. package/src/core/hypernum.ts +240 -240
  85. package/src/core/index.ts +4 -4
  86. package/src/index.ts +179 -182
  87. package/src/operations/arithmetic.ts +332 -332
  88. package/src/operations/bitwise.ts +366 -366
  89. package/src/operations/comparison.ts +271 -271
  90. package/src/operations/conversion.ts +399 -399
  91. package/src/operations/factorial.ts +278 -278
  92. package/src/operations/index.ts +4 -4
  93. package/src/operations/power.ts +315 -315
  94. package/src/storage/Heap.ts +237 -237
  95. package/src/storage/index.ts +0 -0
  96. package/src/structures/ackermann.ts +232 -232
  97. package/src/structures/big-array.ts +305 -305
  98. package/src/structures/index.ts +3 -3
  99. package/src/structures/number-tree.ts +403 -403
  100. package/src/structures/power-tower.ts +277 -277
  101. package/src/types/common.d.ts +356 -356
  102. package/src/types/core.d.ts +160 -160
  103. package/src/types/index.d.ts +1 -1
  104. package/src/utils/formatting.ts +245 -245
  105. package/src/utils/index.ts +4 -4
  106. package/src/utils/parser.ts +244 -244
  107. package/src/utils/precision.ts +216 -216
  108. package/src/utils/validation.ts +182 -182
  109. package/tsconfig.json +83 -83
  110. package/dist/types/config/config-loader.d.ts.map +0 -1
  111. package/dist/types/config/config-parser.d.ts.map +0 -1
  112. package/dist/types/config/config-resolver.d.ts.map +0 -1
  113. package/dist/types/config/config-source.d.ts.map +0 -1
  114. package/dist/types/config/index.d.ts.map +0 -1
  115. package/dist/types/core/common.d.ts.map +0 -1
  116. package/dist/types/core/config.d.ts.map +0 -1
  117. package/dist/types/core/constants.d.ts.map +0 -1
  118. package/dist/types/core/errors.d.ts.map +0 -1
  119. package/dist/types/core/hypernum.d.ts.map +0 -1
  120. package/dist/types/core/index.d.ts.map +0 -1
  121. package/dist/types/index.d.ts.map +0 -1
  122. package/dist/types/operations/arithmetic.d.ts.map +0 -1
  123. package/dist/types/operations/bitwise.d.ts.map +0 -1
  124. package/dist/types/operations/comparison.d.ts.map +0 -1
  125. package/dist/types/operations/conversion.d.ts.map +0 -1
  126. package/dist/types/operations/factorial.d.ts.map +0 -1
  127. package/dist/types/operations/index.d.ts.map +0 -1
  128. package/dist/types/operations/power.d.ts.map +0 -1
  129. package/dist/types/storage/Heap.d.ts.map +0 -1
  130. package/dist/types/storage/index.d.ts.map +0 -1
  131. package/dist/types/structures/ackermann.d.ts.map +0 -1
  132. package/dist/types/structures/big-array.d.ts.map +0 -1
  133. package/dist/types/structures/index.d.ts.map +0 -1
  134. package/dist/types/structures/number-tree.d.ts.map +0 -1
  135. package/dist/types/structures/power-tower.d.ts.map +0 -1
  136. package/dist/types/utils/formatting.d.ts.map +0 -1
  137. package/dist/types/utils/index.d.ts.map +0 -1
  138. package/dist/types/utils/parser.d.ts.map +0 -1
  139. package/dist/types/utils/precision.d.ts.map +0 -1
  140. package/dist/types/utils/validation.d.ts.map +0 -1
@@ -1,102 +1,102 @@
1
- /**
2
- * Core constants for Hypernum library
3
- * Defines fundamental values and limits used across the library
4
- */
5
-
6
- // Numerical limits
7
- export const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
8
- export const MIN_SAFE_INTEGER = BigInt(Number.MIN_SAFE_INTEGER);
9
- export const MAX_PRECISION = 100;
10
- export const MAX_COMPUTATION_STEPS = 1000;
11
- export const MAX_BITS = 1024;
12
-
13
- // Commonly used values
14
- export const ZERO = BigInt(0);
15
- export const ONE = BigInt(1);
16
- export const TWO = BigInt(2);
17
- export const TEN = BigInt(10);
18
- export const NEGATIVE_ONE = BigInt(-1);
19
-
20
- // Power operation limits
21
- export const MAX_POWER_BASE = BigInt(2) ** BigInt(53);
22
- export const MAX_POWER_EXPONENT = BigInt(1000);
23
- export const MAX_TETRATION_HEIGHT = BigInt(4);
24
- export const MAX_FACTORIAL_INPUT = BigInt(1000);
25
-
26
- // Tree and heap configuration
27
- export const DEFAULT_TREE_MAX_DEPTH = 1000;
28
- export const DEFAULT_HEAP_INITIAL_CAPACITY = 16;
29
- export const DEFAULT_ARRAY_GROWTH_FACTOR = 2;
30
- export const MIN_ARRAY_CAPACITY = 16;
31
-
32
- // Formatting configuration
33
- export const DEFAULT_DECIMAL_SEPARATOR = '.';
34
- export const DEFAULT_GROUP_SEPARATOR = ',';
35
- export const DEFAULT_GROUP_SIZE = 3;
36
- export const MAX_GROUP_SIZE = 10;
37
-
38
- // Roman numeral limits
39
- export const MIN_ROMAN_VALUE = 1;
40
- export const MAX_ROMAN_VALUE = 3999;
41
-
42
- // Ackermann function limits
43
- export const MAX_ACKERMANN_M = 4;
44
- export const MAX_ACKERMANN_N = 1000;
45
-
46
- // Cache configuration
47
- export const DEFAULT_CACHE_SIZE = 1000;
48
- export const MAX_CACHE_SIZE = 10000;
49
-
50
- // Error messages
51
- export const ERROR_MESSAGES = {
52
- OVERFLOW: 'Operation would result in overflow',
53
- UNDERFLOW: 'Operation would result in underflow',
54
- NEGATIVE_ROOT: 'Cannot compute root of negative number',
55
- NEGATIVE_EXPONENT: 'Negative exponents not supported for integers',
56
- DIVISION_BY_ZERO: 'Division by zero',
57
- INVALID_PRECISION: 'Precision must be non-negative and not exceed MAX_PRECISION',
58
- INVALID_BASE: 'Base must be a positive integer',
59
- INVALID_ROMAN: 'Invalid Roman numeral',
60
- COMPUTATION_LIMIT: 'Computation exceeded maximum allowed steps',
61
- NEGATIVE_INDEX: 'Array index cannot be negative',
62
- TREE_DEPTH_EXCEEDED: 'Maximum tree depth exceeded',
63
- INVALID_HEAP_PROPERTY: 'Heap property violation detected'
64
- } as const;
65
-
66
- // Feature flags for optional functionality
67
- export const FEATURES = {
68
- OVERFLOW_CHECKING: true,
69
- AUTOMATIC_PRECISION: true,
70
- MEMOIZATION: true,
71
- TREE_BALANCING: true,
72
- DEBUG_MODE: false
73
- } as const;
74
-
75
- // Default options for various operations
76
- export const DEFAULT_OPTIONS = {
77
- precision: 0,
78
- roundingMode: 'HALF_EVEN',
79
- checkOverflow: true,
80
- maxSteps: MAX_COMPUTATION_STEPS,
81
- grouping: true,
82
- uppercase: false,
83
- cache: true
84
- } as const;
85
-
86
- // Units for number formatting (powers of 1000)
87
- export const NUMBER_UNITS = [
88
- { value: 1n, symbol: '' },
89
- { value: 1000n, symbol: 'K' },
90
- { value: 1000000n, symbol: 'M' },
91
- { value: 1000000000n, symbol: 'B' },
92
- { value: 1000000000000n, symbol: 'T' },
93
- { value: 1000000000000000n, symbol: 'Q' }
94
- ] as const;
95
-
96
- // Performance monitoring thresholds
97
- export const PERFORMANCE = {
98
- WARN_THRESHOLD_MS: 100,
99
- ERROR_THRESHOLD_MS: 1000,
100
- MAX_ARRAY_SIZE: 1000000,
101
- MAX_TREE_SIZE: 1000000
1
+ /**
2
+ * Core constants for Hypernum library
3
+ * Defines fundamental values and limits used across the library
4
+ */
5
+
6
+ // Numerical limits
7
+ export const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
8
+ export const MIN_SAFE_INTEGER = BigInt(Number.MIN_SAFE_INTEGER);
9
+ export const MAX_PRECISION = 100;
10
+ export const MAX_COMPUTATION_STEPS = 1000;
11
+ export const MAX_BITS = 1024;
12
+
13
+ // Commonly used values
14
+ export const ZERO = BigInt(0);
15
+ export const ONE = BigInt(1);
16
+ export const TWO = BigInt(2);
17
+ export const TEN = BigInt(10);
18
+ export const NEGATIVE_ONE = BigInt(-1);
19
+
20
+ // Power operation limits
21
+ export const MAX_POWER_BASE = BigInt(2) ** BigInt(53);
22
+ export const MAX_POWER_EXPONENT = BigInt(1000);
23
+ export const MAX_TETRATION_HEIGHT = BigInt(4);
24
+ export const MAX_FACTORIAL_INPUT = BigInt(1000);
25
+
26
+ // Tree and heap configuration
27
+ export const DEFAULT_TREE_MAX_DEPTH = 1000;
28
+ export const DEFAULT_HEAP_INITIAL_CAPACITY = 16;
29
+ export const DEFAULT_ARRAY_GROWTH_FACTOR = 2;
30
+ export const MIN_ARRAY_CAPACITY = 16;
31
+
32
+ // Formatting configuration
33
+ export const DEFAULT_DECIMAL_SEPARATOR = '.';
34
+ export const DEFAULT_GROUP_SEPARATOR = ',';
35
+ export const DEFAULT_GROUP_SIZE = 3;
36
+ export const MAX_GROUP_SIZE = 10;
37
+
38
+ // Roman numeral limits
39
+ export const MIN_ROMAN_VALUE = 1;
40
+ export const MAX_ROMAN_VALUE = 3999;
41
+
42
+ // Ackermann function limits
43
+ export const MAX_ACKERMANN_M = 4;
44
+ export const MAX_ACKERMANN_N = 1000;
45
+
46
+ // Cache configuration
47
+ export const DEFAULT_CACHE_SIZE = 1000;
48
+ export const MAX_CACHE_SIZE = 10000;
49
+
50
+ // Error messages
51
+ export const ERROR_MESSAGES = {
52
+ OVERFLOW: 'Operation would result in overflow',
53
+ UNDERFLOW: 'Operation would result in underflow',
54
+ NEGATIVE_ROOT: 'Cannot compute root of negative number',
55
+ NEGATIVE_EXPONENT: 'Negative exponents not supported for integers',
56
+ DIVISION_BY_ZERO: 'Division by zero',
57
+ INVALID_PRECISION: 'Precision must be non-negative and not exceed MAX_PRECISION',
58
+ INVALID_BASE: 'Base must be a positive integer',
59
+ INVALID_ROMAN: 'Invalid Roman numeral',
60
+ COMPUTATION_LIMIT: 'Computation exceeded maximum allowed steps',
61
+ NEGATIVE_INDEX: 'Array index cannot be negative',
62
+ TREE_DEPTH_EXCEEDED: 'Maximum tree depth exceeded',
63
+ INVALID_HEAP_PROPERTY: 'Heap property violation detected'
64
+ } as const;
65
+
66
+ // Feature flags for optional functionality
67
+ export const FEATURES = {
68
+ OVERFLOW_CHECKING: true,
69
+ AUTOMATIC_PRECISION: true,
70
+ MEMOIZATION: true,
71
+ TREE_BALANCING: true,
72
+ DEBUG_MODE: false
73
+ } as const;
74
+
75
+ // Default options for various operations
76
+ export const DEFAULT_OPTIONS = {
77
+ precision: 0,
78
+ roundingMode: 'HALF_EVEN',
79
+ checkOverflow: true,
80
+ maxSteps: MAX_COMPUTATION_STEPS,
81
+ grouping: true,
82
+ uppercase: false,
83
+ cache: true
84
+ } as const;
85
+
86
+ // Units for number formatting (powers of 1000)
87
+ export const NUMBER_UNITS = [
88
+ { value: 1n, symbol: '' },
89
+ { value: 1000n, symbol: 'K' },
90
+ { value: 1000000n, symbol: 'M' },
91
+ { value: 1000000000n, symbol: 'B' },
92
+ { value: 1000000000000n, symbol: 'T' },
93
+ { value: 1000000000000000n, symbol: 'Q' }
94
+ ] as const;
95
+
96
+ // Performance monitoring thresholds
97
+ export const PERFORMANCE = {
98
+ WARN_THRESHOLD_MS: 100,
99
+ ERROR_THRESHOLD_MS: 1000,
100
+ MAX_ARRAY_SIZE: 1000000,
101
+ MAX_TREE_SIZE: 1000000
102
102
  } as const;
@@ -1,203 +1,203 @@
1
- /**
2
- * Custom error types for Hypernum library
3
- * Provides specific error classes for different types of errors that can occur
4
- * during mathematical operations and data structure manipulations
5
- */
6
-
7
- import { ERROR_MESSAGES } from './constants';
8
-
9
- /**
10
- * Base error class for Hypernum library
11
- * All other error classes inherit from this
12
- */
13
- export class HypernumError extends Error {
14
- constructor(message: string) {
15
- super(message);
16
- this.name = 'HypernumError';
17
- Object.setPrototypeOf(this, HypernumError.prototype);
18
- }
19
- }
20
-
21
- /**
22
- * Error for validation failures
23
- */
24
- export class ValidationError extends HypernumError {
25
- constructor(message: string) {
26
- super(message);
27
- this.name = 'ValidationError';
28
- Object.setPrototypeOf(this, ValidationError.prototype);
29
- }
30
- }
31
-
32
- /**
33
- * Error for arithmetic overflow conditions
34
- */
35
- export class OverflowError extends HypernumError {
36
- constructor(message: string = ERROR_MESSAGES.OVERFLOW) {
37
- super(message);
38
- this.name = 'OverflowError';
39
- Object.setPrototypeOf(this, OverflowError.prototype);
40
- }
41
- }
42
-
43
- /**
44
- * Error for arithmetic underflow conditions
45
- */
46
- export class UnderflowError extends HypernumError {
47
- constructor(message: string = ERROR_MESSAGES.UNDERFLOW) {
48
- super(message);
49
- this.name = 'UnderflowError';
50
- Object.setPrototypeOf(this, UnderflowError.prototype);
51
- }
52
- }
53
-
54
- /**
55
- * Error for division by zero
56
- */
57
- export class DivisionByZeroError extends HypernumError {
58
- constructor(message: string = ERROR_MESSAGES.DIVISION_BY_ZERO) {
59
- super(message);
60
- this.name = 'DivisionByZeroError';
61
- Object.setPrototypeOf(this, DivisionByZeroError.prototype);
62
- }
63
- }
64
-
65
- /**
66
- * Error for precision-related issues
67
- */
68
- export class PrecisionError extends HypernumError {
69
- constructor(message: string = ERROR_MESSAGES.INVALID_PRECISION) {
70
- super(message);
71
- this.name = 'PrecisionError';
72
- Object.setPrototypeOf(this, PrecisionError.prototype);
73
- }
74
- }
75
-
76
- /**
77
- * Error for computation limits exceeded
78
- */
79
- export class ComputationLimitError extends HypernumError {
80
- constructor(message: string = ERROR_MESSAGES.COMPUTATION_LIMIT) {
81
- super(message);
82
- this.name = 'ComputationLimitError';
83
- Object.setPrototypeOf(this, ComputationLimitError.prototype);
84
- }
85
- }
86
-
87
- /**
88
- * Error for invalid operations on data structures
89
- */
90
- export class DataStructureError extends HypernumError {
91
- constructor(message: string) {
92
- super(message);
93
- this.name = 'DataStructureError';
94
- Object.setPrototypeOf(this, DataStructureError.prototype);
95
- }
96
- }
97
-
98
- /**
99
- * Error for heap property violations
100
- */
101
- export class HeapPropertyError extends DataStructureError {
102
- constructor(message: string = ERROR_MESSAGES.INVALID_HEAP_PROPERTY) {
103
- super(message);
104
- this.name = 'HeapPropertyError';
105
- Object.setPrototypeOf(this, HeapPropertyError.prototype);
106
- }
107
- }
108
-
109
- /**
110
- * Error for tree-related issues
111
- */
112
- export class TreeError extends DataStructureError {
113
- constructor(message: string = ERROR_MESSAGES.TREE_DEPTH_EXCEEDED) {
114
- super(message);
115
- this.name = 'TreeError';
116
- Object.setPrototypeOf(this, TreeError.prototype);
117
- }
118
- }
119
-
120
- /**
121
- * Error for array index out of bounds
122
- */
123
- export class IndexError extends DataStructureError {
124
- constructor(message: string = ERROR_MESSAGES.NEGATIVE_INDEX) {
125
- super(message);
126
- this.name = 'IndexError';
127
- Object.setPrototypeOf(this, IndexError.prototype);
128
- }
129
- }
130
-
131
- /**
132
- * Error for invalid number format or conversion
133
- */
134
- export class FormatError extends HypernumError {
135
- constructor(message: string) {
136
- super(message);
137
- this.name = 'FormatError';
138
- Object.setPrototypeOf(this, FormatError.prototype);
139
- }
140
- }
141
-
142
- /**
143
- * Error for invalid Roman numeral operations
144
- */
145
- export class RomanNumeralError extends FormatError {
146
- constructor(message: string = ERROR_MESSAGES.INVALID_ROMAN) {
147
- super(message);
148
- this.name = 'RomanNumeralError';
149
- Object.setPrototypeOf(this, RomanNumeralError.prototype);
150
- }
151
- }
152
-
153
- /**
154
- * Type guard to check if an error is a Hypernum error
155
- */
156
- export function isHypernumError(error: unknown): error is HypernumError {
157
- return error instanceof HypernumError;
158
- }
159
-
160
- /**
161
- * Helper function to wrap unknown errors into HypernumError
162
- */
163
- export function wrapError(error: unknown): HypernumError {
164
- if (isHypernumError(error)) {
165
- return error;
166
- }
167
- if (error instanceof Error) {
168
- return new HypernumError(error.message);
169
- }
170
- return new HypernumError('An unknown error occurred');
171
- }
172
-
173
- /**
174
- * Helper function to create an appropriate error from a message and optional type
175
- */
176
- export function createError(message: string, type?: string): HypernumError {
177
- switch (type) {
178
- case 'validation':
179
- return new ValidationError(message);
180
- case 'overflow':
181
- return new OverflowError(message);
182
- case 'underflow':
183
- return new UnderflowError(message);
184
- case 'division':
185
- return new DivisionByZeroError(message);
186
- case 'precision':
187
- return new PrecisionError(message);
188
- case 'computation':
189
- return new ComputationLimitError(message);
190
- case 'heap':
191
- return new HeapPropertyError(message);
192
- case 'tree':
193
- return new TreeError(message);
194
- case 'index':
195
- return new IndexError(message);
196
- case 'format':
197
- return new FormatError(message);
198
- case 'roman':
199
- return new RomanNumeralError(message);
200
- default:
201
- return new HypernumError(message);
202
- }
1
+ /**
2
+ * Custom error types for Hypernum library
3
+ * Provides specific error classes for different types of errors that can occur
4
+ * during mathematical operations and data structure manipulations
5
+ */
6
+
7
+ import { ERROR_MESSAGES } from './constants';
8
+
9
+ /**
10
+ * Base error class for Hypernum library
11
+ * All other error classes inherit from this
12
+ */
13
+ export class HypernumError extends Error {
14
+ constructor(message: string) {
15
+ super(message);
16
+ this.name = 'HypernumError';
17
+ Object.setPrototypeOf(this, HypernumError.prototype);
18
+ }
19
+ }
20
+
21
+ /**
22
+ * Error for validation failures
23
+ */
24
+ export class ValidationError extends HypernumError {
25
+ constructor(message: string) {
26
+ super(message);
27
+ this.name = 'ValidationError';
28
+ Object.setPrototypeOf(this, ValidationError.prototype);
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Error for arithmetic overflow conditions
34
+ */
35
+ export class OverflowError extends HypernumError {
36
+ constructor(message: string = ERROR_MESSAGES.OVERFLOW) {
37
+ super(message);
38
+ this.name = 'OverflowError';
39
+ Object.setPrototypeOf(this, OverflowError.prototype);
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Error for arithmetic underflow conditions
45
+ */
46
+ export class UnderflowError extends HypernumError {
47
+ constructor(message: string = ERROR_MESSAGES.UNDERFLOW) {
48
+ super(message);
49
+ this.name = 'UnderflowError';
50
+ Object.setPrototypeOf(this, UnderflowError.prototype);
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Error for division by zero
56
+ */
57
+ export class DivisionByZeroError extends HypernumError {
58
+ constructor(message: string = ERROR_MESSAGES.DIVISION_BY_ZERO) {
59
+ super(message);
60
+ this.name = 'DivisionByZeroError';
61
+ Object.setPrototypeOf(this, DivisionByZeroError.prototype);
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Error for precision-related issues
67
+ */
68
+ export class PrecisionError extends HypernumError {
69
+ constructor(message: string = ERROR_MESSAGES.INVALID_PRECISION) {
70
+ super(message);
71
+ this.name = 'PrecisionError';
72
+ Object.setPrototypeOf(this, PrecisionError.prototype);
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Error for computation limits exceeded
78
+ */
79
+ export class ComputationLimitError extends HypernumError {
80
+ constructor(message: string = ERROR_MESSAGES.COMPUTATION_LIMIT) {
81
+ super(message);
82
+ this.name = 'ComputationLimitError';
83
+ Object.setPrototypeOf(this, ComputationLimitError.prototype);
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Error for invalid operations on data structures
89
+ */
90
+ export class DataStructureError extends HypernumError {
91
+ constructor(message: string) {
92
+ super(message);
93
+ this.name = 'DataStructureError';
94
+ Object.setPrototypeOf(this, DataStructureError.prototype);
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Error for heap property violations
100
+ */
101
+ export class HeapPropertyError extends DataStructureError {
102
+ constructor(message: string = ERROR_MESSAGES.INVALID_HEAP_PROPERTY) {
103
+ super(message);
104
+ this.name = 'HeapPropertyError';
105
+ Object.setPrototypeOf(this, HeapPropertyError.prototype);
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Error for tree-related issues
111
+ */
112
+ export class TreeError extends DataStructureError {
113
+ constructor(message: string = ERROR_MESSAGES.TREE_DEPTH_EXCEEDED) {
114
+ super(message);
115
+ this.name = 'TreeError';
116
+ Object.setPrototypeOf(this, TreeError.prototype);
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Error for array index out of bounds
122
+ */
123
+ export class IndexError extends DataStructureError {
124
+ constructor(message: string = ERROR_MESSAGES.NEGATIVE_INDEX) {
125
+ super(message);
126
+ this.name = 'IndexError';
127
+ Object.setPrototypeOf(this, IndexError.prototype);
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Error for invalid number format or conversion
133
+ */
134
+ export class FormatError extends HypernumError {
135
+ constructor(message: string) {
136
+ super(message);
137
+ this.name = 'FormatError';
138
+ Object.setPrototypeOf(this, FormatError.prototype);
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Error for invalid Roman numeral operations
144
+ */
145
+ export class RomanNumeralError extends FormatError {
146
+ constructor(message: string = ERROR_MESSAGES.INVALID_ROMAN) {
147
+ super(message);
148
+ this.name = 'RomanNumeralError';
149
+ Object.setPrototypeOf(this, RomanNumeralError.prototype);
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Type guard to check if an error is a Hypernum error
155
+ */
156
+ export function isHypernumError(error: unknown): error is HypernumError {
157
+ return error instanceof HypernumError;
158
+ }
159
+
160
+ /**
161
+ * Helper function to wrap unknown errors into HypernumError
162
+ */
163
+ export function wrapError(error: unknown): HypernumError {
164
+ if (isHypernumError(error)) {
165
+ return error;
166
+ }
167
+ if (error instanceof Error) {
168
+ return new HypernumError(error.message);
169
+ }
170
+ return new HypernumError('An unknown error occurred');
171
+ }
172
+
173
+ /**
174
+ * Helper function to create an appropriate error from a message and optional type
175
+ */
176
+ export function createError(message: string, type?: string): HypernumError {
177
+ switch (type) {
178
+ case 'validation':
179
+ return new ValidationError(message);
180
+ case 'overflow':
181
+ return new OverflowError(message);
182
+ case 'underflow':
183
+ return new UnderflowError(message);
184
+ case 'division':
185
+ return new DivisionByZeroError(message);
186
+ case 'precision':
187
+ return new PrecisionError(message);
188
+ case 'computation':
189
+ return new ComputationLimitError(message);
190
+ case 'heap':
191
+ return new HeapPropertyError(message);
192
+ case 'tree':
193
+ return new TreeError(message);
194
+ case 'index':
195
+ return new IndexError(message);
196
+ case 'format':
197
+ return new FormatError(message);
198
+ case 'roman':
199
+ return new RomanNumeralError(message);
200
+ default:
201
+ return new HypernumError(message);
202
+ }
203
203
  }