分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

js函数 eql,equal,equalp

发布时间:2023-09-06 01:57责任编辑:胡小海关键词:js

如果你喜欢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

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved