-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
052db81
commit b9bb4c8
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { curry } from "../deps.ts"; | ||
import { charAt as _charAt } from "../uncurry/string/char_at.ts"; | ||
|
||
/** | ||
* Returns the character at the specified index. | ||
* @param pos - The zero-based index of the desired character | ||
* @param val - Any `String` | ||
* @returns The result of `val.charAt(pos)` | ||
* | ||
* @example | ||
* ```ts | ||
* charAt(0, 'hello') // 'h' | ||
* charAt(4, 'hello') // 'o' | ||
* charAt(100, 'hello') // '' | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const charAt = curry(_charAt); | ||
|
||
export { charAt }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { charAt } from "./char_at.ts"; | ||
import { assertEqualsType } from "../dev_deps.ts"; | ||
|
||
Deno.test("charAt", () => { | ||
assertEqualsType<(val: string) => string>(charAt(0)); | ||
assertEqualsType<string>(charAt(0)("")); | ||
assertEqualsType<string>(charAt(0, "")); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
|
||
type IsNegative<T extends number | bigint> = `${T}` extends `-${infer _}` ? true | ||
: false; | ||
type String2Array<T extends string> = T extends "" ? [] | ||
: T extends `${infer F}${infer R}` ? [F, ...String2Array<R>] | ||
: string[]; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
type CharAt<T extends number, U extends string> = IsNegative<T> extends true | ||
? "" | ||
: String2Array<U>[T] extends infer R ? R extends undefined ? "" : R | ||
: string; | ||
|
||
/** | ||
* Returns the character at the specified index. | ||
* @param pos - The zero-based index of the desired character | ||
* @param val - Any `String` | ||
* @returns The result of `val.charAt(pos)` | ||
* | ||
* @remarks | ||
* Due to the TypeScript recursion limit, the maximum number of valid characters is `23`. If you want to adapt a `string` longer than that, need to cast it to a `string` type. | ||
* | ||
* @example | ||
* ```ts | ||
* charAt(0, 'hello') // 'h' | ||
* charAt(4, 'hello') // 'o' | ||
* charAt(100, 'hello') // '' | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const charAt = <T extends number, U extends string>( | ||
pos: T, | ||
val: U, | ||
): CharAt<T, U> => val.charAt(pos) as CharAt<T, U>; | ||
|
||
export { charAt }; | ||
export type { CharAt }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { assertEquals, assertEqualsType } from "../../dev_deps.ts"; | ||
import { CharAt, charAt } from "./char_at.ts"; | ||
|
||
Deno.test("charAt", () => { | ||
const table: [number, string, string][] = [ | ||
[0, "", ""], | ||
[0, "abc", "a"], | ||
[1, "abc", "b"], | ||
[2, "abc", "c"], | ||
[3, "abc", ""], | ||
[100, "abc", ""], | ||
[-1, "abc", ""], | ||
[-100, "abc", ""], | ||
[-0, "abc", "a"], | ||
[-0, "", ""], | ||
]; | ||
table.forEach(([pos, val, expected]) => { | ||
assertEquals( | ||
charAt(pos, val), | ||
expected, | ||
`charAt(${pos}, ${val}) -> ${expected}`, | ||
); | ||
}); | ||
|
||
assertEqualsType<"", CharAt<0, "">>(); | ||
assertEqualsType<"", CharAt<1, "">>(); | ||
assertEqualsType<"", CharAt<2, "">>(); | ||
assertEqualsType<"a", CharAt<0, "a">>(); | ||
assertEqualsType<"", CharAt<1, "a">>(); | ||
assertEqualsType<"a", CharAt<0, "ab">>(); | ||
assertEqualsType<"b", CharAt<1, "ab">>(); | ||
assertEqualsType<"", CharAt<2, "ab">>(); | ||
assertEqualsType<"", CharAt<-1, "">>(); | ||
assertEqualsType<"", CharAt<-1, "a">>(); | ||
assertEqualsType<"", CharAt<-1, "ab">>(); | ||
assertEqualsType<"", CharAt<-100, "ab">>(); | ||
assertEqualsType<"", CharAt<100, "ab">>(); | ||
assertEqualsType<"a", CharAt<-0, "ab">>(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters