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

Commit e072824

Browse files
committed
Merge pull request #14 from butterflyhug/fix-startof
Fix frozenMoment methods where moment uses mutators internally
2 parents 1ae9fc2 + 57f1a67 commit e072824

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

frozen-moment.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,17 @@
8383

8484
function frozenMethodGenerator(name) {
8585
return function () {
86-
return momentProto[name].apply(this.freeze(), arguments);
86+
var thawed = this.thaw();
87+
var result = thawed[name].apply(thawed, arguments);
88+
return (moment.isMoment(result) ? result.freeze() : result);
8789
};
8890
}
8991
function frozenIfArgumentsMethodGenerator(name) {
9092
return function () {
9193
if (arguments.length) {
92-
return momentProto[name].apply(this.freeze(), arguments);
94+
var thawed = this.thaw();
95+
var result = thawed[name].apply(thawed, arguments);
96+
return (moment.isMoment(result) ? result.freeze() : result);
9397
}
9498
return momentProto[name].apply(this);
9599
};

test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ assert(frozenClone.thaw, 'cloning frozen moment creates another frozen moment');
5757
frozenClone.add(1, 'days');
5858
assert(frozenClone.isSame(frozen), 'mutators do not change value of cloned frozen moment');
5959

60+
var frozenStartOfYear = moment.frozen('2015-11-09T09:44:04').startOf('year');
61+
assert(frozenStartOfYear.valueOf() === moment('2015-01-01T00:00:00').valueOf(), 'start of year is calculated accurately');
62+
63+
assert(frozenStartOfYear.format().substr(0, 19) === '2015-01-01T00:00:00', 'format works with frozen moment')
64+
6065

6166
// prototype chain and moment.frozen.fn
6267

0 commit comments

Comments
 (0)