-
-
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.startsWith and String.endsWith function
- Loading branch information
1 parent
5542e78
commit 4ef80d0
Showing
5 changed files
with
106 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 |
---|---|---|
@@ -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 }; |
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,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}`, | ||
); | ||
}); | ||
}); |
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,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"; |
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,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}`, | ||
); | ||
}); | ||
}); |
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,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 }; |