Skip to content

Commit

Permalink
feat(operators): add and operator
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 23, 2023
1 parent f5df543 commit 9d0b8e3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions operators/and.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { iter } from "../iter_utils.ts";
import {
Assert,
AssertiveValidator,
Validation,
ValidationError,
} from "../types.ts";

export class AndValidator<In, In_ extends Via, Via extends In = In & In_>
implements AssertiveValidator<In, In & In_ & Via> {
declare [Assert.symbol]: In & In_ & Via;
validators: Validation[] = [];

constructor(left: Validation<In, Via>, right: Validation<Via, In_>);
constructor(...validators: Validation[]) {
this.validators = validators;
}

*validate(input: In): Iterable<ValidationError> {
for (const validator of this.validators) {
const iterable = validator.validate(input);
const result = iter(iterable);

if (result.done) continue;

yield result.value;
yield* iterable;
return;
}
}
}

0 comments on commit 9d0b8e3

Please sign in to comment.