diff --git a/generate.js b/generate.js index 1a14d0c..5aab75f 100644 --- a/generate.js +++ b/generate.js @@ -2,19 +2,19 @@ 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 ); } } @@ -22,24 +22,24 @@ for ( const country of countries ) { 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; } @@ -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"; @@ -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.' ); diff --git a/index.ts b/index.ts index 31015e8..4052596 100644 --- a/index.ts +++ b/index.ts @@ -6,7 +6,9 @@ export default { AI: 'XCD', AL: 'ALL', AM: 'AMD', + AN: 'ANG', AO: 'AOA', + AQ: 'USD', AR: 'ARS', AS: 'USD', AT: 'EUR', @@ -69,6 +71,7 @@ export default { ET: 'ETB', FI: 'EUR', FJ: 'FJD', + FK: 'FKP', FM: 'USD', FO: 'DKK', FR: 'EUR', @@ -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', @@ -120,6 +125,7 @@ export default { KW: 'KWD', KY: 'KYD', KZ: 'KZT', + LA: 'LAK', LB: 'LBP', LC: 'XCD', LI: 'CHF', @@ -137,6 +143,7 @@ export default { MF: 'EUR', MG: 'MGA', MH: 'USD', + MK: 'MKD', ML: 'XOF', MM: 'MMK', MN: 'MNT', @@ -175,6 +182,7 @@ export default { PM: 'EUR', PN: 'NZD', PR: 'USD', + PS: 'ILS', PT: 'EUR', PW: 'USD', PY: 'PYG', @@ -216,6 +224,7 @@ export default { TM: 'TMT', TN: 'TND', TO: 'TOP', + TR: 'TRY', TT: 'TTD', TV: 'AUD', TW: 'TWD', @@ -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' } diff --git a/readme.md b/readme.md index a491a9a..df8a8ff 100644 --- a/readme.md +++ b/readme.md @@ -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.