Created
June 22, 2019 23:24
-
-
Save offbynull/2fa94d51cd99a71e85333400982a2acb to your computer and use it in GitHub Desktop.
An easy way to do automated equals / hashCode for sets and maps in collection-js and immutable-js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Easy doesn't mean fast. See immutable-js docs for how to implement hashCode | |
| // and equals (see ValueObject -- you just add in the methods to your | |
| // class/object, no TS interfaces to implement). | |
| import { MD5 } from 'object-hash'; | |
| import deepEqual from 'deep-equal'; | |
| const o1 = { | |
| a: 'hi', | |
| b: 4, | |
| c: [1, 2, 3], | |
| d: { | |
| da: 'hi', | |
| db: 'bye' | |
| } | |
| }; | |
| const o2 = { | |
| a: 'hi', | |
| b: 4, | |
| c: [1, 2, 3], | |
| d: { | |
| da: 'hi', | |
| db: 'bye' | |
| } | |
| }; | |
| const hashNum1 = parseInt('0x' + MD5(o1).substring(0, 8), 16) | 0; | |
| const hashNum2 = parseInt('0x' + MD5(o2).substring(0, 8), 16) | 0; | |
| console.log(hashNum1); | |
| console.log(hashNum2); | |
| console.log(MD5(o1)); | |
| console.log(MD5(o2)); | |
| console.log(deepEqual(o1, o2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment