-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: add startsWith and endsWith #27
Conversation
: false // T (Text) is exhausted with S (Search) leftover | ||
: StartsWith<Slice<T, P>, S, 0> // P is >0, slice | ||
: StartsWith<T, S, 0> // P (Position) is negative, ignore | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wishing for a more succinct way to express this type, but position
creates a bunch of extra if/else blocks and corner cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is positioning part of the native API? I didn't know about it.
In this case I think you can use Slice in to simplify the type implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, but the way startsWith
and slice
handle negative indices is a little different
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to capture all these cases in the unit tests
src/primitives.ts
Outdated
? P extends 0 | ||
? T extends `${infer TFirst}${infer TRest}` | ||
? S extends `${infer SFirst}${infer SRest}` | ||
? TFirst extends SFirst | ||
? StartsWith<TRest, SRest, P> // Compare next character | ||
: false // T differs from S | ||
: true // S (Search) is exhausted (and didn't fail) | ||
: false // T (Text) is exhausted with S (Search) leftover | ||
: StartsWith<Slice<T, P>, S, 0> // P is >0, slice |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? P extends 0 | |
? T extends `${infer TFirst}${infer TRest}` | |
? S extends `${infer SFirst}${infer SRest}` | |
? TFirst extends SFirst | |
? StartsWith<TRest, SRest, P> // Compare next character | |
: false // T differs from S | |
: true // S (Search) is exhausted (and didn't fail) | |
: false // T (Text) is exhausted with S (Search) leftover | |
: StartsWith<Slice<T, P>, S, 0> // P is >0, slice | |
? Slice<T, P> extends `${S}${string}` ? true : false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooooh I forgot that TS could do
`${S}${string}`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your suggestion won't work as-is because of the way startsWith
handles negative position, but with a little bit of adaptation I can replace the existing type with something a bit shorter
Go on and implement endsWith along with it 😉 |
I'll start a second PR with it if I can make it work. (PR on this branch, or on main if you accept/merge this one.) |
I believe you can use Slice<S, 0, P> to implement endsWith, can't you? You probably wouldn't need the negative as it is done here |
@jly36963 I just merged the fix for |
README.md
Outdated
@@ -100,6 +100,7 @@ npm install string-ts | |||
- [split](#split) | |||
- [toLowerCase](#tolowercase) | |||
- [toUpperCase](#touppercase) | |||
- [startsWith](#startsWith) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you keep alphabetical order here? I think it is the only place where it was overlooked.
False alarm on my part -- negative indices will always return false for The behavior for out-of-bounds indices and negative indices are opposite for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
No description provided.