-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoll.mjs
249 lines (205 loc) · 6.28 KB
/
coll.mjs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import * as l from './lang.mjs'
export function bset(val) {return new Bset(val)}
export function bsetOf(...val) {return Bset.of(...val)}
export class Bset extends Set {
constructor(...val) {super().mut(...val)}
mut(val) {
if (l.isNil(val)) return this
if (l.isIter(val)) return this.mutFromIter(val)
throw l.errConvInst(val, this)
}
mutFromIter(src) {
for (const val of l.reqIter(src)) this.add(val)
return this
}
addOpt(val) {
if (l.isSome(val)) this.add(val)
return this
}
added(val) {return !this.has(val) && (this.add(val), true)}
reset(src) {return this.clear(), this.mut(src)}
clear() {return (super.size && super.clear()), this}
clone() {return new this.constructor(this)}
toArray() {return [...this.values()]}
toJSON() {return this.toArray()}
static of(...val) {return new this(val)}
static from(val) {return new this(l.optIter(val))}
}
// TODO doc.
export class TypedSet extends Bset {
reqVal() {throw l.errImpl()}
add(val) {return super.add(this.reqVal(val))}
}
export class ClsSet extends Bset {
get cls() {return Object}
add(val) {return super.add(this.make(val))}
make(val) {return l.toInst(val, this.cls)}
}
export function bmap(val) {return new Bmap(val)}
export function bmapOf(...val) {return Bmap.of(...val)}
export class Bmap extends Map {
constructor(...val) {super().mut(...val)}
mut(val) {
if (l.isNil(val)) return this
if (l.isIter(val)) return this.mutFromIter(val)
if (l.isStruct(val)) return this.mutFromStruct(val)
throw l.errConvInst(val, this)
}
mutFromIter(src) {
for (const [key, val] of l.reqIter(src)) this.set(key, val)
return this
}
mutFromStruct(val) {
for (const key of l.structKeys(val)) this.set(key, val[key])
return this
}
setOpt(key, val) {
if (l.isSome(key) && l.isSome(val)) this.set(key, val)
return this
}
setted(key, val) {return this.set(key, val).get(key)}
reset(src) {return this.clear(), this.mut(src)}
clear() {return (super.size && super.clear()), this}
clone() {return new this.constructor(this)}
toDict() {
const out = l.Emp()
for (const [key, val] of this.entries()) {
if (l.isStr(key)) out[key] = val
}
return out
}
toJSON() {return this.toDict()}
// Mirror of `iter.mjs` → `mapOf`.
static of(...val) {
const out = new this()
let ind = 0
while (ind < val.length) out.set(val[ind++], val[ind++])
return out
}
}
export class TypedMap extends Bmap {
reqKey() {throw l.errImpl()}
reqVal() {throw l.errImpl()}
set(key, val) {return super.set(this.reqKey(key), this.reqVal(val))}
}
/*
TODO better name. Restricts key type to strings, for compatibility with plain
dicts, without restricting value types.
*/
export class CompatMap extends TypedMap {
reqKey(key) {return l.reqStr(key)}
reqVal(val) {return val}
}
export class ClsMap extends TypedMap {
get cls() {return Object}
reqVal(val) {return l.toInst(val, this.cls)}
}
// Short for "primary key optional".
// TODO move to `lang.mjs`.
export function pkOpt(val) {return l.hasMeth(val, `pk`) ? val.pk() : undefined}
// Short for "primary key".
// TODO move to `lang.mjs`.
export function pk(val) {return reqPkOf(pkOpt(val), val)}
function reqPkOf(key, val) {
if (l.isPk(key)) return key
throw TypeError(`expected ${l.show(val)} to provide key, got ${l.show(key)}`)
}
export class Coll extends TypedMap {
mut(val) {
if (l.isSome(val)) for (val of l.reqIter(val)) this.add(val)
return this
}
getKey(val) {return reqPkOf(this.getKeyOpt(val), val)}
getKeyOpt(val) {return pkOpt(val)}
reqKey(key) {return l.reqPk(key)}
reqVal(val) {return val}
add(val) {return this.set(this.getKey(val), val)}
addOpt(val) {return this.setOpt(this.getKeyOpt(val), val)}
added(val) {
const key = this.getKey(val)
const got = this.has(key)
this.set(key, val)
return got
}
toArray() {return [...this.values()]}
toJSON() {return this.toArray()}
[Symbol.iterator]() {return this.values()}
}
export class ClsColl extends Coll {
get cls() {return Object}
reqVal(val) {return l.reqInst(val, this.cls)}
add(val) {return (val = this.make(val)), this.set(this.getKey(val), val)}
addOpt(val) {return l.isSome(val) ? super.addOpt(this.make(val)) : this}
added(val) {return super.added(this.make(val))}
make(val) {return l.toInst(val, this.cls)}
}
export class Vec extends l.Emp {
constructor(val) {super().$ = l.laxTrueArr(val)}
mut(val) {return this.clear(), this.addFrom(val)}
add(val) {return this.$.push(val), this}
// TODO rename to `.addVals`?
addFrom(val) {
if (l.optIter(val)) for (val of val) this.add(val)
return this
}
clear() {
if (this.$.length) this.$.length = 0
return this
}
at(ind) {return this.$[l.reqInt(ind)]}
sort(fun) {return this.$.sort(fun), this}
clone() {return new this.constructor(this.$.slice())}
toArray() {return this.$} // Used by `iter.mjs`.
toJSON() {return this.toArray()}
get size() {return this.$.length}
[Symbol.iterator]() {return this.$.values()}
static make(len) {return new this(Array(l.reqNat(len)))}
static from(val) {return new this(l.toTrueArr(val))}
static of(...val) {return new this(val)}
}
// TODO test, doc.
export class TypedVec extends Vec {
constructor(...val) {
super(...val)
this.validate()
}
add(val) {return super.add(this.reqVal(val))}
reqVal() {throw l.errImpl()}
validate() {mapMut(this.$, this.reqVal, this)}
}
function mapMut(arr, fun, ctx) {
l.reqTrueArr(arr)
l.reqFun(fun)
let ind = -1
while (++ind < arr.length) {
const prev = arr[ind]
const next = fun.call(ctx, prev)
if (!l.is(prev, next)) arr[ind] = next
}
}
// Short for "class vector".
export class ClsVec extends TypedVec {
get cls() {return Object}
constructor(src) {super().mut(src)}
reqVal(val) {return l.reqInst(val, this.cls)}
add(val) {return super.add(this.make(val))}
make(val) {return l.toInst(val, this.cls)}
}
export class Que extends Set {
constructor(val) {
super(val)
this.flushing = false
}
add(fun) {
l.reqFun(fun)
if (this.flushing) fun()
else super.add(fun)
return this
}
open() {return this.flushing = true, this.run()}
close() {return this.flushing = false, this}
run() {
if (this.size) for (const fun of this.values()) this.delete(fun), fun()
return this
}
}