Skip to content

Commit

Permalink
✨ Add validator for isTrimmable and such function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jun 8, 2021
1 parent c0de1d0 commit 15c0fb8
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export {
not,
or,
pipe,
trim,
trimLeft,
trimRight,
tryCatch,
} from "https://deno.land/x/fonction@v1.8.1/mod.ts";

Expand Down
49 changes: 49 additions & 0 deletions validation/isTrimmable.ts
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 };
65 changes: 65 additions & 0 deletions validation/isTrimmable_test.ts
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}`,
);
});
});
1 change: 1 addition & 0 deletions validation/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from "./isSymbol.ts";
export * from "./isUndefined.ts";
export * from "./isLowerCase.ts";
export * from "./isUpperCase.ts";
export * from "./isTrimmable.ts";

0 comments on commit 15c0fb8

Please sign in to comment.