8fff38f9195568ff71fc00f84897ccde68f0ddfdf50de4ea1a5a29c138f54da1d086b2a73db637539407033cbd3673fc411612ed2942b8a3f5b0124a38f00d 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { copyConfig } from '../moment/constructor';
  2. import { configFromStringAndFormat } from './from-string-and-format';
  3. import getParsingFlags from './parsing-flags';
  4. import { isValid } from './valid';
  5. import extend from '../utils/extend';
  6. // date from string and array of format strings
  7. export function configFromStringAndArray(config) {
  8. var tempConfig,
  9. bestMoment,
  10. scoreToBeat,
  11. i,
  12. currentScore,
  13. validFormatFound,
  14. bestFormatIsValid = false,
  15. configfLen = config._f.length;
  16. if (configfLen === 0) {
  17. getParsingFlags(config).invalidFormat = true;
  18. config._d = new Date(NaN);
  19. return;
  20. }
  21. for (i = 0; i < configfLen; i++) {
  22. currentScore = 0;
  23. validFormatFound = false;
  24. tempConfig = copyConfig({}, config);
  25. if (config._useUTC != null) {
  26. tempConfig._useUTC = config._useUTC;
  27. }
  28. tempConfig._f = config._f[i];
  29. configFromStringAndFormat(tempConfig);
  30. if (isValid(tempConfig)) {
  31. validFormatFound = true;
  32. }
  33. // if there is any input that was not parsed add a penalty for that format
  34. currentScore += getParsingFlags(tempConfig).charsLeftOver;
  35. //or tokens
  36. currentScore += getParsingFlags(tempConfig).unusedTokens.length * 10;
  37. getParsingFlags(tempConfig).score = currentScore;
  38. if (!bestFormatIsValid) {
  39. if (
  40. scoreToBeat == null ||
  41. currentScore < scoreToBeat ||
  42. validFormatFound
  43. ) {
  44. scoreToBeat = currentScore;
  45. bestMoment = tempConfig;
  46. if (validFormatFound) {
  47. bestFormatIsValid = true;
  48. }
  49. }
  50. } else {
  51. if (currentScore < scoreToBeat) {
  52. scoreToBeat = currentScore;
  53. bestMoment = tempConfig;
  54. }
  55. }
  56. }
  57. extend(config, bestMoment || tempConfig);
  58. }