Skip to content

Commit

Permalink
feat(fsm): add altsLitObj(), update deps & char range matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 5, 2020
1 parent c3ff006 commit 300fe2b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/fsm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@thi.ng/arrays": "^0.5.5",
"@thi.ng/equiv": "^1.0.15",
"@thi.ng/errors": "^1.2.6",
"@thi.ng/strings": "^1.6.0",
"@thi.ng/transducers": "^6.4.0"
},
"keywords": [
Expand Down
12 changes: 11 additions & 1 deletion packages/fsm/src/alts-lit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LitCallback, Matcher, Match } from "./api";
import { LitCallback, Match, Matcher } from "./api";
import { result } from "./result";
import type { IObjectOf } from "@thi.ng/api";

export const altsLit = <T, C, R>(
opts: Set<T>,
Expand All @@ -9,3 +10,12 @@ export const altsLit = <T, C, R>(
opts.has(x)
? result(success && success(ctx, x))
: result(fail && fail(ctx, x), Match.FAIL);

export const altsLitObj = <C, R>(
opts: IObjectOf<boolean>,
success?: LitCallback<string, C, R>,
fail?: LitCallback<string, C, R>
): Matcher<string, C, R> => () => (ctx, x) =>
opts[x]
? result(success && success(ctx, x))
: result(fail && fail(ctx, x), Match.FAIL);
39 changes: 15 additions & 24 deletions packages/fsm/src/range.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { alts } from "./alts";
import { altsLit } from "./alts-lit";
import { ALPHA, DIGITS, WS } from "@thi.ng/strings";
import { altsLitObj } from "./alts-lit";
import { result } from "./result";
import type {
AltCallback,
AltFallback,
LitCallback,
Matcher
} from "./api";
import type { LitCallback, Matcher } from "./api";

/**
* Returns a single input matcher which returns `Match.FULL` if the
Expand Down Expand Up @@ -36,7 +31,7 @@ export const range = <T extends number | string, C, R>(
export const digit = <C, R>(
success?: LitCallback<string, C, R>,
fail?: LitCallback<string, C, R>
): Matcher<string, C, R> => range<string, C, R>("0", "9", success, fail);
): Matcher<string, C, R> => altsLitObj(DIGITS, success, fail);

/**
* Matcher for single A-Z or a-z characters.
Expand All @@ -45,15 +40,9 @@ export const digit = <C, R>(
* @param fail - failure callback
*/
export const alpha = <C, R>(
success?: AltCallback<string, C, R>,
fail?: AltFallback<string, C, R>
): Matcher<string, C, R> =>
alts(
[range<string, C, R>("a", "z"), range<string, C, R>("A", "Z")],
undefined,
success,
fail
);
success?: LitCallback<string, C, R>,
fail?: LitCallback<string, C, R>
): Matcher<string, C, R> => altsLitObj(ALPHA, success, fail);

/**
* Combination of {@link digit} and {@link alpha}.
Expand All @@ -62,11 +51,13 @@ export const alpha = <C, R>(
* @param fail - failure callback
*/
export const alphaNum = <C, R>(
success?: AltCallback<string, C, R>,
fail?: AltFallback<string, C, R>
): Matcher<string, C, R> => alts([alpha(), digit()], undefined, success, fail);

const WS = new Set([" ", "\n", "\t", "\r"]);
success?: LitCallback<string, C, R>,
fail?: LitCallback<string, C, R>
): Matcher<string, C, R> => altsLitObj(
{ ...ALPHA, ...DIGITS },
success,
fail
);

/**
* Matcher for single whitespace characters.
Expand All @@ -77,4 +68,4 @@ const WS = new Set([" ", "\n", "\t", "\r"]);
export const whitespace = <C, R>(
success?: LitCallback<string, C, R>,
fail?: LitCallback<string, C, R>
): Matcher<string, C, R> => altsLit(WS, success, fail);
): Matcher<string, C, R> => altsLitObj(WS, success, fail);

0 comments on commit 300fe2b

Please sign in to comment.