1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.sequence = exports.generate = exports.compile = exports.parse = void 0;
|
4 | var parse_js_1 = require("./parse.js");
|
5 | Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parse_js_1.parse; } });
|
6 | var compile_js_1 = require("./compile.js");
|
7 | Object.defineProperty(exports, "compile", { enumerable: true, get: function () { return compile_js_1.compile; } });
|
8 | Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return compile_js_1.generate; } });
|
9 | /**
|
10 | * Parses and compiles a formula to a highly optimized function.
|
11 | * Combination of {@link parse} and {@link compile}.
|
12 | *
|
13 | * If the formula doesn't match any elements,
|
14 | * it returns [`boolbase`](https://github.com/fb55/boolbase)'s `falseFunc`.
|
15 | * Otherwise, a function accepting an _index_ is returned, which returns
|
16 | * whether or not the passed _index_ matches the formula.
|
17 | *
|
18 | * Note: The nth-rule starts counting at `1`, the returned function at `0`.
|
19 | *
|
20 | * @param formula The formula to compile.
|
21 | * @example
|
22 | * const check = nthCheck("2n+3");
|
23 | *
|
24 | * check(0); // `false`
|
25 | * check(1); // `false`
|
26 | * check(2); // `true`
|
27 | * check(3); // `false`
|
28 | * check(4); // `true`
|
29 | * check(5); // `false`
|
30 | * check(6); // `true`
|
31 | */
|
32 | function nthCheck(formula) {
|
33 | return (0, compile_js_1.compile)((0, parse_js_1.parse)(formula));
|
34 | }
|
35 | exports.default = nthCheck;
|
36 | /**
|
37 | * Parses and compiles a formula to a generator that produces a sequence of indices.
|
38 | * Combination of {@link parse} and {@link generate}.
|
39 | *
|
40 | * @param formula The formula to compile.
|
41 | * @returns A function that produces a sequence of indices.
|
42 | * @example <caption>Always increasing</caption>
|
43 | *
|
44 | * ```js
|
45 | * const gen = nthCheck.sequence('2n+3')
|
46 | *
|
47 | * gen() // `1`
|
48 | * gen() // `3`
|
49 | * gen() // `5`
|
50 | * gen() // `8`
|
51 | * gen() // `11`
|
52 | * ```
|
53 | *
|
54 | * @example <caption>With end value</caption>
|
55 | *
|
56 | * ```js
|
57 | *
|
58 | * const gen = nthCheck.sequence('-2n+5');
|
59 | *
|
60 | * gen() // 0
|
61 | * gen() // 2
|
62 | * gen() // 4
|
63 | * gen() // null
|
64 | * ```
|
65 | */
|
66 | function sequence(formula) {
|
67 | return (0, compile_js_1.generate)((0, parse_js_1.parse)(formula));
|
68 | }
|
69 | exports.sequence = sequence;
|
70 | //# sourceMappingURL=index.js.map |
\ | No newline at end of file |