-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
661 lines (555 loc) · 18.1 KB
/
index.js
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
'use strict'
const arrify = require('arrify')
const bfind = require('bfind')
const binsert = require('binsert')
const emptyIterator = require('empty-iterator')
const entriesArray = require('entries-array')
const entriesIterator = require('entries-iterator')
const isArrayOfLength = require('is-array-of-length')
const isIterable = require('is-iterable')
const kdel = require('kdel')
const kedit = require('kedit')
const keysArray = require('keys-array')
const keysIterator = require('keys-iterator')
const kget = require('kget')
const khas = require('khas')
const moment = require('moment')
const pfn = require('pfn')
const purge = require('purge')
const set = require('kset')
const toNumber = require('2/number')
const valuesIterator = require('values-iterator')
const valuesArray = require('values-array')
const vfn = require('vfn')
const vhas = require('vhas')
const vdel = require('vdel')
const WeakishMap = require('weakish-map')
const _compare = Symbol('compare')
const _entries = Symbol('entries')
const _find = Symbol('find')
const _getAllByKey = Symbol('getAllByKey')
const _getAllValuesByKey = Symbol('getAllValuesByKey')
const _getConstructor = Symbol('getConstructor')
const _getRootLayer = Symbol('getRootLayer')
const _kOptions = Symbol('kOptions')
const _layerOptions = Symbol('layerOptions')
const _looselyEquals = Symbol('looselyEquals')
const _looseKeys = Symbol('looseKeys')
const _looseValues = Symbol('looseValues')
const _map = Symbol('map')
const _multi = Symbol('multi')
const _removeExpired = Symbol('removeExpired')
const _setProxy = Symbol('setProxy')
const _sortValues = Symbol('sortValues')
const _rootLayer = Symbol('rootLayer')
const _ttl = Symbol('ttl')
const _vOptions = Symbol('vOptions')
const variadicMethods = {
hasEntry: 0,
hasValue: 0,
hasDeepValue: 0,
getElse: 0,
getAnyElse: 0,
getElseThrow: 0,
getAnyElseThrow: 0,
getElseSet: 0,
branchSet: 1,
set: 0,
branchSetThenGet: 1,
setThenGet: 0,
branchSetAll: 1,
setAll: 0,
branchEdit: 1,
edit: 0,
branchEditThenGet: 1,
editThenGet: 0,
deleteEntry: 0,
deleteValue: 0,
}
function getFromLayer (layer, key, options, notFound) {
if (!(layer instanceof Map8Layer)) throw new RangeError('Map does not extend to this level')
return layer.getElse(key, notFound)
}
class Map8 {
constructor (entries, layerOptions) {
if (arguments.length === 1 && !isIterable(entries)) {
layerOptions = entries
entries = []
}
layerOptions = arrify(layerOptions)
this[_layerOptions] = layerOptions
if (entries) this.setAll(...entries)
}
[_getRootLayer] (extraLayerOptions) {
if (!this[_rootLayer]) this[_rootLayer] = this[_getConstructor](extraLayerOptions)(-1)
return this[_rootLayer]
}
has (...keys) {
return khas(this[_getRootLayer](), keys, {get (layer, key, options, notFound) {
if (!(layer instanceof Map8Layer)) throw new RangeError('Map does not extend to this level')
return layer.has(key) ? layer.get(key) : notFound
}})
}
hasKey (...keys) {
return this.has(...keys)
}
hasAnyKey (...keys) {
return khas.any(this[_getRootLayer](), keys, {get: getFromLayer})
}
hasEntry (keys, value) {
if (!keys.length) return false
const key = keys.pop()
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.hasEntry(key, value) : false
}
hasValue (keys, value) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.hasValue(value) : false
}
hasDeepValue (keys, value) {
if (this.hasValue(...keys, value)) return true
for (const v of this.deepValues(...keys)) {
if (v === value) return true
}
return false
}
get (...keys) {
return kget(this[_getRootLayer](), keys, {get: getFromLayer})
}
getAny (...keys) {
return kget.any(this[_getRootLayer](), keys, {get: getFromLayer})
}
getElse (keys, fallback) {
return kget(this[_getRootLayer](), keys, {elseReturn: fallback, get: getFromLayer, maps: Map8Layer})
}
getAnyElse (keys, elseReturn) {
return kget.any(this[_getRootLayer](), keys, {elseReturn, get: getFromLayer, maps: Map8Layer})
}
getElseThrow (keys, error) {
return kget(this[_getRootLayer](), keys, {elseThrow: error, get: getFromLayer, maps: Map8Layer})
}
getAnyElseThrow (keys, error) {
return kget.any(this[_getRootLayer](), keys, {elseThrow: error, get: getFromLayer, maps: Map8Layer})
}
getElseSet (keys, value) {
return this.has(...keys) ? this.get(...keys) : this.setThenGet(...keys, pfn(value, value)())
}
[_getConstructor] (extraLayerOptions = []) {
const layerOptions = this[_layerOptions].concat(arrify(extraLayerOptions))
return i => new Map8Layer(layerOptions[i + 1])
}
branchSet (extraLayerOptions, keys, value) {
set(this[_getRootLayer](extraLayerOptions), keys, value, {construct: this[_getConstructor](extraLayerOptions), get: getFromLayer, maps: Map8Layer})
return this
}
set (keys, value) {
return this.branchSet([], ...keys, value)
}
branchSetThenGet (extraLayerOptions, keys, value) {
this.branchSet(extraLayerOptions, ...keys, value)
return this.get(...keys)
}
setThenGet (keys, value) {
return this.branchSetThenGet([], ...keys, value)
}
branchSetAll (extraLayerOptions, entries) {
for (const keys of entries) {
if (!Array.isArray(keys)) throw new TypeError('Key-value pairs must be arrays')
if (keys.length === 0) continue
if (keys.length === 1) throw new TypeError('Must provide both a key and a value')
const value = keys.pop()
this.branchSet(extraLayerOptions, ...keys, value)
}
return this
}
setAll (entries) {
return this.branchSetAll([], ...entries)
}
branchEdit (extraLayerOptions, keys, callback) {
this.branchEditThenGet(extraLayerOptions, ...keys, callback)
return this
}
edit (keys, callback) {
this.branchEditThenGet([], ...keys, callback)
return this
}
branchEditThenGet (extraLayerOptions, keys, callback) {
return kedit(this[_getRootLayer](extraLayerOptions), keys, callback, {
construct: this[_getConstructor](extraLayerOptions),
get: getFromLayer,
maps: Map8Layer,
set: (layer, key, values, options, defaultSet) => {
if ((layer instanceof Map8Layer) && layer.multi) {
if (!Array.isArray(values)) throw new TypeError('Edit callback must return an array')
layer.delete(key)
for (const value of arrify(values)) layer.set(key, value)
} else {
defaultSet()
}
},
})
}
editThenGet (keys, callback) {
return this.branchEditThenGet([], ...keys, callback)
}
branchEditAll (extraLayerOptions, callbacks) {
for (const [keys, callback] of callbacks) this.branchEditThenGet(extraLayerOptions, keys, callback)
return this
}
editAll (callbacks) {
return this.branchEditAll([], callbacks)
}
delete (...keys) {
return kdel(this[_getRootLayer](), keys, {get: getFromLayer, maps: Map8Layer})
}
deleteKey (...keys) {
return this.delete(...keys)
}
deleteEntry (keys, value) {
if (!keys.length) return false
const key = keys.pop()
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.deleteEntry(key, value) : false
}
deleteValue (keys, value) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.deleteValue(value) : false
}
toObject (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.toObject() : {}
}
toJSON () {
return this.toObject()
}
entries (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.entries() : entriesIterator(layer)
}
entriesArray (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.entriesArray() : entriesArray(layer)
}
groupedEntries (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.groupedEntries() : emptyIterator()
}
groupedEntriesArray (...keys) {
return Array.from(this.groupedEntries(...keys))
}
deepEntries (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.deepEntries() : emptyIterator()
}
deepEntriesArray (...keys) {
return Array.from(this.deepEntries(...keys))
}
keys (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.keys() : keysIterator(layer)
}
keysArray (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.keysArray() : keysArray(layer)
}
deepKeys (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.deepKeys() : emptyIterator()
}
deepKeysArray (...keys) {
return Array.from(this.deepKeys(...keys))
}
values (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.values() : valuesIterator(layer)
}
valuesArray (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.valuesArray() : valuesArray(layer)
}
deepValues (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.deepValues() : emptyIterator()
}
deepValuesArray (...keys) {
return Array.from(this.deepValues(...keys))
}
groupedValues (...keys) {
const layer = keys.length ? this.get(...keys) : this[_getRootLayer]()
return layer instanceof Map8Layer ? layer.groupedValues() : emptyIterator()
}
groupedValuesArray (...keys) {
return Array.from(this.groupedValues(...keys))
}
}
for (const [func, variadicIndex] of Object.entries(variadicMethods)) {
Map8.prototype[func] = vfn(variadicIndex, Map8.prototype[func])
}
class Map8Layer {
constructor (options = {}) {
const {loose, looseKeys = loose, looseValues = loose, looselyEquals, multi, ttl, setProxy, weak} = options
let {sortKeys, sortValues} = options
if (sortKeys === true) sortKeys = []
if (sortValues === true) sortValues = []
this[_kOptions] = {loose: looseKeys, looselyEquals, maps: SortedMap, weakMaps: WeakishMap}
this[_vOptions] = {loose: looseValues, looselyEquals} // We want WeakishMap to be considered a strong map for the v* functions because it can iterate entries
this[_looselyEquals] = looselyEquals
this[_looseKeys] = looseKeys
this[_looseValues] = looseValues
this[_map] = weak ? new WeakishMap([], sortKeys ? {strongMap: () => new SortedMap(sortKeys)} : {}) : sortKeys ? new SortedMap(sortKeys) : new Map()
this[_multi] = !!multi
this[_setProxy] = setProxy
this[_sortValues] = sortValues
this[_ttl] = Math.abs(toNumber(ttl, {round: true}))
}
get multi () {
return this[_multi]
}
[_removeExpired] (arr) {
if (this[_ttl]) purge(arr, ({datetimeSet}) => -datetimeSet.diff() > this[_ttl])
return arr
}
[_getAllByKey] (key) {
const values = this[_removeExpired](kget(this[_map], [key], {elseReturn: [], ...this[_kOptions]}))
if (values.length === 0) this.delete(key)
return values
}
[_getAllValuesByKey] (key) {
return this[_getAllByKey](key).map(({value}) => value)
}
has (key) {
return this[_getAllByKey](key).length > 0
}
hasEntry (key, value) {
return vhas(this[_getAllValuesByKey](key), value, this[_vOptions])
}
hasValue (value) {
return Array.from(this[_map].entries()).some(
([key, values]) => vhas(this[_getAllValuesByKey](key), value, this[_vOptions])
)
}
get (key) {
return this.getElse(key)
}
getElse (key, fallback) {
const values = this[_getAllValuesByKey](key)
if (this[_multi]) return values
if (values.length === 0) return fallback
return values[0]
}
set (key, value) {
if (this[_setProxy]) {
const proxyReturn = this[_setProxy](key, value)
if (isArrayOfLength(proxyReturn, 2)) [key, value] = proxyReturn
}
const items = this[_getAllByKey](key)
if (items.length === 0) {
this[_map].set(key, items)
}
const item = {datetimeSet: moment(), value}
if (items.length === 0 || this[_multi]) {
if (this[_sortValues]) {
binsert({
compare: this[_sortValues],
get: i => items[i].value,
length: items.length,
insert: i => { items.splice(i, 0, item) },
value: item.value,
})
} else {
items.push(item)
}
} else {
items[0] = item
}
return this
}
delete (key) {
return kdel(this[_map], [key], this[_kOptions])
}
deleteEntry (key, value) {
return vdel(this[_getAllByKey](key), value, {compareBy: 'value', ...this[_vOptions]})
}
deleteValue (value) {
return Array.from(this[_map].entries()).reduce(
(itemsDeleted, [key, values]) => itemsDeleted + vdel(this[_removeExpired](values), value, {compareBy: 'value', ...this[_vOptions]}), 0
)
}
clear () {
return this[_map].clear()
}
toObject () {
const obj = {}
for (let [key, values] of this[_map].entries()) { // eslint-disable-line prefer-const
if (this[_removeExpired](values).length === 0) continue
values = values.map(({value}) => {
if (value instanceof Map8Layer) value = value.toObject()
return value
})
if (!this[_multi]) values = values[0]
if (key in obj) throw new TypeError('Multiple keys have equivalent string representations and would overwrite each other in the generated object')
obj[key] = values
}
return obj
}
* entries () {
for (const [key, values] of this[_map].entries()) {
yield * this[_removeExpired](values).map(({value}) => {
if (value instanceof Map8Layer) value = value.entriesArray()
return [key, value]
})
}
}
entriesArray () {
return Array.from(this.entries())
}
* deepEntries () {
for (const [key, values] of this[_map].entries()) {
if (this[_removeExpired](values).length === 0) continue
for (const {value} of values) {
if (value instanceof Map8Layer) {
for (const subentry of value.deepEntries()) {
yield [key, ...subentry]
}
} else {
yield [key, value]
}
}
}
}
deepEntriesArray () {
return Array.from(this.deepEntries())
}
* groupedEntries () {
for (let [key, values] of this[_map].entries()) { // eslint-disable-line prefer-const
if (this[_removeExpired](values).length === 0) continue
values = values.map(({value}) => {
if (value instanceof Map8Layer) value = value.groupedEntriesArray()
return value
})
if (!this[_multi]) values = values[0]
yield [key, values]
}
}
groupedEntriesArray () {
return Array.from(this.groupedEntries())
}
* keys () {
for (const [key, values] of this[_map].entries()) {
if (this[_removeExpired](values).length > 0) yield key
}
}
keysArray () {
return Array.from(this.keys())
}
* deepKeys () {
for (const [key, values] of this[_map].entries()) {
if (this[_removeExpired](values).length === 0) continue
let yieldedSubkeys = false
for (const {value} of values) {
if (value instanceof Map8Layer) {
for (const subkeys of value.deepKeys()) {
yieldedSubkeys = true
yield [key, ...subkeys]
}
}
}
if (!yieldedSubkeys) yield [key]
}
}
deepKeysArray () {
return Array.from(this.deepKeys())
}
* values () {
for (const [, values] of this[_map].entries()) {
yield * this[_removeExpired](values).map(data => data.value)
}
}
valuesArray () {
return Array.from(this.values())
}
* deepValues () {
for (const [, values] of this[_map].entries()) {
if (this[_removeExpired](values).length === 0) continue
for (const {value} of values) {
if (value instanceof Map8Layer) {
yield * value.deepValues()
} else {
yield value
}
}
}
}
deepValuesArray () {
return Array.from(this.deepValues())
}
* groupedValues () {
for (let values of this[_map].values()) {
if (this[_removeExpired](values).length === 0) return []
values = values.map(
({value}) => (value instanceof Map8Layer) ? value.groupedValuesArray() : value
)
if (!this[_multi]) values = values[0]
yield values
}
}
groupedValuesArray () {
return Array.from(this.groupedValues())
}
}
class SortedMap {
constructor (compare) {
this[_compare] = compare
this[_entries] = []
}
[_find] (value) {
return bfind({
compare: this[_compare],
get: i => this[_entries][i][0],
length: this[_entries].length,
multiple: 'identical',
value,
})
}
has (key) {
return this[_find](key).identical
}
get (key) {
const {identical, index} = this[_find](key)
if (identical) return this[_entries][index][1]
}
set (key, value) {
binsert({
compare: this[_compare],
get: i => this[_entries][i][0],
length: this[_entries].length,
insert: i => { this[_entries].splice(i, 0, [key, value]) },
set: i => { this[_entries][i][1] = value },
unique: true,
})
}
delete (key) {
const {found, index} = this[_find](key)
if (found) this[_entries].splice(index, 1)
return found
}
entriesArray () {
return this[_entries].slice()
}
entries () {
return this.entriesArray()[Symbol.iterator]()
}
keysArray () {
return this[_entries].map(([k, v]) => k)
}
keys () {
return this.keysArray()[Symbol.iterator]()
}
valuesArray () {
return this[_entries].map(([k, v]) => v)
}
values () {
return this.valuesArray()[Symbol.iterator]()
}
}
module.exports = Map8