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: add "between" input #220

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/core/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export interface Input<
before: <I extends InputSource>(
input: I
) => Input<`${V}(?=${GetValue<I>})`, G, [...C, ...GetCapturedGroupsArr<I>]>

between: <I1 extends InputSource, I2 extends InputSource>(
input1: I1,
input2: I2
) => Input<
`(?<=${GetValue<I1>})${V}(?=${GetValue<I2>})`,
G,
[...GetCapturedGroupsArr<I1>, ...C, ...GetCapturedGroupsArr<I2>]
>
/** 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: <I extends InputSource>(
input: I
Expand Down Expand Up @@ -110,6 +119,7 @@ export const createInput = <
or: input => createInput(`(?:${s}|${exactly(input)})`),
after: input => createInput(`(?<=${exactly(input)})${s}`),
before: input => createInput(`${s}(?=${exactly(input)})`),
between: (input1, input2) => createInput(`(?<=${exactly(input1)})${s}(?=${exactly(input2)})`),
notAfter: input => createInput(`(?<!${exactly(input)})${s}`),
notBefore: input => createInput(`${s}(?!${exactly(input)})`),
times: Object.assign((number: number) => createInput(`${wrap(s)}{${number}}`) as any, {
Expand Down
8 changes: 8 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ describe('inputs', () => {
expect('fooafoo'.match(regExp)?.[0]).toMatchInlineSnapshot('"a"')
expect(regExp.test('foo')).toBeFalsy()
})

it('between', () => {
const regExp = createRegExp(char.between('foo', 'bar'))
expect(regExp).toMatchInlineSnapshot('/\\(\\?<=foo\\)\\.\\(\\?=bar\\)/')
expect('fooabar'.match(regExp)?.[0]).toMatchInlineSnapshot('"a"')
expect(regExp.test('foo')).toBeFalsy()
expect(regExp.test('bar')).toBeFalsy()
})
it('notBefore', () => {
const regExp = createRegExp(exactly('bar').notBefore('foo'))
expect(regExp).toMatchInlineSnapshot('/bar\\(\\?!foo\\)/')
Expand Down