Skip to content

Commit

Permalink
feat: Add a charAt function
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavoguichard committed Oct 3, 2023
1 parent 7f61453 commit 4d9a618
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ npm install string-ts
- [trim](#trim)
- [trimStart](#trimstart)
- [trimEnd](#trimend)
- [chartAt](#charat)
- [join](#join)
- [replace](#replace)
- [replaceAll](#replaceall)
Expand Down Expand Up @@ -137,6 +138,17 @@ const result = trimEnd(str);
// ^ ' hello world'
```

### charAt
This function is a strongly-typed counterpart of `String.prototype.charAt`.

```ts
import { charAt } from 'string-ts';

const str = 'hello world';
const result = charAt(str, 6);
// ^ 'w'
```

### join
This function is a strongly-typed counterpart of `Array.prototype.join`.

Expand Down Expand Up @@ -380,6 +392,7 @@ Uppercase<'hello world'> // 'HELLO WORLD'
### General Type utilities from this library
```ts
St.Words<'hello-world'> // ['hello', 'world']
St.CharAt<'hello world', 6> // 'w'
St.Join<['hello', 'world'], '-'> // 'hello-world'
St.Replace<'hello-world', 'l', '1'> // 'he1lo-world'
St.ReplaceAll<'hello-world', 'l', '1'> // 'he11o-wor1d'
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// PRIMITIVES
export type {
CharAt,
Join,
Replace,
ReplaceAll,
Expand All @@ -9,6 +10,7 @@ export type {
Trim,
} from './primitives'
export {
charAt,
join,
replace,
replaceAll,
Expand Down
10 changes: 10 additions & 0 deletions src/primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ namespace TypeTests {
type test7 = Expect<
Equal<Subject.Split<'some nice string', ' '>, ['some', 'nice', 'string']>
>
type test8 = Expect<Equal<Subject.CharAt<'some nice string', 5>, 'n'>>
}

describe('primitives', () => {
describe('charAt', () => {
test('should get the character of a string at the given index in both type and runtime level', () => {
const data = 'some nice string'
const result = subject.charAt(data, 5)
expect(result).toEqual('n')
type test = Expect<Equal<typeof result, 'n'>>
})
})

describe('join', () => {
test('should join words in both type level and runtime level', () => {
const data: ['a', 'b', 'c'] = ['a', 'b', 'c']
Expand Down
37 changes: 33 additions & 4 deletions src/primitives.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { Is } from './utils'
import type { Is } from './utils'

/**
* Gets the character at the given index.
* T: The string to get the character from.
* index: The index of the character.
*/
type CharAt<T extends string, index extends number> = Split<T, ''>[index]
/**
* A strongly typed version of `String.prototype.charAt`.
* @param str the string to get the character from.
* @param index the index of the character.
* @returns the character in both type level and runtime.
* @example charAt('hello world', 6) // 'w'
*/
function charAt<T extends string, I extends number>(
str: T,
index: I,
): CharAt<T, I> {
return str.charAt(index)
}

/**
* Joins a tuple of strings with the given delimiter.
* T: The current tuple of strings.
* T: The tuple of strings to join.
* delimiter: The delimiter.
*/
type Join<
Expand Down Expand Up @@ -168,5 +188,14 @@ function trim<T extends string>(str: T) {
return str.trim() as Trim<T>
}

export type { Join, Replace, ReplaceAll, Split, TrimStart, TrimEnd, Trim }
export { join, replace, replaceAll, split, trim, trimStart, trimEnd }
export type {
CharAt,
Join,
Replace,
ReplaceAll,
Split,
TrimStart,
TrimEnd,
Trim,
}
export { charAt, join, replace, replaceAll, split, trim, trimStart, trimEnd }

0 comments on commit 4d9a618

Please sign in to comment.