moment-biz
Version:
Handle business days & weekends over multiple years
40 lines (30 loc) • 994 B
JavaScript
;
const moment = require('moment');
// Detect if we are on node or not
const isNode = new Function('try { return this === global; } catch(e) { return false; } ');
const loaded = {};
function wrapLocale(obj) {
const localeFn = obj.locale;
// On browsers, locales should be loaded manually so this makes no sense to wrap locale anyway
/* istanbul ignore if */
if (!isNode())
return;
// Node.js implementation
obj.locale = function(locale) {
if (!arguments.length || loaded[locale] || locale.split('-').length !== 2)
return localeFn.call(this, ...arguments);
const globalLocale = moment.locale();
try {
const path = require('path');
require(path.join(__dirname, '../locale', locale));
loaded[locale] = true;
moment.locale(globalLocale);
}
finally {
return localeFn.call(this, ...arguments);
}
};
}
// Wrap locale functions to auto load locales on node.js
wrapLocale(moment);
wrapLocale(moment.fn);