Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement between #162

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const anyOf = <New extends InputSource[]>(...args: New) =>
MapToCapturedGroupsArr<New>
>

export const between = createInput('[\\w-\\w]')
export const char = createInput('.')
export const word = createInput('\\b\\w+\\b')
export const wordChar = createInput('\\w')
Expand Down
6 changes: 6 additions & 0 deletions src/core/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export interface Input<
max: Max
) => Input<IfUnwrapped<V, `(?:${V}){${Min},${Max}}`, `${V}{${Min},${Max}}`>, G, C>
}
between: <First extends number | string, Last extends number | string>(
first: First,
last: First extends string ? string : number
) => Input<IfUnwrapped<V, `(?:${V}){${First},${Last}}`, `${V}{${First},${Last}}`>, G, C>
/** 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()`. Alias for `groupedAs` */
as: <K extends string>(
key: K
Expand Down Expand Up @@ -117,6 +121,8 @@ export const createInput = <
atLeast: (min: number) => createInput(`${wrap(s)}{${min},}`) as any,
between: (min: number, max: number) => createInput(`${wrap(s)}{${min},${max}}`) as any,
}),
between: (first: string | number, last: string | number) =>
createInput(`[${first}-${last}]`) as any,
optionally: () => createInput(`${wrap(s)}?`) as any,
as: groupedAsFn,
groupedAs: groupedAsFn,
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ describe('inputs', () => {
expect(pattern.toString()).toMatchInlineSnapshot('"test\\\\/thing"')
expect(createRegExp(pattern).test('test/thing')).toBeTruthy()
})
it('between', () => {
const letterBetween = exactly('').between('c', 'g')
expect(letterBetween.toString()).toMatchInlineSnapshot('"[c-g]"')
expect(createRegExp(letterBetween).test('abcdefghi')).toBeTruthy()

const digitBetween = exactly('').between(2, 4)
expect(digitBetween.toString()).toMatchInlineSnapshot('"[2-4]"')
expect(createRegExp(digitBetween).test('1234567')).toBeTruthy()
})
it('times', () => {
expect(exactly('test').times.between(1, 3).toString()).toMatchInlineSnapshot('"(?:test){1,3}"')
expect(exactly('test').times(4).or('foo').toString()).toMatchInlineSnapshot(
Expand Down