Skip to content

Commit

Permalink
trigger-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Mar 28, 2024
1 parent 54b3668 commit e510c2c
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import {
alljuxt,
complement,
includedIn,
join,
letIn,
logAround,
lowercase,
map,
max,
nonempty,
pipe,
range,
replace,
reverse,
sideLog,
sort,
split,
take,
Expand Down Expand Up @@ -364,3 +365,40 @@ export const someKewyordMatches = (keywords: string[]) => (x: string) =>

export const urlsInText = (text: string) =>
text.match(/https?:\/\/[^\s/$.?#].[^\s]*/g) || [];

type Keywords = { keywords: string[]; antiKeywords?: string[] };

type ValueToKeywords<T extends number | string | symbol> = Record<
T,
Keywords
>;

type PredicateAndValue<T> = [(txt: string) => boolean, T];

const keywordMatchers = <T extends number | string | symbol>(
valuesAndKeywords: ValueToKeywords<T>,
) =>
(Object.entries(valuesAndKeywords) as [T, Keywords][]).map(
(
[value, { keywords, antiKeywords }]: [T, Keywords],
): PredicateAndValue<T> => [
alljuxt(
someKewyordMatches(keywords),
complement(someKewyordMatches(antiKeywords ?? [])),
),
value,
],
);

export const triggerByText = <T extends number | string | symbol>(
x: ValueToKeywords<T>,
) =>
pipe(
keywordMatchers<T>,
(matcher: PredicateAndValue<T>[]): (text: string) => T[] =>
// @ts-expect-error pipe typing doesn't handle generics
pipe(
(text) => matcher.filter(([predicate]) => predicate(text)),
map(([, value]) => value),
),
)(x);

0 comments on commit e510c2c

Please sign in to comment.