-
Notifications
You must be signed in to change notification settings - Fork 5
/
moment-cache.js
101 lines (84 loc) · 3.01 KB
/
moment-cache.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
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var version = '0.1.3';
var initial = typeof moment !== 'undefined' ? moment : null;
if (typeof require !== 'undefined' && (typeof moment === 'undefined' || moment === null)) {
initial = require('moment') || initial;
}
(function (moment, scope) {
'use strict';
if (scope == null) {
if (typeof self != 'undefined') {
scope = self;
} else if (typeof root != 'undefined') {
scope = root;
}
}
var _cache = {};
var _prevCache = null;
var toMoment = function toMoment(date, format, clone) {
var momentResult = moment(date, format);
return clone ? momentResult.clone() : momentResult;
};
var initCache = function initCache(opts) {
if (opts.storage) {
if (_prevCache == null) _prevCache = _cache;
_cache = opts.storage;
}
};
var getKey = function getKey(date, format) {
var dateType = typeof date === 'undefined' ? 'undefined' : _typeof(date);
if (dateType === 'string') {
var currentKey = new Date(date);
if (format) {
return isNaN(currentKey) ? date : currentKey.toString();
} else {
return currentKey.toString();
}
} else if (dateType === 'number') {
var currentDate = new Date(date);
return currentDate.toString();
} else if (date instanceof Date) {
return date.toString();
} else {
return null;
}
};
var getFromCache = function getFromCache(key, clone) {
var cache = _cache[key];
if (cache == null) {
return;
} else {
return clone ? _cache[key].clone() : _cache[key];
}
};
var addToCache = function addToCache(key, date, format, clone) {
var added = _cache[key] = toMoment(date, format, true);
return clone ? added.clone() : added;
};
var getCache = function getCache(date, format, clone) {
var result = void 0;
if (clone == null) clone = true;
if (typeof format === 'boolean') clone = format;
var key = getKey(date);
if (key) {
result = getFromCache(key, clone) || addToCache(key, date, format, clone);
} else {
result = toMoment(date, format, clone);
}
return result;
};
for (var key in moment) {
if (moment.hasOwnProperty(key)) getCache[key] = moment[key];
}
getCache.updateStorage = function (storage) {
if (storage == null && _prevCache) {
storage = _prevCache;
};
initCache({ storage: storage });
return getCache;
};
getCache.version = version;
var momentCache = moment.fn.cache = getCache;
(typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object' && typeof module !== 'undefined' ? module.exports = momentCache : typeof define === 'function' && define.amd ? define(momentCache) : scope != null ? scope.momentCache = momentCache : null;
})(initial, undefined);