<commit_before>"use strict";

var currentLocale = new ReactiveVar();
currentLocale.set(moment.locale());

var originalLocale = moment.locale;

moment.locale = function(key, values){
  var locale = originalLocale(key, values);

  if (key) {
    if (typeof(values) === 'undefined') {
      currentLocale.set(locale);
    }
  }

  return locale;
}

Template.registerHelper('moFormat', function () {
  // Calling this reactive property ensure the helper is updated whenever the
  // moment.locale change
  var locale = currentLocale.get();

  var args = _.toArray(arguments);
  var kw = args.pop();
  var date = args[0] || kw.hash.d;
  var formatToken = args[1] || kw.hash.f;
  var moDate = mo._getMoment(date);
  if (moDate) {
    return moDate.format(mo._getToken(formatToken));
  }
  return;
});

Template.registerHelper('moDiff', function () {
  // Calling this reactive property ensure the helper is updated whenever the
  // moment.locale change
  var locale = currentLocale.get();

  var result;
  var args = _.toArray(arguments);
  var kw = args.pop();
  var dateA = mo._getMoment(args[0] || kw.hash.a);
  var dateB = mo._getMoment(args[1] || kw.hash.b);
  var units = args[2] || kw.hash.units || 'seconds';
  var returnFloat = args[3] || kw.hash.returnFloat;
  if (returnFloat) { returnFloat = true; } else { returnFloat = false; }
  if (dateA && dateB) { result = dateA.diff(dateB, units, returnFloat); }
  return result;
});

Template.registerHelper('moFromNow', function () {
  // Calling this reactive property ensure the helper is updated whenever the
  // moment.locale change
  var locale = currentLocale.get();

  var args = _.toArray(arguments);
  var kw = args.pop();
  var date = args[0] || kw.hash.d;
  var withoutSuffix = args[1] || kw.hash.withoutSuffix;
  if (withoutSuffix) { withoutSuffix = true; } else { withoutSuffix = false; }
  var moDate = mo._getMoment(date);
  if (moDate) { return moDate.fromNow(withoutSuffix); }
  return;
});
<commit_msg>provide a method to change moment locale without overriding its methods and maintaining reactivity<commit_after>"use strict";

var currentLocale = new ReactiveVar();

currentLocale.set(moment.locale());

mo.setLocale = function(locale){
  moment.locale(locale);
  currentLocale.set(moment.locale());
};

Template.registerHelper('moFormat', function () {
  // Calling this reactive property ensure the helper is updated whenever the
  // moment.locale change
  var locale = currentLocale.get();

  var args = _.toArray(arguments);
  var kw = args.pop();
  var date = args[0] || kw.hash.d;
  var formatToken = args[1] || kw.hash.f;
  var moDate = mo._getMoment(date);
  if (moDate) {
    return moDate.format(mo._getToken(formatToken));
  }
  return;
});

Template.registerHelper('moDiff', function () {
  // Calling this reactive property ensure the helper is updated whenever the
  // moment.locale change
  var locale = currentLocale.get();

  var result;
  var args = _.toArray(arguments);
  var kw = args.pop();
  var dateA = mo._getMoment(args[0] || kw.hash.a);
  var dateB = mo._getMoment(args[1] || kw.hash.b);
  var units = args[2] || kw.hash.units || 'seconds';
  var returnFloat = args[3] || kw.hash.returnFloat;
  if (returnFloat) { returnFloat = true; } else { returnFloat = false; }
  if (dateA && dateB) { result = dateA.diff(dateB, units, returnFloat); }
  return result;
});

Template.registerHelper('moFromNow', function () {
  // Calling this reactive property ensure the helper is updated whenever the
  // moment.locale change
  var locale = currentLocale.get();

  var args = _.toArray(arguments);
  var kw = args.pop();
  var date = args[0] || kw.hash.d;
  var withoutSuffix = args[1] || kw.hash.withoutSuffix;
  if (withoutSuffix) { withoutSuffix = true; } else { withoutSuffix = false; }
  var moDate = mo._getMoment(date);
  if (moDate) { return moDate.fromNow(withoutSuffix); }
  return;
});

{'fraction_non_alphanumeric': 0.14223251028806586, 'fraction_numerical': 0.00411522633744856, 'mean_word_length': 3.935279187817259, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7499536', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1230, 'n_words': 341}