t-comm
Version:
专业、稳定、纯粹的工具库
87 lines (84 loc) • 1.98 kB
JavaScript
import _typeof from '@babel/runtime/helpers/typeof';
/**
* 判断数据是不是正则对象
* @param {any} value - 输入数据
* @returns {boolean} 是否是正则对象
*
* @example
*
* isRegExp(1)
*
* // => false
*
* isRegExp(/\d/)
*
* // => true
*/
function isRegExp(value) {
return Object.prototype.toString.call(value) === '[object RegExp]';
}
/**
* 判断数据是不是时间对象
* @param {any} value - 输入数据
* @returns {boolean} 是否是时间对象
*
* @example
*
* isDate(1)
*
* // => false
*
* isDate(new Date())
*
* // => true
*/
function isDate(value) {
return Object.prototype.toString.call(value) === '[object Date]';
}
/**
* 判断数据是不是函数
* @param {any} value - 输入数据
* @returns {boolean} 是否是函数
*
* @example
*
* isFunction(1)
*
* // => false
*
* isFunction(()=>{})
*
* // => true
*/
function isFunction(value) {
// typeof val === 'function' // 也可以
return Object.prototype.toString.call(value) === '[object Function]';
}
function isPlainObject(val) {
return val !== null && _typeof(val) === 'object' && !Array.isArray(val);
}
function isPromise(val) {
return isPlainObject(val) && isFunction(val.then) && isFunction(val["catch"]);
}
function isDef(value) {
return value !== undefined && value !== null;
}
function isObj(x) {
var type = _typeof(x);
return x !== null && (type === 'object' || type === 'function');
}
function isObject(val) {
return val !== null && _typeof(val) === 'object';
}
var IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i;
var VIDEO_REGEXP = /\.(mp4|mpg|mpeg|dat|asf|avi|rm|rmvb|mov|wmv|flv|mkv)/i;
function isImageUrl(url) {
return IMAGE_REGEXP.test(url);
}
function isVideoUrl(url) {
return VIDEO_REGEXP.test(url);
}
function isNumber(value) {
return /^\d+(\.\d+)?$/.test(value);
}
export { isDate, isDef, isFunction, isImageUrl, isNumber, isObj, isObject, isPlainObject, isPromise, isRegExp, isVideoUrl };