/
equiv-map.ts
195 lines (171 loc) · 4.77 KB
/
equiv-map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {
Fn3,
ICopy,
IEmpty,
IEquiv,
IObjectOf,
Pair,
SEMAPHORE,
} from "@thi.ng/api";
import { isPlainObject } from "@thi.ng/checks";
import { equiv } from "@thi.ng/equiv";
import { pairs } from "@thi.ng/transducers";
import type { EquivMapOpts, IEquivSet } from "./api";
import { ArraySet } from "./array-set";
import { dissoc } from "./dissoc";
import { equivMap } from "./internal/equiv";
import { inspectable } from "./internal/inspect";
import { into } from "./into";
interface MapProps<K, V> {
keys: IEquivSet<K>;
map: Map<K, V>;
opts: EquivMapOpts<K>;
}
const __private = new WeakMap<EquivMap<any, any>, MapProps<any, any>>();
const __map = (map: EquivMap<any, any>) => __private.get(map)!.map;
@inspectable
export class EquivMap<K, V>
extends Map<K, V>
implements
Iterable<Pair<K, V>>,
ICopy<EquivMap<K, V>>,
IEmpty<EquivMap<K, V>>,
IEquiv {
/**
* Creates a new instance with optional initial key-value pairs and
* provided options. If no `opts` are given, uses `ArraySet` for
* storing canonical keys and {@link @thi.ng/equiv#equiv} for
* checking key equivalence.
*
* @param pairs - key-value pairs
* @param opts - config options
*/
constructor(
pairs?: Iterable<Pair<K, V>> | null,
opts?: Partial<EquivMapOpts<K>>
) {
super();
const _opts: EquivMapOpts<K> = { equiv, keys: ArraySet, ...opts };
__private.set(this, {
keys: new _opts.keys(null, { equiv: _opts.equiv }),
map: new Map<K, V>(),
opts: _opts,
});
if (pairs) {
this.into(pairs);
}
}
[Symbol.iterator](): IterableIterator<Pair<K, V>> {
return this.entries();
}
get [Symbol.species]() {
return EquivMap;
}
get [Symbol.toStringTag]() {
return "EquivMap";
}
get size(): number {
return __private.get(this)!.keys.size;
}
clear() {
const $this = __private.get(this)!;
$this.keys.clear();
$this.map.clear();
}
empty(): EquivMap<K, V> {
return new EquivMap<K, V>(null, __private.get(this)!.opts);
}
copy() {
const $this = __private.get(this)!;
const m = new EquivMap<K, V>();
__private.set(m, {
keys: $this.keys.copy(),
map: new Map<K, V>($this.map),
opts: $this.opts,
});
return m;
}
equiv(o: any) {
return equivMap(this, o);
}
delete(key: K) {
const $this = __private.get(this)!;
key = $this.keys.get(key, SEMAPHORE);
if (key !== <any>SEMAPHORE) {
$this.map.delete(key);
$this.keys.delete(key);
return true;
}
return false;
}
dissoc(keys: Iterable<K>) {
return <this>dissoc(this, keys);
}
/**
* The key & value args given the callback `fn` MUST be treated as
* readonly/immutable. This could be enforced via TS, but would
* break ES6 Map interface contract.
*
* @param fn
* @param thisArg
*/
forEach(fn: Fn3<V, K, Map<K, V>, void>, thisArg?: any) {
for (let pair of __map(this)) {
fn.call(thisArg, pair[1], pair[0], this);
}
}
get(key: K, notFound?: V): V | undefined {
const $this = __private.get(this)!;
key = $this.keys.get(key, SEMAPHORE);
if (key !== <any>SEMAPHORE) {
return $this.map.get(key);
}
return notFound;
}
has(key: K): boolean {
return __private.get(this)!.keys.has(key);
}
set(key: K, value: V) {
const $this = __private.get(this)!;
const k = $this.keys.get(key, SEMAPHORE);
if (k !== <any>SEMAPHORE) {
$this.map.set(k, value);
} else {
$this.keys.add(key);
$this.map.set(key, value);
}
return this;
}
into(pairs: Iterable<Pair<K, V>>) {
return <this>into(this, pairs);
}
entries(): IterableIterator<Pair<K, V>> {
return __map(this).entries();
}
keys(): IterableIterator<K> {
return __map(this).keys();
}
values(): IterableIterator<V> {
return __map(this).values();
}
opts(): EquivMapOpts<K> {
return __private.get(this)!.opts;
}
}
export function defEquivMap<K, V>(
pairs?: Iterable<Pair<K, V>> | null,
opts?: Partial<EquivMapOpts<K>>
): EquivMap<K, V>;
export function defEquivMap<V>(
obj: IObjectOf<V>,
opts?: Partial<EquivMapOpts<string>>
): EquivMap<string, V>;
export function defEquivMap<V>(
src: any,
opts?: Partial<EquivMapOpts<any>>
): EquivMap<any, V> {
return new EquivMap(
isPlainObject(src) ? pairs(<IObjectOf<V>>src) : src,
opts
);
}