|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { |
| 4 | + return typeof obj; |
| 5 | +} : function (obj) { |
| 6 | + return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; |
| 7 | +}; |
| 8 | + |
| 9 | +function _toConsumableArray(arr) { |
| 10 | + if (Array.isArray(arr)) { |
| 11 | + for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { |
| 12 | + arr2[i] = arr[i]; |
| 13 | + }return arr2; |
| 14 | + } else { |
| 15 | + return Array.from(arr); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +module.exports = { |
| 20 | + install: function install(Vue, options) { |
| 21 | + var moment = options && options.moment ? options.moment : require('moment'); |
| 22 | + |
| 23 | + Object.defineProperties(Vue.prototype, { |
| 24 | + $moment: { |
| 25 | + get: function get() { |
| 26 | + return moment; |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + Vue.moment = moment; |
| 32 | + |
| 33 | + Vue.filter('moment', function () { |
| 34 | + var arguments$1 = arguments; |
| 35 | + |
| 36 | + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { |
| 37 | + args[_key] = arguments$1[_key]; |
| 38 | + } |
| 39 | + |
| 40 | + args = Array.prototype.slice.call(args); |
| 41 | + var input = args.shift(); |
| 42 | + var date = void 0; |
| 43 | + |
| 44 | + if (Array.isArray(input) && typeof input[0] === 'string') { |
| 45 | + // If input is array, assume we're being passed a format pattern to parse against. |
| 46 | + // Format pattern will accept an array of potential formats to parse against. |
| 47 | + // Date string should be at [0], format pattern(s) should be at [1] |
| 48 | + date = moment(input[0], input[1], true); |
| 49 | + } else if (typeof input === 'number') { |
| 50 | + if (input.toString().length < 12) { |
| 51 | + // If input is an integer with fewer than 12 digits, assume Unix seconds... |
| 52 | + date = moment.unix(input); |
| 53 | + } else { |
| 54 | + // ..otherwise, assume milliseconds. |
| 55 | + date = moment(input); |
| 56 | + } |
| 57 | + } else { |
| 58 | + // Otherwise, throw the input at moment and see what happens... |
| 59 | + date = moment(input); |
| 60 | + } |
| 61 | + |
| 62 | + if (!input || !date.isValid()) { |
| 63 | + // Log a warning if moment couldn't reconcile the input. Better than throwing an error? |
| 64 | + console.warn('Could not build a valid `moment` object from input.'); |
| 65 | + return input; |
| 66 | + } |
| 67 | + |
| 68 | + function parse() { |
| 69 | + var arguments$1 = arguments; |
| 70 | + |
| 71 | + for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { |
| 72 | + args[_key2] = arguments$1[_key2]; |
| 73 | + } |
| 74 | + |
| 75 | + args = Array.prototype.slice.call(args); |
| 76 | + var method = args.shift(); |
| 77 | + |
| 78 | + switch (method) { |
| 79 | + case 'add': |
| 80 | + { |
| 81 | + /* |
| 82 | + * Mutates the original moment by adding time. |
| 83 | + * http://momentjs.com/docs/#/manipulating/add/ |
| 84 | + */ |
| 85 | + |
| 86 | + var addends = args.shift().split(',').map(Function.prototype.call, String.prototype.trim); |
| 87 | + var obj = {}; |
| 88 | + |
| 89 | + for (var n = 0; n < addends.length; n++) { |
| 90 | + var addend = addends[n].split(' '); |
| 91 | + obj[addend[1]] = addend[0]; |
| 92 | + } |
| 93 | + date.add(obj); |
| 94 | + break; |
| 95 | + } |
| 96 | + |
| 97 | + case 'subtract': |
| 98 | + { |
| 99 | + /* |
| 100 | + * Mutates the original moment by subtracting time. |
| 101 | + * http://momentjs.com/docs/#/manipulating/subtract/ |
| 102 | + */ |
| 103 | + |
| 104 | + var subtrahends = args.shift().split(',').map(Function.prototype.call, String.prototype.trim); |
| 105 | + var _obj = {}; |
| 106 | + |
| 107 | + for (var _n = 0; _n < subtrahends.length; _n++) { |
| 108 | + var subtrahend = subtrahends[_n].split(' '); |
| 109 | + _obj[subtrahend[1]] = subtrahend[0]; |
| 110 | + } |
| 111 | + date.subtract(_obj); |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + case 'from': |
| 116 | + { |
| 117 | + /* |
| 118 | + * Display a moment in relative time, either from now or from a specified date. |
| 119 | + * http://momentjs.com/docs/#/displaying/fromnow/ |
| 120 | + */ |
| 121 | + |
| 122 | + var from = 'now'; |
| 123 | + var removeSuffix = false; |
| 124 | + |
| 125 | + if (args[0] === 'now') { args.shift(); } |
| 126 | + // If valid, assume it is a date we want the output computed against. |
| 127 | + if (moment(args[0]).isValid()) { from = moment(args.shift()); } |
| 128 | + |
| 129 | + if (args[0] === true) { |
| 130 | + args.shift(); |
| 131 | + removeSuffix = true; |
| 132 | + } |
| 133 | + |
| 134 | + if (from !== 'now') { |
| 135 | + date = date.from(from, removeSuffix); |
| 136 | + } else { |
| 137 | + date = date.fromNow(removeSuffix); |
| 138 | + } |
| 139 | + break; |
| 140 | + } |
| 141 | + |
| 142 | + case 'diff': |
| 143 | + { |
| 144 | + /* |
| 145 | + * Mutates the original moment by doing a difference with another date. |
| 146 | + * http://momentjs.com/docs/#/displaying/difference/ |
| 147 | + */ |
| 148 | + |
| 149 | + var referenceTime = moment(); |
| 150 | + var units = ''; |
| 151 | + var float = false; |
| 152 | + |
| 153 | + if (moment(args[0]).isValid()) { |
| 154 | + // If valid, assume it is a date we want the output computed against. |
| 155 | + referenceTime = moment(args.shift()); |
| 156 | + } else if (args[0] === null || args[0] === 'now') { |
| 157 | + // If null or 'now', remove argument and proceed with default referenceTime. |
| 158 | + args.shift(); |
| 159 | + } |
| 160 | + |
| 161 | + if (args[0]) { units = args.shift(); } |
| 162 | + |
| 163 | + if (args[0] === true) { float = args.shift(); } |
| 164 | + |
| 165 | + date = date.diff(referenceTime, units, float); |
| 166 | + break; |
| 167 | + } |
| 168 | + |
| 169 | + case 'calendar': |
| 170 | + { |
| 171 | + /* |
| 172 | + * Formats a date with different strings depending on how close |
| 173 | + * to a certain date (today by default) the date is. |
| 174 | + * http://momentjs.com/docs/#/displaying/calendar-time/ |
| 175 | + */ |
| 176 | + |
| 177 | + var _referenceTime = moment(); |
| 178 | + var formats = {}; |
| 179 | + |
| 180 | + if (moment(args[0]).isValid()) { |
| 181 | + // If valid, assume it is a date we want the output computed against. |
| 182 | + _referenceTime = moment(args.shift()); |
| 183 | + } else if (args[0] === null || args[0] === 'now') { |
| 184 | + // If null or 'now', remove argument and proceed with default referenceTime. |
| 185 | + args.shift(); |
| 186 | + } |
| 187 | + |
| 188 | + if (_typeof(args[0]) === 'object') { formats = args.shift(); } |
| 189 | + |
| 190 | + date = date.calendar(_referenceTime, formats); |
| 191 | + break; |
| 192 | + } |
| 193 | + |
| 194 | + case 'utc': |
| 195 | + { |
| 196 | + /* |
| 197 | + * Mutates the original moment by converting to UTC |
| 198 | + * https://momentjs.com/docs/#/manipulating/utc/ |
| 199 | + */ |
| 200 | + date.utc(); |
| 201 | + break; |
| 202 | + } |
| 203 | + |
| 204 | + case 'timezone': |
| 205 | + { |
| 206 | + /* |
| 207 | + * Mutates the original moment by converting to a new timezone. |
| 208 | + * https://momentjs.com/timezone/docs/#/using-timezones/converting-to-zone/ |
| 209 | + */ |
| 210 | + date.tz(args.shift()); |
| 211 | + break; |
| 212 | + } |
| 213 | + |
| 214 | + default: |
| 215 | + { |
| 216 | + /* |
| 217 | + * Formats a date by taking a string of tokens and replacing |
| 218 | + * them with their corresponding values. |
| 219 | + * http://momentjs.com/docs/#/displaying/format/ |
| 220 | + */ |
| 221 | + |
| 222 | + var format = method; |
| 223 | + date = date.format(format); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + if (args.length) { parse.apply(parse, args); } |
| 228 | + } |
| 229 | + |
| 230 | + parse.apply(parse, args); |
| 231 | + |
| 232 | + return date; |
| 233 | + }); |
| 234 | + |
| 235 | + Vue.filter('duration', function () { |
| 236 | + var arguments$1 = arguments; |
| 237 | + |
| 238 | + for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { |
| 239 | + args[_key3] = arguments$1[_key3]; |
| 240 | + } |
| 241 | + |
| 242 | + /* |
| 243 | + * Basic pass-through filter for leveraging moment.js's ability |
| 244 | + * to manipulate and display durations. |
| 245 | + * https://momentjs.com/docs/#/durations/ |
| 246 | + */ |
| 247 | + args = Array.prototype.slice.call(args); |
| 248 | + var input = args.shift(); |
| 249 | + var method = args.shift(); |
| 250 | + |
| 251 | + function createDuration(time) { |
| 252 | + if (!Array.isArray(time)) { time = [time]; } |
| 253 | + var result = moment.duration.apply(moment, _toConsumableArray(time)); |
| 254 | + if (!result.isValid()) { console.warn('Could not build a valid `duration` object from input.'); } |
| 255 | + return result; |
| 256 | + } |
| 257 | + var duration = createDuration(input); |
| 258 | + |
| 259 | + if (method === 'add' || method === 'subtract') { |
| 260 | + // Generates a duration object and either adds or subtracts it |
| 261 | + // from our original duration. |
| 262 | + var durationChange = createDuration(args); |
| 263 | + duration[method](durationChange); |
| 264 | + } else if (duration && duration[method]) { |
| 265 | + var _duration; |
| 266 | + |
| 267 | + // This gives a full proxy to moment.duration functions. |
| 268 | + duration = (_duration = duration)[method].apply(_duration, _toConsumableArray(args)); |
| 269 | + } |
| 270 | + |
| 271 | + return duration; |
| 272 | + }); |
| 273 | + } |
| 274 | +}; |
0 commit comments