Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.3.0 #27

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 119 additions & 101 deletions money.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* money.js / fx() v0.2
* money.js / fx() v0.3
* Copyright 2014 Open Exchange Rates
*
* JavaScript library for realtime currency conversion and exchange rate calculation.
Expand All @@ -11,138 +11,156 @@
* http://openexchangerates.github.io/money.js/
*/
(function(root, undefined) {
var factory = function() {
// Create a safe reference to the money.js object for use below.
var fx = function(obj) {
return new fxWrapper(obj);
};

// Create a safe reference to the money.js object for use below.
var fx = function(obj) {
return new fxWrapper(obj);
};

// Current version.
fx.version = '0.2';
// Current version.
fx.version = '0.3.0';


/* --- Setup --- */
/* --- Setup --- */

// fxSetup can be defined before loading money.js, to set the exchange rates and the base
// (and default from/to) currencies - or the rates can be loaded in later if needed.
var fxSetup = root.fxSetup || {
rates : {},
base : ""
};
// fxSetup can be defined before loading money.js, to set the exchange rates and the base
// (and default from/to) currencies - or the rates can be loaded in later if needed.
var fxSetup = root.fxSetup || {
rates : {},
base : ""
};

// Object containing exchange rates relative to the fx.base currency, eg { "GBP" : "0.64" }
fx.rates = fxSetup.rates;
// Object containing exchange rates relative to the fx.base currency, eg { "GBP" : "0.64" }
fx.rates = fxSetup.rates;

// Default exchange rate base currency (eg "USD"), which all the exchange rates are relative to
fx.base = fxSetup.base;
// Default exchange rate base currency (eg "USD"), which all the exchange rates are relative to
fx.base = fxSetup.base;

// Default from / to currencies for conversion via fx.convert():
fx.settings = {
from : fxSetup.from || fx.base,
to : fxSetup.to || fx.base
};
// Default from / to currencies for conversion via fx.convert():
fx.settings = {
from : fxSetup.from || fx.base,
to : fxSetup.to || fx.base
};


/* --- Conversion --- */
/* --- Conversion --- */

// The base function of the library: converts a value from one currency to another
var convert = fx.convert = function(val, opts) {
// Convert arrays recursively
if (typeof val === 'object' && val.length) {
for (var i = 0; i< val.length; i++ ) {
val[i] = convert(val[i], opts);
// The base function of the library: converts a value from one currency to another
var convert = fx.convert = function(val, opts) {
// Convert arrays recursively
if (typeof val === 'object' && val.length) {
for (var i = 0; i< val.length; i++ ) {
val[i] = convert(val[i], opts);
}
return val;
}
return val;
}

// Make sure we gots some opts
opts = opts || {};

// We need to know the `from` and `to` currencies
if( !opts.from ) opts.from = fx.settings.from;
if( !opts.to ) opts.to = fx.settings.to;
// Make sure we gots some opts
opts = opts || {};

// Multiple the value by the exchange rate
return val * getRate( opts.to, opts.from );
};
// We need to know the `from` and `to` currencies
if( !opts.from ) opts.from = fx.settings.from;
if( !opts.to ) opts.to = fx.settings.to;

// Returns the exchange rate to `target` currency from `base` currency
var getRate = function(to, from) {
// Save bytes in minified version
var rates = fx.rates;

// Make sure the base rate is in the rates object:
rates[fx.base] = 1;

// Throw an error if either rate isn't in the rates array
if ( !rates[to] || !rates[from] ) throw "fx error";

// If `from` currency === fx.base, return the basic exchange rate for the `to` currency
if ( from === fx.base ) {
return rates[to];
}

// If `to` currency === fx.base, return the basic inverse rate of the `from` currency
if ( to === fx.base ) {
return 1 / rates[from];
}
// Multiple the value by the exchange rate
if ( !!opts.rates ) {
return val * getRatesCustom( opts.to, opts.from, opts.rates );
}
return val * getRate( opts.to, opts.from );
};

// Returns the exchange rate to `target` currency from `base` currency
var getRate = fx.getRate = function(to, from) {
// Save bytes in minified version
var rates = fx.rates;

// Make sure the base rate is in the rates object:
rates[fx.base] = 1;

// Throw an error if either rate isn't in the rates array
if ( !rates[to] || !rates[from] ) {
var msg = 'Cannot convert ' + from + ' to ' + to + ': ';

if ( !rates[to] && !rates[from] ) {
msg += 'exhange rates for both currencies are missing';
} else if ( !rates[to] ) {
msg += 'exhange rate for ' + to + ' is missing';
} else if ( !rates[from] ) {
msg += 'exhange rate for ' + from + ' is missing';
}

throw new Error( msg );
}

// Otherwise, return the `to` rate multipled by the inverse of the `from` rate to get the
// relative exchange rate between the two currencies
return rates[to] * (1 / rates[from]);
};
// If `from` currency === fx.base, return the basic exchange rate for the `to` currency
if ( from === fx.base ) {
return rates[to];
}

// If `to` currency === fx.base, return the basic inverse rate of the `from` currency
if ( to === fx.base ) {
return 1 / rates[from];
}

/* --- OOP wrapper and chaining --- */
// Otherwise, return the `to` rate multipled by the inverse of the `from` rate to get the
// relative exchange rate between the two currencies
return rates[to] * (1 / rates[from]);
};

// If fx(val) is called as a function, it returns a wrapped object that can be used OO-style
var fxWrapper = function(val) {
// Experimental: parse strings to pull out currency code and value:
if ( typeof val === "string" ) {
this._v = parseFloat(val.replace(/[^0-9-.]/g, ""));
this._fx = val.replace(/([^A-Za-z])/g, "");
} else {
this._v = val;
}
};

// Expose `wrapper.prototype` as `fx.prototype`
var fxProto = fx.prototype = fxWrapper.prototype;
/* --- OOP wrapper and chaining --- */

// fx(val).convert(opts) does the same thing as fx.convert(val, opts)
fxProto.convert = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(this._v);
return convert.apply(fx, args);
};
// If fx(val) is called as a function, it returns a wrapped object that can be used OO-style
var fxWrapper = function(val) {
// Experimental: parse strings to pull out currency code and value:
if ( typeof val === "string" ) {
this._v = parseFloat(val.replace(/[^0-9-.]/g, ""));
this._fx = val.replace(/([^A-Za-z])/g, "");
} else {
this._v = val;
}
};

// fx(val).from(currency) returns a wrapped `fx` where the value has been converted from
// `currency` to the `fx.base` currency. Should be followed by `.to(otherCurrency)`
fxProto.from = function(currency) {
var wrapped = fx(convert(this._v, {from: currency, to: fx.base}));
wrapped._fx = fx.base;
return wrapped;
};
// Expose `wrapper.prototype` as `fx.prototype`
var fxProto = fx.prototype = fxWrapper.prototype;

// fx(val).to(currency) returns the value, converted from `fx.base` to `currency`
fxProto.to = function(currency) {
return convert(this._v, {from: this._fx ? this._fx : fx.settings.from, to: currency});
// fx(val).convert(opts) does the same thing as fx.convert(val, opts)
fxProto.convert = function() {
var args = Array.prototype.slice.call(arguments);
if (typeof this._v !== 'undefined') {
args.unshift(this._v);
}
return convert.apply(fx, args);
};

// fx(val).from(currency) returns a wrapped `fx` where the value has been converted from
// `currency` to the `fx.base` currency. Should be followed by `.to(otherCurrency)`
fxProto.from = function(currency) {
var wrapped = fx(convert(this._v, {from: currency, to: fx.base}));
wrapped._fx = fx.base;
return wrapped;
};

// fx(val).to(currency) returns the value, converted from `fx.base` to `currency`
fxProto.to = function(currency) {
return convert(this._v, {from: this._fx ? this._fx : fx.settings.from, to: currency});
};

return fx;
};


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

// Export the fx object for CommonJS. If being loaded as an AMD module, define it as such.
// Otherwise, just add `fx` to the global object
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = fx;
exports = module.exports = factory();
}
exports.fx = fx;
exports.factory = factory;
} else if (typeof define === 'function' && define.amd) {
// Return the library as an AMD module:
define([], function() {
return fx;
return factory();
});
} else {
// Use fx.noConflict to restore `fx` back to its original value before money.js loaded.
Expand All @@ -159,7 +177,7 @@
})(root.fx);

// Declare `fx` on the root (global/window) object:
root['fx'] = fx;
root['fx'] = factory();
}

// Root will be `window` in browser or `global` on the server:
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
"Joss Crowcroft <[email protected]> (http://www.josscrowcroft.com)"
],
"dependencies" : {},
"devDependencies": {
"mocha": "^2.3.x",
"chai": "^2.1.x"
},
"repository" : {"type": "git", "url": "git://github.com/openexchangerates/money.js.git"},
"main" : "money.js",
"version" : "0.2.0"
}
"version" : "0.3.0",
"scripts": {
"test": "mocha --reporter spec tests"
}
}
Loading