Skip to content

Commit

Permalink
feat(fsm): update not()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 4, 2019
1 parent 980d488 commit a933607
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions packages/fsm/src/not.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { Match, RES_FAIL, RES_PARTIAL, Matcher } from "./api";
import { Match, RES_FAIL, RES_PARTIAL, Matcher, SeqCallback } from "./api";
import { success } from "./success";

export const not =
<T, C, R>(match: Matcher<T, C, R>, callback?): Matcher<T, C, R> =>
() => {
let m = match();
return (ctx, x) => {
const { type } = m(ctx, x);
return type === Match.FAIL ?
success(callback && callback(ctx)) :
type !== Match.PARTIAL ?
// TODO Match.FULL_NC handling?
RES_FAIL :
RES_PARTIAL;
}
};
export const not = <T, C, R>(
match: Matcher<T, C, R>,
callback?: SeqCallback<T, C, R>
): Matcher<T, C, R> =>
() => {
let m = match();
const buf: T[] = [];
return (ctx, x) => {
buf.push(x);
const { type } = m(ctx, x);
return type === Match.FAIL ?
success(callback && callback(ctx, buf)) :
type !== Match.PARTIAL ?
// TODO Match.FULL_NC handling?
RES_FAIL :
RES_PARTIAL;
}
};

0 comments on commit a933607

Please sign in to comment.