UNPKG

10.4 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/parse.js"],"names":["compareNumbers","a","b","createParser","fields","presets","m","length","j","field","aliases","undefined","symbols","Array","isArray","range","start","forEach","alias","i","Object","keys","aliasesRegExp","RegExp","join","n","pattern","schedule","values","isDigit","c","match","consumeWhitespaces","parseInteger","digits","push","SyntaxError","name","Number","parseInt","parseValue","value","post","lastIndex","matches","exec","parseRange","end","step","add","parseSequence","parse","p","Set","size","from","sort","split"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,MAAMA,cAAc,GAAG,CAACC,CAAD,EAAIC,CAAJ,KAAUD,CAAC,GAAGC,CAArC;;AAEA,MAAMC,YAAY,GAAG,UAAsD;AAAA,kCAAnDC,MAAmD;AAAA,MAAvCA,MAAuC;AAAA,MAAhBC,OAAgB,qBAA9BA,OAA8B;;AACzE,QAAMC,CAAC,GAAGF,MAAM,CAACG,MAAjB;;AAEA,OAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGF,CAApB,EAAuB,EAAEE,CAAzB,EAA4B;AAC1B,UAAMC,KAAK,GAAGL,MAAM,CAACI,CAAD,CAApB;AAD0B,QAEpBE,OAFoB,GAERD,KAFQ,CAEpBC,OAFoB;;AAG1B,QAAIA,OAAO,KAAKC,SAAhB,EAA2B;AACzB,UAAIC,OAAO,GAAGF,OAAd;;AAEA,UAAIG,KAAK,CAACC,OAAN,CAAcJ,OAAd,CAAJ,EAA4B;AAC1BA,QAAAA,OAAO,GAAG,EAAV;;AAD0B,4CAEVD,KAAK,CAACM,KAFI;AAAA,cAEnBC,KAFmB;;AAG1BJ,QAAAA,OAAO,CAACK,OAAR,CAAgB,CAACC,KAAD,EAAQC,CAAR,KAAc;AAC5BT,UAAAA,OAAO,CAACQ,KAAD,CAAP,GAAiBF,KAAK,GAAGG,CAAzB;AACD,SAFD;AAGD,OAND,MAMO;AACLP,QAAAA,OAAO,GAAGQ,MAAM,CAACC,IAAP,CAAYX,O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C,CAAD,EAAI,EAAJ;AAHT,GAbM,EAkBN;AACEL,IAAAA,OAAO,EAAE,8BAA8B+C,KAA9B,CAAoC,GAApC,CADX;AAEEpB,IAAAA,IAAI,EAAE,WAFR;AAGEK,IAAAA,IAAI,EAAED,KAAK,IAAKA,KAAK,KAAK,CAAV,GAAc,CAAd,GAAkBA,KAHpC;AAIE1B,IAAAA,KAAK,EAAE,CAAC,CAAD,EAAI,CAAJ;AAJT,GAlBM,CADkB;AA0B1BV,EAAAA,OAAO,EAAE;AACP,iBAAa,aADN;AAEP,cAAU,WAFH;AAGP,eAAW,WAHJ;AAIP,gBAAY,WAJL;AAKP,eAAW,aALJ;AAMP,eAAW;AANJ;AA1BiB,CAAD,C","sourcesContent":["const compareNumbers = (a, b) => a - b\n\nconst createParser = ({ fields: [...fields], presets: { ...presets } }) => {\n const m = fields.length\n\n for (let j = 0; j < m; ++j) {\n const field = fields[j]\n let { aliases } = field\n if (aliases !== undefined) {\n let symbols = aliases\n\n if (Array.isArray(aliases)) {\n aliases = {}\n const [start] = field.range\n symbols.forEach((alias, i) => {\n aliases[alias] = start + i\n })\n } else {\n symbols = Object.keys(aliases)\n }\n\n fields[j] = {\n ...field,\n aliases,\n aliasesRegExp: new RegExp(symbols.join('|'), 'y'),\n }\n }\n }\n\n let field, i, n, pattern, schedule, values\n\n const isDigit = c => c >= '0' && c <= '9'\n const match = c => pattern[i] === c && (++i, true)\n\n const consumeWhitespaces = () => {\n let c\n while ((c = pattern[i]) === ' ' || c === '\\t') {\n ++i\n }\n }\n\n const parseInteger = () => {\n let c\n const digits = []\n while (isDigit((c = pattern[i]))) {\n ++i\n digits.push(c)\n }\n if (digits.length === 0) {\n throw new SyntaxError(`${field.name}: missing integer at character ${i}`)\n }\n return Number.parseInt(digits.join(''), 10)\n }\n\n const parseValue = () => {\n let value\n\n const { aliasesRegExp } = field\n if (aliasesRegExp === undefined || isDigit(pattern[i])) {\n value = parseInteger()\n const { post } = field\n if (post !== undefined) {\n value = post(value)\n }\n } else {\n aliasesRegExp.lastIndex = i\n const matches = aliasesRegExp.exec(pattern)\n if (matches === null) {\n throw new SyntaxError(\n `${field.name}: missing alias or integer at character ${i}`\n )\n }\n const [alias] = matches\n i += alias.length\n value = field.aliases[alias]\n }\n\n const { range } = field\n if (value < range[0] || value > range[1]) {\n throw new SyntaxError(\n `${field.name}: ${value} is not between ${range[0]} and ${range[1]}`\n )\n }\n return value\n }\n\n const parseRange = () => {\n let end, start, step\n if (match('*')) {\n if (!match('/')) {\n return\n }\n ;[start, end] = field.range\n step = parseInteger()\n } else {\n start = parseValue()\n if (!match('-')) {\n values.add(start)\n return\n }\n end = parseValue()\n step = match('/') ? parseInteger() : 1\n }\n\n for (let i = start; i <= end; i += step) {\n values.add(i)\n }\n }\n\n const parseSequence = () => {\n do {\n parseRange()\n } while (match(','))\n }\n\n const parse = p => {\n {\n const schedule = presets[p]\n if (schedule !== undefined) {\n return typeof schedule === 'string'\n ? (presets[p] = parse(schedule))\n : schedule\n }\n }\n\n try {\n i = 0\n n = p.length\n pattern = p\n schedule = {}\n\n for (let j = 0; j < m; ++j) {\n consumeWhitespaces()\n\n field = fields[j]\n values = new Set()\n parseSequence()\n if (values.size !== 0) {\n schedule[field.name] = Array.from(values).sort(compareNumbers)\n }\n }\n\n consumeWhitespaces()\n if (i !== n) {\n throw new SyntaxError(\n `unexpected character at offset ${i}, expected end`\n )\n }\n\n return schedule\n } finally {\n field = pattern = schedule = values = undefined\n }\n }\n\n return parse\n}\n\nexport default createParser({\n fields: [\n {\n name: 'minute',\n range: [0, 59],\n },\n {\n name: 'hour',\n range: [0, 23],\n },\n {\n name: 'dayOfMonth',\n range: [1, 31],\n },\n {\n aliases: 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' '),\n name: 'month',\n range: [0, 11],\n },\n {\n aliases: 'sun mon tue wen thu fri sat'.split(' '),\n name: 'dayOfWeek',\n post: value => (value === 7 ? 0 : value),\n range: [0, 6],\n },\n ],\n presets: {\n '@annually': '0 0 1 jan *',\n '@daily': '0 0 * * *',\n '@hourly': '0 * * * *',\n '@monthly': '0 0 1 * *',\n '@weekly': '0 0 * * sun',\n '@yearly': '0 0 1 jan *',\n },\n})\n"],"file":"parse.js"}
\No newline at end of file