npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

moment-immutable-methods

v1.0.9

Published

Decorates all the instances returned by moment factory with immutable methods.

Downloads

259

Readme

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'); // or moment-timezone 
import { momentImmutableMethods } from 'moment-immutable-methods';

// to decorate instances with immutable methods we need to extend moment factory as below:
momentImmutableMethods(moment);

// now every instance returned by moment will have Immu methods attached.

// MUTABLE EXAMPLE
// just to show default behaviour with mutable methods as they come with moment by default. Immutable example is further down
const aaa = moment({
  hour: 5,
  minute: 10
});
// Moment {_isAMomentObject: true, _i: {…}, _isUTC: false, _pf: {…}, _locale: Locale, …}
const bbb = aaa.startOf('day')
// Moment {_isAMomentObject: true, _i: {…}, _isUTC: false, _pf: {…}, _locale: Locale, …}
console.log(aaa === bbb);
// true
const ccc = bbb.startOf('month');
// Moment {_isAMomentObject: true, _i: {…}, _isUTC: false, _pf: {…}, _locale: Locale, …}
console.log(aaa === ccc);
// true
console.log(bbb === ccc);
// true
console.log(aaa.format('DD/MM/YY HH:mma'));
// "08/04/18 00:00am"
console.log(bbb.format('DD/MM/YY HH:mma'));
// "08/04/18 00:00am"
console.log(ccc.format('DD/MM/YY HH:mma'));
// "08/04/18 00:00am"


// IMMUTABLE EXAMPLE
// we using immutable methods that were attached to every instance, these have Immu appended to original name
const ddd = moment({
  hour: 5,
  minute: 10
});
// Moment {_isAMomentObject: true, _i: {…}, _isUTC: false, _pf: {…}, _locale: Locale, …}
const eee = ddd.startOfImmu('day');
// Moment {_isAMomentObject: true, _i: {…}, _isUTC: false, _pf: {…}, _locale: Locale, …}
console.log(ddd === eee);
// false
const fff = eee.startOfImmu('month');
// Moment {_isAMomentObject: true, _i: {…}, _isUTC: false, _pf: {…}, _locale: Locale, …}
console.log(ddd === fff);
// false
console.log(eee === fff);
// false
console.log(ddd.format('DD/MM/YY HH:mma'));
// "14/04/18 05:10am"
console.log(eee.format('DD/MM/YY HH:mma'));
// "14/04/18 00:00am"
console.log(fff.format('DD/MM/YY HH:mma'));
// "08/04/18 00:00am"


// PLUGINS
// original methods are not modified in any way and plugins should work as expected. If plugin returns moment, this also should have immutable methods attached automatically. Below example is using moment-range plugin 

// assume its already imported with
// import { extendMoment } from 'moment-range'; 

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); // function

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)) // true
console.log(range1.adjacent(range3)) // false


// timezone plugin, assume its already imported instead of plain moment

const ggg = moment("2014-06-01T12:00:00Z");

const hhh = ggg.tz('America/Los_Angeles'); // 5am PDT
console.log(ggg === hhh); //true

const iii = hhh.tz('America/New_York'); // 8am EDT
console.log(ggg === iii); // true

console.log(ggg.format('ha z')); // 8am EDT
console.log(hhh.format('ha z')); // 8am EDT
console.log(iii.format('ha z')); // 8am EDT

const jjj = moment("2014-06-01T12:00:00Z");

const kkk = jjj.tzImmu('America/Los_Angeles'); // 5am PDT
console.log(jjj === kkk); // false

const lll = kkk.tzImmu('America/New_York'); // 8am EDT
console.log(jjj === lll); // false

console.log(jjj.format('ha z')); // 10pm
console.log(kkk.format('ha z')); // 5am PDT
console.log(lll.format('ha z')); // 8am EDT

How to install

npm install --save moment-immutable-methods