-
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
fix: handle non-literal strings #105
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { type CharAt, charAt } from './char-at.js' | ||
|
||
namespace TypeTests { | ||
type test = Expect<Equal<CharAt<'some nice string', 5>, 'n'>> | ||
type test1 = Expect<Equal<CharAt<'some nice string', 5>, 'n'>> | ||
type test2 = Expect<Equal<CharAt<string, 5>, string>> | ||
type test3 = Expect<Equal<CharAt<'some nice string', number>, string>> | ||
|
||
// TODO: index greater than Length<T> | ||
// type test4 = Expect<Equal<CharAt<'some nice string', 100>, ''>> | ||
Comment on lines
+8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the plan? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of scope, will fix later |
||
} | ||
|
||
describe('charAt', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,12 @@ export type Replace< | |
lookup extends string | RegExp, | ||
replacement extends string = '', | ||
> = lookup extends string | ||
? sentence extends `${infer rest}${lookup}${infer rest2}` | ||
? string extends lookup | sentence | replacement | ||
? string | ||
: sentence extends `${infer rest}${lookup}${infer rest2}` | ||
? `${rest}${replacement}${rest2}` | ||
: sentence | ||
: string | ||
: string // Regex used, can't preserve literal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good one |
||
/** | ||
* A strongly-typed version of `String.prototype.replace`. | ||
* @param sentence the sentence to replace. | ||
|
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.
Clever