forked from farnoy/frozen-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frozen-moment.js
268 lines (238 loc) · 6.53 KB
/
frozen-moment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
(function (global, factory) {
if (typeof exports === 'object') {
module.exports = factory(require('moment'));
} else if (typeof define === 'function' && define.amd) {
define(['moment'], factory);
} else {
global.moment = factory(global.moment);
}
}(this, function (moment) {
if (!moment) {
throw new Error('frozen-moment cannot find moment');
}
function FrozenMoment() {}
function FrozenDuration() {}
var momentProto = moment.fn;
var durationProto = moment.duration.fn;
var includes = Array.prototype.includes || function arrayIncludes(value) {
var length = this.length;
for (var i = 0; i < length; i++) {
if (this[i] === value) {
return true;
}
}
return false;
};
var immutableMomentMethods = [
// Get
'weeksInYear',
'isoWeeksInYear',
'get',
'max',
'min',
// Display
'format',
'fromNow',
'from',
'calendar',
'diff',
'valueOf',
'unix',
'add',
'daysInMonth',
'toArray',
'toJSON',
'toISOString',
// Query
'isBefore',
'isSame',
'isAfter',
'isBetween',
'isLeapYear',
'isDST',
'isDSTShifted',
// Frozen Moment
'freeze'
];
// moment has a lot of overloaded getters and setters, where calling the
// method with no arguments will run an immutable getter, and calling the
// method with one or more arguments will run a mutating setter.
var momentMutatorsIfArguments = [
// Get + Set
'millisecond', 'milliseconds',
'second', 'seconds',
'minute', 'minutes',
'hour', 'hours',
'date', 'dates',
'day', 'days',
'weekday',
'isoWeekday',
'dayOfYear',
'week', 'weeks',
'isoWeek', 'isoWeeks',
'month', 'months',
'quarter',
'year', 'years',
'weekYear',
'isoWeekYear',
'set'
];
var immutableDurationMethods = [
'humanize',
'asMilliseconds',
'asSeconds',
'asMinutes',
'asHours',
'asDays',
'asWeeks',
'asMonths',
'asYears',
'as',
'get',
'format',
'valueOf',
'toJSON',
'toISOString',
'toString',
// Frozen Moment
'freeze'
];
// moment has a lot of overloaded getters and setters, where calling the
// method with no arguments will run an immutable getter, and calling the
// method with one or more arguments will run a mutating setter.
var durationMutatorsIfArguments = [
'milliseconds',
'seconds',
'minutes',
'hours',
'days',
'weeks',
'months',
'years'
];
function frozenMethodGenerator(name, isMoment) {
return function () {
var thawed = this.thaw();
var result = thawed[name].apply(thawed, arguments);
return (isMoment(result) ? result.freeze() : result);
};
}
function frozenIfArgumentsMethodGenerator(name, isMoment, upstreamProto) {
return function () {
if (arguments.length) {
var thawed = this.thaw();
var result = thawed[name].apply(thawed, arguments);
return (isMoment(result) ? result.freeze() : result);
}
return upstreamProto[name].apply(this);
};
}
function mixin(dest, props) {
for (var prop in props) {
if (props.hasOwnProperty(prop)) {
dest[prop] = props[prop];
}
}
}
function freezeMoment() {
var props = momentProto.clone.apply(this);
var frozen = new FrozenMoment();
mixin(frozen, props);
return frozen;
}
function thawMoment() {
return momentProto.clone.call(this);
}
function buildFrozenPrototype() {
function MomentSubclass() {}
MomentSubclass.prototype = momentProto;
var frozenProto = new MomentSubclass();
for (var key in momentProto) {
if (key === "freeze") {
// never wrap Frozen Moment's freeze method
continue;
}
if (momentProto.hasOwnProperty(key)
&& typeof momentProto[key] === 'function'
&& !includes.call(immutableMomentMethods, key)) {
if (!includes.call(momentMutatorsIfArguments, key)) {
frozenProto[key] = frozenMethodGenerator(key, moment.isMoment);
} else {
frozenProto[key] = frozenIfArgumentsMethodGenerator(key, moment.isMoment, momentProto);
}
}
}
frozenProto.isFrozen = function isFrozen() {
return true;
};
frozenProto.clone = freezeMoment;
frozenProto.thaw = thawMoment;
FrozenMoment.prototype = moment.frozen.fn = frozenProto;
}
function freezeDuration() {
var props = moment.duration(this);
var frozen = new FrozenDuration();
mixin(frozen, props);
return frozen;
}
function thawDuration() {
return moment.duration(this);
}
function buildFrozenDurationPrototype() {
function DurationSubclass() {}
DurationSubclass.prototype = durationProto;
var frozenProto = new DurationSubclass();
for (var key in durationProto) {
if (key === "freeze") {
// never wrap Frozen Moment's freeze method
continue;
}
if (durationProto.hasOwnProperty(key)
&& typeof durationProto[key] === 'function'
&& !includes.call(immutableDurationMethods, key)) {
if (!includes.call(durationMutatorsIfArguments, key)) {
frozenProto[key] = frozenMethodGenerator(key, moment.isDuration);
} else {
frozenProto[key] = frozenIfArgumentsMethodGenerator(key, moment.isDuration, durationProto);
}
}
}
frozenProto.isFrozen = function isFrozen() {
return true;
};
frozenProto.clone = freezeDuration;
frozenProto.thaw = thawDuration;
FrozenDuration.prototype = moment.frozenDuration.fn = frozenProto;
}
// wire up prototypes
momentProto.isFrozen = function isFrozen() {
return false;
};
momentProto.freeze = freezeMoment;
moment.frozen = function frozen() {
return moment.apply(this, arguments).freeze();
};
moment.frozenUtc = function frozenUtc() {
return moment.utc.apply(this, arguments).freeze();
};
moment.frozen.autowrap = buildFrozenPrototype;
moment.frozen.unwrap = function unwrapMethods(/* names... */) {
var length = arguments.length;
for (var i = 0, name = arguments[i]; i < length; i++) {
if (FrozenMoment.prototype[name]) {
delete FrozenMoment.prototype[name];
}
immutableMomentMethods.push(name);
}
};
durationProto.isFrozen = function isFrozen() {
return false;
};
durationProto.freeze = freezeDuration;
moment.frozenDuration = function frozen() {
return moment.duration.apply(this, arguments).freeze();
};
buildFrozenPrototype();
buildFrozenDurationPrototype();
return moment;
}));