From 8165005c5c198f7738c185a3d97b7e4ce98f76a8 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Sat, 8 Dec 2018 19:44:41 +0000 Subject: [PATCH] refactor(equiv): minor update (intern string consts) --- packages/equiv/src/index.ts | 137 +++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 65 deletions(-) diff --git a/packages/equiv/src/index.ts b/packages/equiv/src/index.ts index 0ef0a336bf..30e0ba41ab 100644 --- a/packages/equiv/src/index.ts +++ b/packages/equiv/src/index.ts @@ -1,75 +1,82 @@ const OBJP = Object.getPrototypeOf({}); +const FN = "function"; +const STR = "string"; -export const equiv = (a, b): boolean => { - let proto; - if (a === b) { - return true; - } - if (a != null) { - if (typeof a.equiv === "function") { - return a.equiv(b); +export const equiv = + (a: any, b: any): boolean => { + let proto; + if (a === b) { + return true; + } + if (a != null) { + if (typeof a.equiv === FN) { + return a.equiv(b); + } + } else { + return a == b; + } + if (b != null) { + if (typeof b.equiv === FN) { + return b.equiv(a); + } + } else { + return a == b; + } + if (typeof a === STR || typeof b === STR) { + return false; + } + if ((proto = Object.getPrototypeOf(a), proto == null || proto === OBJP) && + (proto = Object.getPrototypeOf(b), proto == null || proto === OBJP)) { + return equivObject(a, b); + } + if (typeof a !== FN && a.length !== undefined && + typeof b !== FN && b.length !== undefined) { + return equivArrayLike(a, b); } - } else { - return a == b; - } - if (b != null) { - if (typeof b.equiv === "function") { - return b.equiv(a); + if (a instanceof Set && b instanceof Set) { + return equivSet(a, b); } - } else { - return a == b; - } - if (typeof a === "string" || typeof b === "string") { - return false; - } - if ((proto = Object.getPrototypeOf(a), proto == null || proto === OBJP) && - (proto = Object.getPrototypeOf(b), proto == null || proto === OBJP)) { - return equivObject(a, b); - } - if (typeof a !== "function" && a.length !== undefined && - typeof b !== "function" && b.length !== undefined) { - return equivArrayLike(a, b); - } - if (a instanceof Set && b instanceof Set) { - return equivSet(a, b); - } - if (a instanceof Map && b instanceof Map) { - return equivMap(a, b); - } - if (a instanceof Date && b instanceof Date) { - return a.getTime() === b.getTime(); - } - if (a instanceof RegExp && b instanceof RegExp) { - return a.toString() === b.toString(); - } - // NaN - return (a !== a && b !== b); -}; + if (a instanceof Map && b instanceof Map) { + return equivMap(a, b); + } + if (a instanceof Date && b instanceof Date) { + return a.getTime() === b.getTime(); + } + if (a instanceof RegExp && b instanceof RegExp) { + return a.toString() === b.toString(); + } + // NaN + return (a !== a && b !== b); + }; -export const equivArrayLike = (a: ArrayLike, b: ArrayLike, _equiv = equiv) => { - let l = a.length; - if (l === b.length) { - while (--l >= 0 && _equiv(a[l], b[l])); - } - return l < 0; -}; +export const equivArrayLike = + (a: ArrayLike, b: ArrayLike, _equiv = equiv) => { + let l = a.length; + if (l === b.length) { + while (--l >= 0 && _equiv(a[l], b[l])); + } + return l < 0; + }; -export const equivSet = (a: Set, b: Set, _equiv = equiv) => - (a.size === b.size) && - _equiv([...a.keys()].sort(), [...b.keys()].sort()); +export const equivSet = + (a: Set, b: Set, _equiv = equiv) => + (a.size === b.size) && + _equiv([...a.keys()].sort(), [...b.keys()].sort()); -export const equivMap = (a: Map, b: Map, _equiv = equiv) => - (a.size === b.size) && - _equiv([...a].sort(), [...b].sort()); +export const equivMap = + (a: Map, b: Map, _equiv = equiv) => + (a.size === b.size) && + _equiv([...a].sort(), [...b].sort()); -export const equivObject = (a: any, b: any, _equiv = equiv) => { - if (Object.keys(a).length !== Object.keys(b).length) { - return false; - } - for (let k in a) { - if (!b.hasOwnProperty(k) || !_equiv(a[k], b[k])) { +export const equivObject = + (a: any, b: any, _equiv = equiv) => { + if (Object.keys(a).length !== Object.keys(b).length) { return false; } - } - return true; -}; + for (let k in a) { + if (!b.hasOwnProperty(k) || !_equiv(a[k], b[k])) { + return false; + } + } + return true; + };