A complex conversion tool handling non-linear units with TDD architecture.
Unit conversion sounds simple until you deal with non-linear mapping (Celsius to Fahrenheit) or live rates (Currency). Flux-Convert is built to demonstrate Test-Driven Development (TDD) and Clean Architecture. It separates the "business logic" of conversion from the UI, ensuring robust and testable code.
- Multi-Category Support: Length, Weight, Temperature, Data, Currency.
- Precision Handling: Correctly handles floating point math (e.g., IEEE 754 errors).
- Separation of Concerns: Logic is decoupled from the DOM.
- Unit Tested: Comprehensive test suite verifying edge cases.
- JavaScript (ES6+): Core logic.
- Jest: Testing framework (or a lightweight custom runner if preferred).
- CSS Grid: for the converter UI layout.
JavaScript's 0.1 + 0.2 !== 0.3 issue is fatal for a precision tool. I implemented a utility helper to handle decimal rounding and epsilon comparisons to ensure conversions like "GB to Bytes" remain accurate to the specific decimal place.
I strictly separated the conversion functions into a logic.js module. This allowed me to write tests for convert(20, 'C', 'F') without needing a browser environment. It proves the ability to write modular, testable code.
Hardcoding if (unit == 'kg') is a bad pattern. I designed a configuration object (Strategy Pattern) where adding a new unit is as simple as adding a conversion factor to a JSON list, rather than rewriting the logic functions.
- Node.js (for running tests)
# Clone the repository
git clone https://github.com/yourusername/antigravity-opencode.git
cd 09-flux-convert
# Install dependencies (for testing)
npm install- Run Tests:
npm test - Use App:
Open
index.htmlin your browser. Select category, input value, and see the result.
09-flux-convert/
├── index.html
├── logic.js
├── dom.js
├── logic.test.js
└── style.css
Please maintain the TDD cycle when adding new units.