Skip to content

Commit

Permalink
✨ Add String trimLeft, trimRight and trim functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jun 17, 2021
1 parent f5c2d46 commit a27d061
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 0 deletions.
1 change: 1 addition & 0 deletions deps.ts
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";
3 changes: 3 additions & 0 deletions string/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
export { replace } from "./replace.ts";
export { startsWith } from "./starts_with.ts";
export { endsWith } from "./ends_with.ts";
export { trim } from "./trim.ts";
export { trimLeft } from "./trim_left.ts";
export { trimRight } from "./trim_right.ts";
46 changes: 46 additions & 0 deletions string/trim.ts
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 };
53 changes: 53 additions & 0 deletions string/trim_left.ts
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 };
30 changes: 30 additions & 0 deletions string/trim_left_test.ts
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 ">>();
});
54 changes: 54 additions & 0 deletions string/trim_right.ts
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 };
30 changes: 30 additions & 0 deletions string/trim_right_test.ts
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 ">>();
});
30 changes: 30 additions & 0 deletions string/trim_test.ts
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 ">>();
});

0 comments on commit a27d061

Please sign in to comment.