xsoh / moment-hijri

A Hijri calendar (Based on Umm al-Qura calculations) plugin for moment.js
http://momentjs.com/docs/#/plugins/hijri/
MIT License
200 stars 82 forks source link

Export as high order function #26

Open lutherism opened 7 years ago

lutherism commented 7 years ago

Hello. I'm concerned using this library has implicit conflicts with other plugins. By adding methods to the singleton moment object at import time, it's possible to have conflicts at a distance in an app.

I'd advocate for exporting a "High Order Function" instead, so that the consumer can decide when/where to apply the plugin, and that will leave the original module unmodified.

ie:

require('moment');
var moment = require('moment-hijri');

becomes

var moment = require('moment-hijri')(require('moment'));

or in es6

import moment from 'moment';
import hijriify from 'moment-hijri';

const hijriMoment = hijiriify(moment);

Also remove need for AMD export in repo, as AMD projects could do:

define(['moment', 'moment-hijri'], function(moment, hijriify) {
  var hijriMoment = hijriify(moment);
});
cmttan commented 6 years ago

were you able to get moment/moment-hijri working in your project? I get an error: Uncaught TypeError: moment.localeData is not a function from this line extend(getPrototypeOf(moment.localeData()), { of line 274 in moment-hijri.js when I try to use it below. When I go and examine moment at this line, i see image

I am using parceljs to bundle my application though.

import moment from "moment";
import momenti from "moment-hijri";

console.log("moment:", moment());
console.log("momenti:", momenti);