-
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 #80
Changes from 10 commits
96758b8
4ac721c
ac553ca
ef56635
207e409
ab7d700
06b990c
026cd06
7ee208d
1436ec2
77c6ab2
516f2d8
5270ff7
15e6e12
779386c
ed22848
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { | ||
UncapitalizeSTS, | ||
LowercaseSTS, | ||
UppercaseSTS, | ||
CapitalizeSTS, | ||
} from './intrinsic.js' | ||
|
||
namespace Internals { | ||
type UncapitalizeSTS1 = Expect<Equal<UncapitalizeSTS<'ABC'>, 'aBC'>> | ||
type UncapitalizeSTS2 = Expect<Equal<UncapitalizeSTS<string>, string>> | ||
|
||
type LowercaseSTS1 = Expect<Equal<LowercaseSTS<'ABC'>, 'abc'>> | ||
type LowercaseSTS2 = Expect<Equal<LowercaseSTS<string>, string>> | ||
|
||
type UppercaseSTS1 = Expect<Equal<UppercaseSTS<'abc'>, 'ABC'>> | ||
type UppercaseSTS2 = Expect<Equal<UppercaseSTS<string>, string>> | ||
|
||
type CapitalizeSTS1 = Expect<Equal<CapitalizeSTS<'abc'>, 'Abc'>> | ||
type CapitalizeSTS2 = Expect<Equal<CapitalizeSTS<string>, string>> | ||
} | ||
|
||
test('dummy test', () => expect(true).toBe(true)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export type UncapitalizeSTS<T extends string> = string extends T | ||
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. Unfortunately this file is necessary. The built-in Uncapitalize/Lowercase/Uppercase/Capitalize types have what (IMO) is bad behavior type CapitalizeSTS1 = Expect<Equal<CapitalizeSTS<'abc'>, 'Abc'>>
type CapitalizeSTS2 = Expect<Equal<CapitalizeSTS<string>, string>>
type bad1 = Expect<Equal<Capitalize<'abc'>, 'Abc'>>
type bad2 = Expect<Equal<Capitalize<string>, Capitalize<string>>>
// ^ WTF The Why doesn't This unfortunate behavior affects everything downstream (ie: word casing and object keys). We need utilities that work on both string and literal types, so I had to replace the built-in casing types with new types that would work correctly for both string and literal type inputs. 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. I don't know the answer but that makes me feel better about our functions also not being prepared to handle loose types |
||
? string | ||
: Uncapitalize<T> | ||
|
||
export type LowercaseSTS<T extends string> = string extends T | ||
? string | ||
: Lowercase<T> | ||
|
||
export type UppercaseSTS<T extends string> = string extends T | ||
? string | ||
: Uppercase<T> | ||
|
||
export type CapitalizeSTS<T extends string> = string extends T | ||
? string | ||
: Capitalize<T> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,26 @@ namespace MathTest { | |
// NOTE: `Subtract` only supports non-negative integers | ||
type testSubtract1 = Expect<Equal<Math.Subtract<2, 1>, 1>> | ||
type testSubtract2 = Expect<Equal<Math.Subtract<2, 2>, 0>> | ||
type testSubtract3 = Expect<Equal<Math.Subtract<number, 2>, number>> | ||
type testSubtract4 = Expect<Equal<Math.Subtract<2, number>, number>> | ||
|
||
type testIsNegative1 = Expect<Equal<Math.IsNegative<2>, false>> | ||
type testIsNegative2 = Expect<Equal<Math.IsNegative<0>, false>> | ||
type testIsNegative3 = Expect<Equal<Math.IsNegative<-1>, true>> | ||
type testIsNegative4 = Expect<Equal<Math.IsNegative<number>, boolean>> | ||
|
||
type testAbs1 = Expect<Equal<Math.Abs<-1>, 1>> | ||
type testAbs2 = Expect<Equal<Math.Abs<1>, 1>> | ||
type testAbs3 = Expect<Equal<Math.Abs<0>, 0>> | ||
type testAbs4 = Expect<Equal<Math.Abs<-0>, 0>> | ||
type testAbs5 = Expect<Equal<Math.Abs<number>, number>> | ||
|
||
type testGetPositiveIndex1 = Expect< | ||
Equal<Math.GetPositiveIndex<'abc', -1>, 2> | ||
> | ||
type testGetPositiveIndex2 = Expect< | ||
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. I would be ok with leaving these methods simpler since they are only used by us internally |
||
Equal<Math.GetPositiveIndex<string, -1>, number> | ||
> | ||
} | ||
|
||
test('dummy test', () => expect(true).toBe(true)) |
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>, ''>> | ||||||
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. This is gonna return undefined which is equal to runtime
Suggested change
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. |
||||||
} | ||||||
|
||||||
describe('charAt', () => { | ||||||
|
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.
Okay with leaving this one as unresolved for now, as
Reject
is not exportedThere 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.
Sure, and also I'm ok about all tests in this file as they don't have runtime counterparts