cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

from-string-and-format.js (4092B)


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