index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. var formattingTokens = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g;
  2. var match1 = /\d/; // 0 - 9
  3. var match2 = /\d\d/; // 00 - 99
  4. var match3 = /\d{3}/; // 000 - 999
  5. var match4 = /\d{4}/; // 0000 - 9999
  6. var match1to2 = /\d\d?/; // 0 - 99
  7. var matchUpperCaseAMPM = /[AP]M/;
  8. var matchLowerCaseAMPM = /[ap]m/;
  9. var matchSigned = /[+-]?\d+/; // -inf - inf
  10. var matchOffset = /[+-]\d\d:?\d\d/; // +00:00 -00:00 +0000 or -0000
  11. var matchWord = /\d*[^\s\d-:/()]+/; // Word
  12. var locale;
  13. function offsetFromString(string) {
  14. var parts = string.match(/([+-]|\d\d)/g);
  15. var minutes = +(parts[1] * 60) + +parts[2];
  16. return minutes === 0 ? 0 : parts[0] === '+' ? -minutes : minutes; // eslint-disable-line no-nested-ternary
  17. }
  18. var addInput = function addInput(property) {
  19. return function (input) {
  20. this[property] = +input;
  21. };
  22. };
  23. var zoneExpressions = [matchOffset, function (input) {
  24. var zone = this.zone || (this.zone = {});
  25. zone.offset = offsetFromString(input);
  26. }];
  27. var getLocalePart = function getLocalePart(name) {
  28. var part = locale[name];
  29. return part && (part.indexOf ? part : part.s.concat(part.f));
  30. };
  31. var expressions = {
  32. A: [matchUpperCaseAMPM, function (input) {
  33. this.afternoon = input === 'PM';
  34. }],
  35. a: [matchLowerCaseAMPM, function (input) {
  36. this.afternoon = input === 'pm';
  37. }],
  38. S: [match1, function (input) {
  39. this.milliseconds = +input * 100;
  40. }],
  41. SS: [match2, function (input) {
  42. this.milliseconds = +input * 10;
  43. }],
  44. SSS: [match3, function (input) {
  45. this.milliseconds = +input;
  46. }],
  47. s: [match1to2, addInput('seconds')],
  48. ss: [match1to2, addInput('seconds')],
  49. m: [match1to2, addInput('minutes')],
  50. mm: [match1to2, addInput('minutes')],
  51. H: [match1to2, addInput('hours')],
  52. h: [match1to2, addInput('hours')],
  53. HH: [match1to2, addInput('hours')],
  54. hh: [match1to2, addInput('hours')],
  55. D: [match1to2, addInput('day')],
  56. DD: [match2, addInput('day')],
  57. Do: [matchWord, function (input) {
  58. var _locale = locale,
  59. ordinal = _locale.ordinal;
  60. var _input$match = input.match(/\d+/);
  61. this.day = _input$match[0];
  62. if (!ordinal) return;
  63. for (var i = 1; i <= 31; i += 1) {
  64. if (ordinal(i).replace(/\[|\]/g, '') === input) {
  65. this.day = i;
  66. }
  67. }
  68. }],
  69. M: [match1to2, addInput('month')],
  70. MM: [match2, addInput('month')],
  71. MMM: [matchWord, function (input) {
  72. var months = getLocalePart('months');
  73. var monthsShort = getLocalePart('monthsShort');
  74. var matchIndex = (monthsShort || months.map(function (_) {
  75. return _.substr(0, 3);
  76. })).indexOf(input) + 1;
  77. if (matchIndex < 1) {
  78. throw new Error();
  79. }
  80. this.month = matchIndex % 12 || matchIndex;
  81. }],
  82. MMMM: [matchWord, function (input) {
  83. var months = getLocalePart('months');
  84. var matchIndex = months.indexOf(input) + 1;
  85. if (matchIndex < 1) {
  86. throw new Error();
  87. }
  88. this.month = matchIndex % 12 || matchIndex;
  89. }],
  90. Y: [matchSigned, addInput('year')],
  91. YY: [match2, function (input) {
  92. input = +input;
  93. this.year = input + (input > 68 ? 1900 : 2000);
  94. }],
  95. YYYY: [match4, addInput('year')],
  96. Z: zoneExpressions,
  97. ZZ: zoneExpressions
  98. };
  99. function correctHours(time) {
  100. var afternoon = time.afternoon;
  101. if (afternoon !== undefined) {
  102. var hours = time.hours;
  103. if (afternoon) {
  104. if (hours < 12) {
  105. time.hours += 12;
  106. }
  107. } else if (hours === 12) {
  108. time.hours = 0;
  109. }
  110. delete time.afternoon;
  111. }
  112. }
  113. function makeParser(format) {
  114. var array = format.match(formattingTokens);
  115. var length = array.length;
  116. for (var i = 0; i < length; i += 1) {
  117. var token = array[i];
  118. var parseTo = expressions[token];
  119. var regex = parseTo && parseTo[0];
  120. var parser = parseTo && parseTo[1];
  121. if (parser) {
  122. array[i] = {
  123. regex: regex,
  124. parser: parser
  125. };
  126. } else {
  127. array[i] = token.replace(/^\[|\]$/g, '');
  128. }
  129. }
  130. return function (input) {
  131. var time = {};
  132. for (var _i = 0, start = 0; _i < length; _i += 1) {
  133. var _token = array[_i];
  134. if (typeof _token === 'string') {
  135. start += _token.length;
  136. } else {
  137. var _regex = _token.regex,
  138. _parser = _token.parser;
  139. var part = input.substr(start);
  140. var match = _regex.exec(part);
  141. var value = match[0];
  142. _parser.call(time, value);
  143. input = input.replace(value, '');
  144. }
  145. }
  146. correctHours(time);
  147. return time;
  148. };
  149. }
  150. var parseFormattedInput = function parseFormattedInput(input, format, utc) {
  151. try {
  152. var parser = makeParser(format);
  153. var _parser2 = parser(input),
  154. year = _parser2.year,
  155. month = _parser2.month,
  156. day = _parser2.day,
  157. hours = _parser2.hours,
  158. minutes = _parser2.minutes,
  159. seconds = _parser2.seconds,
  160. milliseconds = _parser2.milliseconds,
  161. zone = _parser2.zone;
  162. var now = new Date();
  163. var d = day || (!year && !month ? now.getDate() : 1);
  164. var y = year || now.getFullYear();
  165. var M = 0;
  166. if (!(year && !month)) {
  167. M = month > 0 ? month - 1 : now.getMonth();
  168. }
  169. var h = hours || 0;
  170. var m = minutes || 0;
  171. var s = seconds || 0;
  172. var ms = milliseconds || 0;
  173. if (zone) {
  174. return new Date(Date.UTC(y, M, d, h, m, s, ms + zone.offset * 60 * 1000));
  175. }
  176. if (utc) {
  177. return new Date(Date.UTC(y, M, d, h, m, s, ms));
  178. }
  179. return new Date(y, M, d, h, m, s, ms);
  180. } catch (e) {
  181. return new Date(''); // Invalid Date
  182. }
  183. };
  184. export default (function (o, C, d) {
  185. var proto = C.prototype;
  186. var oldParse = proto.parse;
  187. proto.parse = function (cfg) {
  188. var date = cfg.date,
  189. utc = cfg.utc,
  190. args = cfg.args;
  191. this.$u = utc;
  192. var format = args[1];
  193. if (typeof format === 'string') {
  194. var isStrictWithoutLocale = args[2] === true;
  195. var isStrictWithLocale = args[3] === true;
  196. var isStrict = isStrictWithoutLocale || isStrictWithLocale;
  197. var pl = args[2];
  198. if (isStrictWithLocale) {
  199. pl = args[2];
  200. }
  201. if (!isStrictWithoutLocale) {
  202. locale = pl ? d.Ls[pl] : this.$locale();
  203. }
  204. this.$d = parseFormattedInput(date, format, utc);
  205. this.init();
  206. if (pl && pl !== true) this.$L = this.locale(pl).$L;
  207. if (isStrict && date !== this.format(format)) {
  208. this.$d = new Date('');
  209. }
  210. } else if (format instanceof Array) {
  211. var len = format.length;
  212. for (var i = 1; i <= len; i += 1) {
  213. args[1] = format[i - 1];
  214. var result = d.apply(this, args);
  215. if (result.isValid()) {
  216. this.$d = result.$d;
  217. this.$L = result.$L;
  218. this.init();
  219. break;
  220. }
  221. if (i === len) this.$d = new Date('');
  222. }
  223. } else {
  224. oldParse.call(this, cfg);
  225. }
  226. };
  227. });