Get List of Supported Currencies

后端 未结 2 1382
后悔当初
后悔当初 2020-12-21 00:19

Other than just guessing (like I\'ve done below), is there a more direct and efficient way of reflectively retrieving a list of all currencies supported by

2条回答
  •  无人及你
    2020-12-21 00:56

    You could load a known list via this XML:

    https://www.currency-iso.org/dam/downloads/lists/list_one.xml

    The list was found here: https://www.currency-iso.org/en/home/tables/table-a1.html

    
      
        
          
            UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND (THE)
          
          Pound Sterling
          GBP
          826
          2
        
        
          UNITED STATES OF AMERICA (THE)
          US Dollar
          USD
          840
          2
        
      
    
    

    var xmlString = getSampleCurrencyXml();
    var xmlData = (new window.DOMParser()).parseFromString(xmlString, "text/xml");
    var knownCodes = [].slice.call(xmlData.querySelectorAll('Ccy')).map(n => n.textContent)
    
    // Fetch the XML instead?
    fetch('https://www.currency-iso.org/dam/downloads/lists/list_one.xml', { cache: 'default' })
      .then(response => response.text())
      .then(xmlStr => (new window.DOMParser()).parseFromString(xmlStr, "text/xml"))
      .then(data => knownCodes = data); // This may not work in the Stack Snippet
    
    console.log(getSupportedCurrencies().map(c => c.code + '\t' + c.name).join('\n'));
    
    function getSupportedCurrencies() {
      function $(amount, currency) {
        return Intl.NumberFormat('en-US', {
          style: 'currency',
          currency: currency,
          currencyDisplay: 'name'
        }).format(amount);
      }
      return knownCodes.reduce((currencies, cur) => {
        return (output => {
          return output.replace(/^[^ ]+ /, '') !== cur ?
            currencies.concat({
              code: cur,
              name: output.match(/(?<= ).+/)[0]
            }) :
            currencies;
        })($(0, cur).trim());
      }, []);
    }
    
    function getSampleCurrencyXml() {
      return `
        
          
            
              
                UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND (THE)
              
              Pound Sterling
              GBP
              826
              2
            
            
              UNITED STATES OF AMERICA (THE)
              US Dollar
              USD
              840
              2
            
          
        
      `;
    }
    .as-console-wrapper { top: 0; max-height: 100% !important; }


    If you want to generate the codes still, you can use a product iterable.

    The following is based on Python's itertools.product function.

    let ary = product('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''), 3).map(a => a.join(''));
    
    function product(iterables, repeat) {
      var argv = Array.prototype.slice.call(arguments), argc = argv.length;
      if (argc === 2 && !isNaN(argv[argc - 1])) {
        var copies = [];
        for (var i = 0; i < argv[argc - 1]; i++) { copies.push(argv[0].slice()); }
        argv = copies;
      }
      return argv.reduce((accumulator, value) => {
        var tmp = [];
        accumulator.forEach(a0 => value.forEach(a1 => tmp.push(a0.concat(a1))));
        return tmp;
      }, [[]]);
    }
    

    Demo

    console.log(getSupportedCurrencies().map(c => c.code + '\t' + c.name).join('\n'));
    
    function getSupportedCurrencies() {
      function $(amount, currency) {
        return Intl.NumberFormat('en-US', {
          style: 'currency',
          currency: currency,
          currencyDisplay: 'name'
        }).format(amount);
      }
      let ary = product('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''), 3).map(a => a.join(''));
      return ary.reduce((currencies, cur) => {
        return (output => {
          return output.replace(/^[^ ]+ /, '') !== cur
            ? currencies.concat({ code : cur, name : output.match(/(?<= ).+/)[0] })
            : currencies;
        })($(0, cur).trim());
      }, []);
    }
    
    function product(iterables, repeat) {
      var argv = Array.prototype.slice.call(arguments), argc = argv.length;
      if (argc === 2 && !isNaN(argv[argc - 1])) {
        var copies = [];
        for (var i = 0; i < argv[argc - 1]; i++) { copies.push(argv[0].slice()); }
        argv = copies;
      }
      return argv.reduce((accumulator, value) => {
        var tmp = [];
        accumulator.forEach(a0 => value.forEach(a1 => tmp.push(a0.concat(a1))));
        return tmp;
      }, [[]]);
    }
    .as-console-wrapper { top: 0; max-height: 100% !important; }

提交回复
热议问题