|
1 | | -# deep-equality-data-structures |
2 | | -Javascript data structures (e.g., Map, Set) that support deep object equality |
| 1 | +# Deep Equality Javascript Data Structures |
| 2 | + |
| 3 | +A drop-in replacement for ES native `Map` and `Set` with deep equality support for objects. |
| 4 | + |
| 5 | +## Why? |
| 6 | + |
| 7 | +ES `Map` and `Set` only support referential equality: |
| 8 | + |
| 9 | +```typescript |
| 10 | +interface MyObject { |
| 11 | + a: number; |
| 12 | +} |
| 13 | +const set = new Set<MyObject>(); |
| 14 | +set.add({ a: 1 }); |
| 15 | +set.add({ a: 1 }); |
| 16 | +set.size; // 2 |
| 17 | +``` |
| 18 | + |
| 19 | +Now, using deep equality: |
| 20 | + |
| 21 | +```typescript |
| 22 | +import { DeepSet } from '@adamhamlin/deep-equality-data-strucures'; |
| 23 | + |
| 24 | +interface MyObject { |
| 25 | + a: number; |
| 26 | +} |
| 27 | +const set = new DeepSet<MyObject>(); |
| 28 | +set.add({ a: 1 }); |
| 29 | +set.add({ a: 1 }); |
| 30 | +set.size; // 1 |
| 31 | +``` |
| 32 | + |
| 33 | +## How? |
| 34 | + |
| 35 | +This project relies on the [object-hash](https://github.com/puleos/object-hash) library to map object types to strings. |
| 36 | + |
| 37 | +## Comparable Interface |
| 38 | + |
| 39 | +Equality and subset comparisons are supported: |
| 40 | + |
| 41 | +```typescript |
| 42 | +const set1 = new DeepSet([{ a: 1 }, { b: 2 }]); |
| 43 | +const set2 = new DeepSet([{ a: 1 }, { b: 2 }]); |
| 44 | +set1.equals(set2); // true |
| 45 | + |
| 46 | +const set3 = new DeepSet([{ a: 1 }]); |
| 47 | +set1.equals(set3); // false |
| 48 | +set1.contains(set3); // true |
| 49 | +``` |
| 50 | + |
| 51 | +## Configuration Options |
| 52 | + |
| 53 | +```typescript |
| 54 | +new DeepSet(values?, options?) |
| 55 | +new DeepMap(entries?, options?) |
| 56 | +``` |
| 57 | +
|
| 58 | +The `options` argument is a superset of the options defined for [object-hash](https://github.com/puleos/object-hash#hashvalue-options), with the same defaults (exception: the default algoritm is `md5`). |
| 59 | +
|
| 60 | +Additional project-specific options: |
| 61 | +
|
| 62 | +- `jsonSerializableOnly` - if true, only use JSON-serializable properties when computing hashes, equality, etc. (default: false) |
| 63 | +
|
| 64 | + ```typescript |
| 65 | + class A { |
| 66 | + constructor(public x: number) {} |
| 67 | + } |
| 68 | + class B { |
| 69 | + constructor(public x: number) {} |
| 70 | + } |
| 71 | + const b = new B(45); |
| 72 | + const c = new C(45); |
| 73 | + |
| 74 | + const set = new DeepSet([b, c]); |
| 75 | + set.size; // 2 |
| 76 | + const set = new DeepSet([b, c], { jsonSerializableOnly: true }); |
| 77 | + set.size; // 1 |
| 78 | + ``` |
| 79 | +
|
| 80 | +## Notes/Caveats |
| 81 | +
|
| 82 | +- Don't mutate a map key (or set value) while still using the data structure. The internal representation is not affected by this mutation, so behavior may be unexpected. |
| 83 | +- This implementation does not explicitly "handle" key collisions. However, with the default algorithm (MD5), even if a map contained one TRILLION entries, the probability of a collision on the next insert is only 0.000000000000001. If you need better odds, use SHA1, SHA256, etc. |
0 commit comments