Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: infer group names from param with Input type #32

Merged
merged 2 commits into from
Jul 24, 2022
Merged
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
14 changes: 8 additions & 6 deletions src/core/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createInput, Input } from './internal'
import type { GetValue, EscapeChar } from './types/escape'
import type { Join } from './types/join'
import type { MapToGroups, MapToValues, InputSource } from './types/sources'
import type { MapToGroups, MapToValues, InputSource, GetGroup } from './types/sources'
import { IfSingle, wrap } from './wrap'

export type { Input }
Expand Down Expand Up @@ -49,19 +49,21 @@ export const not = {
export const maybe = <New extends InputSource<string>>(str: New) =>
createInput(`${wrap(exactly(str))}?`) as IfSingle<
GetValue<New>,
Input<`${GetValue<New>}?`>,
Input<`(${GetValue<New>})?`>
Input<`${GetValue<New>}?`, GetGroup<New>>,
Input<`(${GetValue<New>})?`, GetGroup<New>>
>

/** This escapes a string input to match it exactly */
export const exactly = <New extends InputSource<string>>(input: New): Input<GetValue<New>> =>
export const exactly = <New extends InputSource<string>>(
input: New
): Input<GetValue<New>, GetGroup<New>> =>
typeof input === 'string'
? (createInput(input.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')) as any)
: input

export const oneOrMore = <New extends InputSource<string>>(str: New) =>
createInput(`${wrap(exactly(str))}+`) as IfSingle<
GetValue<New>,
Input<`${GetValue<New>}+`>,
Input<`(${GetValue<New>})+`>
Input<`${GetValue<New>}+`, GetGroup<New>>,
Input<`(${GetValue<New>})+`, GetGroup<New>>
>
3 changes: 3 additions & 0 deletions src/core/types/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { Input } from '../internal'
import type { GetValue } from './escape'

export type InputSource<S extends string = never, T extends string = never> = S | Input<S, T>
export type GetGroup<T extends InputSource<string>> = T extends Input<string, infer Group>
? Group
: never
export type MapToValues<T extends InputSource<any, any>[]> = T extends [infer First, ...infer Rest]
? First extends InputSource<string>
? [GetValue<First>, ...MapToValues<Rest>]
Expand Down
13 changes: 13 additions & 0 deletions test/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
carriageReturn,
charNotIn,
} from '../src/core/inputs'
import { createRegExp, MagicRegExp } from '../src'

describe('inputs', () => {
it('charIn', () => {
Expand Down Expand Up @@ -54,19 +55,31 @@ describe('inputs', () => {
const regexp = new RegExp(input as any)
expect(regexp).toMatchInlineSnapshot('/\\(foo\\)\\?/')
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'(foo)?'>()
const nestedInputWithGroup = maybe(exactly('foo').as('groupName'))
expectTypeOf(createRegExp(nestedInputWithGroup)).toEqualTypeOf<
MagicRegExp<'/((?<groupName>foo))?/', 'groupName', never>
>()
})
it('oneOrMore', () => {
const input = oneOrMore('foo')
const regexp = new RegExp(input as any)
expect(regexp).toMatchInlineSnapshot('/\\(foo\\)\\+/')
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'(foo)+'>()
const nestedInputWithGroup = oneOrMore(exactly('foo').as('groupName'))
expectTypeOf(createRegExp(nestedInputWithGroup)).toEqualTypeOf<
MagicRegExp<'/((?<groupName>foo))+/', 'groupName', never>
>()
})
it('exactly', () => {
const input = exactly('fo?[a-z]{2}/o?')
expect(new RegExp(input as any)).toMatchInlineSnapshot(
'/fo\\\\\\?\\\\\\[a-z\\\\\\]\\\\\\{2\\\\\\}\\\\/o\\\\\\?/'
)
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'fo\\?\\[a-z\\]\\{2\\}\\/o\\?'>()
const nestedInputWithGroup = exactly(maybe('foo').and('bar').as('groupName'))
expectTypeOf(createRegExp(nestedInputWithGroup)).toEqualTypeOf<
MagicRegExp<'/(?<groupName>(foo)?bar)/', 'groupName', never>
>()
})
it('word', () => {
const input = word
Expand Down