Skip to content

Commit

Permalink
refactor: move Input type alongside createInput util
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 17, 2022
1 parent 9267745 commit 63d8dd8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
37 changes: 2 additions & 35 deletions src/core/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
import { createInput } from './internal'
import { createInput, Input } from './internal'

export interface Input<T extends string = never> {
/** this adds a new pattern to the current input */
and: <X extends string = never>(input: string | Input<X>) => Input<T | X>
/** this provides an alternative to the current input */
or: <X extends string = never>(input: string | Input<X>) => Input<T | X>
/** this is a positive lookbehind. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari) */
after: (input: string | Input) => Input<T>
/** this is a positive lookahead */
before: (input: string | Input) => Input<T>
/** these is a negative lookbehind. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari) */
notAfter: (input: string | Input) => Input<T>
/** this is a negative lookahead */
notBefore: (input: string | Input) => Input<T>
times: {
/** repeat the previous pattern an exact number of times */
(number: number): Input<T>
/** specify a range of times to repeat the previous pattern */
between: (min: number, max: number) => Input<T>
/** specify that the expression can repeat any number of times, _including none_ */
any: () => Input<T>
/** specify that the expression must occur at least x times */
atLeast: (min: number) => Input<T>
}
/** this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()` */
as: <K extends string>(key: K) => Input<T | K>
/** this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()` */
at: {
lineStart: () => Input<T>
lineEnd: () => Input<T>
}
/** this allows you to mark the input so far as optional */
optionally: () => Input<T>
toString: () => string
}
export type { Input }

/** This matches any character in the string provided */
export const charIn = (chars: string) => createInput(`[${chars.replace(/[-\\^\]]/g, '\\$&')}]`)
Expand Down
37 changes: 36 additions & 1 deletion src/core/internal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
import { Input, exactly } from './inputs'
import { exactly } from './inputs'

export interface Input<T extends string = never> {
/** this adds a new pattern to the current input */
and: <X extends string = never>(input: string | Input<X>) => Input<T | X>
/** this provides an alternative to the current input */
or: <X extends string = never>(input: string | Input<X>) => Input<T | X>
/** this is a positive lookbehind. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari) */
after: (input: string | Input) => Input<T>
/** this is a positive lookahead */
before: (input: string | Input) => Input<T>
/** these is a negative lookbehind. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari) */
notAfter: (input: string | Input) => Input<T>
/** this is a negative lookahead */
notBefore: (input: string | Input) => Input<T>
times: {
/** repeat the previous pattern an exact number of times */
(number: number): Input<T>
/** specify a range of times to repeat the previous pattern */
between: (min: number, max: number) => Input<T>
/** specify that the expression can repeat any number of times, _including none_ */
any: () => Input<T>
/** specify that the expression must occur at least x times */
atLeast: (min: number) => Input<T>
}
/** this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()` */
as: <K extends string>(key: K) => Input<T | K>
/** this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()` */
at: {
lineStart: () => Input<T>
lineEnd: () => Input<T>
}
/** this allows you to mark the input so far as optional */
optionally: () => Input<T>
toString: () => string
}

export const createInput = <T extends string = never>(s: string | Input<T>): Input<T> => {
return {
Expand Down

0 comments on commit 63d8dd8

Please sign in to comment.