currency-to-locale
Advanced tools
Comparing version 1.0.2 to 1.1.0
import currencyToLocale from '../index.js'; | ||
// Example 1: Simple usage | ||
console.log("USD locale:", currencyToLocale('USD')); // Output: 'en_US' | ||
console.log( | ||
"Example 1: Simple usage" | ||
); | ||
console.log("USD:", currencyToLocale('USD')); // 'en_US' | ||
// Example 2: Currency code with multiple locales | ||
console.log("EUR with 'fr' locale:", currencyToLocale('EUR', 'fr')); // Output: 'fr_FR' | ||
// Example 2: Currency code with multiple locale IDs | ||
console.log( | ||
"\nExample 2: Currency code with multiple locale IDs" | ||
); | ||
console.log("EUR with fr:", currencyToLocale('EUR', 'fr')); // 'fr_FR' | ||
console.log("EUR with es:", currencyToLocale('EUR', 'es')); // 'es_ES' | ||
// Example 3: Lowercase currency code | ||
console.log("Lowercase 'jpy' locale:", currencyToLocale('jpy')); // Output: 'ja_JP' | ||
// Example 3: Currency code with multiple locale IDs, no language code provided | ||
// (default locale ID returned) | ||
console.log( | ||
"\nExample 3: Currency code with multiple locale IDs, " + | ||
"no language code provided (default locale ID returned)" | ||
); | ||
console.log("EUR with no language code:", currencyToLocale('EUR')); // 'de_DE' | ||
// Example 4: Invalid currency code | ||
console.log("Invalid currency code 'XYZ':", currencyToLocale('XYZ')); // Output: 'Currency code not found.' | ||
// Example 4: Currency code with only one locale ID | ||
// (language code ignored if provided) | ||
console.log( | ||
"\nExample 4: Currency code with only one locale ID " + | ||
"(language code ignored if provided)" | ||
); | ||
console.log( | ||
"HKD with de (incorrect language code for HKD, but ignored):", | ||
currencyToLocale('HKD', 'de') | ||
); // 'zh_HK' | ||
console.log( | ||
"HKD with xyz (invalid language code, but ignored):", | ||
currencyToLocale('HKD', 'xyz') | ||
); // 'zh_HK' | ||
console.log( | ||
"HKD with 1 (invalid language code type, but ignored):", | ||
currencyToLocale('HKD', 1) | ||
); // 'zh_HK' | ||
// Example 5: No input | ||
console.log("No input:", currencyToLocale()); // Output: 'Please provide a currency code.' | ||
// Example 5: Improperly formatted currency or language code (accepted) | ||
console.log( | ||
"\nExample 5: Improperly formatted currency or language code (accepted)" | ||
); | ||
console.log( | ||
"Lowercase 'jpy' currency code:", | ||
currencyToLocale('jpy') | ||
); // 'ja_JP' | ||
console.log( | ||
"Uppercase 'IT' language code:", | ||
currencyToLocale('CHF', 'IT') | ||
); // 'it_CH' | ||
// Example 6: Invalid or not-found currency code | ||
console.log( | ||
"\nExample 6: Invalid currency code" | ||
); | ||
console.log( | ||
"Currency code 1 (number):", | ||
currencyToLocale(1) | ||
); // 'Please provide a valid currency code.' | ||
console.log( | ||
"Currency code 'ABCD' (not 3-character):", | ||
currencyToLocale('ABCD') | ||
); // 'Please provide a valide currency code.' | ||
console.log( | ||
"Currency code 'XYZ':", | ||
currencyToLocale('XYZ'), | ||
"(currency code might be valid, just missing from function's mapping)" | ||
); // 'Currency code not found.' | ||
// Example 7: Invalid or not-found language code with valid currency code | ||
console.log( | ||
"\nExample 7: Invalid or not-found language code with valid currency code" | ||
); | ||
console.log( | ||
"Language code 1 (number) with currency code 'EUR':", | ||
currencyToLocale('EUR', 1) | ||
); // 'Please provide a valid language code.' | ||
console.log( | ||
"Language code 'xyz' (not 2-character) with currency code 'EUR':", | ||
currencyToLocale('EUR', 'xyz') | ||
); // 'Please provide a valid language code.' | ||
console.log( | ||
"Language code 'zh' with currency code 'EUR':", | ||
currencyToLocale('EUR', 'zh'), | ||
"(language code might be valid, just missing from function's mapping)" | ||
); // 'Language code not found for EUR.' | ||
// Example 8: No input | ||
console.log( | ||
"\nExample 8: No input" | ||
); | ||
console.log(currencyToLocale()); // 'Please provide a currency code.' |
67
index.js
@@ -1,3 +0,5 @@ | ||
// function to map currency codes to location identifiers | ||
const currencyToLocale = (currencyCode, language) => { | ||
// function to map currency codes to locale identifiers | ||
const currencyToLocale = (currencyCode, languageCode) => { | ||
// input error handling (language type error handled below) | ||
if (!currencyCode) { | ||
@@ -7,4 +9,8 @@ return "Please provide a currency code."; | ||
if (typeof currencyCode !== 'string' || currencyCode.length !== 3) { | ||
return "Please provide a valid currency code."; | ||
} | ||
// ensure correct formatting | ||
currencyCode = currencyCode.toString().toUpperCase(); | ||
currencyCode = currencyCode.toUpperCase(); | ||
@@ -16,7 +22,7 @@ const currencyLocales = { | ||
"CAD": "en_CA", | ||
"CHF": ["de_CH", "fr_CH", "it_CH"], | ||
"CHF": ["de_CH", "fr_CH", "it_CH"], // multiple locales array | ||
"CNY": "zh_CN", | ||
"CZK": "cs_CZ", | ||
"DKK": "da_DK", | ||
"EUR": ["de_DE", "fr_FR", "es_ES", "it_IT"], | ||
"EUR": ["de_DE", "fr_FR", "es_ES", "it_IT"], // multiple locales array | ||
"GBP": "en_GB", | ||
@@ -46,29 +52,38 @@ "HKD": "zh_HK", | ||
if (language) { | ||
// ensure correct formatting | ||
language = language.toString().toLowerCase(); | ||
// loop through currencyLocales object | ||
for (const key in currencyLocales) { | ||
// check for values that are multi-value arrays | ||
if ( | ||
Array.isArray(currencyLocales[key]) && | ||
currencyLocales[key].length > 1 | ||
) { | ||
// match currency code | ||
if (currencyCode === key) { | ||
// extract language from locale ID | ||
for (const [k, localeID] of Object.entries(currencyLocales[key])) { | ||
let localeLang = localeID.split('_')[0]; | ||
// match beginning of locale ID | ||
if (language === localeLang) { | ||
// return matching locale ID | ||
return localeID; | ||
// multiple locale IDs | ||
// check for values that are multi-value arrays | ||
if ( | ||
Array.isArray(currencyLocales[currencyCode]) && | ||
currencyLocales[currencyCode].length > 1 | ||
) { | ||
if (languageCode) { | ||
// language code input type error handling | ||
if (typeof languageCode !== 'string' || languageCode.length !== 2) { | ||
return "Please provide a valid language code." | ||
} | ||
// ensure correct formatting | ||
languageCode = languageCode.toLowerCase(); | ||
// loop through currencyLocales object | ||
for (const key in currencyLocales) { | ||
{ | ||
// match currency code | ||
if (currencyCode === key) { | ||
// extract language from locale ID | ||
for (const [k, localeID] of Object.entries(currencyLocales[key])) { | ||
let localeLang = localeID.split('_')[0]; | ||
// match beginning of locale ID | ||
if (languageCode === localeLang) { | ||
// return matching locale ID | ||
return localeID; | ||
} | ||
} | ||
return (`Language code not found for ${currencyCode}.`); | ||
} | ||
return (`Language not found for ${currencyCode}.`); | ||
} | ||
} | ||
} | ||
// if no lang provided, return default locale | ||
return currencyLocales[currencyCode][0]; | ||
} | ||
return currencyLocales[currencyCode] || "Currency code not found."; | ||
@@ -75,0 +90,0 @@ } |
{ | ||
"name": "currency-to-locale", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Converts currency codes to locale identifiers following format specified in IETF BCP 47 standard", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
110
README.md
@@ -1,25 +0,91 @@ | ||
# currency-to-locale<br><br> | ||
****************<br> | ||
DESCRIPTION<br> | ||
****************<br><br> | ||
Converts currency codes to locale identifiers following format specified in IETF BCP 47 standard, comprised of two-letter ISO 639-1 language code and ISO 3166-1 alpha-2 country code.<br> | ||
Currency codes initially compiled using Frankfurter's https://api.frankfurter.app/currencies. Additional currency code and locale id additions welcome.<br> | ||
For currency codes with multiple locales/languages, defaults have been selected.<br><br> | ||
<hr> | ||
currencyToLocale function takes two arguments: currency code (required) and language code (optional)<br> | ||
invalid input types converted to strings<br> | ||
currency codes converted to uppercase; language codes converted to lowercase<br> | ||
support for currency codes with multiple locales, e.g. EUR, using language codes to differentiate<br> | ||
<hr> | ||
*****************<br> | ||
INSTALLATION<br> | ||
*****************<br><br> | ||
npm install currency-to-locale<br><br> | ||
<hr> | ||
*******************<br> | ||
USAGE EXAMPLES<br> | ||
*******************<br><br> | ||
To see examples of how to use the currencyToLocale function, run the following command:<br> | ||
```markdown | ||
# currency-to-locale | ||
### **Description** | ||
Converts currency codes to locale identifiers following format specified in IETF BCP 47 standard, comprised of a two-letter ISO 639-1 language code and ISO 3166-1 alpha-2 country code. | ||
Currency codes initially compiled using Frankfurter's [https://api.frankfurter.app/currencies](https://api.frankfurter.app/currencies). | ||
Additional currency code and locale ID additions are welcome; please see "Contributing" section below. | ||
For currency codes with multiple locales/language codes, defaults have been selected. | ||
--- | ||
### **Installation** | ||
Install via npm: | ||
```bash | ||
npm install currency-to-locale | ||
``` | ||
This will add the package to your project's dependencies. | ||
--- | ||
### **Usage** | ||
To use the `currencyToLocale` function in your project, import it as follows: | ||
```javascript | ||
import currencyToLocale from currency-to-locale; | ||
// Example 1: Simple usage | ||
const locale = currencyToLocale('USD'); // 'en_US' | ||
// Example 2: Currency code with multiple locales | ||
const locale = currencyToLocale('EUR', 'fr'); // 'fr_FR' | ||
// Example 3: Currency code with multiple locales, no language code provided - default locale returned | ||
console.log("EUR with no language code:", currencyToLocale('EUR')); // 'de_DE' | ||
// Example 4: Lowercase currency code | ||
const locale = currencyToLocale('jpy'); // 'ja_JP' | ||
// Example 5: Invalid currency code | ||
const locale = currencyToLocale('XYZ'); // 'Currency code not found.' | ||
// Example 6: No input | ||
const locale = currencyToLocale(); // 'Please provide a currency code.' | ||
``` | ||
The `currencyToLocale` function accepts two arguments: | ||
- **currencyCode** (required): The 3-letter currency code (e.g. "USD", "EUR"). | ||
- **languageCode** (optional): The 2-letter language code (e.g. "en", "de"). | ||
It returns the corresponding locale identifier (e.g. "en-US", "de-DE"). | ||
**Run example** | ||
To see examples in action, you can run the following command: | ||
```bash | ||
node examples/examples.js | ||
``` | ||
This will execute the example script provided in the `examples` folder and show the output. | ||
--- | ||
### **Contributing** | ||
I welcome contributions! If you'd like to improve or add to this project, please follow these steps: | ||
1. Fork the repository. | ||
2. Create a new branch (e.g., `feature-xyz` or `bugfix-123`). | ||
3. Make your changes, ensuring they align with the project’s goals and style. | ||
4. Test your changes to ensure they work correctly. | ||
5. Submit a pull request with a description of your changes. | ||
**Code of Conduct** | ||
Please be respectful and follow the [Contributor Covenant Code of Conduct](https://www.contributor-covenant.org/). | ||
I appreciate your contributions and look forward to collaborating with you! | ||
--- | ||
### **License** | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. | ||
``` | ||
import currencyToLocale from '../index.js'; | ||
// valid currency code with no multiple languages | ||
// valid currency code with single locale ID | ||
console.assert( | ||
@@ -9,3 +9,3 @@ currencyToLocale('USD') === 'en_US', | ||
// valid currency code with multiple languages | ||
// valid currency code with multiple locale IDs | ||
console.assert( | ||
@@ -15,3 +15,2 @@ currencyToLocale('CHF', 'fr') === 'fr_CH', | ||
); | ||
console.assert( | ||
@@ -22,4 +21,10 @@ currencyToLocale('EUR', 'es') === 'es_ES', | ||
// valid currency code, irrelevant language | ||
// valid currency code with multiple locale IDs, no language code provided - default returned | ||
console.assert( | ||
currencyToLocale('EUR') === 'de_DE', | ||
"Expected 'de_DE' for EUR" | ||
) | ||
// valid currency code, irrelevant language code | ||
console.assert( | ||
currencyToLocale('AUD', 'en') === 'en_AU', | ||
@@ -29,41 +34,57 @@ "Expected 'en_AU' for AUD, en" | ||
// valid currency code, irrelevant & invalid/nonexistent language | ||
// valid currency code, irrelevant & invalid/not-found language code (ignores) | ||
console.assert( | ||
currencyToLocale('USD', 'cat') === 'en_US', | ||
"Expected 'en_US' for USD, cat" | ||
// language code not found in function's mapping | ||
currencyToLocale('USD', 'xy') === 'en_US', | ||
"Expected 'en_US' for USD, xy" | ||
); | ||
console.assert( | ||
// invalid language code length | ||
currencyToLocale('USD', 'xyz') === 'en_US', | ||
"Expected 'en_US' for USD, xyz" | ||
) | ||
console.assert( | ||
// invalid language code type | ||
currencyToLocale('USD', 1) === 'en_US', | ||
"Expected 'en_US' for USD, 1" | ||
) | ||
// invalid/nonexistent currency code | ||
// invalid/not-found currency code | ||
console.assert( | ||
currencyToLocale('ACAT') === 'Currency code not found.', | ||
"Expected 'Currency code not found.' for ACAT" | ||
// currency code not found in function's mapping | ||
currencyToLocale('XYZ') === 'Currency code not found.', | ||
"Expected 'Currency code not found.' for XYZ" | ||
); | ||
// invalid currency code type (number) | ||
console.assert( | ||
currencyToLocale(1) === 'Currency code not found.', | ||
"Expected 'Currency code not found' for 1" | ||
// invalid currency code length | ||
currencyToLocale('ACAT') === 'Please provide a valid currency code.', | ||
"Expected 'Please provide a valid currency code.' for ACAT" | ||
); | ||
console.assert( | ||
// invalid currency code type (number) | ||
currencyToLocale(1) === 'Please provide a valid currency code.', | ||
"Expected 'Please provide a valid currency code.' for 1" | ||
); | ||
// invalid currency code type (number), valid language | ||
// invalid currency code type (number), valid language code | ||
console.assert( | ||
currencyToLocale(1, 'fr') === 'Currency code not found.', | ||
"Expected 'Currency code not found.' for 1, fr" | ||
currencyToLocale(1, 'fr') === 'Please provide a valid currency code.', | ||
"Expected 'Please provide a valid currency code.' for 1, fr" | ||
); | ||
// valid currency code, invalid language type (number) | ||
// valid currency code, invalid language code | ||
console.assert( | ||
currencyToLocale('EUR', 1) === 'Language not found for EUR.', | ||
"Expected 'Language not found for EUR' for EUR, 1" | ||
// existing language code, not mapped to provided currency code | ||
currencyToLocale('CHF', 'zh') === 'Language code not found for CHF.', | ||
"Expected 'Language code not found for CHF.' for CHF, zh" | ||
); | ||
// valid currency code, nonexistent/invalid language | ||
console.assert( | ||
currencyToLocale('CHF', 'cat') === 'Language not found for CHF.', | ||
"Expected 'Language not found for CHF.' for CHF, cat" | ||
// invalid language code length | ||
currencyToLocale('EUR', 'c') === 'Please provide a valid language code.', | ||
"Expected 'Please provide a valid language code.' for EUR, c" | ||
); | ||
console.assert( | ||
currencyToLocale('EUR', 'cat') === 'Language not found for EUR.', | ||
"Expected 'Language not found for EUR.' for EUR, cat" | ||
// invalid language code type | ||
currencyToLocale('EUR', 1) === 'Please provide a valid language code.', | ||
"Expected 'Please provide a valid language code.' for EUR, 1" | ||
); | ||
@@ -77,3 +98,3 @@ | ||
// uppercase valid language | ||
// uppercase valid language code | ||
console.assert( | ||
@@ -84,3 +105,3 @@ currencyToLocale('EUR', 'FR') === 'fr_FR', | ||
// lowercase valid currency code & uppercase valid language | ||
// lowercase valid currency code & uppercase valid language code | ||
console.assert( | ||
@@ -87,0 +108,0 @@ currencyToLocale('chf', 'IT') === 'it_CH', |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14175
268
92
1