Skip to content

Commit

Permalink
✨ Add String.startsWith and String.endsWith function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jun 17, 2021
1 parent 5542e78 commit 4ef80d0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
32 changes: 32 additions & 0 deletions string/endsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { curry } from "../deps.ts";

/**
* @internal
*/
const _endsWith = <T extends string, U extends string>(
val: T,
target: U,
): boolean => target.endsWith(val);

/**
* Checks if a string ends with the provided substring.
*
* @param val - Search string
* @param target - Target string
* @returns The result of `target.endsWith(val)`
*
* @example
* ```ts
* // Basic
* endsWith('world', 'hello world') // true
* endsWith('earth', 'hello world') // false
* ```
*
* @category `String`
*
* @beta
*/
const endsWith = curry(_endsWith);

export { _endsWith, endsWith };
21 changes: 21 additions & 0 deletions string/endsWith_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { assertEquals } from "../dev_deps.ts";
import { _endsWith } from "./endsWith.ts";

Deno.test("_endsWith", () => {
const table: [string, string, boolean][] = [
["", "", true],
["o", "hello", true],
["world", "hello world", true],
["O", "hello", false],
["earth", "hello world", false],
["Hello", "hello", false],
];
table.forEach(([val, target, expected]) => {
assertEquals(
_endsWith(val, target),
expected,
`_endsWith(${val}, ${target}) -> ${expected}`,
);
});
});
2 changes: 2 additions & 0 deletions string/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
export { replace } from "./replace.ts";
export { startsWith } from "./startsWith.ts";
export { endsWith } from "./endsWith.ts";
19 changes: 19 additions & 0 deletions string/starsWith.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { assertEquals } from "../dev_deps.ts";
import { _startsWith } from "./startsWith.ts";

Deno.test("startsWith", () => {
const table: [string, string, boolean][] = [
["", "", true],
["hello", "hello world", true],
["H", "hello", false],
["Hello", "hello", false],
];
table.forEach(([val, target, expected]) => {
assertEquals(
_startsWith(val, target),
expected,
`startsWith(${val}, ${target}) -> ${expected}`,
);
});
});
32 changes: 32 additions & 0 deletions string/startsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { curry } from "../deps.ts";

/**
* @internal
*/
const _startsWith = <T extends string, U extends string>(
val: T,
target: U,
): boolean => target.startsWith(val);

/**
* Checks if a string starts with the provided substring.
*
* @param val - search string
* @param target - target string
* @returns The result of `target.startsWith(val)`
*
* @example
* ```ts
* // Basic
* startsWith('hello', 'hello world') // true
* startsWith('good', 'hello world') // false
* ```
*
* @category `String`
*
* @beta
*/
const startsWith = curry(_startsWith);

export { _startsWith, startsWith };

0 comments on commit 4ef80d0

Please sign in to comment.