vue-moment.cjs.js 8.7 KB

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