zoukankan      html  css  js  c++  java
  • js函数 eql,equal,equalp

    function eql(obj, other) {
            if(stringp(obj) && stringp(other) && obj === other) return false;
            return obj === other;
        }
    
    function equal(obj, other, equalp) {
        if (equalp === void 0) { equalp = false; }
        var _tostring = function (value) { return Object.prototype.toString.call(value); };
        var emptyp = function (value) {
            return JSON.stringify(value).length === 2 ? true : false;
        };
        function Equal(obj, other, equalp) {
            var objTag = _tostring(obj);
            var otherTag = _tostring(other);
            var objectTag = '[object Object]';
            var arrayTag = '[object Array]';
            if (objTag !== objectTag && objTag !== arrayTag && otherTag !== objectTag && otherTag !== arrayTag) {
                if (equalp && typeof obj === 'string' && typeof other === 'string') {
                    return (obj).toLocaleUpperCase() === (other).toLocaleUpperCase();
                }
                return obj === other;
            }
            if (objTag !== otherTag)
                return false; // 集合类型不一样
            if (Object.getOwnPropertyNames(obj).length !== Object.getOwnPropertyNames(other).length)
                return false; // 集合元素数量不一样
            if (emptyp(obj) && emptyp(other))
                return true; // 类型一样的空集合,永远相等。
            var data = (function () {
                var data = Object.getOwnPropertyNames(obj);
                if (objTag === arrayTag) {
                    data.pop();
                    return data;
                }
                else {
                    return data;
                }
            })();
            for (var i in data) {
                var k = data[i];
                if (k in other) { // 元素是否相交
                    var obj_value = obj[k];
                    var other_value = other[k];
                    var obj_item_tag = _tostring(obj_value);
                    var other_item_tag = _tostring(other_value);
                    if (obj_item_tag === other_item_tag) {
                        if (obj_item_tag === objectTag || obj_item_tag === arrayTag || other_item_tag === objectTag || other_item_tag === arrayTag) {
                            return Equal(obj_value, other_value, equalp);
                        }
                        else {
                            if (obj_value === other_value) {
                                console_1.log('done.');
                            }
                            else {
                                return false;
                            }
                        }
                    }
                    else {
                        return false;
                    }
                }
                else {
                    return false;
                }
            }
            return true;
        }
        return Equal(obj, other, equalp);
    }
    
    function equalp(obj, other) {
        return equal(obj, other, true);
    }
    

    调试

    import {
        log as l
    } from 'console';
    
    
    function eql(obj: any, other: any) {
        return obj === other;
    }
    
    function equal(obj: any, other: any, equalp: boolean = false) {
        const _tostring = (value: any): string => Object.prototype.toString.call(value);
        const emptyp = function (value: any) {
            return JSON.stringify(value).length === 2 ? true : false;
        }
    
        function Equal(obj: any, other: any, equalp: boolean): boolean {
            let objTag = _tostring(obj);
            let otherTag = _tostring(other);
            let objectTag = '[object Object]'
            let arrayTag = '[object Array]'
    
            if (objTag !== objectTag && objTag !== arrayTag && otherTag !== objectTag && otherTag !== arrayTag) {
                if (equalp && typeof obj === 'string' && typeof other === 'string') {
                    return (obj).toLocaleUpperCase() === (other).toLocaleUpperCase();
                }
                return obj === other;
            }
            if (objTag !== otherTag) return false;// 集合类型不一样
            if (Object.getOwnPropertyNames(obj).length !== Object.getOwnPropertyNames(other).length) return false; // 集合元素数量不一样
            if (emptyp(obj) && emptyp(other)) return true; // 类型一样的空集合,永远相等。
    
    
            let data: any[] = (function () {
                let data = Object.getOwnPropertyNames(obj);
                if (objTag === arrayTag) {
                    data.pop()
                    return data
                } else {
                    return data
                }
            })()
    
            for (const i in data) {
                const k = data[i];
                if (k in other) { // 元素是否相交
                    let obj_value = obj[k];
                    let other_value = other[k];
                    let obj_item_tag = _tostring(obj_value);
                    let other_item_tag = _tostring(other_value);
    
                    if (obj_item_tag === other_item_tag) {
                        if (obj_item_tag === objectTag || obj_item_tag === arrayTag || other_item_tag === objectTag || other_item_tag === arrayTag) {
                            return Equal(obj_value, other_value, equalp);
                        } else {
                            if (obj_value === other_value) {
                                l('done.');
                            } else {
                                return false;
                            }
                        }
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
    
            return true;
        }
    
        return Equal(obj, other, equalp)
    }
    
    function equalp(obj: any, other: any) {
        return equal(obj, other, true);
    }
    l(equalp('hello', 'HELLo'))
    
    function equal(obj, other) {
      const objectTag = "[object Object]";
      const arrayTag = "[object Array]";
      const _tostring = value => Object.prototype.toString.call(value);
      const emptyp = value => JSON.stringify(value).length === 2;
      // 记录所有的对象
      function Equal(obj, other) {
        let objTag = _tostring(obj);
        let otherTag = _tostring(other);
    
        // 非集合,使用===判断
        if (
          objTag !== objectTag &&
          objTag !== arrayTag &&
          otherTag !== objectTag &&
          otherTag !== arrayTag
        ) {
          return obj === other;
        }
    
        // 集合类型不一样
        if (objTag !== otherTag) return false;
    
        // 集合元素数量不一样
        if (
          Object.getOwnPropertyNames(obj).length !==
          Object.getOwnPropertyNames(other).length
        )
          return false;
    
        // 类型一样的空集合,永远相等。
        if (emptyp(obj) && emptyp(other)) return true;
    
        let rsult = false;
        for (const k in obj) {
          if (k in other) {
            const obj_value = obj[k];
            const other_value = other[k];
            rsult = Equal(obj_value, other_value);
          } else {
            return false;
          }
        }
        return rsult;
      }
    
      return Equal(obj, other);
    }
    
  • 相关阅读:
    休息一下
    把细节放大街上好孕妇有好多
    在银行钱是这样取的
    Word2003表格的AutoFormatType和Style的兼容问题
    [zz]Boost智能指针——shared_ptr
    thrift 安装运行错误 解决
    [zz]boost/shared_ptr 用法总结
    [zz]理解复杂的C/C++声明 const, typedef , 函数指针(转贴)
    [zz]使用thrift做c++,java和python的相互调用
    [zz]Apache Thrift学习小记
  • 原文地址:https://www.cnblogs.com/ajanuw/p/9123787.html
Copyright © 2011-2022 走看看