Skip to content

fix: update interface Input type parameter to contravariant type #79

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

Merged
merged 1 commit into from
Oct 27, 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
24 changes: 12 additions & 12 deletions src/core/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const charNotIn = <T extends string>(chars: T) =>
createInput(`[^${chars.replace(/[-\\^\]]/g, '\\$&')}]`) as Input<`[^${EscapeChar<T>}]`>

/** This takes an array of inputs and matches any of them */
export const anyOf = <New extends InputSource<string, string>[]>(...args: New) =>
export const anyOf = <New extends InputSource[]>(...args: New) =>
createInput(`(?:${args.map(a => exactly(a)).join('|')})`) as Input<
`(?:${Join<MapToValues<New>>})`,
MapToGroups<New>,
Expand Down Expand Up @@ -54,23 +54,23 @@ export const not = {
}

/** Equivalent to `?` - this marks the input as optional */
export const maybe = <New extends InputSource<string>>(str: New) =>
createInput(`${wrap(exactly(str))}?`) as IfUnwrapped<
GetValue<New>,
Input<`(?:${GetValue<New>})?`, GetGroup<New>, GetCapturedGroupsArr<New>>,
Input<`${GetValue<New>}?`, GetGroup<New>, GetCapturedGroupsArr<New>>
export const maybe = <New extends InputSource>(str: New) =>
createInput(`${wrap(exactly(str))}?`) as Input<
IfUnwrapped<GetValue<New>, `(?:${GetValue<New>})?`, `${GetValue<New>}?`>,
GetGroup<New>,
GetCapturedGroupsArr<New>
>

/** This escapes a string input to match it exactly */
export const exactly = <New extends InputSource<string>>(
export const exactly = <New extends InputSource>(
input: New
): Input<GetValue<New>, GetGroup<New>, GetCapturedGroupsArr<New>> =>
typeof input === 'string' ? (createInput(input.replace(ESCAPE_REPLACE_RE, '\\$&')) as any) : input

/** Equivalent to `+` - this marks the input as repeatable, any number of times but at least once */
export const oneOrMore = <New extends InputSource<string>>(str: New) =>
createInput(`${wrap(exactly(str))}+`) as IfUnwrapped<
GetValue<New>,
Input<`(?:${GetValue<New>})+`, GetGroup<New>, GetCapturedGroupsArr<New>>,
Input<`${GetValue<New>}+`, GetGroup<New>, GetCapturedGroupsArr<New>>
export const oneOrMore = <New extends InputSource>(str: New) =>
createInput(`${wrap(exactly(str))}+`) as Input<
IfUnwrapped<GetValue<New>, `(?:${GetValue<New>})+`, `${GetValue<New>}+`>,
GetGroup<New>,
GetCapturedGroupsArr<New>
>
42 changes: 19 additions & 23 deletions src/core/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const GROUPED_AS_REPLACE_RE = /^(?:\(\?:(.+)\)|(\(?.+\)?))$/
const GROUPED_REPLACE_RE = /^(?:\(\?:(.+)\)([?+*]|{[\d,]+})?|(.+))$/

export interface Input<
V extends string,
in V extends string,
G extends string = never,
C extends (string | undefined)[] = []
> {
/** this adds a new pattern to the current input */
and: {
/** this adds a new pattern to the current input */
<I extends InputSource<string, any>>(input: I): Input<
<I extends InputSource>(input: I): Input<
`${V}${GetValue<I>}`,
G | (I extends Input<any, infer NewGroups> ? NewGroups : never),
[...C, ...GetCapturedGroupsArr<I>]
Expand All @@ -22,63 +22,59 @@ export interface Input<
referenceTo: <N extends G>(groupName: N) => Input<`${V}\\k<${N}>`, G, C>
}
/** this provides an alternative to the current input */
or: <I extends InputSource<string, any>>(
or: <I extends InputSource>(
input: I
) => Input<
`(?:${V}|${GetValue<I>})`,
G | (I extends Input<any, infer NewGroups> ? NewGroups : never),
[...C, ...GetCapturedGroupsArr<I>]
>
/** 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: <I extends InputSource<string>>(
after: <I extends InputSource>(
input: I
) => Input<`(?<=${GetValue<I>})${V}`, G, [...GetCapturedGroupsArr<I>, ...C]>
/** this is a positive lookahead */
before: <I extends InputSource<string>>(
before: <I extends InputSource>(
input: I
) => Input<`${V}(?=${GetValue<I>})`, G, [...C, ...GetCapturedGroupsArr<I>]>
/** 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<string>>(
notAfter: <I extends InputSource>(
input: I
) => Input<`(?<!${GetValue<I>})${V}`, G, [...GetCapturedGroupsArr<I, true>, ...C]>
/** this is a negative lookahead */
notBefore: <I extends InputSource<string>>(
notBefore: <I extends InputSource>(
input: I
) => Input<`${V}(?!${GetValue<I>})`, G, [...C, ...GetCapturedGroupsArr<I, true>]>
/** repeat the previous pattern an exact number of times */
times: {
/** repeat the previous pattern an exact number of times */
<N extends number>(number: N): IfUnwrapped<
V,
Input<`(?:${V}){${N}}`, G, C>,
Input<`${V}{${N}}`, G, C>
>
<N extends number>(number: N): Input<IfUnwrapped<V, `(?:${V}){${N}}`, `${V}{${N}}`>, G, C>
/** specify that the expression can repeat any number of times, _including none_ */
any: () => IfUnwrapped<V, Input<`(?:${V})*`, G>, Input<`${V}*`, G, C>>
any: () => Input<IfUnwrapped<V, `(?:${V})*`, `${V}*`>, G, C>
/** specify that the expression must occur at least x times */
atLeast: <N extends number>(
number: N
) => IfUnwrapped<V, Input<`(?:${V}){${N},}`, G>, Input<`${V}{${N},}`, G, C>>
) => Input<IfUnwrapped<V, `(?:${V}){${N},}`, `${V}{${N},}`>, G, C>
/** specify a range of times to repeat the previous pattern */
between: <Min extends number, Max extends number>(
min: Min,
max: Max
) => IfUnwrapped<V, Input<`(?:${V}){${Min},${Max}}`, G, C>, Input<`${V}{${Min},${Max}}`, G, C>>
) => Input<IfUnwrapped<V, `(?:${V}){${Min},${Max}}`, `${V}{${Min},${Max}}`>, 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
) => Input<
`(?<${K}>${V extends `(?:${infer S})` ? S : V})`,
V extends `(?:${infer S})` ? `(?<${K}>${S})` : `(?<${K}>${V})`,
G | K,
[`(?<${K}>${V extends `(?:${infer S})` ? S : V})`, ...C]
[V extends `(?:${infer S})` ? `(?<${K}>${S})` : `(?<${K}>${V})`, ...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()` */
groupedAs: <K extends string>(
key: K
) => Input<
`(?<${K}>${V extends `(?:${infer S})` ? S : V})`,
V extends `(?:${infer S})` ? `(?<${K}>${S})` : `(?<${K}>${V})`,
G | K,
[`(?<${K}>${V extends `(?:${infer S})` ? S : V})`, ...C]
[V extends `(?:${infer S})` ? `(?<${K}>${S})` : `(?<${K}>${V})`, ...C]
>
/** this capture the entire input so far as an anonymous group */
grouped: () => Input<
Expand All @@ -92,7 +88,7 @@ export interface Input<
lineEnd: () => Input<`${V}$`, G, C>
}
/** this allows you to mark the input so far as optional */
optionally: () => IfUnwrapped<V, Input<`(?:${V})?`, G, C>, Input<`${V}?`, G, C>>
optionally: () => Input<IfUnwrapped<V, `(?:${V})?`, `${V}?`>, G, C>
toString: () => string
}

Expand All @@ -108,7 +104,7 @@ export const createInput = <

return {
toString: () => s.toString(),
and: Object.assign((input: InputSource<string, any>) => createInput(`${s}${exactly(input)}`), {
and: Object.assign((input: InputSource) => createInput(`${s}${exactly(input)}`), {
referenceTo: (groupName: string) => createInput(`${s}\\k<${groupName}>`),
}),
or: input => createInput(`(?:${s}|${exactly(input)})`),
Expand Down
2 changes: 1 addition & 1 deletion src/core/types/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type CharEscapeCharacter = '\\' | '^' | '-' | ']'
export type EscapeChar<T extends string> = Escape<T, CharEscapeCharacter>
export type StripEscapes<T extends string> = T extends `${infer A}\\${infer B}` ? `${A}${B}` : T

export type GetValue<T extends InputSource<string>> = T extends string
export type GetValue<T extends InputSource> = T extends string
? Escape<T, ExactEscapeChar>
: T extends Input<infer R>
? R
Expand Down
23 changes: 12 additions & 11 deletions src/core/types/sources.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
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 InputSource<S extends string = string, T extends string = never> = S | Input<any, T>
export type GetGroup<T extends InputSource> = T extends Input<any, infer Group> ? Group : never
export type GetCapturedGroupsArr<
T extends InputSource<string>,
T extends InputSource,
MapToUndefined extends boolean = false
> = T extends Input<string, any, infer CapturedGroupArr>
> = T extends Input<any, any, infer CapturedGroupArr>
? MapToUndefined extends true
? { [K in keyof CapturedGroupArr]: undefined }
: CapturedGroupArr
: []
export type MapToValues<T extends InputSource<any, any>[]> = T extends [infer First, ...infer Rest]
? First extends InputSource<string>
export type MapToValues<T extends InputSource[]> = T extends [
infer First,
...infer Rest extends InputSource[]
]
? First extends InputSource
? [GetValue<First>, ...MapToValues<Rest>]
: []
: []

export type MapToGroups<T extends InputSource<any, string>[]> = T extends [
export type MapToGroups<T extends InputSource[]> = T extends [
infer First,
...infer Rest
...infer Rest extends InputSource[]
]
? First extends Input<any, infer K>
? K | MapToGroups<Rest>
Expand All @@ -34,6 +35,6 @@ type Flatten<T extends any[]> = T extends [infer L, ...infer R]
: [L, ...Flatten<R>]
: []

export type MapToCapturedGroupsArr<T extends InputSource<any, string>[]> = Flatten<{
export type MapToCapturedGroupsArr<T extends InputSource[]> = Flatten<{
[K in keyof T]: T[K] extends Input<any, any, infer C> ? C : string[]
}>
4 changes: 2 additions & 2 deletions src/core/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { StripEscapes } from './types/escape'

export type IfUnwrapped<
Value extends string,
Yes extends Input<string>,
No extends Input<string>
Yes extends string,
No extends string
> = Value extends `(${string})`
? No
: StripEscapes<Value> extends `${infer A}${infer B}`
Expand Down
2 changes: 1 addition & 1 deletion test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Input } from 'magic-regexp'

export const extractRegExp = <T extends Input<any> = never>(input: T) =>
export const extractRegExp = <T = never>(input: T) =>
input as T extends Input<infer RE> ? RE : never