node.js类型检查

这里提供了一些node.js类型检查的方法

function isArray(ar) {
    return Array.isArray(ar);
}
exports.isArray = isArray;

function isBoolean(arg) {
    return typeof arg === 'boolean';
}
exports.isBoolean = isBoolean;

function isNull(arg) {
    return arg === null;
}
exports.isNull = isNull;

function isNullOrUndefined(arg) {
    return arg == null;
}
exports.isNullOrUndefined = isNullOrUndefined;

function isNumber(arg) {
    return typeof arg === 'number';
}
exports.isNumber = isNumber;

function isString(arg) {
    return typeof arg === 'string';
}
exports.isString = isString;

function isSymbol(arg) {
    return typeof arg === 'symbol';
}
exports.isSymbol = isSymbol;

function isUndefined(arg) {
    return arg === void 0;
}
exports.isUndefined = isUndefined;

function isRegExp(re) {
    return isObject(re) && objectToString(re) === '[object RegExp]';
}
exports.isRegExp = isRegExp;

function isObject(arg) {
    return typeof arg === 'object' && arg !== null;
}
exports.isObject = isObject;

function isDate(d) {
    return isObject(d) && objectToString(d) === '[object Date]';
}
exports.isDate = isDate;

function isError(e) {
    return isObject(e) && objectToString(e) === '[object Error]';
}
exports.isError = isError;

function isFunction(arg) {
    return typeof arg === 'function';
}
exports.isFunction = isFunction;

function isPrimitive(arg) {
    return arg === null ||
        typeof arg === 'boolean' ||
        typeof arg === 'number' ||
        typeof arg === 'string' ||
        typeof arg === 'symbol' ||  // ES6 symbol
        typeof arg === 'undefined';
}
exports.isPrimitive = isPrimitive;

function isBuffer(arg) {
    return arg instanceof Buffer;
}
exports.isBuffer = isBuffer;

function objectToString(o) {
    return Object.prototype.toString.call(o);
}

 

posted @ 2014-03-25 13:56  邪邪  阅读(636)  评论(0编辑  收藏  举报