• 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);
    }
  • 相关阅读:
    uva 442 Matrix Chain Multiplication
    结对编程项目之队友代码分析
    [转] 为什么要使用NoSQL
    Compare Linq2Sql with NHibernate
    使用linq2sql 的DetailView 如何保存多对多关系
    工作流入门
    XML字段的用处
    DesignTimeResourceProviderFactory 不给力啊
    如何给XMLDatasource做分页和排序
    ORM的烦恼
  • 原文地址:https://www.cnblogs.com/racyily/p/3622999.html
Copyright © 2020-2023  润新知