Guest User

Money.js

a guest
Jul 6th, 2017
38
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2.  * money.js / fx() v0.2
  3.  * Copyright 2014 Open Exchange Rates
  4.  *
  5.  * JavaScript library for realtime currency conversion and exchange rate calculation.
  6.  *
  7.  * Freely distributable under the MIT license.
  8.  * Portions of money.js are inspired by or borrowed from underscore.js
  9.  *
  10.  * For details, examples and documentation:
  11.  * http://openexchangerates.github.io/money.js/
  12.  */
  13. (function(root, undefined) {
  14.  
  15.     // Create a safe reference to the money.js object for use below.
  16.     var fx = function(obj) {
  17.         return new fxWrapper(obj);
  18.     };
  19.  
  20.     // Current version.
  21.     fx.version = '0.2';
  22.  
  23.  
  24.     /* --- Setup --- */
  25.  
  26.     // fxSetup can be defined before loading money.js, to set the exchange rates and the base
  27.     // (and default from/to) currencies - or the rates can be loaded in later if needed.
  28.     var fxSetup = root.fxSetup || {
  29.         rates : {},
  30.         base : ""
  31.     };
  32.  
  33.     // Object containing exchange rates relative to the fx.base currency, eg { "GBP" : "0.64" }
  34.     fx.rates = fxSetup.rates;
  35.  
  36.     // Default exchange rate base currency (eg "USD"), which all the exchange rates are relative to
  37.     fx.base = fxSetup.base;
  38.  
  39.     // Default from / to currencies for conversion via fx.convert():
  40.     fx.settings = {
  41.         from : fxSetup.from || fx.base,
  42.         to : fxSetup.to || fx.base
  43.     };
  44.  
  45.  
  46.     /* --- Conversion --- */
  47.  
  48.     // The base function of the library: converts a value from one currency to another
  49.     var convert = fx.convert = function(val, opts) {
  50.         // Convert arrays recursively
  51.         if (typeof val === 'object' && val.length) {
  52.             for (var i = 0; i< val.length; i++ ) {
  53.                 val[i] = convert(val[i], opts);
  54.             }
  55.             return val;
  56.         }
  57.  
  58.         // Make sure we gots some opts
  59.         opts = opts || {};
  60.  
  61.         // We need to know the `from` and `to` currencies
  62.         if( !opts.from ) opts.from = fx.settings.from;
  63.         if( !opts.to ) opts.to = fx.settings.to;
  64.  
  65.         // Multiple the value by the exchange rate
  66.         return val * getRate( opts.to, opts.from );
  67.     };
  68.  
  69.     // Returns the exchange rate to `target` currency from `base` currency
  70.     var getRate = function(to, from) {
  71.         // Save bytes in minified version
  72.         var rates = fx.rates;
  73.  
  74.         // Make sure the base rate is in the rates object:
  75.         rates[fx.base] = 1;
  76.  
  77.         // Throw an error if either rate isn't in the rates array
  78.         if ( !rates[to] || !rates[from] ) throw "fx error";
  79.  
  80.         // If `from` currency === fx.base, return the basic exchange rate for the `to` currency
  81.         if ( from === fx.base ) {
  82.             return rates[to];
  83.         }
  84.  
  85.         // If `to` currency === fx.base, return the basic inverse rate of the `from` currency
  86.         if ( to === fx.base ) {
  87.             return 1 / rates[from];
  88.         }
  89.  
  90.         // Otherwise, return the `to` rate multipled by the inverse of the `from` rate to get the
  91.         // relative exchange rate between the two currencies
  92.         return rates[to] * (1 / rates[from]);
  93.     };
  94.  
  95.  
  96.     /* --- OOP wrapper and chaining --- */
  97.  
  98.     // If fx(val) is called as a function, it returns a wrapped object that can be used OO-style
  99.     var fxWrapper = function(val) {
  100.         // Experimental: parse strings to pull out currency code and value:
  101.         if ( typeof val === "string" ) {
  102.             this._v = parseFloat(val.replace(/[^0-9-.]/g, ""));
  103.             this._fx = val.replace(/([^A-Za-z])/g, "");
  104.         } else {
  105.             this._v = val;
  106.         }
  107.     };
  108.  
  109.     // Expose `wrapper.prototype` as `fx.prototype`
  110.     var fxProto = fx.prototype = fxWrapper.prototype;
  111.  
  112.     // fx(val).convert(opts) does the same thing as fx.convert(val, opts)
  113.     fxProto.convert = function() {
  114.         var args = Array.prototype.slice.call(arguments);
  115.         args.unshift(this._v);
  116.         return convert.apply(fx, args);
  117.     };
  118.  
  119.     // fx(val).from(currency) returns a wrapped `fx` where the value has been converted from
  120.     // `currency` to the `fx.base` currency. Should be followed by `.to(otherCurrency)`
  121.     fxProto.from = function(currency) {
  122.         var wrapped = fx(convert(this._v, {from: currency, to: fx.base}));
  123.         wrapped._fx = fx.base;
  124.         return wrapped;
  125.     };
  126.  
  127.     // fx(val).to(currency) returns the value, converted from `fx.base` to `currency`
  128.     fxProto.to = function(currency) {
  129.         return convert(this._v, {from: this._fx ? this._fx : fx.settings.from, to: currency});
  130.     };
  131.  
  132.  
  133.     /* --- Module Definition --- */
  134.  
  135.     // Export the fx object for CommonJS. If being loaded as an AMD module, define it as such.
  136.     // Otherwise, just add `fx` to the global object
  137.     if (typeof exports !== 'undefined') {
  138.         if (typeof module !== 'undefined' && module.exports) {
  139.             exports = module.exports = fx;
  140.         }
  141.         exports.fx = fx;
  142.     } else if (typeof define === 'function' && define.amd) {
  143.         // Return the library as an AMD module:
  144.         define([], function() {
  145.             return fx;
  146.         });
  147.     } else {
  148.         // Use fx.noConflict to restore `fx` back to its original value before money.js loaded.
  149.         // Returns a reference to the library's `fx` object; e.g. `var money = fx.noConflict();`
  150.         fx.noConflict = (function(previousFx) {
  151.             return function() {
  152.                 // Reset the value of the root's `fx` variable:
  153.                 root.fx = previousFx;
  154.                 // Delete the noConflict function:
  155.                 fx.noConflict = undefined;
  156.                 // Return reference to the library to re-assign it:
  157.                 return fx;
  158.             };
  159.         })(root.fx);
  160.  
  161.         // Declare `fx` on the root (global/window) object:
  162.         root['fx'] = fx;
  163.     }
  164.  
  165.     // Root will be `window` in browser or `global` on the server:
  166. }(this));
RAW Paste Data Copied