init: v3 init
This commit is contained in:
@@ -1,289 +0,0 @@
|
||||
import { isServer } from './validate'
|
||||
const ieVersion = isServer ? 0 : Number((document as any).documentMode)
|
||||
const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g
|
||||
const MOZ_HACK_REGEXP = /^moz([A-Z])/
|
||||
|
||||
export interface ViewportOffsetResult {
|
||||
left: number
|
||||
top: number
|
||||
right: number
|
||||
bottom: number
|
||||
rightIncludeBody: number
|
||||
bottomIncludeBody: number
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
const trim = function (string: string) {
|
||||
return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '')
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
const camelCase = function (name: string) {
|
||||
return name
|
||||
.replace(SPECIAL_CHARS_REGEXP, function (_, __, letter, offset) {
|
||||
return offset ? letter.toUpperCase() : letter
|
||||
})
|
||||
.replace(MOZ_HACK_REGEXP, 'Moz$1')
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export function hasClass(el: Element, cls: string) {
|
||||
if (!el || !cls) return false
|
||||
if (cls.indexOf(' ') !== -1) {
|
||||
throw new Error('className should not contain space.')
|
||||
}
|
||||
if (el.classList) {
|
||||
return el.classList.contains(cls)
|
||||
} else {
|
||||
return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export function addClass(el: Element, cls: string) {
|
||||
if (!el) return
|
||||
let curClass = el.className
|
||||
const classes = (cls || '').split(' ')
|
||||
|
||||
for (let i = 0, j = classes.length; i < j; i++) {
|
||||
const clsName = classes[i]
|
||||
if (!clsName) continue
|
||||
|
||||
if (el.classList) {
|
||||
el.classList.add(clsName)
|
||||
} else if (!hasClass(el, clsName)) {
|
||||
curClass += ' ' + clsName
|
||||
}
|
||||
}
|
||||
if (!el.classList) {
|
||||
el.className = curClass
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export function removeClass(el: Element, cls: string) {
|
||||
if (!el || !cls) return
|
||||
const classes = cls.split(' ')
|
||||
let curClass = ' ' + el.className + ' '
|
||||
|
||||
for (let i = 0, j = classes.length; i < j; i++) {
|
||||
const clsName = classes[i]
|
||||
if (!clsName) continue
|
||||
|
||||
if (el.classList) {
|
||||
el.classList.remove(clsName)
|
||||
} else if (hasClass(el, clsName)) {
|
||||
curClass = curClass.replace(' ' + clsName + ' ', ' ')
|
||||
}
|
||||
}
|
||||
if (!el.classList) {
|
||||
el.className = trim(curClass)
|
||||
}
|
||||
}
|
||||
|
||||
export function getBoundingClientRect(element: Element): DOMRect | number {
|
||||
if (!element || !element.getBoundingClientRect) {
|
||||
return 0
|
||||
}
|
||||
return element.getBoundingClientRect()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前元素的left、top偏移
|
||||
* left:元素最左侧距离文档左侧的距离
|
||||
* top:元素最顶端距离文档顶端的距离
|
||||
* right:元素最右侧距离文档右侧的距离
|
||||
* bottom:元素最底端距离文档底端的距离
|
||||
* rightIncludeBody:元素最左侧距离文档右侧的距离
|
||||
* bottomIncludeBody:元素最底端距离文档最底部的距离
|
||||
*
|
||||
* @description:
|
||||
*/
|
||||
export function getViewportOffset(element: Element): ViewportOffsetResult {
|
||||
const doc = document.documentElement
|
||||
|
||||
const docScrollLeft = doc.scrollLeft
|
||||
const docScrollTop = doc.scrollTop
|
||||
const docClientLeft = doc.clientLeft
|
||||
const docClientTop = doc.clientTop
|
||||
|
||||
const pageXOffset = window.pageXOffset
|
||||
const pageYOffset = window.pageYOffset
|
||||
|
||||
const box = getBoundingClientRect(element)
|
||||
|
||||
const { left: retLeft, top: rectTop, width: rectWidth, height: rectHeight } = box as DOMRect
|
||||
|
||||
const scrollLeft = (pageXOffset || docScrollLeft) - (docClientLeft || 0)
|
||||
const scrollTop = (pageYOffset || docScrollTop) - (docClientTop || 0)
|
||||
const offsetLeft = retLeft + pageXOffset
|
||||
const offsetTop = rectTop + pageYOffset
|
||||
|
||||
const left = offsetLeft - scrollLeft
|
||||
const top = offsetTop - scrollTop
|
||||
|
||||
const clientWidth = window.document.documentElement.clientWidth
|
||||
const clientHeight = window.document.documentElement.clientHeight
|
||||
return {
|
||||
left: left,
|
||||
top: top,
|
||||
right: clientWidth - rectWidth - left,
|
||||
bottom: clientHeight - rectHeight - top,
|
||||
rightIncludeBody: clientWidth - left,
|
||||
bottomIncludeBody: clientHeight - top
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export const on = function (
|
||||
element: HTMLElement | Document | Window,
|
||||
event: string,
|
||||
handler: EventListenerOrEventListenerObject
|
||||
): void {
|
||||
if (element && event && handler) {
|
||||
element.addEventListener(event, handler, false)
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export const off = function (
|
||||
element: HTMLElement | Document | Window,
|
||||
event: string,
|
||||
handler: any
|
||||
): void {
|
||||
if (element && event && handler) {
|
||||
element.removeEventListener(event, handler, false)
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export const once = function (el: HTMLElement, event: string, fn: EventListener): void {
|
||||
const listener = function (this: any, ...args: unknown[]) {
|
||||
if (fn) {
|
||||
// @ts-ignore
|
||||
fn.apply(this, args)
|
||||
}
|
||||
off(el, event, listener)
|
||||
}
|
||||
on(el, event, listener)
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export const getStyle =
|
||||
ieVersion < 9
|
||||
? function (element: Element | any, styleName: string) {
|
||||
if (isServer) return
|
||||
if (!element || !styleName) return null
|
||||
styleName = camelCase(styleName)
|
||||
if (styleName === 'float') {
|
||||
styleName = 'styleFloat'
|
||||
}
|
||||
try {
|
||||
switch (styleName) {
|
||||
case 'opacity':
|
||||
try {
|
||||
return element.filters.item('alpha').opacity / 100
|
||||
} catch (e) {
|
||||
return 1.0
|
||||
}
|
||||
default:
|
||||
return element.style[styleName] || element.currentStyle
|
||||
? element.currentStyle[styleName]
|
||||
: null
|
||||
}
|
||||
} catch (e) {
|
||||
return element.style[styleName]
|
||||
}
|
||||
}
|
||||
: function (element: Element | any, styleName: string) {
|
||||
if (isServer) return
|
||||
if (!element || !styleName) return null
|
||||
styleName = camelCase(styleName)
|
||||
if (styleName === 'float') {
|
||||
styleName = 'cssFloat'
|
||||
}
|
||||
try {
|
||||
const computed = (document as any).defaultView.getComputedStyle(element, '')
|
||||
return element.style[styleName] || computed ? computed[styleName] : null
|
||||
} catch (e) {
|
||||
return element.style[styleName]
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export function setStyle(element: Element | any, styleName: any, value: any) {
|
||||
if (!element || !styleName) return
|
||||
|
||||
if (typeof styleName === 'object') {
|
||||
for (const prop in styleName) {
|
||||
if (Object.prototype.hasOwnProperty.call(styleName, prop)) {
|
||||
setStyle(element, prop, styleName[prop])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
styleName = camelCase(styleName)
|
||||
if (styleName === 'opacity' && ieVersion < 9) {
|
||||
element.style.filter = isNaN(value) ? '' : 'alpha(opacity=' + value * 100 + ')'
|
||||
} else {
|
||||
element.style[styleName] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export const isScroll = (el: Element, vertical: any) => {
|
||||
if (isServer) return
|
||||
|
||||
const determinedDirection = vertical !== null || vertical !== undefined
|
||||
const overflow = determinedDirection
|
||||
? vertical
|
||||
? getStyle(el, 'overflow-y')
|
||||
: getStyle(el, 'overflow-x')
|
||||
: getStyle(el, 'overflow')
|
||||
|
||||
return overflow.match(/(scroll|auto)/)
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export const getScrollContainer = (el: Element, vertical?: any) => {
|
||||
if (isServer) return
|
||||
|
||||
let parent: any = el
|
||||
while (parent) {
|
||||
if ([window, document, document.documentElement].includes(parent)) {
|
||||
return window
|
||||
}
|
||||
if (isScroll(parent, vertical)) {
|
||||
return parent
|
||||
}
|
||||
parent = parent.parentNode
|
||||
}
|
||||
|
||||
return parent
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export const isInContainer = (el: Element, container: any) => {
|
||||
if (isServer || !el || !container) return false
|
||||
|
||||
const elRect = el.getBoundingClientRect()
|
||||
let containerRect
|
||||
|
||||
if ([window, document, document.documentElement, null, undefined].includes(container)) {
|
||||
containerRect = {
|
||||
top: 0,
|
||||
right: window.innerWidth,
|
||||
bottom: window.innerHeight,
|
||||
left: 0
|
||||
}
|
||||
} else {
|
||||
containerRect = container.getBoundingClientRect()
|
||||
}
|
||||
|
||||
return (
|
||||
elRect.top < containerRect.bottom &&
|
||||
elRect.bottom > containerRect.top &&
|
||||
elRect.right > containerRect.left &&
|
||||
elRect.left < containerRect.right
|
||||
)
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
import { isIdCard, isPhone, isCode, isIP, isExternal, isInteger, isEnglish } from '@/utils/validate'
|
||||
|
||||
// 必填项
|
||||
export const requiredRule = {
|
||||
required: true,
|
||||
message: '该项不能为空'
|
||||
}
|
||||
|
||||
// 身份证验证
|
||||
export const idCardRule = {
|
||||
validator: (_, value, callback: Fn) => {
|
||||
if (isIdCard(value)) {
|
||||
callback()
|
||||
} else {
|
||||
return callback(new Error('请输入正确的身份证号码'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 手机号验证
|
||||
export const isPhoneRule = {
|
||||
validator: (_, value, callback: Fn) => {
|
||||
if (isPhone(value)) {
|
||||
callback()
|
||||
} else {
|
||||
return callback(new Error('请输入正确的联系电话'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 邮箱验证
|
||||
export const isEmailRule = {
|
||||
type: 'email',
|
||||
message: '请输入正确的电子邮箱'
|
||||
}
|
||||
|
||||
// url验证
|
||||
export const isUrl = {
|
||||
validator: (_, value, callback: Fn) => {
|
||||
if (isExternal(value)) {
|
||||
callback()
|
||||
} else {
|
||||
return callback(new Error('请输入正确的地址'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 邮编验证
|
||||
export const isCodeRule = {
|
||||
validator: (_, value, callback: Fn) => {
|
||||
if (isCode(value)) {
|
||||
callback()
|
||||
} else {
|
||||
return callback(new Error('请输入正确的邮编'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IP验证
|
||||
export const isIPRule = {
|
||||
validator: (_, value, callback: Fn) => {
|
||||
if (isIP(value)) {
|
||||
callback()
|
||||
} else {
|
||||
return callback(new Error('请输入正确的IP地址'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 正整数
|
||||
export const isIntegerRule = {
|
||||
validator: (_, value, callback: Fn) => {
|
||||
if (isInteger(value)) {
|
||||
callback()
|
||||
} else {
|
||||
return callback(new Error('请输入正整数'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 只能是英文
|
||||
export const isEnglishRule = {
|
||||
validator: (_, value, callback: Fn) => {
|
||||
if (isEnglish(value)) {
|
||||
callback()
|
||||
} else {
|
||||
return callback(new Error('请输入英文字母'))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,208 +0,0 @@
|
||||
import { AxiosResponse } from 'axios'
|
||||
|
||||
/**
|
||||
* 对象数组深拷贝
|
||||
* @param {Array,Object} source 需要深拷贝的对象数组
|
||||
* @param {Array} noClone 不需要深拷贝的属性集合
|
||||
*/
|
||||
export function deepClone(source: any, noClone: string[] = []): any {
|
||||
if (!source && typeof source !== 'object') {
|
||||
throw new Error('error arguments deepClone')
|
||||
}
|
||||
const targetObj: any = source.constructor === Array ? [] : {}
|
||||
Object.keys(source).forEach((keys: string) => {
|
||||
if (source[keys] && typeof source[keys] === 'object' && noClone.indexOf(keys) === -1) {
|
||||
targetObj[keys] = deepClone(source[keys], noClone)
|
||||
} else {
|
||||
targetObj[keys] = source[keys]
|
||||
}
|
||||
})
|
||||
return targetObj
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找数组对象的某个下标
|
||||
* @param {Array} ary 查找的数组
|
||||
* @param {Functon} fn 判断的方法
|
||||
*/
|
||||
// eslint-disable-next-line
|
||||
export function findIndex(ary: Array<any>, fn: Fn): number {
|
||||
if (ary.findIndex) {
|
||||
return ary.findIndex(fn)
|
||||
}
|
||||
let index = -1
|
||||
ary.some((item: any, i: number, ary: Array<any>) => {
|
||||
const ret: any = fn(item, i, ary)
|
||||
if (ret) {
|
||||
index = i
|
||||
return ret
|
||||
}
|
||||
})
|
||||
return index
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机字符串
|
||||
*/
|
||||
export function toAnyString() {
|
||||
const str: string = 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c: string) => {
|
||||
const r: number = (Math.random() * 16) | 0
|
||||
const v: number = c === 'x' ? r : (r & 0x3) | 0x8
|
||||
return v.toString()
|
||||
})
|
||||
return str
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取URL参数
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function param2Obj(url: string) {
|
||||
const search: string = url.split('?')[1]
|
||||
if (!search) {
|
||||
return {}
|
||||
}
|
||||
return JSON.parse(
|
||||
'{"' +
|
||||
decodeURIComponent(search)
|
||||
.replace(/"/g, '\\"')
|
||||
.replace(/&/g, '","')
|
||||
.replace(/=/g, '":"')
|
||||
.replace(/\+/g, ' ') +
|
||||
'"}'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String|Number} value 要验证的字符串或数值
|
||||
* @param {*} validList 用来验证的列表
|
||||
*/
|
||||
export function oneOf(value: string | number, validList: string[] | number[]): boolean {
|
||||
for (let i = 0; i < validList.length; i++) {
|
||||
if (value === validList[i]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {date} time 需要转换的时间
|
||||
* @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
|
||||
*/
|
||||
export function formatTime(time: any, fmt: string) {
|
||||
if (!time) return ''
|
||||
else {
|
||||
const date = new Date(time)
|
||||
const o = {
|
||||
'M+': date.getMonth() + 1,
|
||||
'd+': date.getDate(),
|
||||
'H+': date.getHours(),
|
||||
'm+': date.getMinutes(),
|
||||
's+': date.getSeconds(),
|
||||
'q+': Math.floor((date.getMonth() + 3) / 3),
|
||||
S: date.getMilliseconds()
|
||||
}
|
||||
if (/(y+)/.test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
||||
}
|
||||
for (const k in o) {
|
||||
if (new RegExp('(' + k + ')').test(fmt)) {
|
||||
fmt = fmt.replace(
|
||||
RegExp.$1,
|
||||
RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
|
||||
)
|
||||
}
|
||||
}
|
||||
return fmt
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出
|
||||
* @response {objec} 接收从接口返回的response数据
|
||||
*/
|
||||
export function exportFile(response: AxiosResponse) {
|
||||
const fileName = decodeURI(
|
||||
response.headers['content-disposition']
|
||||
? response.headers['content-disposition'].split(';')[1].split('=')[1]
|
||||
: 'test'
|
||||
)
|
||||
const blob = new Blob([response.data as Blob], {
|
||||
type: response.headers['content-type']
|
||||
})
|
||||
if (typeof (window.navigator as any).msSaveBlob !== 'undefined') {
|
||||
;(window.navigator as any).msSaveBlob(blob, fileName)
|
||||
} else {
|
||||
const blobURL = window.URL.createObjectURL(blob) // 将blob对象转为一个URL
|
||||
const tempLink = document.createElement('a') // 创建一个a标签
|
||||
tempLink.style.display = 'none'
|
||||
tempLink.href = blobURL
|
||||
tempLink.setAttribute('download', fileName) // 给a标签添加下载属性
|
||||
if (typeof tempLink.download === 'undefined') {
|
||||
tempLink.setAttribute('target', '_blank')
|
||||
}
|
||||
document.body.appendChild(tempLink) // 将a标签添加到body当中
|
||||
tempLink.click() // 启动下载
|
||||
document.body.removeChild(tempLink) // 下载完毕删除a标签
|
||||
window.URL.revokeObjectURL(blobURL)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对比两个数组是否一致
|
||||
* @a {array}
|
||||
* @b {array}
|
||||
*/
|
||||
export function valueEquals(a: any[], b: any[]): boolean {
|
||||
// see: https://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript
|
||||
if (a === b) return true
|
||||
if (!(a instanceof Array)) return false
|
||||
if (!(b instanceof Array)) return false
|
||||
if (a.length !== b.length) return false
|
||||
for (let i = 0; i !== a.length; ++i) {
|
||||
if (a[i] !== b[i]) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* IE浏览器版本
|
||||
*/
|
||||
export function IEVersion() {
|
||||
const userAgent = navigator.userAgent // 取得浏览器的userAgent字符串
|
||||
const isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 // 判断是否IE<11浏览器
|
||||
const isEdge = userAgent.indexOf('Edge') > -1 && !isIE // 判断是否IE的Edge浏览器
|
||||
const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1
|
||||
if (isIE) {
|
||||
const reIE = new RegExp('MSIE (\\d+\\.\\d+);')
|
||||
reIE.test(userAgent)
|
||||
const fIEVersion = parseFloat(RegExp['$1'])
|
||||
if (fIEVersion === 7) {
|
||||
return 7
|
||||
} else if (fIEVersion === 8) {
|
||||
return 8
|
||||
} else if (fIEVersion === 9) {
|
||||
return 9
|
||||
} else if (fIEVersion === 10) {
|
||||
return 10
|
||||
} else {
|
||||
return 6 // IE版本<=7
|
||||
}
|
||||
} else if (isEdge) {
|
||||
return 'edge' // edge
|
||||
} else if (isIE11) {
|
||||
return 11 // IE11
|
||||
} else {
|
||||
return -1 // 不是ie浏览器
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰转下划线
|
||||
* @val {string} 需要转换的字符串
|
||||
*/
|
||||
export function humpToUnderline(val: string) {
|
||||
return val.replace(/([A-Z])/g, '_$1').toLowerCase()
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
const toString = Object.prototype.toString
|
||||
|
||||
export function is(val: unknown, type: string) {
|
||||
return toString.call(val) === `[object ${type}]`
|
||||
}
|
||||
|
||||
// 验证网址
|
||||
export function isExternal(path: any): boolean {
|
||||
return /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/.test(
|
||||
path
|
||||
)
|
||||
}
|
||||
|
||||
// 验证邮箱
|
||||
export function isEmail(path: any): boolean {
|
||||
return /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(path)
|
||||
}
|
||||
|
||||
// 验证手机
|
||||
export function isPhone(tel: any): boolean {
|
||||
return /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(tel)
|
||||
}
|
||||
|
||||
// 验证身份证号
|
||||
export function isIdCard(id: any): boolean {
|
||||
return /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
|
||||
id
|
||||
)
|
||||
}
|
||||
|
||||
// 验证固定电话
|
||||
export function isTel(tel: any): boolean {
|
||||
return /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(tel)
|
||||
}
|
||||
|
||||
// 验证数字
|
||||
export function isNumber(num: any): boolean {
|
||||
return /^[0-9]*$/.test(num)
|
||||
}
|
||||
|
||||
// 验证邮编
|
||||
export function isCode(num: any): boolean {
|
||||
return /[1-9]\d{5}(?!\d)/.test(num)
|
||||
}
|
||||
|
||||
// 验证IP
|
||||
export function isIP(val: any): boolean {
|
||||
return /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(
|
||||
val
|
||||
)
|
||||
}
|
||||
|
||||
// 正整数
|
||||
export function isInteger(num: any): boolean {
|
||||
return /^[1-9]\d*$/.test(num)
|
||||
}
|
||||
|
||||
// 英文
|
||||
export function isEnglish(str: any): boolean {
|
||||
return /^[A-Za-z_]+$/.test(str)
|
||||
}
|
||||
|
||||
// 中文
|
||||
export function isChinese(str: any): boolean {
|
||||
return /[\u4E00-\u9FA5]/g.test(str)
|
||||
}
|
||||
|
||||
// 不是浏览器环境
|
||||
export const isServer = typeof window === 'undefined'
|
||||
|
||||
// 是否是对象
|
||||
export function isObject(val: any): val is Record<any, any> {
|
||||
return val !== null && is(val, 'Object')
|
||||
}
|
||||
|
||||
// 是否是火狐
|
||||
export const isFirefox = function () {
|
||||
return !isServer && !!window.navigator.userAgent.match(/firefox/i)
|
||||
}
|
||||
|
||||
// 是否是字符串
|
||||
export function isString(val: unknown): val is string {
|
||||
return is(val, 'String')
|
||||
}
|
||||
|
||||
export const isWindow = (val: any): val is Window => {
|
||||
return typeof window !== 'undefined' && is(val, 'Window')
|
||||
}
|
||||
|
||||
export const isDef = <T = unknown>(val?: T): val is T => {
|
||||
return typeof val !== 'undefined'
|
||||
}
|
||||
|
||||
export const isUnDef = <T = unknown>(val?: T): val is T => {
|
||||
return !isDef(val)
|
||||
}
|
||||
|
||||
export const isFunction = (val: unknown): val is Function => typeof val === 'function'
|
||||
|
||||
export const isClient = () => {
|
||||
return typeof window !== 'undefined'
|
||||
}
|
||||
|
||||
export const isElement = (val: unknown): val is Element => {
|
||||
return isObject(val) && !!val.tagName
|
||||
}
|
||||
Reference in New Issue
Block a user