typezen
Version:
A collection of TypeScript utilities for safe type checking and inference.
499 lines (492 loc) • 15.9 kB
JavaScript
// src/functions/data-format-checks.ts
var isNumber = (value) => typeof value === "number" && !Number.isNaN(value);
var isInteger = (value) => Number.isInteger(value);
var isFloat = (value) => isNumber(value) && !Number.isInteger(value);
var isNaNValue = (value) => Number.isNaN(value);
var isBetween = (value, min, max) => value >= min && value <= max;
var isPositive = (value) => value > 0;
var isNegative = (value) => value < 0;
var isBoolean = (value) => typeof value === "boolean";
var isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
var isArray = (value) => Array.isArray(value);
var isString = (value) => typeof value === "string";
var isArrayOfSting = (value) => isArray(value) && value.every((item) => isString(item));
var isPromise = (value) => value instanceof Promise;
var isError = (value) => value instanceof Error;
var isMap = (value) => value instanceof Map;
var isSet = (value) => value instanceof Set;
var isFunction = (value) => typeof value === "function";
var isAsyncFunction = (value) => isFunction(value) && value.constructor.name === "AsyncFunction";
var isPrimitive = (value) => value === null || typeof value !== "object" && typeof value !== "function";
var isHexColor = (value) => /^#([A-Fa-f0-9]{3,4}){1,2}$/.test(value);
var isEmail = (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
var isUUID = (value) => /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
value
);
var isBase64URL = (value) => {
try {
return Buffer.from(value, "base64").toString("base64") === value;
} catch (e) {
return false;
}
};
var isBinary = (value) => typeof value === "string" || value instanceof Buffer;
var isURL = (value) => {
try {
const urlRegex = new RegExp(
"^(https?:\\/\\/)?([\\da-z.-]+)\\.([a-z.]{2,6})([/\\w .-]*)*\\/?$",
"i"
);
return urlRegex.test(value);
} catch (e) {
return false;
}
};
var isJWT = (value) => {
try {
const [header, payload, signature] = value.split(".");
return [header, payload, signature].every(isBase64URL);
} catch (e) {
return false;
}
};
var isCreditCard = (value) => /^\d{4}-\d{4}-\d{4}-\d{4}$/.test(value);
var isISBN = (value) => /^(?=(?:\D*\d){10}(?:(?:\D*\d){3})?$)[\d-]+$/.test(value);
var isMACAddress = (value) => /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.test(value);
var isIPAddress = (value) => /^([0-9]{1,3}\.){3}[0-9]{1,3}$/.test(value) || /^([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4})$/.test(value);
var isDate = (value) => !isNaN(Date.parse(value));
var isDeepEqual = (a, b) => {
if (a === b) return true;
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
return false;
const keysA = Object.keys(a), keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
return keysA.every((key) => isDeepEqual(a[key], b[key]));
};
// src/functions/data-structure.ts
var isWeakMap = (value) => value instanceof WeakMap;
var isWeakSet = (value) => value instanceof WeakSet;
var isIterable = (value) => typeof value === "object" && value !== null && Symbol.iterator in value;
var isAsyncIterable = (value) => typeof value === "object" && value !== null && Symbol.asyncIterator in value;
var isTypedArray = (value) => ArrayBuffer.isView(value);
var isArrayBuffer = (value) => value instanceof ArrayBuffer;
var isDataView = (value) => value instanceof DataView;
var isSharedArrayBuffer = (value) => value instanceof SharedArrayBuffer;
var isBuffer = (value) => value instanceof Buffer;
function isUndefined(value) {
return value === void 0;
}
function isNull(value) {
return value === null;
}
function isNullOrUndefined(value) {
return value == null;
}
function isPlainObject(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
var isEmpty = (value) => value == null || value === "" || value !== value;
function isEmptyArray(value) {
return Array.isArray(value) && value.length === 0;
}
function isEmptyString(value) {
return typeof value === "string" && value.trim().length === 0;
}
function isNegativeZero(value) {
return value === 0 && 1 / value === -Infinity;
}
function isInfinity(value) {
return value === Infinity || value === -Infinity;
}
function isThenable(value) {
return Boolean(value && typeof value.then === "function");
}
function isDomain(value) {
return /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,6}$/i.test(value);
}
function isSemVer(value) {
return /^\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+)?$/.test(
value
);
}
function isEmptyObject(value) {
return isPlainObject(value) && Object.keys(value).length === 0;
}
function isClass(value) {
return typeof value === "function" && /^class\s/.test(Function.prototype.toString.call(value));
}
function isEventEmitter(value) {
return Boolean(
value && typeof value.on === "function" && typeof value.emit === "function"
);
}
function isMimeType(value) {
return /^(?:application|audio|font|example|image|message|model|multipart|text|video)\/[a-z0-9-+]+$/i.test(
value
);
}
function isHTTPS(value) {
return value.startsWith("https://");
}
function isMapIterator(value) {
return Object.prototype.toString.call(value) === "[object Map Iterator]";
}
function isSetIterator(value) {
return Object.prototype.toString.call(value) === "[object Set Iterator]";
}
function isSafeInteger(value) {
return Number.isSafeInteger(value);
}
// src/functions/env-checks.ts
var isNodeJS = () => typeof process === "object";
var isBrowser = () => typeof window === "object";
var isMobile = () => {
if (typeof navigator === "undefined") return false;
return /Mobi|Android|iPhone|iPad|Windows Phone/i.test(navigator.userAgent);
};
var isDesktop = () => {
if (typeof navigator === "undefined") return false;
return !isMobile();
};
// src/utils/index.ts
var ImageFileExtension = /* @__PURE__ */ ((ImageFileExtension2) => {
ImageFileExtension2["JPG"] = "jpg";
ImageFileExtension2["JPEG"] = "jpeg";
ImageFileExtension2["PNG"] = "png";
ImageFileExtension2["GIF"] = "gif";
ImageFileExtension2["BMP"] = "bmp";
ImageFileExtension2["TIFF"] = "tiff";
ImageFileExtension2["TIF"] = "tif";
ImageFileExtension2["WEBP"] = "webp";
ImageFileExtension2["SVG"] = "svg";
ImageFileExtension2["ICO"] = "ico";
ImageFileExtension2["HEIC"] = "heic";
ImageFileExtension2["HEIF"] = "heif";
ImageFileExtension2["AVIF"] = "avif";
ImageFileExtension2["APNG"] = "apng";
ImageFileExtension2["JXR"] = "jxr";
ImageFileExtension2["HDP"] = "hdp";
ImageFileExtension2["WDP"] = "wdp";
ImageFileExtension2["PSD"] = "psd";
ImageFileExtension2["PSB"] = "psb";
ImageFileExtension2["AI"] = "ai";
ImageFileExtension2["EPS"] = "eps";
ImageFileExtension2["CPT"] = "cpt";
ImageFileExtension2["SGI"] = "sgi";
ImageFileExtension2["RGB"] = "rgb";
ImageFileExtension2["RGBA"] = "rgba";
ImageFileExtension2["BW"] = "bw";
ImageFileExtension2["PBM"] = "pbm";
ImageFileExtension2["PGM"] = "pgm";
ImageFileExtension2["PPM"] = "ppm";
ImageFileExtension2["PNM"] = "pnm";
ImageFileExtension2["PXM"] = "pxm";
ImageFileExtension2["DPX"] = "dpx";
ImageFileExtension2["CIN"] = "cin";
ImageFileExtension2["VDA"] = "vda";
ImageFileExtension2["X3F"] = "x3f";
ImageFileExtension2["ERF"] = "erf";
ImageFileExtension2["THREE_FR"] = "3fr";
ImageFileExtension2["IIQ"] = "iiq";
ImageFileExtension2["FFF"] = "fff";
ImageFileExtension2["MOS"] = "mos";
ImageFileExtension2["MEF"] = "mef";
ImageFileExtension2["J2K"] = "j2k";
ImageFileExtension2["JP2"] = "jp2";
ImageFileExtension2["JPF"] = "jpf";
ImageFileExtension2["JPX"] = "jpx";
ImageFileExtension2["JPM"] = "jpm";
ImageFileExtension2["MJ2"] = "mj2";
ImageFileExtension2["ICNS"] = "icns";
ImageFileExtension2["DDS"] = "dds";
ImageFileExtension2["TGA"] = "tga";
ImageFileExtension2["ICB"] = "icb";
ImageFileExtension2["VST"] = "vst";
ImageFileExtension2["IMG"] = "img";
ImageFileExtension2["CE1"] = "ce1";
ImageFileExtension2["PCX"] = "pcx";
ImageFileExtension2["RLE"] = "rle";
ImageFileExtension2["DCR"] = "dcr";
ImageFileExtension2["DC2"] = "dc2";
ImageFileExtension2["PCD"] = "pcd";
ImageFileExtension2["XPM"] = "xpm";
ImageFileExtension2["XWD"] = "xwd";
ImageFileExtension2["CUR"] = "cur";
ImageFileExtension2["ANI"] = "ani";
ImageFileExtension2["WMF"] = "wmf";
ImageFileExtension2["EMF"] = "emf";
ImageFileExtension2["AFPHOTO"] = "afphoto";
ImageFileExtension2["AFDESIGN"] = "afdesign";
ImageFileExtension2["SAI"] = "sai";
ImageFileExtension2["ORA"] = "ora";
ImageFileExtension2["SPR"] = "spr";
ImageFileExtension2["KDC"] = "kdc";
return ImageFileExtension2;
})(ImageFileExtension || {});
// src/functions/file-data-checks.ts
var isFileType = (filePath, format) => {
if (!filePath) return false;
const ext = filePath.toLowerCase().slice(filePath.lastIndexOf(".") + 1);
return ext === format.toLowerCase();
};
var isImageFile = (file) => {
if (!file) throw new Error("File is required");
if (!(file instanceof File)) throw new Error("Invalid file type");
const validExtensions = Object.values(
ImageFileExtension
).map((ext2) => ext2.toString());
const ext = file.name.split(".").pop();
if (!ext) return false;
return validExtensions.includes(ext.toLowerCase());
};
var isZipFile = (filePath) => isFileType(filePath, "zip");
var isTarFile = (filePath) => isFileType(filePath, "tar");
var isGzFile = (filePath) => isFileType(filePath, "gz");
var isRarFile = (filePath) => isFileType(filePath, "rar");
var is7zFile = (filePath) => isFileType(filePath, "7z");
var isTgzFile = (filePath) => isFileType(filePath, "tgz");
var isBz2File = (filePath) => isFileType(filePath, "bz2");
var isXzFile = (filePath) => isFileType(filePath, "xz");
var isExecutableFile = (filePath) => isFileType(filePath, "exe") || isFileType(filePath, "sh") || isFileType(filePath, "bat") || isFileType(filePath, "cmd") || isFileType(filePath, "msi") || isFileType(filePath, "app") || isFileType(filePath, "deb") || isFileType(filePath, "rpm") || isFileType(filePath, "apk") || isFileType(filePath, "jar") || isFileType(filePath, "bin") || isFileType(filePath, "dmg") || isFileType(filePath, "pkg") || isFileType(filePath, "run");
var isPlainTextFile = (filePath) => isFileType(filePath, "txt");
var isMarkdownFile = (filePath) => isFileType(filePath, "md");
var isHTMLFile = (filePath) => isFileType(filePath, "html") || isFileType(filePath, "htm");
var isCSSFile = (filePath) => isFileType(filePath, "css");
var isJavaScriptFile = (filePath) => isFileType(filePath, "js");
var isTypeScriptFile = (filePath) => isFileType(filePath, "ts");
var isSVG = (filePath) => isFileType(filePath, "svg");
var isPNG = (filePath) => isFileType(filePath, "png");
var isJPG = (filePath) => isFileType(filePath, "jpg");
var isJPEG = (filePath) => isFileType(filePath, "jpeg") || isFileType(filePath, "jpg");
var isGIF = (filePath) => isFileType(filePath, "gif");
var isWEBP = (filePath) => isFileType(filePath, "webp");
var isICO = (filePath) => isFileType(filePath, "ico");
var isBMP = (filePath) => isFileType(filePath, "bmp");
var isTIFF = (filePath) => isFileType(filePath, "tiff");
var isPDF = (filePath) => isFileType(filePath, "pdf");
var isDOC = (filePath) => isFileType(filePath, "doc");
var isDOCX = (filePath) => isFileType(filePath, "docx");
var isXLS = (filePath) => isFileType(filePath, "xls");
var isXLSX = (filePath) => isFileType(filePath, "xlsx");
var isPPT = (filePath) => isFileType(filePath, "ppt");
var isPPTX = (filePath) => isFileType(filePath, "pptx");
var isTXT = (filePath) => isFileType(filePath, "txt");
var isCSV = (filePath) => isFileType(filePath, "csv");
var isJSON = (filePath) => isFileType(filePath, "json");
var isYAML = (filePath) => isFileType(filePath, "yaml");
var isXML = (filePath) => isFileType(filePath, "xml");
var isMp3 = (filePath) => isFileType(filePath, "mp3");
var isMp4 = (filePath) => isFileType(filePath, "mp4");
// src/functions/miscellaneous-checks.ts
var isEven = (value) => value % 2 === 0;
var isOdd = (value) => value % 2 !== 0;
var isPrime = (value) => {
if (value < 2) {
return false;
}
for (let i = 2; i < value; i++) {
if (value % i === 0) {
return false;
}
}
return true;
};
var isPerfect = (value) => {
let sum = 0;
for (let i = 1; i < value; i++) {
if (value % i === 0) {
sum += i;
}
}
return sum === value;
};
var isPalindrome = (value) => {
return value === Number(value.toString().split("").reverse().join(""));
};
var isArmstrong = (value) => {
const digits = value.toString().split("");
const n = digits.length;
let sum = 0;
for (let i = 0; i < n; i++) {
sum += Number(digits[i]) ** n;
}
return sum === value;
};
var isPerfectSquare = (value) => {
return Math.sqrt(value) % 1 === 0;
};
var isFibonacci = (value) => {
return isPerfectSquare(5 * value ** 2 + 4) || isPerfectSquare(5 * value ** 2 - 4);
};
var isFactorial = (value) => {
let i = 1;
let factorial = 1;
while (factorial < value) {
i++;
factorial *= i;
}
return factorial === value;
};
var isLeapYear = (value) => {
return value % 4 === 0 && (value % 100 !== 0 || value % 400 === 0);
};
var isWeekend = (value) => {
return value.getDay() === 0 || value.getDay() === 6;
};
var isWeekday = (value) => {
return !isWeekend(value);
};
var isFutureDate = (value) => {
return value > /* @__PURE__ */ new Date();
};
var isPastDate = (value) => {
return value < /* @__PURE__ */ new Date();
};
var isToday = (value) => {
return value.toDateString() === (/* @__PURE__ */ new Date()).toDateString();
};
var isYesterday = (value) => {
const yesterday = /* @__PURE__ */ new Date();
yesterday.setDate(yesterday.getDate() - 1);
return value.toDateString() === yesterday.toDateString();
};
var isTomorrow = (value) => {
const tomorrow = /* @__PURE__ */ new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
return value.toDateString() === tomorrow.toDateString();
};
var isValidRegExp = (regex) => {
return regex instanceof RegExp;
};
// src/types/index.ts
var TypeValidationError = class extends Error {
constructor(message) {
super(message);
this.name = "TypeValidationError";
}
};
export {
TypeValidationError,
is7zFile,
isArmstrong,
isArray,
isArrayBuffer,
isArrayOfSting,
isAsyncFunction,
isAsyncIterable,
isBMP,
isBase64URL,
isBetween,
isBinary,
isBoolean,
isBrowser,
isBuffer,
isBz2File,
isCSSFile,
isCSV,
isClass,
isCreditCard,
isDOC,
isDOCX,
isDataView,
isDate,
isDeepEqual,
isDesktop,
isDomain,
isEmail,
isEmpty,
isEmptyArray,
isEmptyObject,
isEmptyString,
isError,
isEven,
isEventEmitter,
isExecutableFile,
isFactorial,
isFibonacci,
isFloat,
isFunction,
isFutureDate,
isGIF,
isGzFile,
isHTMLFile,
isHTTPS,
isHexColor,
isICO,
isIPAddress,
isISBN,
isImageFile,
isInfinity,
isInteger,
isIterable,
isJPEG,
isJPG,
isJSON,
isJWT,
isJavaScriptFile,
isLeapYear,
isMACAddress,
isMap,
isMapIterator,
isMarkdownFile,
isMimeType,
isMobile,
isMp3,
isMp4,
isNaNValue,
isNegative,
isNegativeZero,
isNodeJS,
isNull,
isNullOrUndefined,
isNumber,
isObject,
isOdd,
isPDF,
isPNG,
isPPT,
isPPTX,
isPalindrome,
isPastDate,
isPerfect,
isPerfectSquare,
isPlainObject,
isPlainTextFile,
isPositive,
isPrime,
isPrimitive,
isPromise,
isRarFile,
isSVG,
isSafeInteger,
isSemVer,
isSet,
isSetIterator,
isSharedArrayBuffer,
isString,
isTIFF,
isTXT,
isTarFile,
isTgzFile,
isThenable,
isToday,
isTomorrow,
isTypeScriptFile,
isTypedArray,
isURL,
isUUID,
isUndefined,
isValidRegExp,
isWEBP,
isWeakMap,
isWeakSet,
isWeekday,
isWeekend,
isXLS,
isXLSX,
isXML,
isXzFile,
isYAML,
isYesterday,
isZipFile
};