From 054e740d5bef78f3788a7330a9fd2b07351612f9 Mon Sep 17 00:00:00 2001 From: Henrique Leite Date: Fri, 12 Nov 2021 20:31:16 -0300 Subject: [PATCH] Add isAlphanumeric --- CHANGELOG.md | 1 + README.md | 1 + src/index.ts | 2 ++ src/lib/is-alphanumeric/index.ts | 6 +++++ src/tests/is-alphanumeric.spec.ts | 41 +++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+) create mode 100644 src/lib/is-alphanumeric/index.ts create mode 100644 src/tests/is-alphanumeric.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a2e270..44a99cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- `isAlphanumeric` - `unnest` ### Changed diff --git a/README.md b/README.md index c8fab9d..0913267 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ npm i @techmmunity/utils | function | description | | -------------------- | ----------------------------------------------------------------- | +| `isAlphanumeric` | Return true if value is a number or a letter | | `isBetween` | Return true if value is a number between 2 values | | `isBrazilianPhone` | Return true if value is a brazilian phone | | `isCnpj` | Return true if value is a CNPJ | diff --git a/src/index.ts b/src/index.ts index fc7919c..1c9d74e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ export * from "./lib/clean-obj"; export * from "./lib/sleep"; +export * from "./lib/unnest"; /** * --------------------------------------------------------------------------- @@ -44,6 +45,7 @@ export * from "./lib/has-url"; * --------------------------------------------------------------------------- */ +export * from "./lib/is-alphanumeric"; export * from "./lib/is-between"; export * from "./lib/is-brazilian-phone"; export * from "./lib/is-cnpj"; diff --git a/src/lib/is-alphanumeric/index.ts b/src/lib/is-alphanumeric/index.ts new file mode 100644 index 0000000..9c34235 --- /dev/null +++ b/src/lib/is-alphanumeric/index.ts @@ -0,0 +1,6 @@ +import { getTypeof } from "../get-typeof"; + +const IS_ALPHANUMERIC = /^[a-z0-9]+$/i; + +export const isAlphanumeric = (str?: string) => + getTypeof(str) === "string" && IS_ALPHANUMERIC.test(str!); diff --git a/src/tests/is-alphanumeric.spec.ts b/src/tests/is-alphanumeric.spec.ts new file mode 100644 index 0000000..165b3f9 --- /dev/null +++ b/src/tests/is-alphanumeric.spec.ts @@ -0,0 +1,41 @@ +import { isAlphanumeric } from "../lib/is-alphanumeric"; + +/** + * + * True + * + */ + +describe("isAlphanumeric (return True)", () => { + it("valid value (1)", () => { + expect(isAlphanumeric("123")).toBe(true); + }); + + it("valid value (2)", () => { + expect(isAlphanumeric("abc")).toBe(true); + }); + + it("valid value (3)", () => { + expect(isAlphanumeric("abc123")).toBe(true); + }); +}); + +/** + * + * False + * + */ + +describe("isAlphanumeric (return False)", () => { + it("with spaces", () => { + expect(isAlphanumeric("12 abc")).toBe(false); + }); + + it("with special chars", () => { + expect(isAlphanumeric("123@1")).toBe(false); + }); + + it("empty", () => { + expect(isAlphanumeric("")).toBe(false); + }); +});