From 15c0fb8a7852f2f89c932e5295a21f2554d91d14 Mon Sep 17 00:00:00 2001 From: TomokiMiyauci Date: Tue, 8 Jun 2021 17:47:59 +0900 Subject: [PATCH] :sparkles: Add validator for isTrimmable and such function --- deps.ts | 3 ++ validation/isTrimmable.ts | 49 +++++++++++++++++++++++++ validation/isTrimmable_test.ts | 65 ++++++++++++++++++++++++++++++++++ validation/mod.ts | 1 + 4 files changed, 118 insertions(+) create mode 100644 validation/isTrimmable.ts create mode 100644 validation/isTrimmable_test.ts diff --git a/deps.ts b/deps.ts index 1274d43..8294d04 100644 --- a/deps.ts +++ b/deps.ts @@ -14,6 +14,9 @@ export { not, or, pipe, + trim, + trimLeft, + trimRight, tryCatch, } from "https://deno.land/x/fonction@v1.8.1/mod.ts"; diff --git a/validation/isTrimmable.ts b/validation/isTrimmable.ts new file mode 100644 index 0000000..705f5e5 --- /dev/null +++ b/validation/isTrimmable.ts @@ -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 }; diff --git a/validation/isTrimmable_test.ts b/validation/isTrimmable_test.ts new file mode 100644 index 0000000..50b61eb --- /dev/null +++ b/validation/isTrimmable_test.ts @@ -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}`, + ); + }); +}); diff --git a/validation/mod.ts b/validation/mod.ts index fd48f22..8fd67f4 100644 --- a/validation/mod.ts +++ b/validation/mod.ts @@ -17,3 +17,4 @@ export * from "./isSymbol.ts"; export * from "./isUndefined.ts"; export * from "./isLowerCase.ts"; export * from "./isUpperCase.ts"; +export * from "./isTrimmable.ts";