1、是否是Number
const isNumber = val => {
return typeof val === 'number';
}2、是否是String
const isString = val => {
return typeof val === 'string';
}3、是否是对象
const isObject = obj => {
return obj === Object(obj);
}4、是否是undefined
const isUndefined = val => {
return val === undefined;
};5、是否是null
const isNull = val => {
return val === null;
}6、是否是boolean
const isBoolean = val => {
return typeof val === 'boolean';
}7、是否是函数
const isFunction = val => {
return typeof val === 'function';
}8、是否是空的
const isEmpty = val => {
return val == null || !(Object.keys(val) || val).length;
}const UA = typeof window !== 'undefined' && window.navigator.userAgent.toLowerCase();const isIE = UA && /msie|trident/.test(UA)const isIE9 = UA && UA.indexOf('msie 9.0') > 0;const isEdge = UA && UA.indexOf('edge/') > 0;const isAndroid = UA && UA.indexOf('android') > 0;const isIOS = UA && /iphone|ipad|ipod|ios/.test(UA);const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge;const isBrowser = () => {
return ![typeof window, typeof document].includes('undefined');
};const isSupportCanvas = () {
return document.createElement('canvas').getContext ? true : false;
};const isUrl = new RegExp('[a-zA-z]+://[^\s]*') // 网址判断
const isEmail = new RegExp('^\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*$') // 邮箱判断
const isChinese = new RegExp('[\u4e00-\u9fa5]') // 中文判断
const isPhone = new RegExp('^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$') // 中国大陆手机号码
const isIDCard = new RegExp('(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)') // 身份证
const isPositiveInteger = new RegExp('[0-9]*[1-9][0-9]*') // 正整数
const isNegativeInteger = new RegExp('-[0-9]*[1-9][0-9]*') // 负整数
const isInteger = new regExp('-?\d+') // 整数获取查询参数的键值对
const getURLParameters = url =>
(url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
(a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
{}
);const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
};1、数组方法
arrayObj.slice(0); // 返回一个新数组
arrayObj.concat(); // 返回一个新数组2、for...in
function copy(obj){
let newObj = {};
for ( let attr in obj) {
newObj[attr] = obj[attr];
}
return newObj;
}3、对象解构
解构的原理是
Object.assgin
let newObj = {...obj};4、Object.assgin
let newObj = Object.assgin({}, obj);1、递归
function deepCopy(obj){
if(typeof obj !== 'object'){
return obj;
}
let newObj = {};
for ( let item in obj) {
newObj[item] = deepCopy(obj[item]);
}
return newObj;
}2、JSON.stringify(黑魔法)
let newObj = JSON.parse(JSON.stringify(obj));const average = (arr) => {
return arr.reduce((acc, val) => acc + val, 0) / arr.length;
}const capitalizeEveryWord = (str )=> {
return str.replace(/\b[a-z]/g, (char) => {
return char.toUpperCase();
});
}const countOccurrences = (arr, value) => {
return arr.reduce((a, v) => {
return v === value ? a + 1 : a + 0;
}, 0)
};const difference = (a, b) => {
const s = new Set(b);
return a.filter((x) => {
return !s.has(x);
});
};const arrayMax = (arr) => {
return Math.max(...arr);
};const arrayMax = (arr) => {
return Math.min(...arr);
};const reverseString = (str) => {
return [...str].reverse().join('');
};const sum = (arr) => {
return arr.reduce((acc, val) => {
return acc + val;
}, 0);
}const array2String = function (array) {
return array.length === 1 ? array[0] : array.join(' ')
}const getCurrentScriptSource = funtion () {
if (document.currentScript) {
return document.currentScript.getAttribute('src');
}
}