Skip to content

Commit

Permalink
Sort output object's keys
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodp committed May 29, 2023
1 parent f3e6368 commit 6ecce30
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
52 changes: 33 additions & 19 deletions generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@ let countries = require( 'country-list/data' );
let currencies = require( 'currency-codes/data' );
const fs = require( 'fs' );

const r = {};
const result = {};
let count = 0;
let notFound = [];
for ( const country of countries ) {
for ( const currency of currencies ) {
const countryName = country.name.split( ',' )[ 0 ];
if ( currency.countries.join( ',' ).indexOf( countryName ) >= 0 ) {
r[ country.code ] = currency.code;
result[ country.code ] = currency.code;
++count;
break;
}
}
if ( ! r[ country.code ] ) {
if ( ! result[ country.code ] ) {
notFound.push( country );
}
}

if ( notFound.length > 0 ) {

const allNotFound = [ ... notFound ];
for ( const curr of currencies ) {
for ( const currCountry of curr.countries ) {
for ( const currency of currencies ) {
for ( const currencyCountry of currency.countries ) {

const c = currCountry
const currencyCountryName = currencyCountry
.replace( /(\([A-z ]+\)| ?\[|\])/g, '' )
.replace( //, "'" )
.trim()
.toLowerCase();

for ( const nf of allNotFound ) {
for ( const countryNotFound of allNotFound ) {

const nfName = nf.name.replace( /(\(|\))/g, '' )
const nameNotFound = countryNotFound.name.replace( /(\(|\))/g, '' )
.trim()
.toLowerCase();

if ( nfName.indexOf( c ) >= 0 ) {
notFound = notFound.filter( f => f.code != nf.code );
r[ nf.code ] = curr.code;
if ( nameNotFound.indexOf( currencyCountryName ) >= 0 ) {
notFound = notFound.filter( f => f.code != countryNotFound.code );
result[ countryNotFound.code ] = currency.code;
++count;
}

Expand Down Expand Up @@ -75,18 +75,31 @@ const inclusionsOrFixes = [

let inclusions = 0;
for ( const item of inclusionsOrFixes ) {
if ( ! r[ item.countryCode ] ) {
if ( ! result[ item.countryCode ] ) {
inclusions++;
}
r[ item.countryCode ] = item.currencyCode;
result[ item.countryCode ] = item.currencyCode;
}

//
// Sort keys
//

let countryCount = 0;
const sorted = Object.keys( result )
.sort()
.reduce( ( accumulator, key ) => {
++countryCount;
accumulator[ key ] = result[ key ];
return accumulator;
}, {} );

//
// Generate file
//

const content = 'export default ' +
JSON.stringify( r, null, 2 )
JSON.stringify( sorted, null, 2 )
.replace(/"([A-Z]{2})":/g, '$1:')
.replace(/"/g, "'") +
"\n";
Expand All @@ -99,9 +112,10 @@ fs.writeFileSync( 'index.ts', content );
//

console.log( 'index.ts generated.' );
console.log( countries.length + inclusions, '\ttotal');
console.log( countries.length, '\timported');
console.log( notFound.length, '\twere not found', notFound.length > 0 ? notFound : '' );
console.log( inclusions, '\tmanually included.' );
console.log( inclusionsOrFixes.length - inclusions, '\tmanually fixed.' );
console.log( countryCount, '\tcountries in which:' );
console.log( '\t', countries.length, '\tcountries were imported;');
console.log( '\t', notFound.length, '\tcountries were originally not found for currency data;' );
if ( notFound.length > 0 ) console.log( notFound );
console.log( '\t', inclusions, '\tmanual inclusions;' );
console.log( '\t', inclusionsOrFixes.length - inclusions, '\tmanual fixes.' );

20 changes: 10 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export default {
AI: 'XCD',
AL: 'ALL',
AM: 'AMD',
AN: 'ANG',
AO: 'AOA',
AQ: 'USD',
AR: 'ARS',
AS: 'USD',
AT: 'EUR',
Expand Down Expand Up @@ -69,6 +71,7 @@ export default {
ET: 'ETB',
FI: 'EUR',
FJ: 'FJD',
FK: 'FKP',
FM: 'USD',
FO: 'DKK',
FR: 'EUR',
Expand All @@ -86,11 +89,13 @@ export default {
GP: 'EUR',
GQ: 'XAF',
GR: 'EUR',
GS: 'GEL',
GT: 'GTQ',
GU: 'USD',
GW: 'XOF',
GY: 'GYD',
HK: 'HKD',
HM: 'AUD',
HN: 'HNL',
HR: 'EUR',
HT: 'HTG',
Expand Down Expand Up @@ -120,6 +125,7 @@ export default {
KW: 'KWD',
KY: 'KYD',
KZ: 'KZT',
LA: 'LAK',
LB: 'LBP',
LC: 'XCD',
LI: 'CHF',
Expand All @@ -137,6 +143,7 @@ export default {
MF: 'EUR',
MG: 'MGA',
MH: 'USD',
MK: 'MKD',
ML: 'XOF',
MM: 'MMK',
MN: 'MNT',
Expand Down Expand Up @@ -175,6 +182,7 @@ export default {
PM: 'EUR',
PN: 'NZD',
PR: 'USD',
PS: 'ILS',
PT: 'EUR',
PW: 'USD',
PY: 'PYG',
Expand Down Expand Up @@ -216,6 +224,7 @@ export default {
TM: 'TMT',
TN: 'TND',
TO: 'TOP',
TR: 'TRY',
TT: 'TTD',
TV: 'AUD',
TW: 'TWD',
Expand All @@ -239,14 +248,5 @@ export default {
YT: 'EUR',
ZA: 'ZAR',
ZM: 'ZMW',
ZW: 'ZWL',
HM: 'AUD',
FK: 'FKP',
GS: 'GEL',
LA: 'LAK',
MK: 'MKD',
AQ: 'USD',
PS: 'ILS',
TR: 'TRY',
AN: 'ANG'
ZW: 'ZWL'
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ console.log( countryToCurrency[ 'US' ] ); // USD

## Notes

- Countries included: `253`.
- Countries included: `250`.
- For `Antarctica` (`AQ`), the currency `USD` is being assumed.
- For `Palestine` (`PS`), the currency `ILS` is being assumed.
- For `South Korea` (`KR`), the currency `KRW` is being assumed.
Expand Down

0 comments on commit 6ecce30

Please sign in to comment.