目录
值类型校验
手机号校验
/**
* 手机号校验
* @param {String | Number} value
* @returns {Boolean}
*/
export function isMobile(value) {
const REG_MOBILE = /^1(3|4|5|6|7|8|9)[0-9]{9}$/
return REG_MOBILE.test(value)
}
密码校验
/**
* 密码校验
* @param {String | Number} value
* @returns {Boolean}
*/
export function(value) {
const REG_PASSWORD = /^[0-9a-zA-Z]{6,20}$/
return REG_PASSWORD.test(value)
}
中文校验
/**
* 中文字符校验
* @param {String} value
* @returns {Boolean}
*/
export function isChineseString(value) {
const REG_CHINESE_STRING = /[^(\u4e00-\u9fa5)]{8,24}/
return REG_CHINESE_STRING.test(value)
}
邮箱校验
/**
* 邮箱验证
* @param {String} value
* @returns {Boolean}
*/
export function isEmail(value) {
const REG_EMAIL = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
return REG_EMAIL.test(value)
}
检测设备系统
检测是否是手机设备
/**
* 是否是手机设备
*/
export function isMobileDevice() {
const userAgent = window.navigator.userAgent
return !!userAgent.match(/AppleWebKit.*Mobile.*/)
}
检测 ios
/**
* 判断是否是IOS系统
*/
export function isIOS() {
const REG_IOS = /iphone|ipad|ipod/
return REG_IOS.test(userAgent)
}
检测 android
/**
* 判断是否是 android 系统
*/
export function isAndroid() {
const REG_ANFROID = /android/
return REG_ANFROID.test(userAgent)
}
检测 Mac 系统
/**
* 判断是否是 MAC 系统
*/
export function isMac() {
const REG_MAC = /macintosh|mac os x/i
return REG_MAC.test(userAgent)
}
检测 Windows 系统
/**
* 判断是否是 WINDOWS 系统
*/
export function isWindows() {
const REG_WINDOWS = /windows|win32/i
return REG_WINDOWS.test(userAgent)
}
检测 Safari 浏览器
/**
* 判断是否是 Safari 浏览器
*/
export function isSafari() {
const REG_SAFARI = /safari/
const REG_CHROME = /chrome/
return REG_SAFARI.test(userAgent) && !REG_CHROME.test(userAgent)
}
检测 flash 插件
/**
* 检查浏览器是否已经启用 flash 插件
* 注意:
* 1. 如果浏览器禁用了插件,则无法检查
* 2. 谷歌、火狐、微软Edge、Safari等现代浏览器不支持ActiveXObject,它们支持navigator.plugins检查浏览器插件
* 3. chrome浏览器默认是禁用flash插件的
*/
export function isFlash() {
let flash
if (typeof window.ActiveXObject != 'undefined') {
flash = window.ActiveXObject('ShockwaveFlash.ShockwaveFlash')
} else {
flash = navigator.plugins['Shockwave Flash']
}
return flash ? true : false
}
检测数据类型
检测 Null
export function isNull(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Null]'
}
检测 Undefined
export function isUndefined(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Undefined]'
}
检测 Boolean
export function isBoolean(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Boolean]'
}
检测 String
export function isString() {
const type = Object.prototype.toString
return type.call(value) === '[object String]'
}
检测 Number
export function isNumber(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Number]'
}
检测 NaN
export function isNaN(value) {
const type = Object.prototype.toString
return type.call(value) === '[object NaN]'
}
检测 Object
/**
* 是否是一个对象类型
* @param {*} value
*/
export function isObject(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Object]'
}
检测 Array
/**
* 是否是一个数组
* @param {Array} value
*/
export function isArray (value) {
const type = Object.prototype.toString
return type.call(value) === '[object Array]'
}
检测 RegExp
export function isRegExp(value) {
const type = Object.prototype.toString
return type.call(value) === '[object RegExp]'
}
检测 Map
export function isMap(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Map]'
}
检测 Promise
export function isPromise(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Promise]'
}
检测 Date
export function isDate(value) {
const type = Object.prototype.toString
reutrn type.call(value) === '[object Date]'
}
检测 Math
export function isMath(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Math]'
}
检测 JSON
export function isJSON(value) {
const type = Object.prototype.toString
return type.call(value) === '[object JSON]'
}
检测 Error
export function isError(value) {
const type = Object.prototype.toString
return type.call(value) === '[object Error]'
}
其他
HTML结构中移除html标签
/**
* html 过滤为 text(即去除html标签)
* @param {String} value html
*/
export function htmlToText(value) {
const REG_HTML_TO_TEXT = /<\/?.+?\/?>/g
return value.replace(REG_HTML_TO_TEXT, '')
}
生成UUID
/**
* 生成UUID
*/
export function createUuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0
const v = c == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
时间处理器
/**
* 时间过滤器
* @param {String} value 时间字符串
*/
export function moment() {
var datetime = new Date()
if (typeof value === 'string' && /^[0-9]{4}(\-|\/)[0-9]{1,2}(\-|\/)[0-9]{1,2}/.test(value)) {
datetime = new Date(value.replace(/\-/g, '/'))
} else if (Object.prototype.toString.call(value) === '[object Date]') {
datetime = value
}
function repair(value) {
return value > 9 ? value : `0${value}`
}
function format(type) {
var datetime_format,
Y = datetime.getFullYear(),
M = datetime.getMonth() + 1,
D = datetime.getDate(),
H = datetime.getHours(),
m = datetime.getMinutes(),
s = datetime.getSeconds()
switch (type) {
case 'YYYY-MM':
datetime_format = `${Y}-${repair(M)}`
break;
case 'YYYY-MM-DD':
datetime_format = `${Y}-${repair(M)}-${repair(D)}`
break;
case 'HH:mm':
datetime_format = `${repair(H)}:${repair(m)}`
break;
case 'HH:mm:ss':
datetime_format = `${repair(H)}:${repair(m)}:${repair(s)}`
break;
case 'YYYY-MM-DD HH:mm':
datetime_format = `${Y}-${repair(M)}-${repair(D)} ${repair(H)}:${repair(m)}`
break;
case 'YYYY-MM-DD HH:mm:ss':
datetime_format = `${Y}-${repair(M)}-${repair(D)} ${repair(H)}:${repair(m)}:${repair(s)}`
break;
case 'YYYY/MM':
datetime_format = `${Y}/${repair(M)}`
break;
case 'YYYY/MM/DD':
datetime_format = `${Y}/${repair(M)}/${repair(D)}`
break;
case 'YYYY/MM/DD HH:mm':
datetime_format = `${Y}/${repair(M)}/${repair(D)} ${repair(H)}:${repair(m)}`
break;
case 'YYYY/MM/DD HH:mm:ss':
datetime_format = `${Y}/${repair(M)}/${repair(D)} ${repair(H)}:${repair(m)}:${repair(s)}`
break;
default:
break;
}
}
return {
format
}
}
// 用法
moment().format('YYYY-MM-DD HH:mm:ss')
moment('2020-12-12 23:23:24').format('HH:mm:ss')
moment('2020/12/12 23:23:24').format('YYYY-MM')