UNPKG

expressive-ts

Version:

A functional programming library designed to simplify building complex regular expressions

330 lines (329 loc) 8.42 kB
/** * Expressions allow for composition of complex regular expressions in a clear, concise, * and declarative manner. * * Every expression should begin with a call to `compile`, followed by the individual * methods used to build the regular expression. In addition, regular expression `Flags` * can be switched on or off at any time. * * A simple example would be to perform a case-insensitive search of a string for * the pattern `foo`, optionally followed by `bar`. With `expressive-ts`, representing * this logic is as simple as the following * * @example * import { pipe } from 'fp-ts/es6/function' * import * as E from 'expressive-ts/lib/Expression' * * const expression = pipe( * E.compile, * E.caseInsensitive, * E.string('foo'), * E.maybe('bar') * ) * * const regex = pipe(expression, E.toRegex) * regex.test('foo') // => true * regex.test('foobar') // => true * regex.test('bar') // => false * * pipe(expression, E.toRegexString) // => /(?:foo)(?:bar)?/i * * @since 0.0.1 */ import * as M from 'fp-ts/es6/Monoid' import * as T from 'fp-ts/es6/Traced' import { Endomorphism } from 'fp-ts/es6/function' /** * @category models * @since 0.0.1 */ export interface ExpressionBuilder extends T.Traced<Expression, Expression> {} /** * @category models * @since 0.0.1 */ export interface Expression { /** * The expression pattern prefix. */ readonly prefix: string /** * The expression pattern. */ readonly pattern: string /** * The expression pattern suffix. */ readonly suffix: string /** * The expression source. */ readonly source: string /** * The expression flags. */ readonly flags: Flags } /** * @category model * @since 0.0.1 */ export interface Flags { /** * Sets the global search flag (`g`) which indicates that the expression should be * tested against all possible matches in the input. */ readonly allowMultiple: boolean /** * Sets the case-insensitive search flag (`i`) which indicates that the expression * should not distinguish between uppercase and lowercase characters in the input. */ readonly caseInsensitive: boolean /** * Sets the multi-line search flag (`m`) which indicates that the expression should * treat a multiline input as multiple lines. */ readonly lineByLine: boolean /** * Sets the dotAll flag (`s`) which indicates that the expression should allow the * dot special character (`.`) to additionally match line terminator characters in * the input. */ readonly singleLine: boolean /** * Sets the sticky flag (`s`) which indicates that the expression should match only * beginning at the index indicated by the `lastIndex` property of the expression. */ readonly sticky: boolean /** * Sets the unicode flag (`u`) which enables various Unicode-related features within * the expression. */ readonly unicode: boolean } /** * @category destructors * @since 0.0.1 */ export declare const toRegex: (builder: ExpressionBuilder) => RegExp /** * @category destructors * @since 0.0.1 */ export declare const toRegexString: (builder: ExpressionBuilder) => string /** * @category pipeables * @since 0.0.1 */ export declare const withMultiple: (flag: boolean) => (wa: ExpressionBuilder) => Expression /** * @category pipeables * @since 0.0.1 */ export declare const withCaseInsensitive: (flag: boolean) => (wa: ExpressionBuilder) => Expression /** * @category pipeables * @since 0.0.1 */ export declare const withLineByLine: (flag: boolean) => (wa: ExpressionBuilder) => Expression /** * @category pipeables * @since 0.0.1 */ export declare const withSingleLine: (flag: boolean) => (wa: ExpressionBuilder) => Expression /** * @category pipeables * @since 0.0.1 */ export declare const withSticky: (flag: boolean) => (wa: ExpressionBuilder) => Expression /** * @category pipeables * @since 0.0.1 */ export declare const withUnicode: (flag: boolean) => (wa: ExpressionBuilder) => Expression /** * @category combinators * @since 0.0.1 */ export declare const allowMultiple: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const caseInsensitive: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const lineByLine: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const singleLine: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const sticky: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const unicode: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const compile: ExpressionBuilder /** * @category combinators * @since 0.0.1 */ export declare const startOfInput: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const endOfInput: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const string: (value: string) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const maybe: (value: string) => Endomorphism<ExpressionBuilder> /** * @deprecated * @category combinators * @since 0.0.1 */ export declare const or: (value: string) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.2 */ export declare const orExpression: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const anything: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const anythingBut: (value: string) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const something: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const somethingBut: (value: string) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const anyOf: (value: string) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const not: (value: string) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const range: (from: string, to: string) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const lineBreak: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const tab: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const word: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const digit: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const whitespace: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const zeroOrMore: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const zeroOrMoreLazy: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const oneOrMore: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const oneOrMoreLazy: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const exactly: (amount: number) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const atLeast: (min: number) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const between: (min: number, max: number) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const betweenLazy: (min: number, max: number) => Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const beginCapture: Endomorphism<ExpressionBuilder> /** * @category combinators * @since 0.0.1 */ export declare const endCapture: Endomorphism<ExpressionBuilder> /** * @category instances * @since 0.0.1 */ export declare const monoidFlags: M.Monoid<Flags> /** * @category instances * @since 0.0.1 */ export declare const monoidExpression: M.Monoid<Expression>