Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Feature request: a means to make every moment() instance freeze #20

Open
ericlau-solid opened this issue May 23, 2016 · 5 comments
Open

Comments

@ericlau-solid
Copy link

I wish there is a global feature to have all moments be immutable by default (ie. call .freeze()), rather than having to remember to use .freeze() function for each moment instance. I think that's worthwhile to do.

@butterflyhug
Copy link
Contributor

Apologies on my slow response time while on vacation.

I can certainly see value in this idea, but it's a little tricky to make it happen as a plugin. IMO the simplest implementation would extend moment.frozen so that its API could serve as a drop-in replacement for the global moment API. Then, to "enable" immutability globally, you'd simply overwrite the global moment instance: moment = moment.frozen. Does that sound like a good path forward, or did you have a different solution in mind?

I'd welcome a PR (with tests) to solve this!

@rbalicki2
Copy link

I'm concerned about static properties of moment. We have code that relies on (say) moment.weekdays, so if it's going to be a true drop-in replacement, moment.frozen.weekdays needs to be there as well.

Likewise, it would be really helpful for the other constructors (e.g. moment.unix) to also return a frozen oment.

@butterflyhug
Copy link
Contributor

@rbalicki2 I've been working recently with Moment's maintainers to get Frozen Moment's features into an official distribution of Moment. I just opened a PR on a draft RFC to make that happen. We're still sorting out how we'll want to package this, but I think my proposed user-facing API should address your concerns -- let me know what you think!

I'll be working to implement that RFC after we nail down the remaining details. (I think most of the remaining uncertainty centers around distribution and documentation plans.)

@dantman
Copy link

dantman commented Apr 23, 2017

Rather than moment = moment.frozen how about making it so that import moment from 'frozen-moment'; returns a version of moment that has all the required statics, etc... but always returns frozen moments.

@butterflyhug
Copy link
Contributor

That would make sense for folks who are using a module system. That namespace would still need to be available on the moment global for the folks who just use unbundled <script> tags in a browser.

Not sure if/when I'll get the time to do this myself, but I'd certainly welcome a PR with tests.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants