You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18-3
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,7 @@ The following supplemental comparisons/methods are included:
56
56
```typescript
57
57
// COMPARISONS
58
58
const set1 =newDeepSet([{ a: 1 }, { b: 2 }]);
59
-
const set2 =newDeepSet([{ a: 1 }, { b: 2 }]);
59
+
const set2 =newDeepSet([{ b: 2 }, { a: 1 }]);
60
60
set1.equals(set2); // true
61
61
62
62
const set3 =newDeepSet([{ a: 1 }]);
@@ -100,7 +100,7 @@ The `options` argument is a superset of the options defined for [object-hash](ht
100
100
[...set.values()]; // [{ val: 1, other: 2 }]
101
101
```
102
102
103
-
- `mapValueTransformer` - a custom function that transforms Map values prior to hashing. This is only relevant to the `.equals` and `.contains` operations from the `Comparable` interface. It does not affect the values that are stored.
103
+
- `mapValueTransformer` - a custom function that transforms Map values prior to hashing. This is only relevant to the `.equals`/`.contains` operations from the [Comparable interface](#comparable-interface), as well as the [Bi-Directional DeepMap](#bi-directional-deepmap). It does not affect the values that are stored.
104
104
105
105
```typescript
106
106
typeMyType= { val: number; other: number };
@@ -140,9 +140,24 @@ The `options` argument is a superset of the options defined for [object-hash](ht
140
140
set.size; // 1
141
141
```
142
142
143
+
## Bi-Directional DeepMap
144
+
145
+
This library also exposes a `BiDirectionalDeepMap` class, which supports O(1) lookups by both keys and values. It provides the following extended API:
146
+
147
+
- _`hasValue(val: V): boolean`_: Returns true if `val` exists as a value in the map
148
+
- _`getKeyByValue(val: V): K|undefined`_: Returns the key associated with `val` if it exists
149
+
- _`deleteByValue(val: V): boolean`_: Removes the key-value pair whose value is `val` and returns true if found
150
+
151
+
### Caveats
152
+
153
+
Note that this "two-way" map has the traditional caveats:
154
+
155
+
- There is a ~2x memory footprint
156
+
- Keys and values must be 1-to-1, meaning each key must have a distinct value and vice versa. This implementation will error if attempting to set a key-value pair whose _value_ is already present in the map with a different _key_.
157
+
143
158
## Static Utility Methods
144
159
145
-
- _`areEqual(values, options?)`_: Returns `true` if all elements in `values` are equal. This can be useful when you need to quickly
160
+
- _`areEqual(values, options?)`_: Returns true if all elements in `values` are equal. This can be useful when you need to quickly
146
161
test equality of more than 2 values, or when you want to specify an equality transform (via `options.transformer`).
0 commit comments