openexchangerates / money.js

money.js is a tiny (1kb) javascript currency conversion library, for web & nodeJS
http://openexchangerates.github.io/money.js
MIT License
1.46k stars 127 forks source link

Be able to get another instance of the module #25

Open gflandre opened 8 years ago

gflandre commented 8 years ago

I hope this project is still maintained... It doesn't really look like it is though.

When using the lib in node, it is not possible to use different rates at different places. This is due to the fact that the module is a singleton.

I suggest a method to get a fresh instance of fx on which different rates could be set. It could work like this:

var fx = require('money');
// Global settings
fx.base = 'EUR';
fx.rates = { 'USD': 0.9 };

// convert using the global settings

var myVerySpecialMethod = function () {
  var myLocalFx = fx.clone();
  // these settings are only applied to that local instance
  myLocalFx.base = 'CHF';
  myLocalFx.rates = { 'USD': 2.5 };

  // convert using the local settings
};

This way the current API would still work.

gflandre commented 8 years ago

Some inspiration: https://github.com/starsirius/money.js/commit/a736a83912d7f62f360f8d623c785f148e8f50d4