How besrourms/gold-price-converter works
Main Components
• GoldPriceConverter Class:
• Represents the core logic of the library.
• Contains the constructor (that accepts usdPerOunce, exchangeRate, precision, currencyCode) and the method getGoldPricesPerGram().
• Encapsulates the conversion formulas and input validations.
• Input Validation:
• This might be shown as an internal component or sub-module within GoldPriceConverter that ensures the parameters meet the necessary conditions.
• Conversion Logic Unit:
• The part responsible for calculating conversion from USD/ounce to target currency/gram by handling different gold karats.
• Output:
• The results object that returns prices for 24K, 21K, 18K, and 14K.
• External Interface / Usage:
Relationships/Interactions
• Gold price in USD per ounce.
• Exchange rate.
• Optional precision and currency code.
• Inside this block, include a validation sub-component that verifies:
• usdPerOunce and exchangeRate are positive finite numbers.
• Precision is a non-negative integer.
• The currency code is a non-empty string.
• Next, the “Conversion Logic” part of the block processes the validated inputs to compute values for each gold karat.
• Finally, the processed output (an object containing conversions for 24K, 21K, 18K, and 14K) is returned and can be used in an application or logged.
Architectural Patterns & Design Principles
• Modularity:
• The system is highly modular with the conversion logic encapsulated in a standalone JavaScript class.
• Single Responsibility:
• The GoldPriceConverter class has a clear purpose: performing gold price conversions.
• Input Validation:
• Built-in robust error handling ensures the correctness of the inputs before converting.
• No External Dependencies:
• This makes the design lightweight and easy to integrate into multiple environments (Node.js, browser, etc.).
Relevant Technologies and Frameworks
• JavaScript (Node.js/ES6):
• The design will note usage in these environments.
• No external libraries are involved (as per the README).
Adding Extra Detail
• To capture any potential error flows:
• A note showing configuration options like rounding precision.