camote-utils
Version:
A comprehensive TypeScript utility library featuring advanced string and number formatting, data structures, and algorithms
135 lines (134 loc) • 4.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidTime = exports.isStrongPassword = exports.isEmail = exports.isAlphanumeric = exports.isFinite = exports.isUndefined = exports.isNull = exports.isNaN = exports.isDataView = exports.isFunction = exports.isBoolean = exports.exactly = exports.contains = exports.isUuid = exports.isUrl = exports.isObject = exports.isArray = exports.isString = exports.isNumber = exports.isEmpty = exports.isNil = void 0;
const isNil = (value) => {
return value === null || value === undefined;
};
exports.isNil = isNil;
const isEmpty = (value) => {
if ((0, exports.isNil)(value))
return true;
if (typeof value === 'string')
return value.trim().length === 0;
if (Array.isArray(value))
return value.length === 0;
if (typeof value === 'object')
return Object.keys(value).length === 0;
return false;
};
exports.isEmpty = isEmpty;
const isNumber = (value) => {
return typeof value === 'number' && !(0, exports.isNaN)(value) && (0, exports.isFinite)(value);
};
exports.isNumber = isNumber;
const isString = (value) => {
return typeof value === 'string';
};
exports.isString = isString;
const isArray = (value) => {
return Array.isArray(value);
};
exports.isArray = isArray;
const isObject = (value) => {
return typeof value === 'object' && value !== null && !Array.isArray(value);
};
exports.isObject = isObject;
const isUrl = (str) => {
try {
const url = new URL(str);
// Check for valid protocol (must be http, https, or ftp)
if (!url.protocol || !['http:', 'https:', 'ftp:'].includes(url.protocol)) {
return false;
}
// Check for valid hostname
if (!url.hostname || url.hostname.length === 0) {
return false;
}
if (url.pathname.includes('//')) {
return false;
}
if (!str.match(/^(https?|ftp):\/\//i)) {
return false;
}
return true;
}
catch (_a) {
return false;
}
};
exports.isUrl = isUrl;
const isUuid = (str) => {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return uuidRegex.test(str);
};
exports.isUuid = isUuid;
const contains = (str, substring, caseSensitive = true) => {
if (!str || !substring)
return false;
if (!caseSensitive) {
return str.toLowerCase().includes(substring.toLowerCase());
}
return str.includes(substring);
};
exports.contains = contains;
const exactly = (str, match, caseSensitive) => {
if (str === undefined || match === undefined)
return false;
if (caseSensitive === undefined)
caseSensitive = true;
if (!caseSensitive) {
return str.toLowerCase() === match.toLowerCase();
}
return str === match;
};
exports.exactly = exactly;
const isBoolean = (value) => {
return typeof value === 'boolean';
};
exports.isBoolean = isBoolean;
const isFunction = (value) => {
return typeof value === 'function';
};
exports.isFunction = isFunction;
const isDataView = (value) => {
return value instanceof DataView;
};
exports.isDataView = isDataView;
const isNaN = (value) => {
return Number.isNaN(value);
};
exports.isNaN = isNaN;
const isNull = (value) => {
return value === null;
};
exports.isNull = isNull;
const isUndefined = (value) => {
return value === undefined;
};
exports.isUndefined = isUndefined;
const isFinite = (value) => {
return Number.isFinite(value);
};
exports.isFinite = isFinite;
const isAlphanumeric = (str) => {
if (typeof str !== 'string')
return false;
return /^[a-zA-Z0-9]+$/.test(str);
};
exports.isAlphanumeric = isAlphanumeric;
const isEmail = (str) => {
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+$/;
return emailRegex.test(str);
};
exports.isEmail = isEmail;
const isStrongPassword = (password) => {
const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,}$/;
return strongPasswordRegex.test(password);
};
exports.isStrongPassword = isStrongPassword;
const isValidTime = (timeString) => {
const timeRegex = /^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])(:([0-5][0-9]))?$/;
const amPmRegex = /^(0[0-9]|1[0-2]):([0-5][0-9])(:([0-5][0-9]))?\s?(AM|PM)$/i;
return timeRegex.test(timeString) || amPmRegex.test(timeString);
};
exports.isValidTime = isValidTime;