Skip to content

Commit

Permalink
✨ Add String.trimStart and String.trimEnd functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jun 18, 2021
1 parent 5b6c8c3 commit 08bd098
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 20 deletions.
2 changes: 2 additions & 0 deletions string/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export { trimRight } from "./trim_right.ts";
export { toLowerCase } from "./to_lower_case.ts";
export { toUpperCase } from "./to_upper_case.ts";
export { repeat } from "./repeat.ts";
export { trimEnd } from "./trim_end.ts";
export { trimStart } from "./trim_start.ts";
54 changes: 54 additions & 0 deletions string/trim_end.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
* TrimEnd<'hello \n\t'> // 'hello'
* ```
*
* @category `String`
*
* @beta
*/
type TrimEnd<T extends string> = T extends `${infer R}${Space}` ? TrimEnd<R>
: T;

/**
* Removes space from right ends of the string.
*
* @param val - input string
* @returns The result of `val.trimEnd()`
*
* @remarks
* The definition of space
* - `''`
* - `\n`
* - `\t`
*
* @example
* ```ts
* trimEnd('hello ') // 'hello'
* trimEnd('hello \n\t') // 'hello'
* ```
*
* @category `String`
*
* @beta
*/
const trimEnd = <T extends string>(val: T): TrimEnd<T> =>
val.trimEnd() as TrimEnd<T>;

export { trimEnd };
export type { TrimEnd };
20 changes: 10 additions & 10 deletions string/trim_right_test.ts → string/trim_end_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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";
import { TrimEnd, trimEnd } from "./trim_end.ts";

Deno.test("trimRight", () => {
Deno.test("trimEnd", () => {
const table: [string, string][] = [
["", ""],
[" ", ""],
Expand All @@ -17,14 +17,14 @@ Deno.test("trimRight", () => {
[" hello world\n\n", " hello world"],
];
table.forEach(([val, expected]) => {
assertEquals(trimRight(val), expected, `trimRight(${val}) -> ${expected}`);
assertEquals(trimEnd(val), expected, `trimEnd(${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 ">>();
assertEqualsType<"", TrimEnd<"">>();
assertEqualsType<"hello", TrimEnd<"hello">>();
assertEqualsType<"", TrimEnd<"\n">>();
assertEqualsType<"", TrimEnd<"\t">>();
assertEqualsType<"", TrimEnd<"\t\n ">>();
assertEqualsType<"hello", TrimEnd<"hello ">>();
assertEqualsType<"\n\t hello", TrimEnd<"\n\t hello\n\t ">>();
});
53 changes: 53 additions & 0 deletions string/trim_start.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
* TrimStart<' \n\thello'> // 'hello'
* ```
*
* @category `String`
*
* @beta
*/
type TrimStart<T extends string> = T extends `${Space}${infer R}` ? TrimStart<R>
: T;

/**
* Removes the leading white space and line terminator characters from a string.
*
* @param val - input string
* @returns The result of `val.trimStart()`
*
* @remarks
* The definition of space
* - `''`
* - `\n`
* - `\t`
*
* @example
* ```ts
* trimStart(' hello') // 'hello'
* trimStart(' \n\thello') // 'hello'
* ```
*
* @category `String`
*
* @beta
*/
const trimStart = <T extends string>(val: T): TrimStart<T> =>
val.trimStart() as TrimStart<T>;

export { trimStart };
export type { TrimStart };
20 changes: 10 additions & 10 deletions string/trim_left_test.ts → string/trim_start_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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";
import { TrimStart, trimStart } from "./trim_start.ts";

Deno.test("trimLeft", () => {
Deno.test("trimStart", () => {
const table: [string, string][] = [
["", ""],
[" ", ""],
Expand All @@ -17,14 +17,14 @@ Deno.test("trimLeft", () => {
[" hello world\n\n", "hello world\n\n"],
];
table.forEach(([val, expected]) => {
assertEquals(trimLeft(val), expected, `trimLeft(${val}) -> ${expected}`);
assertEquals(trimStart(val), expected, `trimStart(${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 ">>();
assertEqualsType<"", TrimStart<"">>();
assertEqualsType<"hello", TrimStart<"hello">>();
assertEqualsType<"", TrimStart<"\n">>();
assertEqualsType<"", TrimStart<"\t">>();
assertEqualsType<"", TrimStart<"\t\n ">>();
assertEqualsType<"hello ", TrimStart<"hello ">>();
assertEqualsType<"hello\n\t ", TrimStart<"\n\t hello\n\t ">>();
});

0 comments on commit 08bd098

Please sign in to comment.