moment-immutable-methods
Module extends moment factory so each instance returned is decorated with set of immutable methods. Each existing method will receive corresponding immutable version with its original name being appended with Immu
. E.g.:
- for
startOf()
it attaches startOfImmu()
, - for
add()
it attaches addImmu()
- for timezone plugin
tz
method, receives corresponding tzImmu
and so on. All these are generated automatically and do not rely on any sort of hardcoded list so if any new methods become available within new moment.js versions, these should also receive immutable versions.
Existing methods stay exactly same and decorating with this module should be safe for existing code. Also plugins seem to work perfectly and please see sample code below for usage:
var moment = require('moment');
import { momentImmutableMethods } from 'moment-immutable-methods';
momentImmutableMethods(moment);
const aaa = moment({
hour: 5,
minute: 10
});
const bbb = aaa.startOf('day')
console.log(aaa === bbb);
const ccc = bbb.startOf('month');
console.log(aaa === ccc);
console.log(bbb === ccc);
console.log(aaa.format('DD/MM/YY HH:mma'));
console.log(bbb.format('DD/MM/YY HH:mma'));
console.log(ccc.format('DD/MM/YY HH:mma'));
const ddd = moment({
hour: 5,
minute: 10
});
const eee = ddd.startOfImmu('day');
console.log(ddd === eee);
const fff = eee.startOfImmu('month');
console.log(ddd === fff);
console.log(eee === fff);
console.log(ddd.format('DD/MM/YY HH:mma'));
console.log(eee.format('DD/MM/YY HH:mma'));
console.log(fff.format('DD/MM/YY HH:mma'));
extendMoment(moment);
const start = moment('2011-04-15', 'YYYY-MM-DD');
const end = moment('2011-11-27', 'YYYY-MM-DD');
const range = moment.range(start, end);
console.log(typeof range.start.startOfImmu);
const a = moment('2016-03-10');
const b = moment('2016-03-15');
const c = moment('2016-03-29');
const d = moment('2016-04-01');
let range1 = moment.range(a, b);
let range2 = moment.range(b, c);
let range3 = moment.range(c, d);
console.log(range1.adjacent(range2))
console.log(range1.adjacent(range3))
const ggg = moment("2014-06-01T12:00:00Z");
const hhh = ggg.tz('America/Los_Angeles');
console.log(ggg === hhh);
const iii = hhh.tz('America/New_York');
console.log(ggg === iii);
console.log(ggg.format('ha z'));
console.log(hhh.format('ha z'));
console.log(iii.format('ha z'));
const jjj = moment("2014-06-01T12:00:00Z");
const kkk = jjj.tzImmu('America/Los_Angeles');
console.log(jjj === kkk);
const lll = kkk.tzImmu('America/New_York');
console.log(jjj === lll);
console.log(jjj.format('ha z'));
console.log(kkk.format('ha z'));
console.log(lll.format('ha z'));
How to install
npm install --save moment-immutable-methods