Skip to content

Commit 3179956

Browse files
feat: add robust error handling and comprehensive test coverage
BREAKING CHANGE: Methods now throw HyperMathError instead of returning 0 for invalid inputs Major improvements: - Add custom error classes (HyperMathError, DivisionByZeroError) - Implement proper error throwing for all invalid inputs (null, undefined, NaN, Infinity, invalid strings) - Add division by zero protection with descriptive error - Improve input validation with trimming and better type checking - Refactor code with private helper methods (processInput, formatResult) - Add comprehensive JSDoc documentation to all methods Test coverage: - Expand test suite from 4 to 52 tests - Add edge case testing (negative numbers, zero handling, precision) - Add invalid input testing for all methods - Add formatNumber method tests - All tests passing Documentation: - Update README with error handling examples - Add detailed API documentation with parameters, returns, and throws - Document all edge cases and behavior changes - Add support section with GitHub issues link - Correct technical inaccuracies (toFixed vs toPrecision) Package configuration: - Add "types" field pointing to TypeScript definitions - Add "files" field to optimize npm package size - Include only necessary files in package (dist, README, LICENSE) This release transforms the library from a simple utility to a production-ready package with proper error handling, comprehensive testing, and complete documentation.
1 parent 8786e81 commit 3179956

4 files changed

Lines changed: 529 additions & 78 deletions

File tree

README.md

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# HyperMath
22

3-
HyperMath is a simple JavaScript/TypeScript library designed to address common pitfalls in JavaScript math operations. It provides a set of static methods for basic arithmetic operations with improved precision and input handling.
3+
HyperMath is a simple JavaScript/TypeScript library designed to address common pitfalls in JavaScript math operations. It provides a set of static methods for basic arithmetic operations with improved precision and robust error handling.
44

55
![Codecov](https://img.shields.io/codecov/c/github/hyperteksolutions/hypermath)
66
[![Npm package version](https://badgen.net/npm/v/hypermath)](https://npmjs.com/package/hypermath)
@@ -29,37 +29,130 @@ console.log(HyperMath.divide(0.3, 0.1)); // 3 (instead of 2.9999999999999996)
2929

3030
// Subtraction
3131
console.log(HyperMath.subtract(0.3, 0.1)); // 0.2 (instead of 0.19999999999999998)
32+
33+
// Format a number
34+
console.log(HyperMath.formatNumber(1.12345)); // 1.12
35+
36+
// String inputs are supported
37+
console.log(HyperMath.multiply('10.5', '2.5')); // 26.25
38+
```
39+
40+
## Error Handling
41+
42+
The library now throws `HyperMathError` for invalid inputs:
43+
44+
```typescript
45+
import { HyperMath, HyperMathError } from 'hypermath';
46+
47+
try {
48+
HyperMath.add(null, 5); // Throws HyperMathError
49+
} catch (error) {
50+
if (error instanceof HyperMathError) {
51+
console.error(error.message); // "Invalid input provided: null"
52+
}
53+
}
54+
55+
// Invalid string inputs also throw errors
56+
HyperMath.multiply('abc', '123'); // Throws HyperMathError: "Invalid input provided: abc"
57+
58+
// Division by zero throws an error
59+
HyperMath.divide(10, 0); // Throws HyperMathError: "Division by zero is not allowed"
3260
```
3361

3462
## Features
3563

36-
- Handles both number and string inputs
37-
- Attempts to maintain precision up to 2 significant digits
38-
- Provides static methods for multiplication, addition, and division
39-
- Throws an error for invalid string inputs
64+
- **Precision handling**: Maintains precision up to 2 decimal places using `.toFixed(2)`
65+
- **Flexible inputs**: Handles both number and string inputs seamlessly
66+
- **Robust error handling**: Throws descriptive `HyperMathError` exceptions for invalid inputs
67+
- **Edge case protection**: Proper handling of division by zero and invalid operations
68+
- **TypeScript support**: Full TypeScript definitions included
69+
- **Zero dependencies**: Lightweight with no external dependencies
70+
- **Comprehensive tests**: 52+ unit tests ensuring reliability
4071

4172
## API
4273

4374
### `HyperMath.multiply(firstValue: number | string, secondValue: number | string): number`
4475

4576
Multiplies two numbers with improved precision.
4677

78+
**Parameters:**
79+
- `firstValue`: Number or string to multiply
80+
- `secondValue`: Number or string to multiply
81+
82+
**Returns:** The product rounded to 2 decimal places
83+
84+
**Throws:** `HyperMathError` if inputs are invalid (null, undefined, or non-numeric strings)
85+
86+
---
87+
4788
### `HyperMath.add(firstValue: number | string, secondValue: number | string): number`
4889

4990
Adds two numbers with improved precision.
5091

92+
**Parameters:**
93+
- `firstValue`: Number or string to add
94+
- `secondValue`: Number or string to add
95+
96+
**Returns:** The sum rounded to 2 decimal places
97+
98+
**Throws:** `HyperMathError` if inputs are invalid
99+
100+
---
101+
51102
### `HyperMath.divide(firstValue: number | string, secondValue: number | string): number`
52103

53104
Divides two numbers with improved precision.
54105

106+
**Parameters:**
107+
- `firstValue`: Dividend (number or string)
108+
- `secondValue`: Divisor (number or string)
109+
110+
**Returns:** The quotient rounded to 2 decimal places
111+
112+
**Throws:**
113+
- `HyperMathError` if inputs are invalid
114+
- `HyperMathError` with message "Division by zero is not allowed" if divisor is 0
115+
116+
---
117+
55118
### `HyperMath.subtract(firstValue: number | string, secondValue: number | string): number`
56119

57120
Subtracts two numbers with improved precision.
58121

122+
**Parameters:**
123+
- `firstValue`: Number or string (minuend)
124+
- `secondValue`: Number or string to subtract (subtrahend)
125+
126+
**Returns:** The difference rounded to 2 decimal places
127+
128+
**Throws:** `HyperMathError` if inputs are invalid
129+
130+
---
131+
132+
### `HyperMath.formatNumber(number: number | string): number`
133+
134+
Formats a number to maintain precision up to 2 decimal places.
135+
136+
**Parameters:**
137+
- `number`: Number or string to format
138+
139+
**Returns:** The formatted number rounded to 2 decimal places
140+
141+
**Throws:** `HyperMathError` if input is invalid
142+
59143
## Notes
60144

61-
- This library uses `toPrecision(2)` internally, which may lead to rounding in some cases. Be aware of this limitation when using the library for calculations requiring high precision.
62-
- Input strings are parsed to numbers. Invalid string inputs will throw an error.
145+
- **Precision**: This library uses `.toFixed(2)` internally to maintain precision up to 2 decimal places. This may lead to rounding in some cases. Be aware of this limitation when using the library for calculations requiring higher precision.
146+
- **Error handling**: The library now throws `HyperMathError` exceptions instead of returning 0 for invalid inputs. Make sure to handle these errors appropriately in your code.
147+
- **Edge cases**:
148+
- **Division by zero**: Throws `HyperMathError` instead of returning 0
149+
- **Invalid inputs**: Any null, undefined, or non-numeric string values will throw `HyperMathError`
150+
- **String parsing**: String inputs are parsed using `parseFloat()`, so partial numeric strings like "123abc" will parse as 123
151+
- **Type safety**: TypeScript users benefit from full type definitions and the custom `HyperMathError` class for better error handling
152+
153+
## Support
154+
155+
If you encounter any issues or have questions, please open an issue on the [GitHub repository](https://github.com/hyperteksolutions/hypermath/issues).
63156

64157
## Contributing
65158

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"this really shouldn't exist"
1515
],
1616
"main": "dist/main.js",
17+
"types": "dist/main.d.ts",
18+
"files": [
19+
"dist",
20+
"README.md",
21+
"LICENSE"
22+
],
1723
"scripts": {
1824
"build": "nest build",
1925
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
@@ -75,4 +81,4 @@
7581
"coverageDirectory": "../coverage",
7682
"testEnvironment": "node"
7783
}
78-
}
84+
}

0 commit comments

Comments
 (0)