(目录)
typeof 检测数据类型
console.log(typeof 100); //"number"
console.log(typeof "abc"); //"string"
console.log(typeof false); //"boolean"
console.log(typeof undefined); //"undefined"
console.log(typeof function () {}); //"function"
console.log(typeof null); //"object"
console.log(typeof [1, 2, 3]); //"object"
console.log(typeof { a: 1, b: 2, c: 3 }); //"object"
console.log(typeof new Date()); //"object"
console.log(typeof /^[a-zA-Z]{5,20}$/); //"object"
console.log(typeof new RegExp('\d+')); //"object"
console.log(typeof new Error()); //"object"
console.log(typeof new Number(100)); //'object'
console.log(typeof new String("abc")); // object
console.log(typeof new Boolean(true)); // object
toString 检测数据类型
var toString = Object.prototype.toString
console.log(toString.call(123)); //"[object Number]"
console.log(toString.call(new Number(100))); // [object Number]
console.log(toString.call('abc')); //"[object String]"
console.log(toString.call(new String("abc"))); // [object String]
console.log(toString.call(true)); //"[object Boolean]"
console.log(toString.call(new Boolean(true))); // [object Boolean]
console.log(toString.call(undefined)); //"[object Undefined]"
console.log(toString.call(null)); //"[object Null]"
console.log(toString.call(function(){ })); //"[object Function]"
console.log(toString.call([1, 2, 3, 4])); //"[object Array]"
console.log(toString.call({name:'wenzi', age:25})); //"[object Object]"
console.log(toString.call(new Date())); //"[object Date]"
console.log(toString.call(/^[a-zA-Z]{5,20}$/)); //"[object RegExp]"
console.log(toString.call(new RegExp('\d+'))); // [object RegExp]
console.log(toString.call(new Error())); //"[object Error]"
工具方法
type-util.ts
/**
* 类型枚举值
*/
export const typeEnum = {
NUMBER: "number",
STRING: "string",
BOOLEAN: "boolean",
ARRAY: "array",
OBJECT: "object",
FUNCTION: "function",
UNDEFINED: "undefined",
NULL: "null",
DATE: "date",
REGEXP: "regexp",
ERROR: "error",
};
/**
* 获取对象的数据类型
* @param obj
* @returns string
*/
export function getType(obj: any): string {
// 如果不是object类型的数据,直接用typeof就能判断出来
const objType = typeof obj;
if (objType !== typeEnum.OBJECT) {
return objType;
}
// 如果是object类型数据,准确判断类型必须使用Object.prototype.toString.call(obj)的方式才能判断
return Object.prototype.toString
.call(obj)
.replace(/^\[object (\S+)\]$/, "$1")
.toLowerCase();
}
/**
* 判断是否为对象
* @param value
* @returns boolean
*/
export function isObject(value: any): boolean {
return getType(value) == typeEnum.OBJECT;
}
/**
* 判断是否是数组
* @param value
* @returns boolean
*/
export function isArray(value: any): boolean {
return Array.isArray(value);
}
/**
* 判断对象是否为空
* null {} [] "" 0 false
* @param {any} value
*/
export function isEmpty(value: any): boolean {
if (!value) {
// false null "" 0
return true;
} else if (isObject(value)) {
// 空对象 {}
return JSON.stringify(value) == "{}";
} else if (isArray(value)) {
// 空数组 []
return value.length == 0;
} else {
// 非空值
return false;
}
}
工具类测试用例
type-util.test.js
const { getType, isObject, isArray, isEmpty } = require("../src/type-util");
// test getType
test("getType", function () {
expect(getType(123)).toBe("number");
expect(getType(new Number(100))).toBe("number");
expect(getType("abc")).toBe("string");
expect(getType(new String("abc"))).toBe("string");
expect(getType(true)).toBe("boolean");
expect(getType(false)).toBe("boolean");
expect(getType(new Boolean(true))).toBe("boolean");
expect(getType(undefined)).toBe("undefined");
expect(getType(null)).toBe("null");
expect(getType(function () {})).toBe("function");
expect(getType({})).toBe("object");
expect(getType({ name: "tom", age: 25 })).toBe("object");
expect(getType([])).toBe("array");
expect(getType([1, 2, 3, 4])).toBe("array");
expect(getType(new Date())).toBe("date");
expect(getType(/^[a-zA-Z]{5,20}$/)).toBe("regexp");
expect(getType(new RegExp("d+"))).toBe("regexp");
expect(getType(new Error())).toBe("error");
});
// test isObject
test("isObject", function () {
expect(isObject(null)).toBe(false);
expect(isObject({})).toBe(true);
});
// test isArray
test("isArray", function () {
expect(isArray(null)).toBe(false);
expect(isArray({})).toBe(false);
expect(isArray([])).toBe(true);
});
// test isEmpty
test("isEmpty", function () {
expect(isEmpty(null)).toBe(true);
expect(isEmpty(undefined)).toBe(true);
expect(isEmpty(false)).toBe(true);
expect(isEmpty({})).toBe(true);
expect(isEmpty([])).toBe(true);
expect(isEmpty(0)).toBe(true);
expect(isEmpty('')).toBe(true);
expect(isEmpty([1])).toBe(false);
expect(isEmpty({ name: "tom" })).toBe(false);
expect(isEmpty('hello')).toBe(false);
expect(isEmpty(true)).toBe(false);
expect(isEmpty(100)).toBe(false);
});