-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue-moment.js
172 lines (133 loc) · 4.55 KB
/
vue-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
var moment = require('jalali-moment');
module.exports = {
install: function (Vue, options) {
Object.defineProperties(Vue.prototype, {
$moment: {
get: function() {
return moment;
},
},
});
if (options && options.moment) {
moment = options.moment
}
Vue.moment = moment;
Vue.filter('moment', function() {
var args = Array.prototype.slice.call(arguments),
input = args.shift(),
date;
if (Array.isArray(input) && typeof input[0] === 'string') {
// If input is array, assume we're being passed a format pattern to parse against.
// Format pattern will accept an array of potential formats to parse against.
// Date string should be at [0], format pattern(s) should be at [1]
date = moment(string = input[0], formats = input[1], true);
} else if (typeof input === 'number') {
// If input is an integer, assume it's a Unix timestamp.
date = moment.unix(input);
} else {
// Otherwise, throw the input at moment and see what happens...
date = moment(input);
}
if (!input || !date.isValid()) {
// Log a warning if moment couldn't reconcile the input. Better than throwing an error?
console.warn('Could not build a valid `moment` object from input.');
return input;
}
function parse() {
var args = Array.prototype.slice.call(arguments),
method = args.shift();
switch (method) {
case 'add':
// Mutates the original moment by adding time.
// http://momentjs.com/docs/#/manipulating/add/
var addends = args.shift()
.split(',')
.map(Function.prototype.call, String.prototype.trim);
var obj = {};
for (var n = 0; n < addends.length; n++) {
var addend = addends[n].split(' ');
obj[addend[1]] = addend[0];
}
date = date.add(obj);
break;
case 'subtract':
// Mutates the original moment by subtracting time.
// http://momentjs.com/docs/#/manipulating/subtract/
var subtrahends = args.shift()
.split(',')
.map(Function.prototype.call, String.prototype.trim);
obj = {};
for (var n = 0; n < subtrahends.length; n++) {
var subtrahend = subtrahends[n].split(' ');
obj[subtrahend[1]] = subtrahend[0];
}
date = date.subtract(obj);
break;
case 'from':
// Display a moment in relative time, either from now or from a specified date.
// http://momentjs.com/docs/#/displaying/fromnow/
var from = 'now';
if (args[0] == 'now') args.shift();
if (moment(args[0]).isValid()) {
// If valid, assume it is a date we want the output computed against.
from = moment(args.shift());
}
var removeSuffix = false;
if (args[0] === true) {
args.shift();
var removeSuffix = true;
}
if (from != 'now') {
date = date.from(from, removeSuffix);
break;
}
date = date.fromNow(removeSuffix);
break;
case 'diff':
// Mutates the original moment by doing a difference with another date.
// http://momentjs.com/docs/#/displaying/difference/
var dateDiff = 'now';
if (args[0] == 'now') args.shift();
if (moment(args[0]).isValid()) {
dateDiff = moment(args.shift());
}
var units = '';
if (args[0]) {
var units = args.shift();
}
var floatingValue = false;
if (args[0] === true) {
args.shift();
var floatingValue = true;
}
date = date.diff(dateDiff, units, floatingValue)
break;
case 'calendar':
// Formats a date with different strings depending on how close to a certain date (today by default) the date is.
// http://momentjs.com/docs/#/displaying/calendar-time/
var referenceTime = moment();
if (moment(args[0]).isValid()) {
// If valid, assume it is a date we want the output computed against.
referenceTime = moment(args.shift());
}
date = date.calendar(referenceTime);
break;
case 'timezone':
// Mutates the original moment by converting to a new timezone.
// https://momentjs.com/timezone/docs/#/using-timezones/converting-to-zone/
date = date.tz(args.shift());
break;
default:
// Format
// Formats a date by taking a string of tokens and replacing them with their corresponding values.
// http://momentjs.com/docs/#/displaying/format/
var format = method;
date = date.format(format);
}
if (args.length) parse.apply(parse, args);
}
parse.apply(parse, args);
return date;
});
},
};