vue-moment.es.js 8.7 KB

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