44
55The MIT License (MIT)
66
7- Copyright (c) 2017 -2025 Haydn Paterson
7+ Copyright (c) 2024 -2025 Haydn Paterson
88
99Permission is hereby granted, free of charge, to any person obtaining a copy
1010of this software and associated documentation files (the "Software"), to deal
@@ -27,55 +27,80 @@ THE SOFTWARE.
2727---------------------------------------------------------------------------*/
2828
2929// deno-fmt-ignore-file
30+
3031import { type TTrim , Trim } from './trim.ts'
31- import { type TTake , type TTakeResult , Take , IsTakeTrue } from './take.ts'
32+ import { type TTake , Take , IsTakeTrue } from './take.ts'
3233
3334import * as Char from './char.ts'
3435
3536// ------------------------------------------------------------------
36- // TFirst
37+ // TakeFirst
3738// ------------------------------------------------------------------
38- type TIdentFirst = [ ...Char . TAlpha , Char . TUnderscore , Char . TDollarSign ]
39- type TIdentRest = [ ...TIdentFirst , ...Char . TDigit ]
40-
41- const IdentFirst = [ ...Char . Alpha , Char . Underscore , Char . DollarSign ]
42- const IdentRest = [ ...IdentFirst , ...Char . Digit ]
43-
44-
39+ type TFirst = [ ...Char . TAlpha , Char . TUnderscore , Char . TDollarSign ]
40+ const First = [ ...Char . Alpha , Char . Underscore , Char . DollarSign ]
4541
42+ type TTakeFirst < Input extends string > = (
43+ TTake < Input , TFirst > extends [ infer First extends string , infer Right extends string ]
44+ ? [ First , Right ]
45+ : [ ]
46+ )
47+ function TakeFirst < Input extends string > ( input : Input ) : TTakeFirst < Input > {
48+ const firstResult = Take ( input , First ) as string [ ]
49+ return (
50+ IsTakeTrue ( firstResult )
51+ ? firstResult
52+ : [ ]
53+ ) as never
54+ }
55+ // ------------------------------------------------------------------
56+ // TakeRemaining
57+ // ------------------------------------------------------------------
58+ type TRemaining = [ ...TFirst , ...Char . TDigit ]
59+ const Remaining = [ ...First , ...Char . Digit ]
4660
61+ type TTakeRemaining < Input extends string , Result extends string = '' > = (
62+ TTake < Input , TRemaining > extends [ infer Next extends string , infer Right extends string ]
63+ ? TTakeRemaining < Right , `${Result } ${Next } `>
64+ : [ Result , Input ]
65+ )
66+ function TakeRemaining < Input extends string > ( input : Input , result : string = '' ) : TTakeRemaining < Input > {
67+ const remainingResult = Take ( input , Remaining ) as string [ ]
68+ return (
69+ IsTakeTrue ( remainingResult )
70+ ? TakeRemaining ( remainingResult [ 1 ] , `${ result } ${ remainingResult [ 1 ] } ` )
71+ : [ input , result ]
72+ ) as never
73+ }
4774// ------------------------------------------------------------------
48- // TakeFirst
75+ // TakeIdent
4976// ------------------------------------------------------------------
50- type TTaskFirst < Input extends string > = (
51- TTake < Input , TIdentFirst > extends [ infer First extends string , infer Rest extends string ]
52- ? TTake < Rest , TIdentRest > extends [ infer Remaining extends string , infer Rest extends string ]
77+ type TTakeIdent < Input extends string > = (
78+ TTakeFirst < Input > extends [ infer First extends string , infer Remaining extends string ]
79+ ? TTakeRemaining < Remaining > extends [ infer Remaining extends string , infer Rest extends string ]
5380 ? [ `${First } ${Remaining } `, Rest ]
54- : [ ]
81+ : never
5582 : [ ]
5683)
57- function TakeFirst < Input extends string > ( input : Input ) : TTaskFirst < Input > {
58- const firstResult = Take ( input , IdentFirst ) as TTakeResult
84+ function TakeIdent < Input extends string > ( input : Input ) : TTakeIdent < Input > {
85+ const firstResult = TakeFirst ( input )
5986 return (
60- IsTakeTrue ( firstResult ) ? ( ( ) => {
61- const restResult = Take ( firstResult [ 1 ] , IdentRest as never ) as TTakeResult
62- return IsTakeTrue ( restResult )
63- ? [ `${ firstResult [ 0 ] } ${ restResult [ 0 ] } ` , restResult [ 1 ] ]
64- : [ ]
87+ IsTakeTrue ( firstResult ) ? ( ( ) => {
88+ const remainingResult = TakeRemaining ( firstResult [ 1 ] )
89+ return IsTakeTrue ( remainingResult )
90+ ? [ `${ firstResult [ 0 ] } ${ remainingResult [ 0 ] } ` , remainingResult [ 1 ] ]
91+ : ( ( ) => { throw 'Unreachable' } ) ( )
6592 } ) ( ) : [ ]
6693 ) as never
6794}
68-
69- type A = TTaskFirst < 'hello world' >
70-
7195// ------------------------------------------------------------------
7296// Tdent
7397// ------------------------------------------------------------------
74- export type TIdent < Input extends string ,
75- Trimmed extends string = TTrim < Input > ,
76- > = TTaskFirst < Trimmed >
98+ export type TIdent < Input extends string > = (
99+ TTakeIdent < TTrim < Input > >
100+ )
77101export function Ident < Input extends string > ( input : Input ) : TIdent < Input > {
78- const trimmed = Trim ( input )
79- return TakeFirst ( trimmed ) as never
102+ return TakeIdent ( Trim ( input ) ) as never
80103}
81104
105+ type B = TIdent < ' hello world' >
106+
0 commit comments