-
-
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.
✨ Add String trimLeft, trimRight and trim functions
- Loading branch information
1 parent
f5c2d46
commit a27d061
Showing
8 changed files
with
247 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
export { curry } from "https://deno.land/x/curry@v1.0.0/mod.ts"; | ||
export type { Space } from "https://deno.land/x/fonction@v1.8.1/mod.ts"; |
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,46 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { TrimLeft } from "./trim_left.ts"; | ||
import { TrimRight } from "./trim_right.ts"; | ||
|
||
/** | ||
* Infer the trimmed string. | ||
* | ||
* @typeParam T - Any string | ||
* @returns Trimmed string | ||
* | ||
* @remarks | ||
* The definition of space | ||
* - `''` | ||
* - `\n` | ||
* - `\t` | ||
* | ||
* @example | ||
* ```ts | ||
* Trim<'\t\n hello \t\n'> // 'hello' | ||
* ``` | ||
* | ||
* @category `String` | ||
* | ||
* @beta | ||
*/ | ||
type Trim<T extends string> = TrimLeft<TrimRight<T>>; | ||
|
||
/** | ||
* Removes whitespace from both ends of the string. | ||
* | ||
* @param val - `string` to trim | ||
* @returns The result of `val.trim()` | ||
* | ||
* @example | ||
* ```ts | ||
* trim(' hello ') // 'hello' | ||
* ``` | ||
* | ||
* @category `String` | ||
* | ||
* @beta | ||
*/ | ||
const trim = <T extends string>(val: T): Trim<T> => val.trim() as Trim<T>; | ||
|
||
export { trim }; | ||
export type { Trim }; |
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,53 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { Space } from "../deps.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' | ||
* ``` | ||
* | ||
* @category `String` | ||
* | ||
* @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' | ||
* ``` | ||
* | ||
* @category `String` | ||
* | ||
* @beta | ||
*/ | ||
const trimLeft = <T extends string>(val: T): TrimLeft<T> => | ||
val.trimLeft() as TrimLeft<T>; | ||
|
||
export { trimLeft }; | ||
export type { TrimLeft }; |
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,30 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { assertEquals, assertEqualsType } from "../dev_deps.ts"; | ||
import { TrimLeft, trimLeft } from "./trim_left.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}`); | ||
}); | ||
|
||
assertEqualsType<"", TrimLeft<"">>(); | ||
assertEqualsType<"hello", TrimLeft<"hello">>(); | ||
assertEqualsType<"", TrimLeft<"\n">>(); | ||
assertEqualsType<"", TrimLeft<"\t">>(); | ||
assertEqualsType<"", TrimLeft<"\t\n ">>(); | ||
assertEqualsType<"hello ", TrimLeft<"hello ">>(); | ||
assertEqualsType<"hello\n\t ", TrimLeft<"\n\t hello\n\t ">>(); | ||
}); |
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,54 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { Space } from "../deps.ts"; | ||
|
||
/** | ||
* Infer the string with the right ends of trimmed. | ||
* | ||
* @typeParam T - Any string | ||
* @returns String right ends of trimmed | ||
* | ||
* @remarks | ||
* The definition of space | ||
* - `''` | ||
* - `\n` | ||
* - `\t` | ||
* | ||
* @example | ||
* ```ts | ||
* TrimRight<'hello \n\t'> // 'hello' | ||
* ``` | ||
* | ||
* @category `String` | ||
* | ||
* @beta | ||
*/ | ||
type TrimRight<T extends string> = T extends `${infer R}${Space}` ? TrimRight<R> | ||
: T; | ||
|
||
/** | ||
* Removes space from right ends of the string. | ||
* | ||
* @param val - input string | ||
* @returns The result of `val.trimRight()` | ||
* | ||
* @remarks | ||
* The definition of space | ||
* - `''` | ||
* - `\n` | ||
* - `\t` | ||
* | ||
* @example | ||
* ```ts | ||
* trimRight('hello ') // 'hello' | ||
* trimRight('hello \n\t') // 'hello' | ||
* ``` | ||
* | ||
* @category `String` | ||
* | ||
* @beta | ||
*/ | ||
const trimRight = <T extends string>(val: T): TrimRight<T> => | ||
val.trimRight() as TrimRight<T>; | ||
|
||
export { trimRight }; | ||
export type { TrimRight }; |
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,30 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { assertEquals, assertEqualsType } from "../dev_deps.ts"; | ||
import { TrimRight, trimRight } from "./trim_right.ts"; | ||
|
||
Deno.test("trimRight", () => { | ||
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"], | ||
]; | ||
table.forEach(([val, expected]) => { | ||
assertEquals(trimRight(val), expected, `trimRight(${val}) -> ${expected}`); | ||
}); | ||
|
||
assertEqualsType<"", TrimRight<"">>(); | ||
assertEqualsType<"hello", TrimRight<"hello">>(); | ||
assertEqualsType<"", TrimRight<"\n">>(); | ||
assertEqualsType<"", TrimRight<"\t">>(); | ||
assertEqualsType<"", TrimRight<"\t\n ">>(); | ||
assertEqualsType<"hello", TrimRight<"hello ">>(); | ||
assertEqualsType<"\n\t hello", TrimRight<"\n\t hello\n\t ">>(); | ||
}); |
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,30 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { assertEquals, assertEqualsType } from "../dev_deps.ts"; | ||
import { Trim, trim } from "./trim.ts"; | ||
|
||
Deno.test("trim", () => { | ||
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"], | ||
]; | ||
table.forEach(([val, expected]) => { | ||
assertEquals(trim(val), expected, `trim(${val}) -> ${expected}`); | ||
}); | ||
|
||
assertEqualsType<"", Trim<"">>(); | ||
assertEqualsType<"hello", Trim<"hello">>(); | ||
assertEqualsType<"", Trim<"\n">>(); | ||
assertEqualsType<"", Trim<"\t">>(); | ||
assertEqualsType<"", Trim<"\t\n ">>(); | ||
assertEqualsType<"hello", Trim<"hello ">>(); | ||
assertEqualsType<"hello", Trim<"\n\t hello\n\t ">>(); | ||
}); |