Skip to content

Commit

Permalink
Adds fx.pair() according to issue openexchangerates#4.
Browse files Browse the repository at this point in the history
  • Loading branch information
thewarpaint committed Mar 19, 2013
1 parent 209680f commit 85eed2d
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions money.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,39 @@
return val * getRate( opts.to, opts.from );
};

// Returns the exchange rate between two currencies.
// Accepts a string parameter of the form "FROM/TO", or an object with "from" and "to" properties.
var pair = fx.pair = function(currencies) {
var to, from;

if(typeof currencies == 'object') {
// Throw an error if currencies.to or currencies.from isn't set
if(!currencies.from || !currencies.to) throw "fx error: expected an object with properties 'to' and 'from' set.";

from = currencies.from;
to = currencies.to;
}
else if(typeof currencies == 'string') {
if(currencies.indexOf('/') == -1) {
throw "fx error: expected a string of the form FROM/TO.";
}

currencies = currencies.split('/');

if(currencies.length != 2) {
throw "fx error: expected a string of the form FROM/TO.";
}

from = currencies[0];
to = currencies[1];
}
else {
throw "fx error: unknown format.";
}

return getRate(to, from);
};

// Returns the exchange rate to `target` currency from `base` currency
var getRate = function(to, from) {
// Save bytes in minified version
Expand Down Expand Up @@ -129,7 +162,6 @@
return convert(this._v, {from: this._fx ? this._fx : fx.settings.from, to: currency});
};


/* --- Module Definition --- */

// Export the fx object for CommonJS. If being loaded as an AMD module, define it as such.
Expand Down Expand Up @@ -163,4 +195,4 @@
}

// Root will be `window` in browser or `global` on the server:
}(this));
}(this));

0 comments on commit 85eed2d

Please sign in to comment.