UNPKG

@schema-render/core-react

Version:

Through a set of simple JSON Schema, efficiently build a set of forms.

78 lines (77 loc) 2.27 kB
const funcToString = Function.prototype.toString; const objectCtorString = funcToString.call(Object); /** * 类型检测 * @param value 目标值 * @param type 预期类型 */ export function typeChecking(value, type) { // 直接使用 toString.call(value) 在 ie 会下报错 return Object.prototype.toString.call(value) === type; } /** * 检测 value 是否为函数 */ export function isFunction(value) { return typeChecking(value, '[object Function]') || typeChecking(value, '[object AsyncFunction]'); } /** * 检测 value 是否为数组 */ export function isArray(value) { return Array.isArray(value); } /** * 检测 value 是否为 object 类型 * * isObject({}) => true * isObject([]) => true * isObject(Function) => true * isObject(null) => false */ export function isObject(value) { const type = typeof value; return value !== null && (type === 'object' || type === 'function'); } /** * 检测 value 是否为纯对象,即 {} 或 new Object() 创建的对象 * 参见 https://lodash.com/docs/4.17.15#isPlainObject */ export function isPlainObject(value) { if (!typeChecking(value, '[object Object]')) { return false; } // 过滤 Object.create(null) const proto = Object.getPrototypeOf(value); if (proto === null) { return true; } // 过滤 Object.create({}) 与 new Foo() const Ctor = Object.hasOwnProperty.call(proto, 'constructor') && proto.constructor; return typeof Ctor === 'function' && Ctor instanceof Ctor && funcToString.call(Ctor) === objectCtorString; } /** * 检测 value 是否为字符串 */ export function isString(value) { return typeof value === 'string'; } /** * 检测 value 是否为数值 */ export function isNumber(value) { return typeof value === 'number'; } /** * 检测 value 是否为布尔值 */ export function isBoolean(value) { return typeof value === 'boolean'; } /** * 检测 value 是否为 Undefined */ export function isUndefined(value) { return value === undefined; } /** * 检测 value 是否为 Null */ export function isNull(value) { return value === null; } /** * 检测 value 是否为 Undefined 或者 Null */ export function isNil(value) { return isUndefined(value) || isNull(value); }