source: trip-planner-front/node_modules/nth-check/lib/parse.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
1"use strict";
2// Following http://www.w3.org/TR/css3-selectors/#nth-child-pseudo
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.parse = void 0;
5// Whitespace as per https://www.w3.org/TR/selectors-3/#lex is " \t\r\n\f"
6var whitespace = new Set([9, 10, 12, 13, 32]);
7var ZERO = "0".charCodeAt(0);
8var NINE = "9".charCodeAt(0);
9/**
10 * Parses an expression.
11 *
12 * @throws An `Error` if parsing fails.
13 * @returns An array containing the integer step size and the integer offset of the nth rule.
14 * @example nthCheck.parse("2n+3"); // returns [2, 3]
15 */
16function parse(formula) {
17 formula = formula.trim().toLowerCase();
18 if (formula === "even") {
19 return [2, 0];
20 }
21 else if (formula === "odd") {
22 return [2, 1];
23 }
24 // Parse [ ['-'|'+']? INTEGER? {N} [ S* ['-'|'+'] S* INTEGER ]?
25 var idx = 0;
26 var a = 0;
27 var sign = readSign();
28 var number = readNumber();
29 if (idx < formula.length && formula.charAt(idx) === "n") {
30 idx++;
31 a = sign * (number !== null && number !== void 0 ? number : 1);
32 skipWhitespace();
33 if (idx < formula.length) {
34 sign = readSign();
35 skipWhitespace();
36 number = readNumber();
37 }
38 else {
39 sign = number = 0;
40 }
41 }
42 // Throw if there is anything else
43 if (number === null || idx < formula.length) {
44 throw new Error("n-th rule couldn't be parsed ('" + formula + "')");
45 }
46 return [a, sign * number];
47 function readSign() {
48 if (formula.charAt(idx) === "-") {
49 idx++;
50 return -1;
51 }
52 if (formula.charAt(idx) === "+") {
53 idx++;
54 }
55 return 1;
56 }
57 function readNumber() {
58 var start = idx;
59 var value = 0;
60 while (idx < formula.length &&
61 formula.charCodeAt(idx) >= ZERO &&
62 formula.charCodeAt(idx) <= NINE) {
63 value = value * 10 + (formula.charCodeAt(idx) - ZERO);
64 idx++;
65 }
66 // Return `null` if we didn't read anything.
67 return idx === start ? null : value;
68 }
69 function skipWhitespace() {
70 while (idx < formula.length &&
71 whitespace.has(formula.charCodeAt(idx))) {
72 idx++;
73 }
74 }
75}
76exports.parse = parse;
Note: See TracBrowser for help on using the repository browser.