/node_modules/moment/src/lib/create/from-string-and-format.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 113 lines · 86 code · 13 blank · 14 comment · 27 complexity · e5dd26d9d3692c3e9e268d96be0655a1 MD5 · raw file

  1. import { configFromISO, configFromRFC2822 } from './from-string';
  2. import { configFromArray } from './from-array';
  3. import { getParseRegexForToken } from '../parse/regex';
  4. import { addTimeToArrayFromToken } from '../parse/token';
  5. import { expandFormat, formatTokenFunctions, formattingTokens } from '../format/format';
  6. import checkOverflow from './check-overflow';
  7. import { HOUR } from '../units/constants';
  8. import { hooks } from '../utils/hooks';
  9. import getParsingFlags from './parsing-flags';
  10. // constant that refers to the ISO standard
  11. hooks.ISO_8601 = function () {};
  12. // constant that refers to the RFC 2822 form
  13. hooks.RFC_2822 = function () {};
  14. // date from string and format string
  15. export function configFromStringAndFormat(config) {
  16. // TODO: Move this to another part of the creation flow to prevent circular deps
  17. if (config._f === hooks.ISO_8601) {
  18. configFromISO(config);
  19. return;
  20. }
  21. if (config._f === hooks.RFC_2822) {
  22. configFromRFC2822(config);
  23. return;
  24. }
  25. config._a = [];
  26. getParsingFlags(config).empty = true;
  27. // This array is used to make a Date, either with `new Date` or `Date.UTC`
  28. var string = '' + config._i,
  29. i, parsedInput, tokens, token, skipped,
  30. stringLength = string.length,
  31. totalParsedInputLength = 0;
  32. tokens = expandFormat(config._f, config._locale).match(formattingTokens) || [];
  33. for (i = 0; i < tokens.length; i++) {
  34. token = tokens[i];
  35. parsedInput = (string.match(getParseRegexForToken(token, config)) || [])[0];
  36. // console.log('token', token, 'parsedInput', parsedInput,
  37. // 'regex', getParseRegexForToken(token, config));
  38. if (parsedInput) {
  39. skipped = string.substr(0, string.indexOf(parsedInput));
  40. if (skipped.length > 0) {
  41. getParsingFlags(config).unusedInput.push(skipped);
  42. }
  43. string = string.slice(string.indexOf(parsedInput) + parsedInput.length);
  44. totalParsedInputLength += parsedInput.length;
  45. }
  46. // don't parse if it's not a known token
  47. if (formatTokenFunctions[token]) {
  48. if (parsedInput) {
  49. getParsingFlags(config).empty = false;
  50. }
  51. else {
  52. getParsingFlags(config).unusedTokens.push(token);
  53. }
  54. addTimeToArrayFromToken(token, parsedInput, config);
  55. }
  56. else if (config._strict && !parsedInput) {
  57. getParsingFlags(config).unusedTokens.push(token);
  58. }
  59. }
  60. // add remaining unparsed input length to the string
  61. getParsingFlags(config).charsLeftOver = stringLength - totalParsedInputLength;
  62. if (string.length > 0) {
  63. getParsingFlags(config).unusedInput.push(string);
  64. }
  65. // clear _12h flag if hour is <= 12
  66. if (config._a[HOUR] <= 12 &&
  67. getParsingFlags(config).bigHour === true &&
  68. config._a[HOUR] > 0) {
  69. getParsingFlags(config).bigHour = undefined;
  70. }
  71. getParsingFlags(config).parsedDateParts = config._a.slice(0);
  72. getParsingFlags(config).meridiem = config._meridiem;
  73. // handle meridiem
  74. config._a[HOUR] = meridiemFixWrap(config._locale, config._a[HOUR], config._meridiem);
  75. configFromArray(config);
  76. checkOverflow(config);
  77. }
  78. function meridiemFixWrap (locale, hour, meridiem) {
  79. var isPm;
  80. if (meridiem == null) {
  81. // nothing to do
  82. return hour;
  83. }
  84. if (locale.meridiemHour != null) {
  85. return locale.meridiemHour(hour, meridiem);
  86. } else if (locale.isPM != null) {
  87. // Fallback
  88. isPm = locale.isPM(meridiem);
  89. if (isPm && hour < 12) {
  90. hour += 12;
  91. }
  92. if (!isPm && hour === 12) {
  93. hour = 0;
  94. }
  95. return hour;
  96. } else {
  97. // this is not supposed to happen
  98. return hour;
  99. }
  100. }