storeExpressionVals = function()

in packages/amplify-category-function/src/provider-utils/awscloudformation/utils/cronExpression.ts [169:333]


  storeExpressionVals = function(pos: number, s: string, type: number): number {
    let incr: number = 0;
    let i: number = this.skipWhiteSpace(pos, s);
    if (i >= s.length) {
      return i;
    }
    let c: String = s.charAt(i);
    if (c >= 'A' && c <= 'Z' && !(s === 'L') && !(s === 'LW')) {
      let sub: string = s.substring(i, i + 3);
      let sVal = -1;
      let eVal = -1;
      if (type === MONTH) {
        sVal = this.getMonthNumber(sub) + 1;
        if (sVal <= 0) {
          throw new Error("Invalid Month value: '" + sub + "'" + String(i));
        }
        if (s.length > i + 3) {
          c = s.charAt(i + 3);
          if (c === '-') {
            i += 4;
            sub = s.substring(i, i + 3);
            eVal = this.getMonthNumber(sub) + 1;
            if (eVal <= 0) {
              throw new Error("Invalid Month value: '" + sub + "'" + String(i));
            }
          }
        }
      } else if (type === DAY_OF_WEEK) {
        sVal = this.getDayOfWeekNumber(sub);
        if (sVal < 0) {
          throw new Error("Invalid Day-of-Week value: '" + sub + "'" + String(i));
        }
        if (s.length > i + 3) {
          c = s.charAt(i + 3);
          if (c === '-') {
            i += 4;
            sub = s.substring(i, i + 3);
            eVal = this.getDayOfWeekNumber(sub);
            if (eVal < 0) {
              throw new Error("Invalid Day-of-Week value: '" + sub + "'" + String(i));
            }
            if (sVal > eVal) {
              throw new Error('Invalid Day-of-Week sequence: ' + String(sVal) + ' > ' + String(eVal) + String(i));
            }
          } else if (c === '#') {
            try {
              i += 4;
              this.nthdayOfWeek = Number(s.substring(i));
              if (this.nthdayOfWeek < 1 || this.nthdayOfWeek > 5) {
                throw new Error();
              }
            } catch (e) {
              throw new Error("A numeric value between 1 and 5 must follow the '#' option" + String(i));
            }
          } else if (c === 'L') {
            this.lastdayOfWeek = true;
            i = i + 1;
          }
        }
      } else {
        throw new Error("Illegal characters for this position: '" + String(sub) + "'" + String(i));
      }
      if (eVal != -1) {
        incr = 1;
      }
      this.addToSet(sVal, eVal, incr, type);
      return i + 3;
    }

    if (c === '?') {
      i++;
      if (i + 1 < s.length && s.charAt(i) != ' ' && s.charAt(i + 1) != '\t') {
        throw new Error("Illegal character after '?': " + s.charAt(i) + String(i));
      }
      if (type != DAY_OF_WEEK && type != DAY_OF_MONTH) {
        throw new Error("'?' can only be specfied for Day-of-Month or Day-of-Week." + String(i));
      }
      if (type === DAY_OF_WEEK && !this.lastdayOfMonth) {
        let val: number = this.daysOfMonth.last();
        if (val === NO_SPEC_INT) {
          throw new Error("'?' can only be specfied for Day-of-Month -OR- Day-of-Week." + String(i));
        }
      }

      this.addToSet(NO_SPEC_INT, -1, 0, type);
      return i;
    }

    if (c === '*' || c === '/') {
      if (c === '*' && i + 1 >= s.length) {
        this.addToSet(ALL_SPEC_INT, -1, incr, type);
        return i + 1;
      } else if (c === '/' && (i + 1 >= s.length || s.charAt(i + 1) === ' ' || s.charAt(i + 1) === '\t')) {
        throw new Error("'/' must be followed by an integer." + String(i));
      } else if (c === '*') {
        i++;
      }
      c = s.charAt(i);
      if (c === '/') {
        // is an increment specified?
        i++;
        if (i >= s.length) {
          throw new Error('Unexpected end of string.' + String(i));
        }

        incr = this.getNumericValue(s, i);

        i++;
        if (incr > 10) {
          i++;
        }
        if (incr > 59 && (type === SECOND || type === MINUTE)) {
          throw new Error('Increment > 60 : ' + incr + i);
        } else if (incr > 23 && type === HOUR) {
          throw new Error('Increment > 24 : ' + incr + i);
        } else if (incr > 31 && type === DAY_OF_MONTH) {
          throw new Error('Increment > 31 : ' + incr + i);
        } else if (incr > 7 && type === DAY_OF_WEEK) {
          throw new Error('Increment > 7 : ' + incr + i);
        } else if (incr > 12 && type === MONTH) {
          throw new Error('Increment > 12 : ' + incr + i);
        }
      } else {
        incr = 1;
      }

      this.addToSet(ALL_SPEC_INT, -1, incr, type);
      return i;
    } else if (c === 'L') {
      i++;
      if (type === DAY_OF_MONTH) {
        this.lastdayOfMonth = true;
      }
      if (type === DAY_OF_WEEK) {
        this.addToSet(7, 7, 0, type);
      }
      if (type === DAY_OF_MONTH && s.length > i) {
        c = s.charAt(i);
        if (c === 'W') {
          this.nearestWeekday = true;
          i++;
        }
      }
      return i;
    } else if (c >= '0' && c <= '9') {
      let val: number = Number(c.valueOf());
      i++;
      if (i >= s.length) {
        this.addToSet(val, -1, -1, type);
      } else {
        c = s.charAt(i);
        if (c >= '0' && c <= '9') {
          let vs: [number, number] = this.getValue(val, s, i);
          val = vs[1];
          i = vs[0];
        }
        i = this.checkNext(i, s, val, type);
        return i;
      }
    } else {
      throw new Error('Unexpected character: ' + c + i);
    }

    return i;
  };