-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeakStrongSet.mts
154 lines (131 loc) · 4.05 KB
/
WeakStrongSet.mts
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* @file
* This is generated code. Do not edit.
*
* Generator: https://github.com/ajvincent/composite-collection/
* Template: Weak/Set
* @license MPL-2.0
* @author Alexander J. Vincent <[email protected]>
* @copyright © 2021-2022 Alexander J. Vincent
*/
import WeakKeyComposer from "./keys/Composite.mjs";
class WeakStrongSet<
__SK0__ extends object,
__SK1__
>
{
// eslint-disable-next-line jsdoc/require-property
/** @typedef {object} WeakKey */
/** @type {WeakKeyComposer} @constant */
#keyComposer = new WeakKeyComposer(["weakKey"], ["strongKey"]);
/** @type {WeakSet<WeakKey>} @constant */
#weakKeySet = new WeakSet;
constructor(iterable? : [__SK0__, __SK1__][]) {
if (iterable) {
for (const [weakKey, strongKey] of iterable) {
this.add(weakKey, strongKey);
}
}
}
/**
* Add a key set to this collection.
*
* @param {object} weakKey The weakly held key.
* @param {*} strongKey The strongly held key.
* @returns {WeakStrongSet} This collection.
* @public
*/
add(weakKey: __SK0__, strongKey: __SK1__) : this
{
this.#requireValidKey(weakKey, strongKey);
const __key__ = this.#keyComposer.getKey([weakKey], [strongKey]);
this.#weakKeySet.add(__key__);
return this;
}
/**
* Delete an element from the collection by the given key sequence.
*
* @param {object} weakKey The weakly held key.
* @param {*} strongKey The strongly held key.
* @returns {boolean} True if we found the value and deleted it.
* @public
*/
delete(weakKey: __SK0__, strongKey: __SK1__) : boolean
{
this.#requireValidKey(weakKey, strongKey);
const __key__ = this.#keyComposer.getKeyIfExists([weakKey], [strongKey]);
if (!__key__)
return false;
const __returnValue__ = this.#weakKeySet.delete(__key__);
this.#keyComposer.deleteKey([weakKey], [strongKey]);
return __returnValue__;
}
/**
* Report if the collection has a value for a key set.
*
* @param {object} weakKey The weakly held key.
* @param {*} strongKey The strongly held key.
* @returns {boolean} True if the key set refers to a value in the collection.
* @public
*/
has(weakKey: __SK0__, strongKey: __SK1__) : boolean
{
this.#requireValidKey(weakKey, strongKey);
const __key__ = this.#keyComposer.getKeyIfExists([weakKey], [strongKey]);
return __key__ ? this.#weakKeySet.has(__key__) : false;
}
/**
* Determine if a set of keys is valid.
*
* @param {object} weakKey The weakly held key.
* @param {*} strongKey The strongly held key.
* @returns {boolean} True if the validation passes, false if it doesn't.
* @public
*/
isValidKey(weakKey: __SK0__, strongKey: __SK1__) : boolean
{
return this.#isValidKey(weakKey, strongKey);
}
/**
* Throw if the key set is not valid.
*
* @param {object} weakKey The weakly held key.
* @param {*} strongKey The strongly held key.
* @throws for an invalid key set.
*/
#requireValidKey(weakKey: __SK0__, strongKey: __SK1__) : void
{
if (!this.#isValidKey(weakKey, strongKey))
throw new Error("The ordered key set is not valid!");
}
/**
* Determine if a set of keys is valid.
*
* @param {object} weakKey The weakly held key.
* @param {*} strongKey The strongly held key.
* @returns {boolean} True if the validation passes, false if it doesn't.
*/
#isValidKey(weakKey: __SK0__, strongKey: __SK1__) : boolean
{
if (!this.#keyComposer.isValidForKey([weakKey], [strongKey]))
return false;
return true;
}
[Symbol.toStringTag] = "WeakStrongSet";
}
Object.freeze(WeakStrongSet);
Object.freeze(WeakStrongSet.prototype);
export type ReadonlyWeakStrongSet<
__SK0__ extends object,
__SK1__
> =
Pick<
WeakStrongSet<__SK0__, __SK1__>,
"has" | "isValidKey"
>
export default WeakStrongSet;