Skip to content

Commit bec63b7

Browse files
committed
Unsigned | Match
1 parent 4b287e7 commit bec63b7

12 files changed

Lines changed: 70 additions & 64 deletions

File tree

src/token/bigint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { Match } from './internal/result.ts'
31+
import { Match } from './internal/match.ts'
3232
import { type TTake, Take } from './internal/take.ts'
3333
import { type TInteger, Integer } from './integer.ts'
3434

src/token/ident.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { Match } from './internal/result.ts'
31+
import { Match } from './internal/match.ts'
3232
import { type TTrim, Trim } from './internal/trim.ts'
3333
import { type TTake, Take } from './internal/take.ts'
3434

src/token/integer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { Match } from './internal/result.ts'
31+
import { Match } from './internal/match.ts'
3232
import { type TTrim, Trim } from './internal/trim.ts'
3333
import { type TOptional, Optional } from './internal/optional.ts'
3434
import { type THyphen, Hyphen } from './internal/char.ts'

src/token/internal/many.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { IsResultTrue } from './result.ts'
31+
import { Match } from './match.ts'
3232
import { type TTake, Take } from './take.ts'
3333

3434
// ------------------------------------------------------------------
@@ -58,15 +58,10 @@ export type TMany<Allowed extends string[], Discard extends string[], Input exte
5858
: [Result, Input]
5959
)
6060
/** Takes characters from the Input until no-match. The Discard set is used to omit characters from the match */
61-
export function Many<Allowed extends string[], Discard extends string[], Input extends string>
62-
(allowed: [...Allowed], discard: [...Discard], input: Input, result: string = ''):
63-
TMany<Allowed, Discard, Input> {
64-
const takeResult = Take(allowed, input) as [string, string]
65-
return (
66-
IsResultTrue(takeResult)
67-
? IsDiscard(discard, takeResult[0])
68-
? Many(allowed, discard, takeResult[1], result)
69-
: Many(allowed, discard, takeResult[1], `${result}${takeResult[0]}`)
70-
: [result, input]
71-
) as never
61+
export function Many<Allowed extends string[], Discard extends string[], Input extends string>(allowed: [...Allowed], discard: [...Discard], input: Input, result: string = ''): TMany<Allowed, Discard, Input> {
62+
return Match(Take(allowed, input), (Char, Rest) =>
63+
IsDiscard(discard, Char)
64+
? Many(allowed, discard, Rest, result)
65+
: Many(allowed, discard, Rest, `${result}${Char}`),
66+
() => [result, input]) as never
7267
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import { IsArray, IsEqual } from './guard.ts'
3131
type TResult = [string, string] | []
3232

3333
/** Checks the value is a Tuple-2 [string, string] result */
34-
export function IsResultTrue(value: unknown): value is [string, string] {
35-
return IsArray(value) && IsEqual(value.length, 2)
34+
export function IsMatch(value: TResult): value is [string, string] {
35+
return IsEqual(value.length, 2)
3636
}
3737
/** Matches on a result and dispatches either left or right arm */
3838
export function Match(input: TResult, ok: (value: string, rest: string) => TResult, fail: () => TResult): TResult {
39-
return (IsResultTrue(input) ? ok(input[0], input[1]) : fail()) as never
40-
}
39+
return IsMatch(input) ? ok(input[0], input[1]) : fail()
40+
}

src/token/internal/optional.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { IsResultTrue } from './result.ts'
31+
import { Match } from './match.ts'
3232
import { type TTake, Take } from './take.ts'
3333

3434
/** Matches the given Value or empty string if no match. This function never fails */
@@ -38,12 +38,8 @@ export type TOptional<Value extends string, Input extends string> = (
3838
: ['', Input]
3939
)
4040
/** Matches the given Value or empty string if no match. This function never fails */
41-
export function Optional<Value extends string, Input extends string>
42-
(value: Value, input: Input): TOptional<Value, Value> {
43-
const result = Take([value], input)
44-
return (
45-
IsResultTrue(result)
46-
? result
47-
: ['', input]
48-
) as never
41+
export function Optional<Value extends string, Input extends string>(value: Value, input: Input): TOptional<Value, Value> {
42+
return Match(Take([value], input), (Optional, Rest) =>
43+
[Optional, Rest],
44+
() => ['', input]) as never
4945
}

src/token/number.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ THE SOFTWARE.
2929
// deno-fmt-ignore-file
3030

3131
import { IsEqual } from './internal/guard.ts'
32-
import { IsResultTrue, Match } from './internal/result.ts'
32+
import { IsMatch, Match } from './internal/match.ts'
3333
import { type TTrim, Trim } from './internal/trim.ts'
3434
import { type TTake, Take } from './internal/take.ts'
3535
import { type TMany, Many } from './internal/many.ts'
@@ -62,7 +62,7 @@ type TIsLeadingDot<Input extends string> = (
6262
TTake<[TDot], Input> extends [string, string] ? true : false
6363
)
6464
function IsLeadingDot<Input extends string>(input: Input): TIsLeadingDot<Input> {
65-
return IsResultTrue(Take([Dot], input)) as never
65+
return IsMatch(Take([Dot], input)) as never
6666
}
6767
// ------------------------------------------------------------------
6868
// TakeFractional

src/token/span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { Match } from './internal/result.ts'
31+
import { Match } from './internal/match.ts'
3232
import { type TTrim, Trim } from './internal/trim.ts'
3333
import { type TNewLine, NewLine } from './internal/char.ts'
3434
import { type TTake, Take } from './internal/take.ts'

src/token/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { Match } from './internal/result.ts'
31+
import { Match } from './internal/match.ts'
3232
import { type TTake, Take } from './internal/take.ts'
3333
import { type TTrim, Trim } from './internal/trim.ts'
3434
import { type TSpan, Span } from './span.ts'

src/token/unsigned_integer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { Match } from './internal/result.ts'
31+
import { Match } from './internal/match.ts'
3232
import { type TTrim, Trim } from './internal/trim.ts'
3333
import { type TTake, Take } from './internal/take.ts'
3434
import { type TMany, Many } from './internal/many.ts'

0 commit comments

Comments
 (0)