songcomutil
Version:
sxj工具库
68 lines (64 loc) • 1.96 kB
JavaScript
const {getFileExt,findObjectByProp,sortByKey,throttle,average,downloadFileFromUrl,isMobile,debounce} = require('./other')
const {flatten,unique} = require('./array/index')
const {filterObjProps} = require('./object/index')
const {formatDate} = require('./date/index')
const {} = require('./string/index')
//判断数组类型
function isArray(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
}
//判断对象类型
function isObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
//判断函数类型
function isFunction(fn) {
return Object.prototype.toString.call(fn) === '[object Function]';
}
//深度克隆对象
function cloneDeep(obj) {
if (!obj || typeof obj !== 'object') {
return obj;
}
let newObj = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = cloneDeep(obj[key]);
}
}
return newObj;
}
//合并对象
function merge(target, ...sources) {
sources.forEach(source => {
for (let key in source) {
if (source.hasOwnProperty(key)) {
if (isObject(source[key])) {
target[key] = merge({}, target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
});
return target;
}
//随机数生成
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//获取url参数
function getUrlParams(url) {
const params = {};
new URL(url || window.location.href).searchParams.forEach((value, key) => {
params[key] = value;
});
return params;
}
module.exports= {
getFileExt,findObjectByProp,sortByKey,throttle,average,downloadFileFromUrl,isMobile,debounce,
filterObjProps,
flatten,unique,
formatDate,
getUrlParams,randomNum,merge,cloneDeep,isFunction,isArray
};