diff --git a/src/index.ts b/src/index.ts index db1ff55..b987b5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,17 @@ // Utils // Compensation type As = unknown; -type Head = T[0]; -type Tail = T extends [infer _, ...infer Rest] +export type Head = T[0]; +/** + * Grab the tail of a list (all elements except the first) + * + * @example + * ```ts + * // Will omit the 0-th capture, leaving only the capture groups + * type Captures = Tail>; + * ``` + */ +export type Tail = T extends [infer _, ...infer Rest] ? Rest : never ; @@ -273,7 +282,7 @@ type IndexTokenInternal = TToken ex left: IndexTokenInternal, right: IndexTokenInternal['length'] + FlattenToken['length'] & number >> } : TToken extends { type: 'groups' } ? { @@ -283,7 +292,7 @@ type IndexTokenInternal = TToken ex ; type IndexToken = IndexTokenInternal; // TokenWithIndex type -type TokenWithIndex = IsSatisfied<{type: string}, +type TokenWithIndex = IsSatisfied<{type: string}, { type: 'alternation', left: TokenWithIndex, @@ -374,7 +383,10 @@ type Distribute = string extends T +/** + * Parse the capture groups from a regex-like string literal type + */ +export type Parse = string extends T ? { captures: [string, ...(string | undefined)[]], namedCaptures: Record; @@ -391,6 +403,30 @@ type Parse = string extends T }>>> ; +/** + * Get the list of indexed captures from a regex-like string literal type + * + * @example + * ```ts + * // Will get all non-named captures, including the string itself at index 0 + * type AllCaptures = ParseCaptures; + * + * // Will omit the 0-th capture, leaving only the capture groups + * type OnlyGroups = Tail>; + * ``` + */ +export type ParseCaptures = Parse['captures']; + +/** + * Get the dictionary of named captures from a regex-like string literal type + * + * @example + * ```ts + * type AllNamedCaptures = ParseNamedCaptures; + * ``` + */ +export type ParseNamedCaptures = Parse['namedCaptures']; + type Remove = unknown extends AsLinked ? TMatch extends First ? Rest @@ -594,4 +630,5 @@ export const typedRegExp = < & (GlobalFalseIndicesBehavior | GlobalFalseIndicesBehavior) ) ) & (IndicesBehavior | IndicesBehavior)>; -}; \ No newline at end of file + +};