Skip to content

Instantly share code, notes, and snippets.

@timrwood
Created September 16, 2014 17:16
Show Gist options
  • Save timrwood/fcd0925bb779f4374a7c to your computer and use it in GitHub Desktop.
Save timrwood/fcd0925bb779f4374a7c to your computer and use it in GitHub Desktop.
Immutable Moments
(function (root, factory) {
"use strict";
if (typeof define === 'function' && define.amd) {
define(['moment'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('moment'));
} else {
factory(root.moment);
}
}(this, function (moment) {
"use strict";
function wrap (name) {
var old = moment.fn[name];
moment.fn[name] = function () {
return old.apply(this.clone(), arguments);
};
}
var methods = 'add subtract startOf endOf'.split(' ');
for (var i = 0; i < methods.length; i++) {
wrap(methods[i]);
}
}));
@timrwood
Copy link
Author

@jehoshua02
Copy link

Javascript is magic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment