Skip to content

Commit 63d8dd8

Browse files
committed
refactor: move Input type alongside createInput util
1 parent 9267745 commit 63d8dd8

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

src/core/inputs.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,6 @@
1-
import { createInput } from './internal'
1+
import { createInput, Input } from './internal'
22

3-
export interface Input<T extends string = never> {
4-
/** this adds a new pattern to the current input */
5-
and: <X extends string = never>(input: string | Input<X>) => Input<T | X>
6-
/** this provides an alternative to the current input */
7-
or: <X extends string = never>(input: string | Input<X>) => Input<T | X>
8-
/** 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) */
9-
after: (input: string | Input) => Input<T>
10-
/** this is a positive lookahead */
11-
before: (input: string | Input) => Input<T>
12-
/** 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) */
13-
notAfter: (input: string | Input) => Input<T>
14-
/** this is a negative lookahead */
15-
notBefore: (input: string | Input) => Input<T>
16-
times: {
17-
/** repeat the previous pattern an exact number of times */
18-
(number: number): Input<T>
19-
/** specify a range of times to repeat the previous pattern */
20-
between: (min: number, max: number) => Input<T>
21-
/** specify that the expression can repeat any number of times, _including none_ */
22-
any: () => Input<T>
23-
/** specify that the expression must occur at least x times */
24-
atLeast: (min: number) => Input<T>
25-
}
26-
/** 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()` */
27-
as: <K extends string>(key: K) => Input<T | K>
28-
/** this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()` */
29-
at: {
30-
lineStart: () => Input<T>
31-
lineEnd: () => Input<T>
32-
}
33-
/** this allows you to mark the input so far as optional */
34-
optionally: () => Input<T>
35-
toString: () => string
36-
}
3+
export type { Input }
374

385
/** This matches any character in the string provided */
396
export const charIn = (chars: string) => createInput(`[${chars.replace(/[-\\^\]]/g, '\\$&')}]`)

src/core/internal.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
1-
import { Input, exactly } from './inputs'
1+
import { exactly } from './inputs'
2+
3+
export interface Input<T extends string = never> {
4+
/** this adds a new pattern to the current input */
5+
and: <X extends string = never>(input: string | Input<X>) => Input<T | X>
6+
/** this provides an alternative to the current input */
7+
or: <X extends string = never>(input: string | Input<X>) => Input<T | X>
8+
/** 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) */
9+
after: (input: string | Input) => Input<T>
10+
/** this is a positive lookahead */
11+
before: (input: string | Input) => Input<T>
12+
/** 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) */
13+
notAfter: (input: string | Input) => Input<T>
14+
/** this is a negative lookahead */
15+
notBefore: (input: string | Input) => Input<T>
16+
times: {
17+
/** repeat the previous pattern an exact number of times */
18+
(number: number): Input<T>
19+
/** specify a range of times to repeat the previous pattern */
20+
between: (min: number, max: number) => Input<T>
21+
/** specify that the expression can repeat any number of times, _including none_ */
22+
any: () => Input<T>
23+
/** specify that the expression must occur at least x times */
24+
atLeast: (min: number) => Input<T>
25+
}
26+
/** 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()` */
27+
as: <K extends string>(key: K) => Input<T | K>
28+
/** this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()` */
29+
at: {
30+
lineStart: () => Input<T>
31+
lineEnd: () => Input<T>
32+
}
33+
/** this allows you to mark the input so far as optional */
34+
optionally: () => Input<T>
35+
toString: () => string
36+
}
237

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

0 commit comments

Comments
 (0)