UNPKG

ipink-util

Version:

util.js

363 lines (360 loc) 15.6 kB
import { parseDate } from './date.mjs'; import { getEnv, ENV_TYPE } from './env.mjs'; import { TinyColor } from '../node_modules/.pnpm/@ctrl_tinycolor@4.1.0/node_modules/@ctrl/tinycolor/dist/module/index.mjs'; const isString = (str2) => Object.prototype.toString.call(str2) === "[object String]"; const isArray = (arr) => Object.prototype.toString.call(arr) === "[object Array]"; const isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]"; const isJson = (obj) => isArray(obj) || isObject(obj); const isFunc = (func) => Object.prototype.toString.call(func) === "[object Function]" || typeof func === "function"; const isBoo = (boo) => Object.prototype.toString.call(boo) === "[object Boolean]"; const isChar = (char) => isString(char) && char.length === 1; const isDom = (dom) => isObject(dom) && dom.nodeType > 0; const isError = (error) => Object.prototype.toString.call(error) === "[object Error]"; const isNaN = (value) => Number.isNaN(value); const isNull = (value, checkString = false) => (checkString ? value === "null" : false) || value === null; const isNumber = (num) => Number.isFinite(num); const isUndefined = (value, checkString = false) => (checkString ? value === "undefined" : false) || value === void 0; const isOdd = (num) => isNumber(num) && (num % 2 === 1 || num % 2 === -1); const isRegexp = (error) => Object.prototype.toString.call(error) === "[object RegExp]"; const isRegexpStr = (str) => eval(str) instanceof RegExp; const isInArray = (value, array = []) => { if (!isArray(array)) return false; let has = false; for (let i = 0; i < array.length; i++) { if (array[i] === value) { has = true; break; } } return has; }; const isInObject = (key, object) => isObject(object) && Object.prototype.hasOwnProperty.call(object, key); const isDate = (date) => Object.prototype.toString.call(date) === "[object Date]"; const isFuture = (date) => { if (!isDate(date)) date = parseDate(date); if (isDate(date)) return date.getTime() > (/* @__PURE__ */ new Date()).getTime(); return false; }; const isYear = (date, year) => { if (!isDate(date)) date = parseDate(date); if (isDate(date)) return +year === date.getFullYear(); return false; }; const isMonth = (date, month) => { if (!isDate(date)) date = parseDate(date); if (isDate(date)) return +month === date.getMonth() + 1; return false; }; const isWeekend = (date) => { if (!isDate(date)) date = parseDate(date); if (isDate(date)) return date.getDay() === 6 || date.getDay() === 0; return false; }; const isTomorrow = (date) => { if (!isDate(date)) date = parseDate(date); const now = /* @__PURE__ */ new Date(); const tomorrow = new Date(now.setDate(now.getDate() + 1)); return isDate(date) && date.toDateString() === tomorrow.toDateString(); }; const isToday = (date) => { if (!isDate(date)) date = parseDate(date); const today = /* @__PURE__ */ new Date(); return isDate(date) && date.toDateString() === today.toDateString(); }; const isYesterday = (date) => { if (!isDate(date)) date = parseDate(date); const now = /* @__PURE__ */ new Date(); const yesterday = new Date(now.setDate(now.getDate() - 1)); return isDate(date) && date.toDateString() === yesterday.toDateString(); }; const isExternal = (path) => { const reg = /^(http?:|https?:|mailto:|tel:)/; return reg.test(path); }; const isEmpty = (obj, trim = false) => { let empty = false; if (obj === null || obj === "null" || obj === void 0 || obj === "undefined" || typeof obj === "string" && obj === "" || typeof obj === "string" && (trim && obj.trim() === "")) empty = true; else empty = false; return empty; }; const isEmptyObject = (obj, trim = false) => { let empty = false; if (isEmpty(obj, trim)) empty = true; else if (Array.isArray(obj) && !obj.length) empty = true; else if (Array.isArray(obj) && obj.length && !obj.filter((item) => !isEmpty(item, trim)).length) empty = true; else if (!Array.isArray(obj) && typeof obj === "object" && !Object.values(obj).filter((item) => !isEmpty(item, trim)).length) empty = true; else empty = false; return empty; }; const isJsonString = (string) => { if (!string) return false; if (typeof string != "string") return false; try { return typeof JSON.parse(string) == "object"; } catch (e) { return false; } }; const isLowerCase = (string) => isString(string) && string === string.toLowerCase(); const isUpperCase = (string) => isString(string) && string === string.toUpperCase(); const isFistCharUpperCase = (string) => { if (!isString(string)) return false; const firstChar = string[0]; return firstChar === firstChar.toUpperCase(); }; const isDateString = (dateString) => !isNaN(Date.parse(dateString)); const isTimeString = (timeString) => /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])(:([0-5]?[0-9]))?$/.test(timeString); const isTel = (tel, telType = 3) => { telType = telType == 1 || telType == 2 || telType == 3 || telType == 4 || false ? telType : 3; let T = telType == 1 ? /^\d{7,8}$/ : telType == 2 ? /^\d{7,8}-\d{1,6}$/ : /^\d{7,8}(-\d{1,6})?$/; let T2 = telType == 1 ? /^0\d{2,3}(-)?\d{7,8}$/ : telType == 2 ? /^0\d{2,3}(-)?\d{7,8}-\d{1,6}$/ : /^0\d{2,3}(-)?\d{7,8}(-\d{1,6})?$/; let P = /^1[3,4,5,6,7,8,9]\d{9}$/; return telType == 4 ? T.test(tel) || T2.test(tel) || P.test(tel) : T2.test(tel) || T.test(tel); }; const isZipCode = (code) => /^[0-9]\d{5}$/.test(code); const isPhoneNumber = (phone) => /^1[3,4,5,6,7,8,9]\d{9}$/.test(phone); const isEmail = (email) => /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(email); const isIdCard = (code) => { code = code.toString(); const city = { "11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁", "22": "吉林", "23": "黑龙江 ", "31": "上海", "32": "江苏", "33": "浙江", "34": "安徽", "35": "福建", "36": "江西", "37": "山东", "41": "河南", "42": "湖北 ", "43": "湖南", "44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川", "52": "贵州", "53": "云南", "54": "西藏 ", "61": "陕西", "62": "甘肃", "63": "青海", "64": "宁夏", "65": "新疆", "71": "台湾", "81": "香港", "82": "澳门", "91": "国外" }; if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) return false; const addressCode = code.substr(0, 2); if (!city[addressCode]) return false; if (code.length == 18) { let sBirthday = code.substr(6, 4) + "-" + Number(code.substr(10, 2)) + "-" + Number(code.substr(12, 2)); let d = new Date(sBirthday.replace(/-/g, "/")); if (sBirthday != d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate()) return false; } if (code.length == 18) { const codeArr = code.split(""); let factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; let parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]; let sum = 0, ai = 0, wi = 0, i = 0; for (; i < 17; i++) { ai = +codeArr[i]; wi = factor[i]; sum += ai * wi; } let last = parity[sum % 11]; let cardLastChar = codeArr[17].toUpperCase(); if (last != cardLastChar) return false; } return true; }; const isJuLiuCardV1 = (code) => { if (!code || !isString(code)) { console.error("为获取到正确的居留证件码!"); return false; } if (code.length !== 15) return false; const map = { A: 10, B: 11, C: 12, D: 13, E: 14, F: 15, G: 16, H: 17, I: 18, J: 19, K: 20, L: 21, M: 22, N: 23, O: 24, P: 25, Q: 26, R: 27, S: 28, T: 29, U: 30, V: 31, W: 32, X: 33, Y: 34, Z: 35 }; let wiArr = [7, 3, 1]; const codeArr = code.split(""); let checkNum = codeArr[codeArr.length - 1]; let sum = 0; codeArr.forEach((v, index) => { if (index > 13) return; let n = map[v] || v; sum += n * wiArr[index % 3]; }); const ai = sum % 10; return "" + ai == "" + checkNum; }; const isJuLiuCardV2 = (code) => { if (!code || !isString(code)) { console.error("为获取到正确的居留证件码!"); return false; } if (code.length !== 18) return false; if (!code.startsWith("9")) return false; let wiArr = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; const codeArr = code.split(""); let aiArr = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]; let checkNum = codeArr[17].toUpperCase(); let sum = 0; codeArr.forEach((v, index) => { if (index > 16) return; sum += Number(v) * wiArr[index]; }); const ai = aiArr[sum % 11]; return ai == checkNum; }; const isJuLiuCard = (code) => isJuLiuCardV1(code) || isJuLiuCardV2(code); const isIp = (ip) => /^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/.test(ip); const isIpv6 = (ip) => /^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i.test(ip); const isUrl = (url) => /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/i.test(url); const isWindowObject = (value) => value != null && typeof value === "object" && "setInterval" in value; const freeSelf = isWindowObject(typeof self == "object" && self) && self; const navigator = freeSelf && freeSelf.navigator; const platform = (navigator && navigator.platform || "").toLowerCase(); const userAgent = (navigator && navigator.userAgent || "").toLowerCase(); const vendor = (navigator && navigator.vendor || "").toLowerCase(); const isOpera = () => { return userAgent.match(/(?:^opera.+?version|opr)\/(\d+)/) !== null; }; const isOperaMini = () => { return userAgent.match(/opera mini\/(\d+)/) !== null; }; const isChrome = () => { return (/google inc/.test(vendor) ? userAgent.match(/(?:chrome|crios)\/(\d+)/) : null) !== null; }; const isFirefox = () => { return userAgent.match(/(?:firefox|fxios)\/(\d+)/) !== null; }; const isEdge = () => { return userAgent.match(/edge\/(\d+)/) !== null; }; const isIe = () => { return userAgent.match(/(?:msie |trident.+?; rv:)(\d+)/) !== null; }; const isSafari = () => { return userAgent.match(/version\/(\d+).+?safari/) !== null; }; const isIpad = () => { return userAgent.match(/ipad.+?os (\d+)/) !== null; }; const isIphone = () => { return (isIpad() ? null : userAgent.match(/iphone(?:.+?os (\d+))?/)) !== null; }; const isIpod = () => { return userAgent.match(/ipod.+?os (\d+)/) !== null; }; const isIos = () => { if (userAgent) { return isIpad() || isIphone() || isIpod(); } try { const SystemInfo = uni && uni.getSystemInfoSync && uni.getSystemInfoSync(); if (!SystemInfo) return false; else { if (SystemInfo.platform) return SystemInfo.platform.toLowerCase().indexOf("ios") > -1; if (SystemInfo.osName) return SystemInfo.osName.toLowerCase().indexOf("ios") > -1; if (SystemInfo.system) return SystemInfo.system.toLowerCase().indexOf("ios") > -1; } } catch (e) { } return false; }; const isAndroid = () => { if (userAgent) { return userAgent.indexOf("Android") > -1 || userAgent.indexOf("Adr") > -1; } try { const SystemInfo = uni && uni.getSystemInfoSync && uni.getSystemInfoSync(); if (!SystemInfo) return false; else { if (SystemInfo.platform) return SystemInfo.platform.toLowerCase().indexOf("android") > -1; if (SystemInfo.osName) return SystemInfo.osName.toLowerCase().indexOf("android") > -1; if (SystemInfo.system) return SystemInfo.system.toLowerCase().indexOf("android") > -1; } } catch (e) { } return false; }; const isMini = () => { if (navigator && navigator.userAgent && navigator.userAgent.toLowerCase().indexOf("Mini") > -1) return true; try { const SystemInfo = typeof uni !== "undefined" ? uni.getSystemInfoSync() : wx.getSystemInfoSync(); if (SystemInfo && SystemInfo.uniPlatform && SystemInfo.uniPlatform.startsWith("mp")) return true; } catch (e) { } return false; }; const isWxMini = () => { if (typeof window !== "undefined") return getEnv() == ENV_TYPE.WXMINI; try { const SystemInfo = typeof uni !== "undefined" ? wx.getSystemInfoSync() : wx.getSystemInfoSync(); if (SystemInfo?.host?.env) { return (SystemInfo.host.env || "").toLowerCase() == "wechat"; } else { return (SystemInfo?.uniPlatform || SystemInfo?.platform).toLowerCase() == "mp-weixin"; } } catch (e) { } return false; }; const isWindows = () => /win/.test(platform); const isMac = () => /mac/.test(platform); const isLinux = () => /linux/.test(platform) && !isAndroid(); const isAndroidH5 = () => /android/.test(userAgent); const isWindowsPhone = () => isWindows() && /phone/.test(userAgent); const isAndroidPhone = () => /android/.test(userAgent) && /mobile/.test(userAgent); const isBlackberry = () => /blackberry/.test(userAgent) || /bb10/.test(userAgent); const isAndroidTablet = () => /android/.test(userAgent) && !/mobile/.test(userAgent); const isMobile = () => isIphone() || isIpod() || isAndroidPhone() || isBlackberry() || isWindowsPhone(); const isPhoneSize = () => { if (window.matchMedia) return window.matchMedia("(max-width:500px)").matches; return window.innerWidth <= 500; }; const isOnline = () => !navigator || navigator.onLine === true; const isOffline = !isOnline(); const isDarkColor = (color) => { const colorInfo = new TinyColor(color); const { r, g, b, isValid } = colorInfo || {}; if (!isValid) return false; return r * 0.299 + g * 0.587 + b * 0.114 > 186; }; export { isAndroid, isAndroidH5, isAndroidPhone, isAndroidTablet, isArray, isBlackberry, isBoo, isChar, isChrome, isDarkColor, isDate, isDateString, isDom, isEdge, isEmail, isEmpty, isEmptyObject, isError, isExternal, isFirefox, isFistCharUpperCase, isFunc, isFuture, isIdCard, isIe, isInArray, isInObject, isIos, isIp, isIpad, isIphone, isIpod, isIpv6, isJson, isJsonString, isJuLiuCard, isLinux, isLowerCase, isMac, isMini, isMobile, isMonth, isNaN, isNull, isNumber, isObject, isOdd, isOffline, isOnline, isOpera, isOperaMini, isPhoneNumber, isPhoneSize, isRegexp, isRegexpStr, isSafari, isString, isTel, isTimeString, isToday, isTomorrow, isUndefined, isUpperCase, isUrl, isWeekend, isWindowObject, isWindows, isWindowsPhone, isWxMini, isYear, isYesterday, isZipCode };