码迷,mamicode.com
首页 > Web开发 > 详细

js函数 eql,equal,equalp

时间:2018-06-01 23:17:33      阅读:682      评论:0      收藏:0      [点我收藏+]

标签:empty   names   hello   ||   from   name   import   array   property   

如果你喜欢common lisp 的这3个函数真是太好了。

function eql(obj, other) {
    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; // 类型一样的空集合,永远相等。
        for (var k in obj) {
            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; // 类型一样的空集合,永远相等。


        for (const k in obj) {
            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'))

js函数 eql,equal,equalp

标签:empty   names   hello   ||   from   name   import   array   property   

原文地址:https://www.cnblogs.com/ajanuw/p/9123787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!