-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
28 lines (22 loc) · 929 Bytes
/
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
'use strict'
const weakable = require('weakable')
const _set = Symbol('set')
const _strong = Symbol('strong')
const _weak = Symbol('weak')
const _WeakSet = Symbol('WeakSet')
module.exports = class WeakishSet {
constructor (items = [], {StrongSet: Strong = Set, WeakSet: Weak = WeakSet} = {}) {
this[_strong] = new Strong()
this[_weak] = new Weak()
this[_WeakSet] = Weak
for (const item of items) this.add(item)
}
[_set] (item) { return weakable(item) ? this[_weak] : this[_strong] }
has (item) { return this[_set](item).has(item) }
add (item) { return this[_set](item).add(item) }
delete (item) { return this[_set](item).delete(item) }
clear () { this[_strong].clear(); this[_weak] = new this[_WeakSet]() }
entries (...args) { return this[_strong].entries(...args) }
forEach (...args) { return this[_strong].forEach(...args) }
values (...args) { return this[_strong].values(...args) }
}