@nejs/basic-extensions 2.9.0 → 2.11.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 (176) hide show
  1. package/dist/@nejs/basic-extensions.bundle.2.11.0.js +19 -0
  2. package/dist/@nejs/basic-extensions.bundle.2.11.0.js.map +7 -0
  3. package/dist/cjs/array.extensions.js +174 -0
  4. package/dist/cjs/array.extensions.js.map +1 -1
  5. package/dist/cjs/big.int.extension.js +1 -0
  6. package/dist/cjs/big.int.extension.js.map +1 -1
  7. package/dist/cjs/classes/descriptor.d.ts +67 -155
  8. package/dist/cjs/classes/descriptor.js +185 -278
  9. package/dist/cjs/classes/descriptor.js.map +1 -1
  10. package/dist/cjs/classes/index.d.ts +1 -0
  11. package/dist/cjs/classes/index.js +3 -0
  12. package/dist/cjs/classes/index.js.map +1 -1
  13. package/dist/cjs/classes/iterable.d.ts +44 -0
  14. package/dist/cjs/classes/iterable.js +64 -0
  15. package/dist/cjs/classes/iterable.js.map +1 -1
  16. package/dist/cjs/classes/param.parser.d.ts +10 -10
  17. package/dist/cjs/classes/property.d.ts +86 -0
  18. package/dist/cjs/classes/property.js +284 -0
  19. package/dist/cjs/classes/property.js.map +1 -0
  20. package/dist/cjs/classes/symkeys.d.ts +68 -11
  21. package/dist/cjs/classes/symkeys.js +103 -17
  22. package/dist/cjs/classes/symkeys.js.map +1 -1
  23. package/dist/cjs/classes/type.d.ts +4 -4
  24. package/dist/cjs/function.extensions.js +1 -0
  25. package/dist/cjs/function.extensions.js.map +1 -1
  26. package/dist/cjs/global.this.js +29 -0
  27. package/dist/cjs/global.this.js.map +1 -1
  28. package/dist/cjs/index.d.ts +3 -0
  29. package/dist/cjs/index.js +19 -0
  30. package/dist/cjs/index.js.map +1 -1
  31. package/dist/cjs/json.extensions.js +19 -18
  32. package/dist/cjs/json.extensions.js.map +1 -1
  33. package/dist/cjs/map.extensions.js +1 -0
  34. package/dist/cjs/map.extensions.js.map +1 -1
  35. package/dist/cjs/number.extension.js +1 -0
  36. package/dist/cjs/number.extension.js.map +1 -1
  37. package/dist/cjs/object.extensions.d.ts +0 -29
  38. package/dist/cjs/object.extensions.js +218 -255
  39. package/dist/cjs/object.extensions.js.map +1 -1
  40. package/dist/cjs/set.extensions.js +1 -0
  41. package/dist/cjs/set.extensions.js.map +1 -1
  42. package/dist/cjs/string.extensions.js +474 -469
  43. package/dist/cjs/string.extensions.js.map +1 -1
  44. package/dist/cjs/symbol.extensions.js +387 -32
  45. package/dist/cjs/symbol.extensions.js.map +1 -1
  46. package/dist/cjs/utils/copy.object.d.ts +408 -0
  47. package/dist/cjs/utils/copy.object.js +720 -0
  48. package/dist/cjs/utils/copy.object.js.map +1 -0
  49. package/dist/cjs/utils/descriptor.utils.d.ts +146 -0
  50. package/dist/cjs/utils/descriptor.utils.js +614 -0
  51. package/dist/cjs/utils/descriptor.utils.js.map +1 -0
  52. package/dist/cjs/utils/index.d.ts +60 -0
  53. package/dist/cjs/utils/index.js +52 -0
  54. package/dist/cjs/utils/index.js.map +1 -0
  55. package/dist/cjs/utils/toolkit.d.ts +1898 -0
  56. package/dist/cjs/utils/toolkit.js +1378 -0
  57. package/dist/cjs/utils/toolkit.js.map +1 -0
  58. package/dist/mjs/array.extensions.js +174 -0
  59. package/dist/mjs/array.extensions.js.map +1 -1
  60. package/dist/mjs/big.int.extension.js +1 -0
  61. package/dist/mjs/big.int.extension.js.map +1 -1
  62. package/dist/mjs/classes/descriptor.d.ts +67 -155
  63. package/dist/mjs/classes/descriptor.js +185 -278
  64. package/dist/mjs/classes/descriptor.js.map +1 -1
  65. package/dist/mjs/classes/index.d.ts +1 -0
  66. package/dist/mjs/classes/index.js +3 -0
  67. package/dist/mjs/classes/index.js.map +1 -1
  68. package/dist/mjs/classes/iterable.d.ts +44 -0
  69. package/dist/mjs/classes/iterable.js +64 -0
  70. package/dist/mjs/classes/iterable.js.map +1 -1
  71. package/dist/mjs/classes/param.parser.d.ts +10 -10
  72. package/dist/mjs/classes/property.d.ts +86 -0
  73. package/dist/mjs/classes/property.js +280 -0
  74. package/dist/mjs/classes/property.js.map +1 -0
  75. package/dist/mjs/classes/symkeys.d.ts +68 -11
  76. package/dist/mjs/classes/symkeys.js +103 -17
  77. package/dist/mjs/classes/symkeys.js.map +1 -1
  78. package/dist/mjs/classes/type.d.ts +4 -4
  79. package/dist/mjs/function.extensions.js +1 -0
  80. package/dist/mjs/function.extensions.js.map +1 -1
  81. package/dist/mjs/global.this.js +6 -0
  82. package/dist/mjs/global.this.js.map +1 -1
  83. package/dist/mjs/index.d.ts +3 -0
  84. package/dist/mjs/index.js +5 -0
  85. package/dist/mjs/index.js.map +1 -1
  86. package/dist/mjs/json.extensions.js +19 -18
  87. package/dist/mjs/json.extensions.js.map +1 -1
  88. package/dist/mjs/map.extensions.js +1 -0
  89. package/dist/mjs/map.extensions.js.map +1 -1
  90. package/dist/mjs/number.extension.js +1 -0
  91. package/dist/mjs/number.extension.js.map +1 -1
  92. package/dist/mjs/object.extensions.d.ts +0 -29
  93. package/dist/mjs/object.extensions.js +215 -251
  94. package/dist/mjs/object.extensions.js.map +1 -1
  95. package/dist/mjs/set.extensions.js +1 -0
  96. package/dist/mjs/set.extensions.js.map +1 -1
  97. package/dist/mjs/string.extensions.js +474 -469
  98. package/dist/mjs/string.extensions.js.map +1 -1
  99. package/dist/mjs/symbol.extensions.js +387 -32
  100. package/dist/mjs/symbol.extensions.js.map +1 -1
  101. package/dist/mjs/utils/copy.object.d.ts +408 -0
  102. package/dist/mjs/utils/copy.object.js +702 -0
  103. package/dist/mjs/utils/copy.object.js.map +1 -0
  104. package/dist/mjs/utils/descriptor.utils.d.ts +146 -0
  105. package/dist/mjs/utils/descriptor.utils.js +606 -0
  106. package/dist/mjs/utils/descriptor.utils.js.map +1 -0
  107. package/dist/mjs/utils/index.d.ts +60 -0
  108. package/dist/mjs/utils/index.js +36 -0
  109. package/dist/mjs/utils/index.js.map +1 -0
  110. package/dist/mjs/utils/toolkit.d.ts +1898 -0
  111. package/dist/mjs/utils/toolkit.js +1373 -0
  112. package/dist/mjs/utils/toolkit.js.map +1 -0
  113. package/package.json +29 -37
  114. package/repl.bootstrap.js +12 -1
  115. package/src/array.extensions.js +191 -1
  116. package/src/big.int.extension.js +3 -1
  117. package/src/classes/descriptor.js +206 -305
  118. package/src/classes/index.js +4 -0
  119. package/src/classes/iterable.js +74 -0
  120. package/src/classes/property.js +333 -0
  121. package/src/classes/symkeys.js +120 -19
  122. package/src/function.extensions.js +2 -0
  123. package/src/global.this.js +8 -0
  124. package/src/index.js +6 -0
  125. package/src/json.extensions.js +18 -19
  126. package/src/map.extensions.js +3 -1
  127. package/src/number.extension.js +3 -1
  128. package/src/object.extensions.js +240 -277
  129. package/src/set.extensions.js +3 -1
  130. package/src/string.extensions.js +512 -506
  131. package/src/symbol.extensions.js +413 -30
  132. package/src/utils/copy.object.js +780 -0
  133. package/src/utils/descriptor.utils.js +707 -0
  134. package/src/utils/index.js +74 -0
  135. package/src/utils/toolkit.js +1473 -0
  136. package/tests/arrayextensions.test.js +2 -0
  137. package/tests/index.test.js +1 -0
  138. package/tests/newClasses/asyncIterable.test.js +2 -0
  139. package/tests/newClasses/deferred.test.js +5 -3
  140. package/tests/newClasses/descriptor.test.js +6 -8
  141. package/tests/newClasses/iterable.test.js +2 -0
  142. package/tests/newClasses/refmap.test.js +2 -1
  143. package/tests/newClasses/refset.test.js +2 -0
  144. package/tests/objectextensions.test.js +2 -0
  145. package/tests/setextensions.test.js +2 -0
  146. package/tests/stringextensions.test.js +1 -0
  147. package/tests/utils/toolkit.test.js +223 -0
  148. package/tsconfig.base.json +1 -1
  149. package/vitest.config.js +7 -0
  150. package/dist/@nejs/basic-extensions.bundle.2.8.0.js +0 -19
  151. package/dist/@nejs/basic-extensions.bundle.2.8.0.js.map +0 -7
  152. package/docs/assets/anchor.js +0 -350
  153. package/docs/assets/bass-addons.css +0 -12
  154. package/docs/assets/bass.css +0 -544
  155. package/docs/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  156. package/docs/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  157. package/docs/assets/fonts/LICENSE.txt +0 -93
  158. package/docs/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  159. package/docs/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  160. package/docs/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  161. package/docs/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  162. package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  163. package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  164. package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  165. package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  166. package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  167. package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  168. package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  169. package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  170. package/docs/assets/fonts/source-code-pro.css +0 -23
  171. package/docs/assets/github.css +0 -123
  172. package/docs/assets/site.js +0 -168
  173. package/docs/assets/split.css +0 -15
  174. package/docs/assets/split.js +0 -782
  175. package/docs/assets/style.css +0 -147
  176. package/docs/index.html +0 -35485
@@ -0,0 +1,707 @@
1
+ /** @typedef {string|symbol|number} PropertyKey */
2
+
3
+ /**
4
+ * The `IsDescriptorStats` block contains all the information used to make
5
+ * a determination as to whether a given value is a an instance of the type
6
+ * {@link PropertyDescriptor} or at least compatible to used as one.
7
+ *
8
+ * @typedef {Object} IsDescriptorStats
9
+ * @property {number} confidence this is going to be a number from 0.0-1.0
10
+ * indicating the confidence ratio that the object supplied to `isDescriptor`
11
+ * is actually a valid `PropertyDescriptor` object. If the number is greater
12
+ * than 0 but less than 1.0, it likely indicates that the object can be used
13
+ * as a `PropertyDescriptor` but one or more factors gives it less than 100%
14
+ * confidence that being such is the objects intended purpose (e.g. it could
15
+ * be that there are more properties than a descriptor might have or that
16
+ * it is missing crucial properties such as `value`, `get` or `set`)
17
+ * @property {boolean} isAccessor true if the object is functional as an
18
+ * accessor descriptor; false otherwise
19
+ * @property {boolean} isData true if the object is functional as a data
20
+ * descriptor; false otherwise
21
+ * @property {boolean} isValid true if the object is technically a valid
22
+ * `PropertyDescriptor` object or compatible as one.
23
+ * @property {boolean} hasBaseDescriptorKeys true if the object has either
24
+ * `configurable` or `enumerable` property keys and both are `undefined` or
25
+ * of type `boolean`.
26
+ * @property {boolean} hasAccessorKeys true if the object has either `get`
27
+ * or `set` property keys and both are either `undefined` or of type
28
+ * `function`
29
+ * @property {boolean} hasDataKeys true if the object has either `value` or
30
+ * `writable` property keys and if `writable` is present, then it is of type
31
+ * `boolean`
32
+ */
33
+
34
+ /**
35
+ * The response from a call to {@link DescriptorUtils.isDescriptor} can
36
+ * be either a {@link boolean} or a {@link IsDescriptorStats} object.
37
+ *
38
+ * @typedef {IsDescriptorStats | boolean} IsDescriptorResponse
39
+ */
40
+
41
+ /**
42
+ * A set of utility functions work with {@link PropertyDescriptor} objects.
43
+ * The creation of property descriptors can be large in boiler plate so
44
+ * these tools can reduce the boiler plate and increase readability.
45
+ *
46
+ * @name DescriptorUtils
47
+ * @typedef {DescriptorUtils}
48
+ */
49
+ export const DescriptorUtils = {
50
+ /**
51
+ * Creates an accessor descriptor object
52
+ *
53
+ * This function has multiple possible overloads
54
+ *
55
+ * ```markdown
56
+ * _**zeroFn** is a function that takes no parameters_
57
+ * _**oneFn** is a function that takes a single parameter_
58
+ * _**oneOrNone** is a either a function that takes a single parameter or nullish_
59
+ * _**nonFn** is any value that is not a function_
60
+ * _**nullish** is either null or undefined_
61
+ * _**...** means configurable?, enumerable?, storage?, key? liaison? as
62
+ * subsequent following parameters in that order_
63
+ *
64
+ * **accessor()**
65
+ * creates a storage backed accessor that is both read and write.
66
+ * The storage object will be a provided empty object with the key
67
+ * being 'value'. Configurable and enumerable flags will be set to
68
+ * `true`.
69
+ *
70
+ * **accessor(options)**
71
+ * this single argument variant of accessor() consists of a single
72
+ * options object. If neither `get` nor `set` are provided, a
73
+ * storage backed read-write accessor with undefined as the initial
74
+ * value will be constructed.
75
+ *
76
+ * **accessor(nonFn)**
77
+ * **accessor(any, true, options?)**
78
+ * **accessor(any, true, ...)**
79
+ * supplying only a non-function only parameter or any value and the
80
+ * value `true` as the second parameter, you will get a read-write
81
+ * accessor stored in the default or specified storage. The resultant
82
+ * initial value will be whatever is provided as the first parameter.
83
+ * See options to customize `configurable`, `enumerable`, `storage`,
84
+ * `key` and the `liaison` factory function.
85
+ *
86
+ * **accessor(any, false, options?)**
87
+ * **accessor(any, false, ...)**
88
+ * supplying only a non-function only parameter or any value and the
89
+ * value `false` as the second parameter, you will get a read-only
90
+ * getter stored in the default or specified storage. The resultant
91
+ * value will be whatever is provided as the first parameter.
92
+ * See options to customize `configurable`, `enumerable`, `storage`,
93
+ * `key` and the `liaison` factory function.
94
+ *
95
+ * **accessor(zeroFn)**
96
+ * **accessor(zeroFn, nullish, options?)**
97
+ * **accessor(zeroFn, nullish, ...)**
98
+ * creates a generic read-only accessor with the first no-argument
99
+ * function parameter being the getter and either null or undefined
100
+ * for the setter. Either an options object or the manually ordered
101
+ * parameters can optionally follow if a nullish value for setter
102
+ * is provided.
103
+ *
104
+ * **accessor(zeroFn, oneOrNone, options?)**
105
+ * **accessor(zeroFn, oneOrNone, ...)**
106
+ * creates a generic read-write accessor with the first no-argument
107
+ * function parameter being the getter and the second single-argument
108
+ * function parameter being the setter. Either an options object or
109
+ * the manually ordered parameters can optionally follow.
110
+ *
111
+ * **accessor(oneFn, oneFn, options?)**
112
+ * **accessor(oneFn, oneFn, ...)**
113
+ * this special variant of the accessor() invocation, allows a single
114
+ * argument getter and setter factory to be supplied. These will
115
+ * automatically be invoked with the specified or default storage
116
+ * object. The result of the getter factory should be a no argument
117
+ * function. And the result of the setter factory should be a single
118
+ * argument function. The options for `liaison` and `key` will be
119
+ * ignored and should be handled specifically in the factory
120
+ * functions to suit your own use case.
121
+ * ```
122
+ *
123
+ * Options are an object oriented way of supplying the alternate
124
+ * flags to the data descriptor. They are
125
+ *
126
+ * • `get` - only referenced when an options object is the only parameter
127
+ * • `set` - only referenced when an options object is the only parameter
128
+ * • `configurable` - if true, the descriptor of the object that this
129
+ * accessor descriptor represents can be redefined at later time by
130
+ * subsequent calls to {@link Object.defineProperty} or
131
+ * {@link Object.defineProperties}
132
+ * • `enumerable` - if true, enumeration over the object this
133
+ * descriptor is applied to, will show the property
134
+ * represented by this descriptor. See the associated MDN
135
+ * page regarding this {@link PropertyDescriptor} if you
136
+ * want to know more.
137
+ * • `storage` - an object, usually {@link Object} or {@link Map} or
138
+ * nullish if unused
139
+ * • `key` - a {@link PropertyKey} of your choosing or the default
140
+ * string `"value"`
141
+ * • `bind` - true if you wish to have the `storage` object bound as
142
+ * the `thisObj` for both the `get` and `set` functions when
143
+ * storage is used. **note* this will not work if you supply a
144
+ * big arrow function for the accessor function in question. This
145
+ * defaults to `false`.
146
+ * • `liaison` - an optional factory function that must return an
147
+ * object with two properties. The first is a `get()` function that
148
+ * returns a value and the second is a `set(value)` function that
149
+ * stores a value. The factory function receives `storage` and
150
+ * `key`, in that order. The default uses {@link Map.get} and
151
+ * {@link Map.set} if the storage is an instance of {@link Map}.
152
+ * Otherwise, the typical `object[key]` format is used if the
153
+ * storage object is an instanceof {@link Object}
154
+ *
155
+ * @param {(object|any)?} value the JavaScript value representing
156
+ * this descriptor's value or an options object if it is the
157
+ * only parameter.
158
+ * @param {(object|boolean)?} optionsOrConfigurable true or false if
159
+ * you wish the `configurable` flag to be set. Optionally supply an
160
+ * object with one of the supported options to configure the run
161
+ * of the function.
162
+ * @param {boolean?} enumerable true or false if you wish to
163
+ * configure the `.enumerable` property of the descriptor
164
+ * @param {object?} storage an object upon which data storage should
165
+ * be written to and read from. Defaults to an empty {@link Object}
166
+ * when storage is needed and not supplied via this parameter or
167
+ * an options object.
168
+ * @param {PropertyKey?} key the key used to store content on the
169
+ * storage object.
170
+ * @param {(object, PropertyKey) => { get: ()=>any, set: (v)=>void}} liaison
171
+ * an optional function that, given a storage object and property key,
172
+ * returns an object with a no argument `get()` function that returns
173
+ * the value on the storage object with a given key and a single argument
174
+ * `set(value)` function that stores a new value using the property key
175
+ * on the supplied storage object. This exists because access to a
176
+ * {@link Map} and {@link Object} values are handled differently. If you
177
+ * need support for some other class than `Map` or `Object` then you
178
+ * should provide a liaison function to define access.
179
+ *
180
+ * @returns {PropertyDescriptor}
181
+ *
182
+ * @see {@link PropertyDescriptor}
183
+ * @note More info on this can be found at the
184
+ * [MDN Object.defineProperty/ies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)
185
+ * page
186
+ *
187
+ * @type {Function & { keys: string[] }}
188
+ */
189
+ get accessor() {
190
+ function accessor(
191
+ get,
192
+ set,
193
+ optionsOrConfigurable = true,
194
+ enumerable = true,
195
+ storage,
196
+ key = 'value',
197
+ liaison
198
+ ) {
199
+ const count = arguments.length
200
+ const storageKeys = ['storage', 'key', 'liaison', 'initial', 'bind']
201
+ const optionKeys = [
202
+ // accessor functions
203
+ 'get', 'set',
204
+
205
+ // descriptor flags
206
+ 'configurable', 'enumerable',
207
+
208
+ // storage configuration keys
209
+ ...storageKeys
210
+ ]
211
+ const has = (object, key) => isObject(object) && Reflect.has(object, key)
212
+ const isOpts = object => optionKeys.some(key => has(object, key))
213
+
214
+ let configurable = !!optionsOrConfigurable
215
+ let initial = undefined
216
+ let bind = false
217
+ let options = {}
218
+
219
+ if (count === 1 && isObject(get) && hasSome(get, ...optionKeys)) {
220
+ options = { ...get };
221
+ ({ get, set } = get);
222
+ }
223
+
224
+ if (isObject(optionsOrConfigurable) || isObject(set)) {
225
+ options = isObject(set) && count === 2
226
+ ? { ...set }
227
+ : { ...optionsOrConfigurable };
228
+
229
+ ({ configurable, enumerable, storage, key, bind, initial } = options)
230
+ }
231
+
232
+ liaison = options?.liaison ?? liaison ?? ((storage, key) => ({
233
+ get() {
234
+ if (storage instanceof Map)
235
+ return storage.get(key)
236
+ else if (isObject(storage))
237
+ return storage[key]
238
+ },
239
+ set(value) {
240
+ if (storage instanceof Map)
241
+ storage.set(key, value)
242
+ else if (isObject(storage))
243
+ storage[key] = value
244
+ }
245
+ }))
246
+
247
+ configurable = configurable ?? true
248
+ enumerable = enumerable ?? true
249
+ key = key ?? 'value'
250
+ bind = bind ?? false
251
+
252
+ const yesFn = (value) => typeof value === 'function'
253
+ const zeroFn = (value) => (yesFn(value) && value.length === 0)
254
+ const oneFn = (value) => (yesFn(value) && value.length === 1)
255
+ const isTrue = (value) => value === true
256
+ const isFalse = (value) => value === false
257
+ const addRefs = (fn, value) => Object.defineProperties(fn, {
258
+ storage: { value, configurable: true, enumerable: true },
259
+ key: { value: key, configurable: true, enumerable: true },
260
+ })
261
+
262
+ if (count === 0 || (!get && !set)) {
263
+ storage = { [key]: initial }
264
+ const _ = liaison(storage, key)
265
+
266
+ get = addRefs(function() { return _.get() }, storage)
267
+ set = addRefs(function(value) { _.set(value) }, storage)
268
+
269
+ return { get, set, configurable, enumerable }
270
+ }
271
+
272
+ if (count === 1 && oneFn(get)) {
273
+ set = false
274
+ }
275
+
276
+ if (isTrue(set) || isFalse(set)) {
277
+ storage = { [key]: get }
278
+ const _ = liaison(storage, key)
279
+
280
+ let _get = function() { return _.get() }
281
+ let _set = function(value) { _.set(value) }
282
+
283
+ if (bind) {
284
+ _get = _get.bind(storage)
285
+ _set = _set.bind(storage)
286
+ }
287
+
288
+ get = addRefs(_get, storage)
289
+ set = addRefs(_set, storage)
290
+
291
+ return {
292
+ get,
293
+ set: isFalse(set) ? undefined : set,
294
+ configurable,
295
+ enumerable
296
+ }
297
+ }
298
+
299
+ if ((zeroFn(get) && !set) || (zeroFn(get) && oneFn(set))) {
300
+ const descriptor = { get, set, configurable, enumerable }
301
+
302
+ if (isObject(options) && Reflect.has(options, 'initial'))
303
+ descriptor.set(initial)
304
+
305
+ return descriptor
306
+ }
307
+
308
+ if (oneFn(get) && oneFn(set)) {
309
+ storage = storage || { }
310
+
311
+ let _get = get(storage)
312
+ let _set = set(storage)
313
+
314
+ if (bind) {
315
+ _get = _get.bind(storage)
316
+ _set = _set.bind(storage)
317
+ }
318
+
319
+ return {
320
+ get: addRefs(_get, storage),
321
+ set: addRefs(_set, storage),
322
+ configurable,
323
+ enumerable,
324
+ }
325
+ }
326
+
327
+ return { get, set, configurable, enumerable }
328
+ }
329
+
330
+ Object.defineProperty(accessor, 'keys', {
331
+ get() { return ['get', 'set', 'configurable', 'enumerable'] },
332
+ configurable: true,
333
+ enumerable: true,
334
+ })
335
+
336
+ return accessor
337
+ },
338
+
339
+ /**
340
+ * Creates a data descriptor object
341
+ *
342
+ * This function has multiple possible overloads
343
+ *
344
+ * ```markdown
345
+ * **data()**
346
+ * creates a data descriptor with a value of `undefined` that
347
+ * is writable, configurable and enumerable.
348
+ *
349
+ * **data(options)**
350
+ * if the only parameter is an object and that object contains
351
+ * at least a `.value` property, this funtion will return a
352
+ * data descriptor with the associated values. This variant
353
+ * is useful if you want to extract the normal data descriptor
354
+ * properties: value, writable, configurable and/or enumerable
355
+ * from an object that has properties with these names, in
356
+ * addition to other properties or functions. Note that if you
357
+ * wish for the value of the descriptor to be an object that
358
+ * also contains a `.value` property, use `data({value: obj})`
359
+ * instead.
360
+ *
361
+ * **data(value)**
362
+ * **data(value, options?)**
363
+ * creates a data descriptor from the supplied `value`,
364
+ * optionally augmented by additional `options`. The defaults
365
+ * for this writable, configurable and enumerable values set
366
+ * to `true`
367
+ *
368
+ * **data(value, writable?, configurable?, enumerable?)**
369
+ * if writable, configurable or enumerable or true or false
370
+ * then this function creates a data descriptor with those
371
+ * flags and the supplied value (there's no real reason to
372
+ * invoke this function if you're supplying all four values)
373
+ * ```
374
+ *
375
+ * Options are an object oriented way of supplying the alternate
376
+ * flags to the data descriptor. They are
377
+ *
378
+ * • `value` - only referenced when an options object is the
379
+ * only parameter
380
+ * • `writable` - true if the value represented by this data
381
+ * descriptor can be reassigned a new value.
382
+ * • `configurable` - if true, the descriptor of the object
383
+ * that this data descriptor represents can be redefined at
384
+ * later time by subsequent calls to `Object.defineProperty`
385
+ * or `Object.defineProperties`. If `.configurable` is true
386
+ * this can be done even if `.writable` is set to false
387
+ * • `enumerable` - if true, enumeration over the object this
388
+ * descriptor is applied to, will show the property
389
+ * represented by this descriptor. See the associated MDN
390
+ * page regarding this `PropertyDescriptor` if you want to
391
+ * know more.
392
+ *
393
+ * @param {(object|any)?} value the JavaScript value representing
394
+ * this descriptor's value or an options object if it is the
395
+ * only parameter.
396
+ * @param {(object|boolean)?} optionsOrWritable true or false if
397
+ * you wish the writable flag to be set. Optionally supply an
398
+ * object with one of the supported options to configure the run
399
+ * of the function.
400
+ * @param {boolean?} configurable true or false if you wish to
401
+ * configure the `.configurable` property of the descriptor
402
+ * @param {boolean?} enumerable true or false if you wish to
403
+ * configure the `.enumerable` property of the descriptor
404
+ * @returns {PropertyDescriptor}
405
+ *
406
+ * @note More info on this can be found at the
407
+ * [MDN Object.defineProperty/ies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)
408
+ * page
409
+ *
410
+ * @type {Function & { keys: string[] }}
411
+ */
412
+ get data() {
413
+ function data(value, optionsOrWritable, configurable, enumerable) {
414
+ if (arguments.length === 0) {
415
+ return {
416
+ value: undefined,
417
+ writable: true,
418
+ configurable: true,
419
+ enumerable: true
420
+ }
421
+ }
422
+
423
+ let writable = isObject(optionsOrWritable)
424
+ ? !!optionsOrWritable // further defined below
425
+ : true;
426
+ let options = typeof optionsOrWritable === 'boolean'
427
+ ? {}
428
+ : optionsOrWritable;
429
+
430
+ configurable = configurable ?? true
431
+ enumerable = enumerable ?? true
432
+
433
+ if (isObject(value) && hasSome(value, 'value')) {
434
+ options = value;
435
+ ({ value } = value);
436
+ }
437
+
438
+ if (isObject(optionsOrWritable)) {
439
+ ({writable, configurable, enumerable} = {
440
+ ...{writable: true, configurable: true, enumerable: true,},
441
+ ...optionsOrWritable
442
+ })
443
+ }
444
+
445
+ return { value, writable, configurable, enumerable }
446
+ }
447
+
448
+ Object.defineProperty(data, 'keys', {
449
+ value: ['value', 'writable', 'configurable', 'enumerable'],
450
+ writable: false,
451
+ configurable: true,
452
+ enumerable: true
453
+ })
454
+
455
+ return data
456
+ },
457
+
458
+ /**
459
+ * A function that, given a value that might be a `PropertyDescriptor`,
460
+ * calculates a deterministic probability that the supplied value is
461
+ * an object that either is a `PropertyDescriptor` or that can function
462
+ * as one.
463
+ *
464
+ * @param {unknown} value a JavaScript value that might be a
465
+ * `PropertyDescriptor` type.
466
+ * @param {boolean?} returnStats if this value is true, instead of returning
467
+ * a determined boolean value indicating the supplied value might be a
468
+ * `PropertyDescriptor`, an object containing the determined flags and score
469
+ * the led to the determination instead is returned.
470
+ * @param {boolean?} strict if this value is `true`, which is the default,
471
+ * then the function will not allow descriptor compatible objects, rather it
472
+ * will only return true if the object has keys that belong in a descriptor
473
+ * and do not form an invalid combination.
474
+ * @returns {IsDescriptorResponse} if `returnStats` is `true`
475
+ * an object of type {@link IsDescriptorStats} is returned. This object
476
+ * will have a lot of {@link Boolean} flags pertaining to the `true`/`false`
477
+ * evaluation. If `returnStats` is `false`, then a boolean value denoting
478
+ * whether or not the value is a {@link PropertyDescriptor} is returned
479
+ * instead. This is effectively the same as the `isValid` parameter from the
480
+ * stats block.
481
+ */
482
+ isDescriptor(value, returnStats = false, strict = true) {
483
+ if (!value || typeof value !== 'object' || !(value instanceof Object))
484
+ return false
485
+
486
+ const areBools = (...props) => props.flat().every(
487
+ prop => boolTypes.includes(typeof value[prop])
488
+ );
489
+
490
+ const areFuncs = (...props) => props.flat().every(
491
+ prop => funcTypes.includes(typeof value[prop])
492
+ );
493
+
494
+ const hasKeyFn = (property) => Reflect.has(value, property)
495
+ const isOfType = (type) => (element) => typeof element === type
496
+
497
+ const baseProps = [ 'configurable', 'enumerable' ]
498
+ const dataProps = [ 'value', 'writable' ]
499
+ const accessorProps = [ 'get', 'set' ]
500
+ const anyDescProps = [ ...baseProps, ...dataProps, ...accessorProps ]
501
+ const boolTypes = [ 'undefined', 'boolean' ]
502
+ const funcTypes = [ 'undefined', 'function' ]
503
+ const stats = {
504
+ confidence: 0,
505
+ hasAccessorKeys: false,
506
+ hasBaseDescriptorKeys: false,
507
+ hasDataKeys: false,
508
+ isAccessor: false,
509
+ isData: false,
510
+ isValid: false,
511
+ }
512
+
513
+ let score = 0
514
+
515
+ if (value && typeof value === 'object') {
516
+ const objKeys = Reflect.ownKeys(value)
517
+ const nonDescKeys = objKeys.filter(k => !(anyDescProps.includes(k)))
518
+
519
+ if (strict && nonDescKeys.length)
520
+ return false
521
+
522
+ if (objKeys.length <= 4)
523
+ score++
524
+
525
+ stats.hasAccessorKeys =
526
+ accessorProps.some(hasKeyFn) && areFuncs(accessorProps)
527
+
528
+ stats.hasDataKeys =
529
+ dataProps.some(hasKeyFn) && areBools('writable')
530
+
531
+ stats.hasBaseDescriptorKeys =
532
+ baseProps.some(hasKeyFn) && areBools(baseProps)
533
+
534
+ if (stats.hasBaseDescriptorKeys)
535
+ score++
536
+
537
+ if (stats.hasAccessorKeys || stats.hasDataKeys)
538
+ score++
539
+
540
+ if (score > 0)
541
+ stats.isValid = true
542
+
543
+ if (score > 0 && stats.hasAccessorKeys)
544
+ stats.isAccessor = true
545
+
546
+ if (score > 0 && stats.hasDataKeys)
547
+ stats.isData = true
548
+
549
+ if (stats.isValid && stats.isData && Reflect.has(value, 'value'))
550
+ score++
551
+
552
+ else if (stats.isValid && stats.isAccessor) {
553
+ if ([value?.get, value?.set].some(isOfType('function')))
554
+ score++
555
+ }
556
+
557
+ if (stats.hasAccessorKeys && stats.hasDataKeys) {
558
+ score = 0
559
+ stats.isValid = false
560
+ }
561
+
562
+ stats.confidence = parseFloat(score / 4.0)
563
+ }
564
+
565
+ if (returnStats)
566
+ return stats
567
+
568
+ return score >= 0.0
569
+ ? true
570
+ : false;
571
+ },
572
+
573
+ /**
574
+ * Retrieves the keys associated with accessor descriptors.
575
+ *
576
+ * Accessor descriptors are property descriptors that define
577
+ * getter and/or setter functions for a property.
578
+ *
579
+ * @type {string[]}
580
+ *
581
+ * @example
582
+ * // Example usage:
583
+ * const keys = DescriptorUtils.kAccessorDescriptorKeys
584
+ * console.log(keys) // Output: ['get', 'set']
585
+ */
586
+ get kAccessorDescriptorKeys() {
587
+ return ['get', 'set']
588
+ },
589
+
590
+ /**
591
+ * Retrieves the keys associated with data descriptors.
592
+ *
593
+ * Data descriptors are property descriptors that define a value
594
+ * and optionally a writable attribute for a property.
595
+ *
596
+ * @type {string[]}
597
+ *
598
+ * @example
599
+ * // Example usage:
600
+ * const keys = DescriptorUtils.kDataDescriptorKeys
601
+ * console.log(keys) // Output: ['value', 'writable']
602
+ */
603
+ get kDataDescriptorKeys() {
604
+ return ['value', 'writable']
605
+ },
606
+
607
+ /**
608
+ * Retrieves the keys associated with shared descriptors.
609
+ *
610
+ * Shared descriptors are property descriptors that define
611
+ * common attributes for a property, such as whether the
612
+ * property is configurable or enumerable.
613
+ *
614
+ * @type {string[]}
615
+ *
616
+ * @example
617
+ * // Example usage:
618
+ * const keys = DescriptorUtils.kSharedDescriptorKeys
619
+ * console.log(keys) // Output: ['configurable', 'enumerable']
620
+ */
621
+ get kSharedDescriptorKeys() {
622
+ return ['configurable', 'enumerable']
623
+ },
624
+
625
+ /**
626
+ * Retrieves all descriptor keys, combining accessor, data, and shared
627
+ * descriptor keys.
628
+ *
629
+ * This getter method aggregates keys from accessor descriptors,
630
+ * data descriptors, and shared descriptors into a single array.
631
+ * This can be useful when you need to validate or inspect all
632
+ * possible descriptor keys.
633
+ *
634
+ * @type {string[]}
635
+ *
636
+ * @example
637
+ * // Example usage:
638
+ * const allKeys = DescriptorUtils.kDescriptorKeys
639
+ * console.log(allKeys)
640
+ * // Output: [
641
+ * // 'get', 'set', 'value', 'writable', 'configurable', 'enumerable'
642
+ * // ]
643
+ */
644
+ get kDescriptorKeys() {
645
+ return [
646
+ ...(this.kAccessorDescriptorKeys),
647
+ ...(this.kDataDescriptorKeys),
648
+ ...(this.kSharedDescriptorKeys),
649
+ ]
650
+ }
651
+ }
652
+
653
+ // Destructure the functions individually...
654
+ const { accessor, data, isDescriptor } = DescriptorUtils
655
+
656
+ // ...also destructure the constants individually....
657
+ const {
658
+ kAccessorDescriptorKeys,
659
+ kDataDescriptorKeys,
660
+ kDescriptorKeys,
661
+ kSharedDescriptorKeys
662
+ } = DescriptorUtils
663
+
664
+ // ... so they can also be individually exported.
665
+ export {
666
+ accessor,
667
+ data,
668
+ isDescriptor,
669
+
670
+ kAccessorDescriptorKeys,
671
+ kDataDescriptorKeys,
672
+ kDescriptorKeys,
673
+ kSharedDescriptorKeys
674
+ }
675
+
676
+ // Provide default exports as well
677
+ export default {
678
+ DescriptorUtils,
679
+
680
+ accessor,
681
+ data,
682
+ isDescriptor,
683
+ kAccessorDescriptorKeys,
684
+ kDataDescriptorKeys,
685
+ kDescriptorKeys,
686
+ kSharedDescriptorKeys
687
+ }
688
+
689
+ // ---- non-exported helper functions ----
690
+
691
+ function isObject(o) { return o && typeof o === 'object' }
692
+ function isValidKey(o) { return ['string','symbol'].some(t => typeof o === t) }
693
+ function hasAll(object, ...keys) { return hasQuantity('every', object, keys) }
694
+ function hasSome(object, ...keys) { return hasQuantity('some', object, keys) }
695
+ function hasQuantity(quantityFn, object, keys) {
696
+ return isObject(object) && (keys.flat(Infinity)
697
+ .map(key => Reflect.has(object, key))
698
+ [quantityFn](has => has)
699
+ )
700
+ }
701
+ function hasOne(object, ...keys) {
702
+ return isObject(object) && (keys.flat(Infinity)
703
+ .map(key => Reflect.has(object, key))
704
+ .filter(has => has)
705
+ .length === 1
706
+ )
707
+ }