Debian Codegraph @

vendor/static/moment/src/lib/create/from-string-and-array.js

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