Skip to content

rohitninawe/validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@rohitninawe/validation

npm (scoped) npm bundle size (minified)

Validate the code as much as possible.

Install

Using npm:

npm install @rohitninawe/validation

or using yarn:

yarn add @rohitninawe/validation

Usage

import { safeArray, isValidEmail } from '@rohitninawe/validation';

safeArray()

If the parameter is not an array, safeArray() will return an empty array to prevent a crash, for instance when using the .map() method.

Examples

safeArray([2, 3, 4, "git", {"name": "John Smith"}])
// [2, 3, 4, "git", {"name": "John Smith"}]

safeArray("[1, 2, 3]")
// [1, 2, 3]

safeArray(undefined)
// []    

safeArray(123)
// []

safeArray("")
// []

Pro Tip

If you're unsure whether a key or index exists, always use optional chaining. Example:

let response = null; // expected output => {"users": {"transaction":[]} };

let transaction = response.users.transaction;
//Uncaught TypeError: Cannot read properties of null (reading 'users')
transaction.map(item => item); //Error

//Using Optional Chaining 🤔👇
let transaction = response?.users?.transaction; //returns undefined
transaction.map(item => item);
//Uncaught TypeError: Cannot read properties of undefined (reading 'map')

//Optional Chaining + safeArray 😎
let transaction = response?.users?.transaction; //returns undefined
safeArray(transaction).map(item => item); //returns an empty array [].

isValidEmail()

Checks the email's valid format

Examples

isValidEmail("developer@gmail.com")
// true

isValidEmail("lorem ipsum")
// false

isValidEmail(""); //or any falsy value
// false

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published