Skip to content

Commit

Permalink
✨ Add trimLeft function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 3, 2021
1 parent cc7ce48 commit 3aa6af9
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 1 deletion.
73 changes: 73 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,33 @@ trim(' hello ') // 'hello'
```
### trimLeft
<span class="tag beta">beta</span>
Removes space from left ends of the string.
::: warning
This API is provided as a preview for developers and may change based on feedback that we receive.
Do not use this API in a production environment.
:::
**Signature:**
```ts
trimLeft: <T extends string>(val: T) => TrimLeft<T>
```
#### Example
```ts
trimLeft(' hello') // 'hello'
trimLeft(' \n\thello') // 'hello'
```
### upperCase
Expand Down Expand Up @@ -1755,3 +1782,49 @@ type Primitive = string | number | bigint | boolean | symbol | undefined | null;
```
### Space
<span class="tag beta">beta</span>
Alias for Space values.
::: warning
This API is provided as a preview for developers and may change based on feedback that we receive.
Do not use this API in a production environment.
:::
**Signature:**
```ts
type Space = " " | "\n" | "\t";
```
### TrimLeft
<span class="tag beta">beta</span>
Infer the string with the left ends of trimmed.
::: warning
This API is provided as a preview for developers and may change based on feedback that we receive.
Do not use this API in a production environment.
:::
**Signature:**
```ts
type TrimLeft<T extends string> = T extends `${Space}${infer R}` ? TrimLeft<R> : T;
```
#### Example
```ts
TrimLeft<' \n\thello'> // 'hello'
```
11 changes: 10 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ export { sum } from './src/sum.ts'
export { T } from './src/T.ts'
export { tail } from './src/tail.ts'
export { trim } from './src/trim.ts'
export type { AnyFn, Empty, Falsy, Ord, Primitive } from './src/types/index.ts'
export type { TrimLeft } from './src/trimLeft.ts'
export { trimLeft } from './src/trimLeft.ts'
export type {
AnyFn,
Empty,
Falsy,
Ord,
Primitive,
Space
} from './src/types/index.ts'
export { upperCase } from './src/upperCase.ts'
export { values } from './src/values.ts'
export { xor } from './src/xor.ts'
49 changes: 49 additions & 0 deletions src/trimLeft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Space } from './types/index.ts'

/**
* Infer the string with the left ends of trimmed.
*
* @returns String left ends of trimmed
*
* @remarks
* The definition of space
* - `''`
* - `\n`
* - `\t`
*
* @example
* ```ts
* TrimLeft<' \n\thello'> // 'hello'
* ```
*
* @beta
*/
type TrimLeft<T extends string> = T extends `${Space}${infer R}`
? TrimLeft<R>
: T

/**
* Removes space from left ends of the string.
*
* @param val - input string
* @returns The result of `val.trimLeft()`
*
* @remarks
* The definition of space
* - `''`
* - `\n`
* - `\t`
*
* @example
* ```ts
* trimLeft(' hello') // 'hello'
* trimLeft(' \n\thello') // 'hello'
* ```
*
* @beta
*/
const trimLeft = <T extends string>(val: T): TrimLeft<T> =>
val.trimLeft() as TrimLeft<T>

export { trimLeft }
export type { TrimLeft }
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,12 @@ export type Empty = '' | [] | {}
*/
export type Falsy = false | '' | 0 | null | undefined

/**
* Alias for Space values.
*
* @beta
*/
export type Space = ' ' | '\n' | '\t'

export type InferArray<T> = T extends (infer R)[] ? R : never
export type Whitespace = ' ' | '\t' | '\n'
32 changes: 32 additions & 0 deletions test/trimLeft.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { assertEquals } from '../deps.ts'
import { TrimLeft, trimLeft } from '../src/trimLeft.ts'
import { assertEqual } from './asserts.ts'

Deno.test('trimLeft', () => {
const table: [string, string][] = [
['', ''],
[' ', ''],
['\n\n', ''],
['\t\t\t\t', ''],
['hello', 'hello'],
[' hello', 'hello'],
['hello ', 'hello '],
[' hello ', 'hello '],
[' hello ', 'hello '],
[' hello world ', 'hello world '],
[' hello world\n\n', 'hello world\n\n']
]
table.forEach(([val, expected]) => {
assertEquals(trimLeft(val), expected, `trimLeft(${val}) -> ${expected}`)
})
})

Deno.test('types', () => {
assertEqual<'', TrimLeft<''>>()
assertEqual<'hello', TrimLeft<'hello'>>()
assertEqual<'', TrimLeft<'\n'>>()
assertEqual<'', TrimLeft<'\t'>>()
assertEqual<'', TrimLeft<'\t\n '>>()
assertEqual<'hello ', TrimLeft<'hello '>>()
assertEqual<'hello\n\t ', TrimLeft<'\n\t hello\n\t '>>()
})

0 comments on commit 3aa6af9

Please sign in to comment.