@nejs/basic-extensions 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/.esdoc.json +9 -0
  2. package/README.md +2025 -11
  3. package/bin/clean +6 -0
  4. package/dist/@nejs/basic-extensions.bundle.1.6.1.js +4 -0
  5. package/dist/@nejs/basic-extensions.bundle.1.6.1.js.map +7 -0
  6. package/dist/cjs/arrayextensions.js +1 -0
  7. package/dist/cjs/arrayextensions.js.map +1 -0
  8. package/dist/cjs/functionextensions.js +1 -0
  9. package/dist/cjs/functionextensions.js.map +1 -0
  10. package/dist/cjs/globals.js +2 -1
  11. package/dist/cjs/globals.js.map +1 -0
  12. package/dist/cjs/index.d.ts +10 -19
  13. package/dist/cjs/index.js +69 -76
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/cjs/mapextensions.d.ts +2 -0
  16. package/dist/cjs/mapextensions.js +30 -0
  17. package/dist/cjs/mapextensions.js.map +1 -0
  18. package/dist/cjs/newClasses/asyncIterable.d.ts +123 -0
  19. package/dist/cjs/{asyncIterable.js → newClasses/asyncIterable.js} +7 -4
  20. package/dist/cjs/newClasses/asyncIterable.js.map +1 -0
  21. package/dist/cjs/newClasses/descriptor.d.ts +401 -0
  22. package/dist/cjs/{descriptor.js → newClasses/descriptor.js} +150 -80
  23. package/dist/cjs/newClasses/descriptor.js.map +1 -0
  24. package/dist/cjs/newClasses/iterable.d.ts +125 -0
  25. package/dist/cjs/{iterable.js → newClasses/iterable.js} +36 -10
  26. package/dist/cjs/newClasses/iterable.js.map +1 -0
  27. package/dist/cjs/newClasses/refmap.d.ts +238 -0
  28. package/dist/cjs/newClasses/refmap.js +433 -0
  29. package/dist/cjs/newClasses/refmap.js.map +1 -0
  30. package/dist/cjs/newClasses/refset.d.ts +186 -0
  31. package/dist/cjs/{refset.js → newClasses/refset.js} +18 -3
  32. package/dist/cjs/newClasses/refset.js.map +1 -0
  33. package/dist/cjs/objectextensions.d.ts +7 -6
  34. package/dist/cjs/objectextensions.js +85 -43
  35. package/dist/cjs/objectextensions.js.map +1 -0
  36. package/dist/cjs/reflectextensions.js +16 -12
  37. package/dist/cjs/reflectextensions.js.map +1 -0
  38. package/dist/cjs/stringextensions.js +1 -0
  39. package/dist/cjs/stringextensions.js.map +1 -0
  40. package/dist/cjs/symbolextensions.js +3 -1
  41. package/dist/cjs/symbolextensions.js.map +1 -0
  42. package/dist/cjs/weakrefextensions.js +1 -0
  43. package/dist/cjs/weakrefextensions.js.map +1 -0
  44. package/dist/mjs/arrayextensions.js +1 -0
  45. package/dist/mjs/arrayextensions.js.map +1 -0
  46. package/dist/mjs/functionextensions.js +1 -0
  47. package/dist/mjs/functionextensions.js.map +1 -0
  48. package/dist/mjs/globals.js +2 -1
  49. package/dist/mjs/globals.js.map +1 -0
  50. package/dist/mjs/index.d.ts +10 -19
  51. package/dist/mjs/index.js +67 -60
  52. package/dist/mjs/index.js.map +1 -0
  53. package/dist/mjs/mapextensions.d.ts +2 -0
  54. package/dist/mjs/mapextensions.js +27 -0
  55. package/dist/mjs/mapextensions.js.map +1 -0
  56. package/dist/mjs/newClasses/asyncIterable.d.ts +123 -0
  57. package/dist/mjs/{asyncIterable.js → newClasses/asyncIterable.js} +106 -105
  58. package/dist/mjs/newClasses/asyncIterable.js.map +1 -0
  59. package/dist/mjs/newClasses/descriptor.d.ts +401 -0
  60. package/dist/mjs/{descriptor.js → newClasses/descriptor.js} +129 -67
  61. package/dist/mjs/newClasses/descriptor.js.map +1 -0
  62. package/dist/mjs/newClasses/iterable.d.ts +125 -0
  63. package/dist/mjs/newClasses/iterable.js +199 -0
  64. package/dist/mjs/newClasses/iterable.js.map +1 -0
  65. package/dist/mjs/newClasses/refmap.d.ts +238 -0
  66. package/dist/mjs/newClasses/refmap.js +417 -0
  67. package/dist/mjs/newClasses/refmap.js.map +1 -0
  68. package/dist/mjs/newClasses/refset.d.ts +186 -0
  69. package/dist/mjs/{refset.js → newClasses/refset.js} +17 -3
  70. package/dist/mjs/newClasses/refset.js.map +1 -0
  71. package/dist/mjs/objectextensions.d.ts +7 -6
  72. package/dist/mjs/objectextensions.js +84 -42
  73. package/dist/mjs/objectextensions.js.map +1 -0
  74. package/dist/mjs/reflectextensions.js +16 -12
  75. package/dist/mjs/reflectextensions.js.map +1 -0
  76. package/dist/mjs/stringextensions.js +1 -0
  77. package/dist/mjs/stringextensions.js.map +1 -0
  78. package/dist/mjs/symbolextensions.js +3 -1
  79. package/dist/mjs/symbolextensions.js.map +1 -0
  80. package/dist/mjs/weakrefextensions.js +1 -0
  81. package/dist/mjs/weakrefextensions.js.map +1 -0
  82. package/docs/assets/anchor.js +350 -0
  83. package/docs/assets/bass-addons.css +12 -0
  84. package/docs/assets/bass.css +544 -0
  85. package/docs/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  86. package/docs/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  87. package/docs/assets/fonts/LICENSE.txt +93 -0
  88. package/docs/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  89. package/docs/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  90. package/docs/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  91. package/docs/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  92. package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  93. package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  94. package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  95. package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  96. package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  97. package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  98. package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  99. package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  100. package/docs/assets/fonts/source-code-pro.css +23 -0
  101. package/docs/assets/github.css +123 -0
  102. package/docs/assets/site.js +168 -0
  103. package/docs/assets/split.css +15 -0
  104. package/docs/assets/split.js +782 -0
  105. package/docs/assets/style.css +147 -0
  106. package/docs/index.html +13060 -0
  107. package/jsdoc-config.json +31 -0
  108. package/package.json +12 -3
  109. package/src/globals.js +1 -1
  110. package/src/index.js +75 -82
  111. package/src/mapextensions.js +30 -0
  112. package/src/{asyncIterable.js → newClasses/asyncIterable.js} +117 -117
  113. package/src/{descriptor.js → newClasses/descriptor.js} +137 -74
  114. package/src/newClasses/iterable.js +221 -0
  115. package/src/newClasses/refmap.js +483 -0
  116. package/src/{refset.js → newClasses/refset.js} +29 -16
  117. package/src/objectextensions.js +97 -46
  118. package/src/reflectextensions.js +16 -14
  119. package/src/symbolextensions.js +2 -1
  120. package/tests/index.test.js +1 -1
  121. package/tests/{asyncIterable.test.js → newClasses/asyncIterable.test.js} +3 -4
  122. package/tests/newClasses/descriptor.test.js +252 -0
  123. package/tests/{iterable.test.js → newClasses/iterable.test.js} +2 -4
  124. package/tests/newClasses/refmap.test.js +69 -0
  125. package/tests/{refset.test.js → newClasses/refset.test.js} +2 -4
  126. package/tests/objectextensions.test.js +128 -0
  127. package/tsconfig.base.json +2 -1
  128. package/dist/@nejs/basic-extensions.bundle.1.5.1.js +0 -2
  129. package/dist/@nejs/basic-extensions.bundle.1.5.1.js.map +0 -7
  130. package/dist/cjs/asyncIterable.d.ts +0 -3
  131. package/dist/cjs/descriptor.d.ts +0 -2
  132. package/dist/cjs/iterable.d.ts +0 -3
  133. package/dist/cjs/refset.d.ts +0 -2
  134. package/dist/mjs/asyncIterable.d.ts +0 -3
  135. package/dist/mjs/descriptor.d.ts +0 -2
  136. package/dist/mjs/iterable.d.ts +0 -3
  137. package/dist/mjs/iterable.js +0 -184
  138. package/dist/mjs/refset.d.ts +0 -2
  139. package/src/iterable.js +0 -203
@@ -0,0 +1,238 @@
1
+ /**
2
+ * RefMap class extends the standard Map object to manage a collection of
3
+ * WeakRef values mapped to strong keys. It provides additional functionality
4
+ * such as objectification of values and various utility methods.
5
+ *
6
+ * Unlike standard Maps or Objects, RefMap stores weak references to objects,
7
+ * allowing them to be garbage-collected if there are no other references to
8
+ * them. This behavior is different from Maps and standard Objects, which
9
+ * maintain strong references to their elements.
10
+ *
11
+ * @extends Map
12
+ */
13
+ export class RefMap extends Map<any, any> {
14
+ constructor(...args: any[]);
15
+ /**
16
+ * Method to control whether the RefMap should objectify its values. When
17
+ * objectifying, primitive values (number, string, boolean, bigint) are
18
+ * converted to their respective object types, which allows them to be used as
19
+ * WeakRef targets.
20
+ *
21
+ * @param {boolean} setObjectification - Flag to enable or disable
22
+ * objectification.
23
+ * @returns {RefMap} - The current RefMap instance to allow method chaining.
24
+ */
25
+ objectifying(setObjectification?: boolean): RefMap;
26
+ /**
27
+ * Setting this value to true, will cause all set values to the Map to
28
+ * be analyzed for validity as a candidate to be wrapped in a `WeakRef`
29
+ * object. If true, and if possible, the object will be turned into an
30
+ * `Object` variant first.
31
+ *
32
+ * @param {boolean} value - The new state to set for objectifyValues.
33
+ */
34
+ set objectifyValues(value: boolean);
35
+ /**
36
+ * Returns the state indicating whether or not `RefMap` will attempt to
37
+ * convert non-valid primitives into targets that are valid input for
38
+ * new `WeakRef` object instances. If this value is `false` then no
39
+ * *objectification* will occur.
40
+ *
41
+ * @returns {boolean} The current state of objectifyValues.
42
+ */
43
+ get objectifyValues(): boolean;
44
+ /**
45
+ * The function converts a JavaScript Map object into a regular JavaScript
46
+ * object, handling invalid keys by converting them to strings.
47
+ *
48
+ * @returns {object} an object; keys that are not either a `String` or a
49
+ * `Symbol` will be converted to a string.
50
+ */
51
+ asObject(): object;
52
+ /**
53
+ * The `get` function retrieves a value from a map and returns it, or returns a
54
+ * default value if the value is null or undefined. The actual retrieved value
55
+ * is a dereferenced `WeakRef`. If the result is `undefined` and this is not the
56
+ * expected response, it is likely the value has been garbage collected.
57
+ *
58
+ * @param {any} key - The key parameter is the key of the value you want to
59
+ * retrieve from the data structure.
60
+ * @param {any} defaultValue - The `defaultValue` parameter is the value that
61
+ * will be returned if the key does not exist in the map or if the value
62
+ * associated with the key has been garbage collected (i.e., it no longer
63
+ * exists).
64
+ * @returns The method is returning the value associated with the given key.
65
+ * If the value is not found or if it has been garbage collected (deref()
66
+ * returns null), then the defaultValue is returned.
67
+ */
68
+ get(key: any, defaultValue: any): any;
69
+ /**
70
+ * Overrides the set method of `Map`. Adds a value to the `RefMap`,
71
+ * converting it to a `WeakRef`. Throws an error if the value is not a
72
+ * valid `WeakRef` target (e.g., `null`, `undefined`, or a registered
73
+ * `symbol`). If {@link objectifyValues} is enabled, an attempt to convert
74
+ * primitives to their object variants will be made. These are `numbers`,
75
+ * `strings`, `boolean` values and `bigint`s.
76
+ *
77
+ * @param {*} key - The `key` to be set on the `RefMap`
78
+ * @param {*} value - The value to be associated with the `key`
79
+ * @throws {TypeError} If the value is not a valid WeakRef target.
80
+ */
81
+ set(key: any, value: any): void;
82
+ /**
83
+ * Sets multiple values at a single time. The format is an array of array
84
+ * or rather an array of {@link Object.entries} (for example,
85
+ * `[[key1,value1], [key2,value2]]`). For each entry pair, if the length
86
+ * is not 2, either missing a key or value, it will be skipped.
87
+ *
88
+ * @param {Iterable} values - An iterable of values to add to the RefMap.
89
+ * @throws {TypeError} If the supplied values are falsey or non-iterable.
90
+ * @returns {RepMap} returns `this` to allow for chaining
91
+ */
92
+ setAll(entries: any): RepMap;
93
+ /**
94
+ * Removes all elements from the RefMap that have been garbage collected
95
+ * (i.e., their WeakRef no longer points to an object).
96
+ *
97
+ * @returns {RefMap} - The current RefMap instance to allow method chaining.
98
+ */
99
+ clean(): RefMap;
100
+ /**
101
+ * Executes a provided function once for each value in the RefMap. The callback
102
+ * function receives the dereferenced value, the value again (as RefMap doesn't
103
+ * use keys), and the RefMap itself. This method provides a way to iterate over
104
+ * and apply operations to the values stored in the RefMap, taking into account
105
+ * that they are weak references and may have been garbage-collected.
106
+ *
107
+ * @param {Function} forEachFn - Function to execute for each element. It
108
+ * takes three arguments: element, element (again, as RefMap has no key), and
109
+ * the RefMap itself.
110
+ * @param {*} thisArg - Value to use as `this` when executing `forEachFn`.
111
+ */
112
+ entries(): Iterator;
113
+ /**
114
+ * Iterate over the items in the map and pass them to the supplied
115
+ * function ala `Map.prototype.forEach`. Note however, there are no
116
+ * indexes on Maps and as such, the index parameter of the callback
117
+ * will always be the value's key. Subsequently the `array` or third
118
+ * parameter will receive the map instance rather than an array.
119
+ *
120
+ * @param {function} forEachFn the function to use for each element in
121
+ * the set.
122
+ * @param {object} thisArg the `this` argument to be applied to each
123
+ * invocation of the `forEachFn` callback. Note, this value is unable
124
+ * to be applied if the `forEachFn` is a big arrow function
125
+ */
126
+ forEach(forEachFn: Function, thisArg: object): void;
127
+ /**
128
+ * Returns an iterator for the values in the RefMap. Each value is
129
+ * dereferenced from its WeakRef before being returned. This method allows
130
+ * iterating over he set's values, similar to how one would iterate over
131
+ * values in a standard Map or Array, but with the understanding that the
132
+ * values are weakly referenced and may no longer exist (in which case
133
+ * they are skipped).
134
+ *
135
+ * @returns {Iterator} An iterator for the values.
136
+ */
137
+ values(): Iterator;
138
+ /**
139
+ * Determines whether an element with the specified value exists in the
140
+ * `RefMap`. For non-objectified sets, this method checks if the dereferenced
141
+ * values of the map include the specified value.
142
+ *
143
+ * For objectified sets, strict is set to false which uses loose
144
+ * equality to allow for things like `Object(5)` to equal `5`. This is important
145
+ * because otherwise primitives could not be weakly referenced. In the grand
146
+ * scheme of things, this is only useful if the objectified value is the
147
+ * one being referenced.
148
+ *
149
+ * @param {*} value - The value to check for presence in the RefMap.
150
+ * @param {boolean} strict - if `true`, the default, then the supplied value
151
+ * is hard compared to the dereferenced value (`===`). If `false`, then a
152
+ * loose comparison is used (`==`)
153
+ * @returns {boolean} - True if an element with the specified value exists
154
+ * in the RefMap, false otherwise.
155
+ */
156
+ hasValue(value: any, strict?: boolean): boolean;
157
+ /**
158
+ * The `filter` function filters the entries of a `RefMap` object based on
159
+ * a given filter function. The dereferenced entries of the values of the map
160
+ * will be passed to the function rather than a `WeakRef` itself.
161
+ *
162
+ * A new resulting entry set will be generated and a new `RefMap` will be made
163
+ * from these entries and returned. Note that this function never returns
164
+ * `null`
165
+ *
166
+ * @param {function} filterFn - The `filterFn` parameter is a function that
167
+ * will be used to filter the entries in the `RefMap`. It will be called with
168
+ * three arguments: the value of the current entry, the key of the current
169
+ * entry, and the `RefMap` itself. The function should return `true`
170
+ * @param {object} thisArg - The `thisArg` parameter is an optional argument
171
+ * that specifies the value to be used as `this` when executing the
172
+ * `filterFn` function. It allows you to explicitly set the context in which
173
+ * the `filterFn` function is called. If `thisArg` is not provided, `this
174
+ * @returns {array} The `filter` method is returning an array of filtered map
175
+ * entries
176
+ */
177
+ filter(filterFn: Function, thisArg: object): array;
178
+ /**
179
+ * The `find` function iterates over a map and calls a given function on
180
+ * each value, returning the first value for which the function returns
181
+ * a truthy value.
182
+ *
183
+ * The function signature of `findFn` is
184
+ * ```
185
+ * function findFn(value, key, map)
186
+ * ```
187
+ * 'value' is passed to findFn up to two times; first with the `WeakRef`
188
+ * value, second with the result of {@link WeakRef.deref}. If `findFn`
189
+ * returns true for either of these the dereferenced value will be
190
+ * returned from the call to {@link RefMap.find}.
191
+ * `key` represents the key object that the value is mapped to.
192
+ * `map` is simply a reference to `this` map.
193
+ *
194
+ * @param findFn - `findFn` is a function that will be called for each
195
+ * element in the map. It takes three arguments: `ref`, `key`, and `map`;
196
+ * where `ref` is the value of the current element in the map, `key` is
197
+ * the key of the current element, and `map` is a reference to the instance
198
+ * being searched.
199
+ * @param thisArg - The `thisArg` parameter is the value to be used as
200
+ * the `this` value when executing the `findFn` function. It allows you
201
+ * to specify the context in which the `findFn` function should be called.
202
+ * @returns the first dereferenced value that satisfies the condition
203
+ * specified by the `findFn` function. If no value satisfies the condition,
204
+ * it returns `null`.
205
+ */
206
+ find(findFn: any, thisArg: any): any;
207
+ /**
208
+ * Creates a new array or `RefMap` with the results of calling a provided
209
+ * function on every element in the calling `RefMap`. This method dereferences
210
+ * each value, applies the `mapFn`, and collects the results. If `toRefMap` is
211
+ * `true`, a new `RefMap` is returned; otherwise, an array. This method
212
+ * differs from `Array.map` in handling weak references and the potential to
213
+ * return a new `RefMap` instead of an array.
214
+ *
215
+ * @param {Function} mapFn - Function that produces an element of the new
216
+ * array or `RefMap`, taking three arguments.
217
+ * @param {*} thisArg - Value to use as this when executing mapFn.
218
+ * @param {boolean} toRefMap - Determines if the output should be a new
219
+ * `RefMap` (`true`) or an array (`false`).
220
+ * @param {boolean} mirrorObjectification - If `true` and `toRefMap` is
221
+ * `true`, the new `RefMap` mirrors the objectification setting of the
222
+ * original.
223
+ * @returns {Array|RefMap} - A new array or `RefMap` with each element being
224
+ * the result of the `mapFn`.
225
+ */
226
+ map(mapFn: Function, thisArg: any, toRefMap: boolean, mirrorObjectification: boolean): any[] | RefMap;
227
+ /**
228
+ * The function returns an iterator that iterates over the entries of an object,
229
+ * dereferencing any weak references.
230
+ *
231
+ * @returns {Iterator} A new iterator object is being returned.
232
+ */
233
+ [Symbol.iterator](): Iterator;
234
+ #private;
235
+ }
236
+ export const RefMapExtensions: Extension;
237
+ import { Iterator } from './iterable.js';
238
+ import { Extension } from '@nejs/extension';
@@ -0,0 +1,433 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8
+ if (kind === "m") throw new TypeError("Private method is not writable");
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
+ };
13
+ var _RefMap_objectifyValues;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.RefMapExtensions = exports.RefMap = void 0;
16
+ const extension_1 = require("@nejs/extension");
17
+ const objectextensions_js_1 = require("../objectextensions.js");
18
+ const symbolextensions_js_1 = require("../symbolextensions.js");
19
+ const weakrefextensions_js_1 = require("../weakrefextensions.js");
20
+ const iterable_js_1 = require("./iterable.js");
21
+ const { isObject, isNullDefined, isValidKey } = objectextensions_js_1.ObjectExtensions.patches;
22
+ const { isRegistered } = symbolextensions_js_1.SymbolExtensions.patches;
23
+ const { isValidReference } = weakrefextensions_js_1.WeakRefExtensions.patches;
24
+ /**
25
+ * RefMap class extends the standard Map object to manage a collection of
26
+ * WeakRef values mapped to strong keys. It provides additional functionality
27
+ * such as objectification of values and various utility methods.
28
+ *
29
+ * Unlike standard Maps or Objects, RefMap stores weak references to objects,
30
+ * allowing them to be garbage-collected if there are no other references to
31
+ * them. This behavior is different from Maps and standard Objects, which
32
+ * maintain strong references to their elements.
33
+ *
34
+ * @extends Map
35
+ */
36
+ class RefMap extends Map {
37
+ constructor(...args) {
38
+ super(...args);
39
+ /**
40
+ * Private field to track whether the RefMap should objectify primitive
41
+ * values.
42
+ *
43
+ * @private
44
+ */
45
+ _RefMap_objectifyValues.set(this, false);
46
+ }
47
+ /**
48
+ * Method to control whether the RefMap should objectify its values. When
49
+ * objectifying, primitive values (number, string, boolean, bigint) are
50
+ * converted to their respective object types, which allows them to be used as
51
+ * WeakRef targets.
52
+ *
53
+ * @param {boolean} setObjectification - Flag to enable or disable
54
+ * objectification.
55
+ * @returns {RefMap} - The current RefMap instance to allow method chaining.
56
+ */
57
+ objectifying(setObjectification = true) {
58
+ this.objectifyValues = setObjectification;
59
+ return this;
60
+ }
61
+ /**
62
+ * The function converts a JavaScript Map object into a regular JavaScript
63
+ * object, handling invalid keys by converting them to strings.
64
+ *
65
+ * @returns {object} an object; keys that are not either a `String` or a
66
+ * `Symbol` will be converted to a string.
67
+ */
68
+ asObject() {
69
+ const object = {};
70
+ for (const [key, value] of this) {
71
+ const useKey = isValidKey(key) ? key : String(key);
72
+ const useValue = value?.valueOf() || value;
73
+ object[useKey] = useValue;
74
+ }
75
+ return object;
76
+ }
77
+ /**
78
+ * Returns the state indicating whether or not `RefMap` will attempt to
79
+ * convert non-valid primitives into targets that are valid input for
80
+ * new `WeakRef` object instances. If this value is `false` then no
81
+ * *objectification* will occur.
82
+ *
83
+ * @returns {boolean} The current state of objectifyValues.
84
+ */
85
+ get objectifyValues() {
86
+ return __classPrivateFieldGet(this, _RefMap_objectifyValues, "f");
87
+ }
88
+ /**
89
+ * The `get` function retrieves a value from a map and returns it, or returns a
90
+ * default value if the value is null or undefined. The actual retrieved value
91
+ * is a dereferenced `WeakRef`. If the result is `undefined` and this is not the
92
+ * expected response, it is likely the value has been garbage collected.
93
+ *
94
+ * @param {any} key - The key parameter is the key of the value you want to
95
+ * retrieve from the data structure.
96
+ * @param {any} defaultValue - The `defaultValue` parameter is the value that
97
+ * will be returned if the key does not exist in the map or if the value
98
+ * associated with the key has been garbage collected (i.e., it no longer
99
+ * exists).
100
+ * @returns The method is returning the value associated with the given key.
101
+ * If the value is not found or if it has been garbage collected (deref()
102
+ * returns null), then the defaultValue is returned.
103
+ */
104
+ get(key, defaultValue) {
105
+ const value = super.get(key);
106
+ if (!value || !value?.deref()) {
107
+ return defaultValue;
108
+ }
109
+ return value?.deref();
110
+ }
111
+ /**
112
+ * Setting this value to true, will cause all set values to the Map to
113
+ * be analyzed for validity as a candidate to be wrapped in a `WeakRef`
114
+ * object. If true, and if possible, the object will be turned into an
115
+ * `Object` variant first.
116
+ *
117
+ * @param {boolean} value - The new state to set for objectifyValues.
118
+ */
119
+ set objectifyValues(value) {
120
+ __classPrivateFieldSet(this, _RefMap_objectifyValues, !!value, "f");
121
+ }
122
+ /**
123
+ * Overrides the set method of `Map`. Adds a value to the `RefMap`,
124
+ * converting it to a `WeakRef`. Throws an error if the value is not a
125
+ * valid `WeakRef` target (e.g., `null`, `undefined`, or a registered
126
+ * `symbol`). If {@link objectifyValues} is enabled, an attempt to convert
127
+ * primitives to their object variants will be made. These are `numbers`,
128
+ * `strings`, `boolean` values and `bigint`s.
129
+ *
130
+ * @param {*} key - The `key` to be set on the `RefMap`
131
+ * @param {*} value - The value to be associated with the `key`
132
+ * @throws {TypeError} If the value is not a valid WeakRef target.
133
+ */
134
+ set(key, value) {
135
+ let useValue = value;
136
+ // Objectify the value if needed
137
+ if (__classPrivateFieldGet(this, _RefMap_objectifyValues, "f") && (typeof useValue === 'number' ||
138
+ typeof useValue === 'string' ||
139
+ typeof useValue === 'boolean' ||
140
+ typeof useValue === 'bigint')) {
141
+ useValue = Object(useValue);
142
+ }
143
+ // Check if the value is an object, and if it's a symbol, ensure it's not registered
144
+ if (typeof useValue === 'symbol' && Symbol.keyFor(useValue) !== undefined) {
145
+ throw new TypeError('RefMap cannot accept registered symbols as values');
146
+ }
147
+ if (typeof useValue !== 'object' && typeof useValue !== 'symbol') {
148
+ throw new TypeError('RefMap values must be objects, non-registered symbols, or objectified primitives');
149
+ }
150
+ // If the value is null or undefined, throw an error
151
+ if (useValue === null || useValue === undefined) {
152
+ throw new TypeError('RefMap values cannot be null or undefined');
153
+ }
154
+ const ref = new WeakRef(useValue);
155
+ super.set(key, ref);
156
+ }
157
+ /**
158
+ * Sets multiple values at a single time. The format is an array of array
159
+ * or rather an array of {@link Object.entries} (for example,
160
+ * `[[key1,value1], [key2,value2]]`). For each entry pair, if the length
161
+ * is not 2, either missing a key or value, it will be skipped.
162
+ *
163
+ * @param {Iterable} values - An iterable of values to add to the RefMap.
164
+ * @throws {TypeError} If the supplied values are falsey or non-iterable.
165
+ * @returns {RepMap} returns `this` to allow for chaining
166
+ */
167
+ setAll(entries) {
168
+ if (!iterable_js_1.Iterable.isIterable(entries)) {
169
+ throw new TypeError('The supplied list of entries must be an array of arrays in the ' +
170
+ 'format [[key1, value1], [key2, value2], ...].');
171
+ }
172
+ const forEach = entry => {
173
+ const [key, value] = entry;
174
+ if (!key || !isObject(value) || !isRegistered(value)) {
175
+ return;
176
+ }
177
+ this.set(key, value);
178
+ };
179
+ for (const entry of entries) {
180
+ forEach(entry);
181
+ }
182
+ return this;
183
+ }
184
+ /**
185
+ * Removes all elements from the RefMap that have been garbage collected
186
+ * (i.e., their WeakRef no longer points to an object).
187
+ *
188
+ * @returns {RefMap} - The current RefMap instance to allow method chaining.
189
+ */
190
+ clean() {
191
+ for (const [key, dereferenced] of this) {
192
+ if (!dereferenced) {
193
+ this.delete(key);
194
+ }
195
+ }
196
+ return this;
197
+ }
198
+ /**
199
+ * Executes a provided function once for each value in the RefMap. The callback
200
+ * function receives the dereferenced value, the value again (as RefMap doesn't
201
+ * use keys), and the RefMap itself. This method provides a way to iterate over
202
+ * and apply operations to the values stored in the RefMap, taking into account
203
+ * that they are weak references and may have been garbage-collected.
204
+ *
205
+ * @param {Function} forEachFn - Function to execute for each element. It
206
+ * takes three arguments: element, element (again, as RefMap has no key), and
207
+ * the RefMap itself.
208
+ * @param {*} thisArg - Value to use as `this` when executing `forEachFn`.
209
+ */
210
+ entries() {
211
+ const entriesIterator = super.entries();
212
+ const refIterator = new iterable_js_1.Iterator(entriesIterator, (entry) => {
213
+ if (entry) {
214
+ const [key, ref] = entry;
215
+ const value = ref?.deref();
216
+ return [key, value];
217
+ }
218
+ return entry;
219
+ });
220
+ return refIterator;
221
+ }
222
+ /**
223
+ * Iterate over the items in the map and pass them to the supplied
224
+ * function ala `Map.prototype.forEach`. Note however, there are no
225
+ * indexes on Maps and as such, the index parameter of the callback
226
+ * will always be the value's key. Subsequently the `array` or third
227
+ * parameter will receive the map instance rather than an array.
228
+ *
229
+ * @param {function} forEachFn the function to use for each element in
230
+ * the set.
231
+ * @param {object} thisArg the `this` argument to be applied to each
232
+ * invocation of the `forEachFn` callback. Note, this value is unable
233
+ * to be applied if the `forEachFn` is a big arrow function
234
+ */
235
+ forEach(forEachFn, thisArg) {
236
+ for (const [key, ref] of super.entries()) {
237
+ const value = ref?.deref();
238
+ if (!value) {
239
+ continue;
240
+ }
241
+ forEachFn.call(thisArg, value, key, this);
242
+ }
243
+ }
244
+ /**
245
+ * Returns an iterator for the values in the RefMap. Each value is
246
+ * dereferenced from its WeakRef before being returned. This method allows
247
+ * iterating over he set's values, similar to how one would iterate over
248
+ * values in a standard Map or Array, but with the understanding that the
249
+ * values are weakly referenced and may no longer exist (in which case
250
+ * they are skipped).
251
+ *
252
+ * @returns {Iterator} An iterator for the values.
253
+ */
254
+ values() {
255
+ return new iterable_js_1.Iterator(super.values(), function perItem(value) {
256
+ const dereferenced = value?.deref();
257
+ return dereferenced || value;
258
+ });
259
+ }
260
+ /**
261
+ * Determines whether an element with the specified value exists in the
262
+ * `RefMap`. For non-objectified sets, this method checks if the dereferenced
263
+ * values of the map include the specified value.
264
+ *
265
+ * For objectified sets, strict is set to false which uses loose
266
+ * equality to allow for things like `Object(5)` to equal `5`. This is important
267
+ * because otherwise primitives could not be weakly referenced. In the grand
268
+ * scheme of things, this is only useful if the objectified value is the
269
+ * one being referenced.
270
+ *
271
+ * @param {*} value - The value to check for presence in the RefMap.
272
+ * @param {boolean} strict - if `true`, the default, then the supplied value
273
+ * is hard compared to the dereferenced value (`===`). If `false`, then a
274
+ * loose comparison is used (`==`)
275
+ * @returns {boolean} - True if an element with the specified value exists
276
+ * in the RefMap, false otherwise.
277
+ */
278
+ hasValue(value, strict = true) {
279
+ if (isNullDefined(value)) {
280
+ return false;
281
+ }
282
+ if (__classPrivateFieldGet(this, _RefMap_objectifyValues, "f")) {
283
+ strict = false;
284
+ }
285
+ for (const [_, dereferenced] of this) {
286
+ if ((strict && value === dereferenced) ||
287
+ (!strict && value == dereferenced)) {
288
+ return true;
289
+ }
290
+ }
291
+ return false;
292
+ }
293
+ /**
294
+ * The `filter` function filters the entries of a `RefMap` object based on
295
+ * a given filter function. The dereferenced entries of the values of the map
296
+ * will be passed to the function rather than a `WeakRef` itself.
297
+ *
298
+ * A new resulting entry set will be generated and a new `RefMap` will be made
299
+ * from these entries and returned. Note that this function never returns
300
+ * `null`
301
+ *
302
+ * @param {function} filterFn - The `filterFn` parameter is a function that
303
+ * will be used to filter the entries in the `RefMap`. It will be called with
304
+ * three arguments: the value of the current entry, the key of the current
305
+ * entry, and the `RefMap` itself. The function should return `true`
306
+ * @param {object} thisArg - The `thisArg` parameter is an optional argument
307
+ * that specifies the value to be used as `this` when executing the
308
+ * `filterFn` function. It allows you to explicitly set the context in which
309
+ * the `filterFn` function is called. If `thisArg` is not provided, `this
310
+ * @returns {array} The `filter` method is returning an array of filtered map
311
+ * entries
312
+ */
313
+ filter(filterFn, thisArg) {
314
+ const resultingEntries = [];
315
+ for (const [key, dereferenced] of this) {
316
+ if (filterFn.call(thisArg, dereferenced, key, this)) {
317
+ resultingEntries.push([key, dereferenced]);
318
+ }
319
+ }
320
+ return resultingEntries;
321
+ }
322
+ /**
323
+ * The `find` function iterates over a map and calls a given function on
324
+ * each value, returning the first value for which the function returns
325
+ * a truthy value.
326
+ *
327
+ * The function signature of `findFn` is
328
+ * ```
329
+ * function findFn(value, key, map)
330
+ * ```
331
+ * 'value' is passed to findFn up to two times; first with the `WeakRef`
332
+ * value, second with the result of {@link WeakRef.deref}. If `findFn`
333
+ * returns true for either of these the dereferenced value will be
334
+ * returned from the call to {@link RefMap.find}.
335
+ * `key` represents the key object that the value is mapped to.
336
+ * `map` is simply a reference to `this` map.
337
+ *
338
+ * @param findFn - `findFn` is a function that will be called for each
339
+ * element in the map. It takes three arguments: `ref`, `key`, and `map`;
340
+ * where `ref` is the value of the current element in the map, `key` is
341
+ * the key of the current element, and `map` is a reference to the instance
342
+ * being searched.
343
+ * @param thisArg - The `thisArg` parameter is the value to be used as
344
+ * the `this` value when executing the `findFn` function. It allows you
345
+ * to specify the context in which the `findFn` function should be called.
346
+ * @returns the first dereferenced value that satisfies the condition
347
+ * specified by the `findFn` function. If no value satisfies the condition,
348
+ * it returns `null`.
349
+ */
350
+ find(findFn, thisArg) {
351
+ for (const [key, dereferenced] of this) {
352
+ const ref = super.get(key);
353
+ let result = findFn.call(thisArg, ref, key, map);
354
+ if (!result) {
355
+ result = findFn.call(thisArg, dereferenced, key, map);
356
+ }
357
+ if (result) {
358
+ return dereferenced;
359
+ }
360
+ }
361
+ return null;
362
+ }
363
+ /**
364
+ * Creates a new array or `RefMap` with the results of calling a provided
365
+ * function on every element in the calling `RefMap`. This method dereferences
366
+ * each value, applies the `mapFn`, and collects the results. If `toRefMap` is
367
+ * `true`, a new `RefMap` is returned; otherwise, an array. This method
368
+ * differs from `Array.map` in handling weak references and the potential to
369
+ * return a new `RefMap` instead of an array.
370
+ *
371
+ * @param {Function} mapFn - Function that produces an element of the new
372
+ * array or `RefMap`, taking three arguments.
373
+ * @param {*} thisArg - Value to use as this when executing mapFn.
374
+ * @param {boolean} toRefMap - Determines if the output should be a new
375
+ * `RefMap` (`true`) or an array (`false`).
376
+ * @param {boolean} mirrorObjectification - If `true` and `toRefMap` is
377
+ * `true`, the new `RefMap` mirrors the objectification setting of the
378
+ * original.
379
+ * @returns {Array|RefMap} - A new array or `RefMap` with each element being
380
+ * the result of the `mapFn`.
381
+ */
382
+ map(mapFn, thisArg, toRefMap, mirrorObjectification) {
383
+ if (typeof mapFn !== 'function') {
384
+ throw new TypeError('mapFn must be a function! Received', mapFn);
385
+ }
386
+ const entries = [];
387
+ const errors = [];
388
+ let needsObjectification = mirrorObjectification && this.objectifyValues;
389
+ let detectNeed = mirrorObjectification === undefined;
390
+ let objectify = needsObjectification;
391
+ for (const [key, dereferenced] of this) {
392
+ const [, VALUE] = [0, 1];
393
+ const transformed = mapFn.call(thisArg, [key, dereferenced], key, this);
394
+ if (!isValidReference(transformed[VALUE])) {
395
+ if (isValidReference(Object(transformed[VALUE]))) {
396
+ needsObjectification = true;
397
+ if (detectNeed && !objectify) {
398
+ objectify = true;
399
+ transformed[VALUE] = Object(transformed[VALUE]);
400
+ }
401
+ }
402
+ }
403
+ entries.push(transformed);
404
+ }
405
+ if (toRefMap) {
406
+ return new RefMap(entries).objectifying(objectify);
407
+ }
408
+ return entries;
409
+ }
410
+ /**
411
+ * The function returns an iterator that iterates over the entries of an object,
412
+ * dereferencing any weak references.
413
+ *
414
+ * @returns {Iterator} A new iterator object is being returned.
415
+ */
416
+ *[(_RefMap_objectifyValues = new WeakMap(), Symbol.iterator)]() {
417
+ for (const [key, ref] of this.entries()) {
418
+ yield [key, ref];
419
+ }
420
+ }
421
+ /**
422
+ * Ensures that the constructor of this object instance's name
423
+ * is returned if the string tag for this instance is queried
424
+ *
425
+ * @returns {string} the name of the class
426
+ */
427
+ get [Symbol.toStringTag]() {
428
+ return this.constructor.name;
429
+ }
430
+ }
431
+ exports.RefMap = RefMap;
432
+ exports.RefMapExtensions = new extension_1.Extension(RefMap);
433
+ //# sourceMappingURL=refmap.js.map