index.js 7.4 KB

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