index.js 7.8 KB

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