@opensig/opendesign
Version:
120 lines (119 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const opt = Object.prototype.toString;
function isUndefined(val) {
return val === void 0;
}
function isNull(val) {
return opt.call(val) === "[object Null]";
}
function isNil(val) {
return isUndefined(val) || isNull(val);
}
function isBoolean(val) {
return opt.call(val) === "[object Boolean]";
}
function isString(val) {
return opt.call(val) === "[object String]";
}
function isNumber(val) {
return opt.call(val) === "[object Number]" && !Number.isNaN(val);
}
function isNumeric(val, float = true) {
if (isNil(val)) {
return false;
}
if (typeof val === "number") {
return true;
}
const reg = float ? /^-?\d+(?:\.\d+)?$/ : /^\d+$/;
return reg.test(val.toString());
}
function isFunction(val) {
return typeof val === "function";
}
function isArray(val) {
return Array.isArray(val);
}
function isEmptyArray(val) {
return isArray(val) && val.length === 0;
}
function isArrayEqual(arr1, arr2, order = false) {
if (!isArray(arr1) || !isArray(arr2)) {
return false;
}
const len = arr1.length;
if (len !== arr2.length) {
return false;
}
if (order) {
for (let i = 0; i < len; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
} else {
const arr2Set = new Set(arr2);
for (let i = 0; i < len; i++) {
if (!arr2Set.has(arr1[i])) {
return false;
}
}
}
return true;
}
function isEmptyObject(val) {
return opt.call(val) === "[object Object]" && Object.keys(val).length === 0;
}
function isValidDate(val) {
return val instanceof Date && !Number.isNaN(val.valueOf());
}
function isObject(val) {
return val !== null && typeof val === "object";
}
function isPlainObject(val) {
return opt.call(val) === "[object Object]";
}
const isPromise = (val) => {
return isObject(val) && isFunction(val.then) && isFunction(val.catch);
};
const isClient = typeof window !== "undefined";
const isTouchDevice = isClient ? "ontouchstart" in document.documentElement : false;
const isHoverDevice = isClient ? window.matchMedia("(hover: hover)").matches : false;
const isIosDevice = isClient ? /iphone|ipad|ipod/.test(window.navigator.userAgent.toLowerCase()) : false;
function isWindow(val) {
return val === window;
}
function isCurrentPageLink(link) {
if (link.startsWith("#")) {
return true;
}
try {
const targetUrl = new URL(link, window.location.href);
return targetUrl.origin + targetUrl.pathname === window.location.origin + window.location.pathname;
} catch {
return false;
}
}
exports.isArray = isArray;
exports.isArrayEqual = isArrayEqual;
exports.isBoolean = isBoolean;
exports.isClient = isClient;
exports.isCurrentPageLink = isCurrentPageLink;
exports.isEmptyArray = isEmptyArray;
exports.isEmptyObject = isEmptyObject;
exports.isFunction = isFunction;
exports.isHoverDevice = isHoverDevice;
exports.isIosDevice = isIosDevice;
exports.isNil = isNil;
exports.isNull = isNull;
exports.isNumber = isNumber;
exports.isNumeric = isNumeric;
exports.isObject = isObject;
exports.isPlainObject = isPlainObject;
exports.isPromise = isPromise;
exports.isString = isString;
exports.isTouchDevice = isTouchDevice;
exports.isUndefined = isUndefined;
exports.isValidDate = isValidDate;
exports.isWindow = isWindow;