diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-06-15 22:44:54 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-06-15 22:44:54 +0200 |
commit | 2ee9431f1ba5bf67546bbf85758a01991c40673f (patch) | |
tree | 4581c4f3c966d742c66ea7f4bae4f9a3f8e2f5ff /packages/idb-bridge/src/util/deepEquals.ts | |
parent | 65eb8b96f894491d406f91070df53ccbd43d19c9 (diff) | |
download | wallet-core-2ee9431f1ba5bf67546bbf85758a01991c40673f.tar.xz |
idb wip
Diffstat (limited to 'packages/idb-bridge/src/util/deepEquals.ts')
-rw-r--r-- | packages/idb-bridge/src/util/deepEquals.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/packages/idb-bridge/src/util/deepEquals.ts b/packages/idb-bridge/src/util/deepEquals.ts new file mode 100644 index 000000000..8d05ad21e --- /dev/null +++ b/packages/idb-bridge/src/util/deepEquals.ts @@ -0,0 +1,75 @@ +/* +Copyright (c) 2017 Evgeny Poberezkin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + + + + +const isArray = Array.isArray; +const keyList = Object.keys; +const hasProp = Object.prototype.hasOwnProperty; + +function deepEquals(a: any, b: any): boolean { + if (a === b) return true; + + if (a && b && typeof a == "object" && typeof b == "object") { + const arrA = isArray(a); + const arrB = isArray(b); + let i; + let length; + let key; + + if (arrA && arrB) { + length = a.length; + if (length != b.length) return false; + for (i = length; i-- !== 0; ) if (!deepEquals(a[i], b[i])) return false; + return true; + } + + if (arrA != arrB) return false; + + const dateA = a instanceof Date; + const dateB = b instanceof Date; + if (dateA != dateB) return false; + if (dateA && dateB) return a.getTime() == b.getTime(); + + const regexpA = a instanceof RegExp; + const regexpB = b instanceof RegExp; + if (regexpA != regexpB) return false; + if (regexpA && regexpB) return a.toString() == b.toString(); + + const keys = keyList(a); + length = keys.length; + + if (length !== keyList(b).length) return false; + + for (i = length; i-- !== 0; ) if (!hasProp.call(b, keys[i])) return false; + + for (i = length; i-- !== 0; ) { + key = keys[i]; + if (!deepEquals(a[key], b[key])) return false; + } + + return true; + } + + return a !== a && b !== b; +} |