summaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 691d676fe5a5eb4c5ac734791854eedff64ac4fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { fold, map } from 'fp-ts/lib/Either'
import { identity, pipe } from 'fp-ts/lib/function'
import { find } from './eval'
import { parser } from './parser'

const toSourceString = (r: string | RegExp): string => typeof r === 'string' ? r : r.source

export const filter = <T>(listExp: string | RegExp, list: T[]): T[] =>
  pipe(
    listExp,
    toSourceString,
    parser,
    map(([lxp, _]) => find(lxp, list)),
    fold(([err, _]) => {
      throw new Error(err)
    }, identity),
  )