JS常用类型判断工具 2

        // 2.6 迭代
        isGeneratorFunction: function(val) { // 生成器函数 判断
            return this.getType(val) === "GeneratorFunction" || (this.isFunction(val) && val.constructor && val.constructor.name === "GeneratorFunction")
        },

        isGeneratorObject: function(val) { // 生成器对象 判断
            return this.getType(val) === "Generator" || 
                (this.isObject(val) && 
                    typeof val.next === "function" && 
                    typeof val.throw === "function" && 
                    typeof val.return === "function" && 
                    this.isIterable(val)
                )
        },

        isIterator: function(val) { // 迭代器 判断
            return val != null && typeof val.next === "function"
        },

        isIterable: function(val) { // 可迭代对象 判断
            if (val == null) return false;
            if (this.isArray(val) || this.isString(val) || this.isMap(val) || this.isSet(val) || this.isGeneratorObject(val) || this.isTypedArray(val)) {return true};
            return typeof val[Symbol.iterator] === "function"
        },

        isIteratorResult: function(val) { // 迭代结果 判断
            return this.isObject(val) && 
                   'value' in val && 
                   'done' in val && 
                   typeof val.done === "boolean"
        },

        // 2.7 Reflect
        isReflect: function(val) {
            if (this.getType(val) === "Reflect") return true;
            if (!this.isObject(val)) return false;
            const reflectMethods = [
                'apply', 'construct', 'defineProperty', 'deleteProperty',
                'get', 'getOwnPropertyDescriptor', 'getPrototypeOf',
                'has', 'isExtensible', 'ownKeys', 'preventExtensions',
                'set', 'setPrototypeOf'
            ];
            return reflectMethods.every(method => typeof val[method] === "function");
        },
        
        // 2.8 Proxy
        isProxy: function(val) { // Proxy实例判断。创建Proxy实例时,handler对象的get函数中有语句 “ if (propKey === "isProxy") {return true}; ” 时可用。
            if (typeof Proxy === 'undefined') return false;
            if(val.isProxy) return true;
            return false
        },
        
        isProxyHandler: function(val) { // Proxy处理器对象判断
            if (!this.isObject(val)) return false;
            const proxyTraps = [
                'get', 'set', 'has', 'deleteProperty', 'apply', 'construct',
                'getPrototypeOf', 'setPrototypeOf', 'isExtensible', 'preventExtensions',
                'getOwnPropertyDescriptor', 'defineProperty', 'ownKeys'
            ];
            return proxyTraps.some(trap => typeof val[trap] === "function")
        },

        isRevocableProxy: function(val) { // 撤销的Proxy实例 判断
            try {
                if (!this.isProxy(val)) return false;
                Object.getOwnPropertyNames(val);
                return false
            } catch (e) {
                return e.message && e.message.includes('revoked')
            }
        },

        // 2.9 参数对象
        isArguments: function(val) {
            return this.getType(val) === "Arguments" || (this.isObject(val) && 'callee' in val && typeof val.callee === "function")
        },
        
        // 2.10 全局对象
        isGlobalThis: function(val) {
            return typeof globalThis !== 'undefined' && val === globalThis
        },
        
        // 2.11 JSON字符串
        isJSON: function(val) {
            if (!this.isString(val)) return false;
            try {
                JSON.parse(val);
                return true
            } catch (e) {
                return false
            }
        },
        
        // 2.12 空值
        isEmpty: function(val) {
            if (this.isNumber(val) || this.isBoolean(val)) return false;
            if (this.isNull(val) || this.isUndefined(val)) return true;
            if (this.isString(val) || this.isArray(val) || this.isTypedArray(val)) return val.length === 0;
            if (this.isObject(val)) return Object.keys(val).length === 0;
            if (this.isMap(val) || this.isSet(val)) return val.size === 0;
            if (this.isArrayBuffer(val)) return val.byteLength === 0;
            return false
        },
云南省
浏览 101
收藏
点赞
分享
+1
+1
全部评论