source: trip-planner-front/node_modules/nth-check/lib/compile.js@ b738035

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

initial commit

  • Property mode set to 100644
File size: 1.9 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.compile = void 0;
4var boolbase_1 = require("boolbase");
5/**
6 * Returns a function that checks if an elements index matches the given rule
7 * highly optimized to return the fastest solution.
8 *
9 * @param parsed A tuple [a, b], as returned by `parse`.
10 * @returns A highly optimized function that returns whether an index matches the nth-check.
11 * @example
12 * const check = nthCheck.compile([2, 3]);
13 *
14 * check(0); // `false`
15 * check(1); // `false`
16 * check(2); // `true`
17 * check(3); // `false`
18 * check(4); // `true`
19 * check(5); // `false`
20 * check(6); // `true`
21 */
22function compile(parsed) {
23 var a = parsed[0];
24 // Subtract 1 from `b`, to convert from one- to zero-indexed.
25 var b = parsed[1] - 1;
26 /*
27 * When `b <= 0`, `a * n` won't be lead to any matches for `a < 0`.
28 * Besides, the specification states that no elements are
29 * matched when `a` and `b` are 0.
30 *
31 * `b < 0` here as we subtracted 1 from `b` above.
32 */
33 if (b < 0 && a <= 0)
34 return boolbase_1.falseFunc;
35 // When `a` is in the range -1..1, it matches any element (so only `b` is checked).
36 if (a === -1)
37 return function (index) { return index <= b; };
38 if (a === 0)
39 return function (index) { return index === b; };
40 // When `b <= 0` and `a === 1`, they match any element.
41 if (a === 1)
42 return b < 0 ? boolbase_1.trueFunc : function (index) { return index >= b; };
43 /*
44 * Otherwise, modulo can be used to check if there is a match.
45 *
46 * Modulo doesn't care about the sign, so let's use `a`s absolute value.
47 */
48 var absA = Math.abs(a);
49 // Get `b mod a`, + a if this is negative.
50 var bMod = ((b % absA) + absA) % absA;
51 return a > 1
52 ? function (index) { return index >= b && index % absA === bMod; }
53 : function (index) { return index <= b && index % absA === bMod; };
54}
55exports.compile = compile;
Note: See TracBrowser for help on using the repository browser.