Skip to content

Instantly share code, notes, and snippets.

@gaurangrshah
Last active April 12, 2021 18:57
Show Gist options
  • Save gaurangrshah/9dbc36e14c801898ccc9d234e3edd649 to your computer and use it in GitHub Desktop.
Save gaurangrshah/9dbc36e14c801898ccc9d234e3edd649 to your computer and use it in GitHub Desktop.
#utils #validation #js
export const is = {
blank: /^\s*$/,
string: /^[a-zA-Z]+$/,
email: /^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$/,
tel: /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/,
specialNumeric: /^(?:[\d,\/().]*[a-zA-Z][a-zA-Z\d,\/().]*)?$/,
streetAddress: /^\d+\s[A-z]+\s[A-z]+/,
stateZip: /([A-Z]{2}) (\d{5})$/,
number: /^\d+$/,
alphaNumeric: /^[a-zA-Z0-9]+$/,
image: /[\/.](gif|jpe?g|tiff|png|svg|pdf)$/,
pdf: /[\/.]pdf$/,
filepath: /(\\\\?([^\\/]*[\\/])*)([^\\/]+)$/,
url: /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/,
};
export const validate = (validator, value, not) => is[validator].test(value);
export const isEmail = (str) => is.email.test(str);
export const isEmpty = (str) => !str || 0 === str.length;
export const isBlank = (str) => !str || /^\s*$/.test(str);
export const isString = (str) => {
return typeof str === "string" && !isEmpty(str) && !isBlank(str);
};
export const patterns = {
blank: "^s*$",
string: "^[a-zA-Z]+$",
email: "^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$",
tel:
"^(?:(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})s*(?:[.-]s*)?([0-9]{4})(?:s*(?:#|x.?|ext.?|extension)s*(d+))?$",
specialNumeric: "^(?:[d,/().]*[a-zA-Z][a-zA-Zd,/().]*)?$",
streetAddress: "^d+s[A-z]+s[A-z]+",
stateZip: "([A-Z]{2}) (d{5})$",
number: "^d+$",
alphaNumeric: "^[a-zA-Z0-9]+$",
image: "[/.](gif|jpe?g|tiff|png|svg|pdf)$",
pdf: "[/.]pdf$",
filepath: "(\\\\?([^\\/]*[\\/])*)([^\\/]+)$",
url:
"^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$",
};
export function isEmpty(s) {
return !s.length;
}
export function isBlank(s) {
return isEmpty(s.trim());
}
export const is = {
string(str) {
return typeof str === "string" || str instanceof String;
},
integer(num) {
return num === parseInt(num, 10);
},
function(fn) {
return typeof fn === "function";
},
object(obj) {
return (obj && obj.constructor === Object) || obj === Object(obj);
},
empty(value) {
// returns true when any of the conditions are met --
// indicating that the value is considered empty
if (value === null) return true;
if (value === undefined) return true;
// is empty string
if (is.string(value) && value.trim().length === 0) return true;
// is empty array
if (Array.isArray(value) && value.length === 0) return true;
// is empty object
if (is.object(value) && Object.keys(value).length === 0) return true;
},
nan(num) {
return isNaN(num);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment