@eljs/utils
Version:
Collection of nodejs utility.
130 lines (117 loc) • 2.99 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function isTypeOf(target, type) {
if (!type) {
return false;
}
try {
type = type.toLocaleLowerCase();
if (target === undefined) {
return type === 'undefined';
}
if (target === null) {
return type === 'null';
}
return Object.prototype.toString.call(target).toLocaleLowerCase() === "[object ".concat(type, "]");
} catch (err) {
return false;
}
}
/**
* 是否为 undefined
* @param target 目标对象
*/
export function isUndefined(target) {
return isTypeOf(target, 'undefined');
}
/**
* 是否为 Null
* @param target 目标对象
*/
export function isNull(target) {
return isTypeOf(target, 'null');
}
/**
* 是否为字符串
* @param target 目标对象
*/
export function isString(target) {
return isTypeOf(target, 'string');
}
/**
* 是否为对象
* @param target 目标对象
*/
export function isObject(target) {
return isTypeOf(target, 'Object');
}
/**
* 是否为数组
* @param target 目标对象
*/
export function isArray(target) {
return isTypeOf(target, 'array');
}
/**
* 是否为函数
* @param target 目标对象
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function isFunction(target) {
return isTypeOf(target, 'function');
}
/**
* 是否为布尔值
* @param target 目标对象
*/
export function isBoolean(target) {
return isTypeOf(target, 'boolean');
}
/**
* 是否为 Object 扩展的对象
* @param target 目标对象
*/
export function isPlainObject(target) {
if (!isObject(target)) {
return false;
}
var proto = Object.getPrototypeOf(target);
// 支持 Object.create(null)
if (proto === null) {
return true;
}
var baseProto = proto;
while (Object.getPrototypeOf(baseProto) !== null) {
baseProto = Object.getPrototypeOf(baseProto);
}
return baseProto === proto;
}
/**
* 是否为 Promise 实例
* @param target 目标对象
*/
export function isPromise(target) {
return target && typeof target.then === 'function';
}
/**
* 是否为 Generator 函数
* @param target 目标对象
*/
export function isGeneratorFunction(target) {
return typeof target === 'function' && target.constructor.name === 'GeneratorFunction';
}
/**
* 是否为 Async 函数
* @param target 目标对象
*/
export function isAsyncFunction(target) {
return typeof target === 'function' && target.constructor.name === 'AsyncFunction';
}
/**
* 是否为 es module
* @param module 模块
*/
export function isESModule(module
// eslint-disable-next-line @typescript-eslint/naming-convention
) {
return !!module && _typeof(module) === 'object' && module.__esModule === true && 'default' in module;
}