Skip to content

Commit

Permalink
make_predicate to pass Predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
imcotton committed Jul 6, 2024
1 parent e955d16 commit 0959a28
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/collect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { make_scanner } from './scanner/index.ts';
import { make_predicate } from './presets/index.ts';
import { filter, split_by_comma } from './common.ts';
import { filter, lookup, split_by_comma } from './common.ts';



Expand Down Expand Up @@ -29,11 +29,11 @@ export function collect ({ flags, lines }: {

}): AsyncIterable<string> {

const { extra, ignore, ...rest } = flags;
const { extra = [], ignore = [], ...rest } = flags;

const pred = make_predicate({
extra: extra?.flatMap(split_by_comma),
ignore: ignore?.flatMap(split_by_comma),
extra: lookup(extra.flatMap(split_by_comma)),
ignore: lookup(ignore.flatMap(split_by_comma)),
...rest,
});

Expand Down
19 changes: 10 additions & 9 deletions src/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as nolyfill from './nolyfill.ts';
export { nolyfill };

import type { Predicate } from '../common.ts';
import { and, not, any, lookup } from '../common.ts';
import { and, not, any, always_false } from '../common.ts';



Expand All @@ -32,19 +32,20 @@ export function * gen_presents (param: Partial<Readonly<Record<



export function make_predicate ({ extra = [], ignore = [], ...rest }: {
export function make_predicate ({ extra, ignore, ...rest }: {

extra?: ReadonlyArray<string> | undefined,
ignore?: ReadonlyArray<string> | undefined,
extra?: Predicate<string> | undefined,
ignore?: Predicate<string> | undefined,

} & Parameters<typeof gen_presents>[0]): Predicate<string> {

const not_ignored = not(ignore ?? always_false);
const with_extra = extra ?? always_false;
const presents = Array.from(gen_presents(rest));

return and(
not(lookup(ignore)),
any(
Array.from(gen_presents(rest)),
lookup(extra),
),
not_ignored,
any(presents, with_extra),
);

}
Expand Down
6 changes: 5 additions & 1 deletion tests/presets/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { describe, it } from '@std/testing/bdd';

import { assert_false, assert__true } from '../utils.ts';

import { lookup } from '../../src/common.ts';

import {

make_predicate,
Expand Down Expand Up @@ -41,7 +43,9 @@ describe('presets', function () {

it(`has NO ${ isarray } on nolyfill with ignore`, function () {

const check = make_predicate({ nolyfill, ignore: [ isarray ] });
const ignore = lookup([ isarray ]);

const check = make_predicate({ nolyfill, ignore });

assert_false(check(__w__a__t__), __w__a__t__);
assert_false(check(isarray), isarray);
Expand Down

0 comments on commit 0959a28

Please sign in to comment.