-
-
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 validator for isTrimmable and such function
- Loading branch information
1 parent
c0de1d0
commit 15c0fb8
Showing
4 changed files
with
118 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
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,49 @@ | ||
// Copyright 2021-present the is-valid authors. All rights reserved. MIT license. | ||
import { trim, trimLeft, trimRight } from "../deps.ts"; | ||
|
||
/** | ||
* Whatever val is trimmable or not. | ||
* | ||
* @param val - Any `string` | ||
* @returns The result of `trim(val) !== val` | ||
* | ||
* @example | ||
* ```ts | ||
* isTrimmable(' test') // true | ||
* isTrimmable('test ') // true | ||
* isTrimmable(' test ') // true | ||
* ``` | ||
*/ | ||
const isTrimmable = (val: string) => trim(val) !== val; | ||
|
||
/** | ||
* Whatever val is left side trimmable or not. | ||
* | ||
* @param val - Any `string` | ||
* @returns The result of `trimLeft(val) !== val` | ||
* | ||
* @example | ||
* ```ts | ||
* isLeftTrimmable('test ') // false | ||
* isLeftTrimmable(' test') // true | ||
* isLeftTrimmable(' test ') // true | ||
* ``` | ||
*/ | ||
const isLeftTrimmable = (val: string) => trimLeft(val) !== val; | ||
|
||
/** | ||
* Whatever val is right side trimmable or not. | ||
* | ||
* @param val - Any `string` | ||
* @returns The result of `trimRight(val) !== val` | ||
* | ||
* @example | ||
* ```ts | ||
* isRightTrimmable(' test') // false | ||
* isRightTrimmable('test ') // true | ||
* isRightTrimmable(' test ') // true | ||
* ``` | ||
*/ | ||
const isRightTrimmable = (val: string) => trimRight(val) !== val; | ||
|
||
export { isLeftTrimmable, isRightTrimmable, isTrimmable }; |
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,65 @@ | ||
// Copyright 2021-present the is-valid authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../dev_deps.ts"; | ||
import { | ||
isLeftTrimmable, | ||
isRightTrimmable, | ||
isTrimmable, | ||
} from "./isTrimmable.ts"; | ||
|
||
Deno.test("isTrimmable", () => { | ||
const table: [string, boolean][] = [ | ||
["", false], | ||
["a", false], | ||
["hoge", false], | ||
[" hoge", true], | ||
["hoge ", true], | ||
[" hoge ", true], | ||
]; | ||
|
||
table.forEach(([val, expected]) => { | ||
assertEquals( | ||
isTrimmable(val), | ||
expected, | ||
`isTrimmable(${val}) -> ${expected}`, | ||
); | ||
}); | ||
}); | ||
|
||
Deno.test("isLeftTrimmable", () => { | ||
const table: [string, boolean][] = [ | ||
["", false], | ||
["a", false], | ||
["hoge", false], | ||
[" hoge", true], | ||
["hoge ", false], | ||
[" hoge ", true], | ||
]; | ||
|
||
table.forEach(([val, expected]) => { | ||
assertEquals( | ||
isLeftTrimmable(val), | ||
expected, | ||
`isLeftTrimmable(${val}) -> ${expected}`, | ||
); | ||
}); | ||
}); | ||
|
||
Deno.test("isRightTrimmable", () => { | ||
const table: [string, boolean][] = [ | ||
["", false], | ||
["a", false], | ||
["hoge", false], | ||
[" hoge", false], | ||
["hoge ", true], | ||
[" hoge ", true], | ||
]; | ||
|
||
table.forEach(([val, expected]) => { | ||
assertEquals( | ||
isRightTrimmable(val), | ||
expected, | ||
`isRightTrimmable(${val}) -> ${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