index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import { u } from '../localizedFormat/utils';
  2. var formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|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. Q: [match1, function (input) {
  61. this.month = (input - 1) * 3 + 1;
  62. }],
  63. S: [match1, function (input) {
  64. this.milliseconds = +input * 100;
  65. }],
  66. SS: [match2, function (input) {
  67. this.milliseconds = +input * 10;
  68. }],
  69. SSS: [match3, function (input) {
  70. this.milliseconds = +input;
  71. }],
  72. s: [match1to2, addInput('seconds')],
  73. ss: [match1to2, addInput('seconds')],
  74. m: [match1to2, addInput('minutes')],
  75. mm: [match1to2, addInput('minutes')],
  76. H: [match1to2, addInput('hours')],
  77. h: [match1to2, addInput('hours')],
  78. HH: [match1to2, addInput('hours')],
  79. hh: [match1to2, addInput('hours')],
  80. D: [match1to2, addInput('day')],
  81. DD: [match2, addInput('day')],
  82. Do: [matchWord, function (input) {
  83. var _locale2 = locale,
  84. ordinal = _locale2.ordinal;
  85. var _input$match = input.match(/\d+/);
  86. this.day = _input$match[0];
  87. if (!ordinal) return;
  88. for (var i = 1; i <= 31; i += 1) {
  89. if (ordinal(i).replace(/\[|\]/g, '') === input) {
  90. this.day = i;
  91. }
  92. }
  93. }],
  94. w: [match1to2, addInput('week')],
  95. ww: [match2, addInput('week')],
  96. M: [match1to2, addInput('month')],
  97. MM: [match2, addInput('month')],
  98. MMM: [matchWord, function (input) {
  99. var months = getLocalePart('months');
  100. var monthsShort = getLocalePart('monthsShort');
  101. var matchIndex = (monthsShort || months.map(function (_) {
  102. return _.slice(0, 3);
  103. })).indexOf(input) + 1;
  104. if (matchIndex < 1) {
  105. throw new Error();
  106. }
  107. this.month = matchIndex % 12 || matchIndex;
  108. }],
  109. MMMM: [matchWord, function (input) {
  110. var months = getLocalePart('months');
  111. var matchIndex = months.indexOf(input) + 1;
  112. if (matchIndex < 1) {
  113. throw new Error();
  114. }
  115. this.month = matchIndex % 12 || matchIndex;
  116. }],
  117. Y: [matchSigned, addInput('year')],
  118. YY: [match2, function (input) {
  119. this.year = parseTwoDigitYear(input);
  120. }],
  121. YYYY: [match4, addInput('year')],
  122. Z: zoneExpressions,
  123. ZZ: zoneExpressions
  124. };
  125. function correctHours(time) {
  126. var afternoon = time.afternoon;
  127. if (afternoon !== undefined) {
  128. var hours = time.hours;
  129. if (afternoon) {
  130. if (hours < 12) {
  131. time.hours += 12;
  132. }
  133. } else if (hours === 12) {
  134. time.hours = 0;
  135. }
  136. delete time.afternoon;
  137. }
  138. }
  139. function makeParser(format) {
  140. format = u(format, locale && locale.formats);
  141. var array = format.match(formattingTokens);
  142. var length = array.length;
  143. for (var i = 0; i < length; i += 1) {
  144. var token = array[i];
  145. var parseTo = expressions[token];
  146. var regex = parseTo && parseTo[0];
  147. var parser = parseTo && parseTo[1];
  148. if (parser) {
  149. array[i] = {
  150. regex: regex,
  151. parser: parser
  152. };
  153. } else {
  154. array[i] = token.replace(/^\[|\]$/g, '');
  155. }
  156. }
  157. return function (input) {
  158. var time = {};
  159. for (var _i = 0, start = 0; _i < length; _i += 1) {
  160. var _token = array[_i];
  161. if (typeof _token === 'string') {
  162. start += _token.length;
  163. } else {
  164. var _regex = _token.regex,
  165. _parser = _token.parser;
  166. var part = input.slice(start);
  167. var match = _regex.exec(part);
  168. var value = match[0];
  169. _parser.call(time, value);
  170. input = input.replace(value, '');
  171. }
  172. }
  173. correctHours(time);
  174. return time;
  175. };
  176. }
  177. var parseFormattedInput = function parseFormattedInput(input, format, utc, dayjs) {
  178. try {
  179. if (['x', 'X'].indexOf(format) > -1) return new Date((format === 'X' ? 1000 : 1) * input);
  180. var parser = makeParser(format);
  181. var _parser2 = parser(input),
  182. year = _parser2.year,
  183. month = _parser2.month,
  184. day = _parser2.day,
  185. hours = _parser2.hours,
  186. minutes = _parser2.minutes,
  187. seconds = _parser2.seconds,
  188. milliseconds = _parser2.milliseconds,
  189. zone = _parser2.zone,
  190. week = _parser2.week;
  191. var now = new Date();
  192. var d = day || (!year && !month ? now.getDate() : 1);
  193. var y = year || now.getFullYear();
  194. var M = 0;
  195. if (!(year && !month)) {
  196. M = month > 0 ? month - 1 : now.getMonth();
  197. }
  198. var h = hours || 0;
  199. var m = minutes || 0;
  200. var s = seconds || 0;
  201. var ms = milliseconds || 0;
  202. if (zone) {
  203. return new Date(Date.UTC(y, M, d, h, m, s, ms + zone.offset * 60 * 1000));
  204. }
  205. if (utc) {
  206. return new Date(Date.UTC(y, M, d, h, m, s, ms));
  207. }
  208. var newDate;
  209. newDate = new Date(y, M, d, h, m, s, ms);
  210. if (week) {
  211. newDate = dayjs(newDate).week(week).toDate();
  212. }
  213. return newDate;
  214. } catch (e) {
  215. return new Date(''); // Invalid Date
  216. }
  217. };
  218. export default (function (o, C, d) {
  219. d.p.customParseFormat = true;
  220. if (o && o.parseTwoDigitYear) {
  221. parseTwoDigitYear = o.parseTwoDigitYear;
  222. }
  223. var proto = C.prototype;
  224. var oldParse = proto.parse;
  225. proto.parse = function (cfg) {
  226. var date = cfg.date,
  227. utc = cfg.utc,
  228. args = cfg.args;
  229. this.$u = utc;
  230. var format = args[1];
  231. if (typeof format === 'string') {
  232. var isStrictWithoutLocale = args[2] === true;
  233. var isStrictWithLocale = args[3] === true;
  234. var isStrict = isStrictWithoutLocale || isStrictWithLocale;
  235. var pl = args[2];
  236. if (isStrictWithLocale) {
  237. pl = args[2];
  238. }
  239. locale = this.$locale();
  240. if (!isStrictWithoutLocale && pl) {
  241. locale = d.Ls[pl];
  242. }
  243. this.$d = parseFormattedInput(date, format, utc, d);
  244. this.init();
  245. if (pl && pl !== true) this.$L = this.locale(pl).$L; // use != to treat
  246. // input number 1410715640579 and format string '1410715640579' equal
  247. // eslint-disable-next-line eqeqeq
  248. if (isStrict && date != this.format(format)) {
  249. this.$d = new Date('');
  250. } // reset global locale to make parallel unit test
  251. locale = {};
  252. } else if (format instanceof Array) {
  253. var len = format.length;
  254. for (var i = 1; i <= len; i += 1) {
  255. args[1] = format[i - 1];
  256. var result = d.apply(this, args);
  257. if (result.isValid()) {
  258. this.$d = result.$d;
  259. this.$L = result.$L;
  260. this.init();
  261. break;
  262. }
  263. if (i === len) this.$d = new Date('');
  264. }
  265. } else {
  266. oldParse.call(this, cfg);
  267. }
  268. };
  269. });