PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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