js函数 eql,equal,equalp

Posted Ajanuw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js函数 eql,equal,equalp相关的知识,希望对你有一定的参考价值。

如果你喜欢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的主要内容,如果未能解决你的问题,请参考以下文章

js简洁代码片段

eql框架。

eql 框架

js代码片段: utils/lcoalStorage/cookie

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

chai 测试数组相等性没有按预期工作