@nejs/basic-extensions 2.6.0 → 2.8.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.
- package/.vscode/settings.json +5 -0
- package/README.md +6129 -1574
- package/dist/@nejs/basic-extensions.bundle.2.7.0.js +19 -0
- package/dist/@nejs/basic-extensions.bundle.2.7.0.js.map +7 -0
- package/dist/cjs/array.extensions.d.ts +39 -0
- package/dist/cjs/array.extensions.js +303 -0
- package/dist/cjs/array.extensions.js.map +1 -0
- package/dist/cjs/big.int.extension.d.ts +31 -0
- package/dist/cjs/big.int.extension.js +164 -0
- package/dist/cjs/big.int.extension.js.map +1 -0
- package/dist/cjs/{newClasses → classes}/asyncIterable.js +32 -44
- package/dist/cjs/classes/asyncIterable.js.map +1 -0
- package/dist/cjs/{newClasses → classes}/deferred.js +66 -138
- package/dist/cjs/classes/deferred.js.map +1 -0
- package/dist/cjs/{newClasses → classes}/descriptor.js +56 -90
- package/dist/cjs/classes/descriptor.js.map +1 -0
- package/dist/cjs/classes/index.d.ts +13 -0
- package/dist/cjs/classes/index.js +57 -0
- package/dist/cjs/classes/index.js.map +1 -0
- package/dist/cjs/classes/introspector.d.ts +20 -0
- package/dist/cjs/classes/introspector.js +130 -0
- package/dist/cjs/classes/introspector.js.map +1 -0
- package/dist/cjs/{newClasses → classes}/iterable.js +42 -63
- package/dist/cjs/classes/iterable.js.map +1 -0
- package/dist/cjs/classes/param.parser.d.ts +227 -0
- package/dist/cjs/classes/param.parser.js +242 -0
- package/dist/cjs/classes/param.parser.js.map +1 -0
- package/dist/cjs/classes/pluggable.proxy.d.ts +152 -0
- package/dist/cjs/classes/pluggable.proxy.js +444 -0
- package/dist/cjs/classes/pluggable.proxy.js.map +1 -0
- package/dist/cjs/{newClasses → classes}/refmap.js +18 -30
- package/dist/cjs/classes/refmap.js.map +1 -0
- package/dist/cjs/{newClasses → classes}/refset.js +28 -47
- package/dist/cjs/classes/refset.js.map +1 -0
- package/dist/cjs/classes/symkeys.d.ts +292 -0
- package/dist/cjs/classes/symkeys.js +424 -0
- package/dist/cjs/classes/symkeys.js.map +1 -0
- package/dist/cjs/classes/type.d.ts +56 -0
- package/dist/cjs/classes/type.js +405 -0
- package/dist/cjs/classes/type.js.map +1 -0
- package/dist/cjs/function.extensions.js +757 -0
- package/dist/cjs/function.extensions.js.map +1 -0
- package/dist/cjs/global.this.js +261 -0
- package/dist/cjs/global.this.js.map +1 -0
- package/dist/cjs/index.d.ts +4 -3
- package/dist/cjs/index.js +62 -32
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/json.extensions.d.ts +2 -0
- package/dist/cjs/json.extensions.js +107 -0
- package/dist/cjs/json.extensions.js.map +1 -0
- package/dist/{mjs/mapextensions.d.ts → cjs/map.extensions.d.ts} +1 -0
- package/dist/cjs/map.extensions.js +142 -0
- package/dist/cjs/map.extensions.js.map +1 -0
- package/dist/cjs/number.extension.d.ts +44 -0
- package/dist/cjs/number.extension.js +260 -0
- package/dist/cjs/number.extension.js.map +1 -0
- package/dist/cjs/object.extensions.d.ts +62 -0
- package/dist/cjs/object.extensions.js +1116 -0
- package/dist/cjs/object.extensions.js.map +1 -0
- package/dist/cjs/proxy.extensions.d.ts +2 -0
- package/dist/cjs/proxy.extensions.js +207 -0
- package/dist/cjs/proxy.extensions.js.map +1 -0
- package/dist/cjs/reflect.extensions.js +316 -0
- package/dist/cjs/reflect.extensions.js.map +1 -0
- package/dist/cjs/regular.expression.extensions.d.ts +2 -0
- package/dist/cjs/regular.expression.extensions.js +423 -0
- package/dist/cjs/regular.expression.extensions.js.map +1 -0
- package/dist/cjs/set.extensions.d.ts +40 -0
- package/dist/cjs/{setextensions.js → set.extensions.js} +150 -2
- package/dist/cjs/set.extensions.js.map +1 -0
- package/dist/cjs/string.extensions.js +471 -0
- package/dist/cjs/string.extensions.js.map +1 -0
- package/dist/{mjs/symbolextensions.d.ts → cjs/symbol.extensions.d.ts} +1 -0
- package/dist/cjs/symbol.extensions.js +259 -0
- package/dist/cjs/symbol.extensions.js.map +1 -0
- package/dist/cjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
- package/dist/cjs/weakref.extensions.js.map +1 -0
- package/dist/mjs/array.extensions.d.ts +39 -0
- package/dist/mjs/array.extensions.js +300 -0
- package/dist/mjs/array.extensions.js.map +1 -0
- package/dist/mjs/big.int.extension.d.ts +31 -0
- package/dist/mjs/big.int.extension.js +161 -0
- package/dist/mjs/big.int.extension.js.map +1 -0
- package/dist/mjs/classes/asyncIterable.js.map +1 -0
- package/dist/mjs/classes/deferred.js.map +1 -0
- package/dist/mjs/{newClasses → classes}/descriptor.js +7 -4
- package/dist/mjs/classes/descriptor.js.map +1 -0
- package/dist/mjs/classes/index.d.ts +13 -0
- package/dist/mjs/classes/index.js +40 -0
- package/dist/mjs/classes/index.js.map +1 -0
- package/dist/mjs/classes/introspector.d.ts +20 -0
- package/dist/mjs/classes/introspector.js +126 -0
- package/dist/mjs/classes/introspector.js.map +1 -0
- package/dist/mjs/classes/iterable.js.map +1 -0
- package/dist/mjs/classes/param.parser.d.ts +227 -0
- package/dist/mjs/classes/param.parser.js +238 -0
- package/dist/mjs/classes/param.parser.js.map +1 -0
- package/dist/mjs/classes/pluggable.proxy.d.ts +152 -0
- package/dist/mjs/classes/pluggable.proxy.js +438 -0
- package/dist/mjs/classes/pluggable.proxy.js.map +1 -0
- package/dist/mjs/{newClasses → classes}/refmap.js +3 -3
- package/dist/mjs/classes/refmap.js.map +1 -0
- package/dist/mjs/classes/refset.js.map +1 -0
- package/dist/mjs/classes/symkeys.d.ts +292 -0
- package/dist/mjs/classes/symkeys.js +420 -0
- package/dist/mjs/classes/symkeys.js.map +1 -0
- package/dist/mjs/classes/type.d.ts +56 -0
- package/dist/mjs/classes/type.js +401 -0
- package/dist/mjs/classes/type.js.map +1 -0
- package/dist/mjs/function.extensions.js +754 -0
- package/dist/mjs/function.extensions.js.map +1 -0
- package/dist/mjs/global.this.js +258 -0
- package/dist/mjs/global.this.js.map +1 -0
- package/dist/mjs/index.d.ts +4 -3
- package/dist/mjs/index.js +49 -19
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/json.extensions.d.ts +2 -0
- package/dist/mjs/json.extensions.js +104 -0
- package/dist/mjs/json.extensions.js.map +1 -0
- package/dist/{cjs/mapextensions.d.ts → mjs/map.extensions.d.ts} +1 -0
- package/dist/mjs/map.extensions.js +139 -0
- package/dist/mjs/map.extensions.js.map +1 -0
- package/dist/mjs/number.extension.d.ts +44 -0
- package/dist/mjs/number.extension.js +257 -0
- package/dist/mjs/number.extension.js.map +1 -0
- package/dist/mjs/object.extensions.d.ts +62 -0
- package/dist/mjs/object.extensions.js +1112 -0
- package/dist/mjs/object.extensions.js.map +1 -0
- package/dist/mjs/proxy.extensions.d.ts +2 -0
- package/dist/mjs/proxy.extensions.js +204 -0
- package/dist/mjs/proxy.extensions.js.map +1 -0
- package/dist/mjs/reflect.extensions.js +313 -0
- package/dist/mjs/reflect.extensions.js.map +1 -0
- package/dist/mjs/regular.expression.extensions.d.ts +2 -0
- package/dist/mjs/regular.expression.extensions.js +420 -0
- package/dist/mjs/regular.expression.extensions.js.map +1 -0
- package/dist/mjs/set.extensions.d.ts +40 -0
- package/dist/mjs/{setextensions.js → set.extensions.js} +149 -1
- package/dist/mjs/set.extensions.js.map +1 -0
- package/dist/mjs/string.extensions.js +468 -0
- package/dist/mjs/string.extensions.js.map +1 -0
- package/dist/{cjs/symbolextensions.d.ts → mjs/symbol.extensions.d.ts} +1 -0
- package/dist/mjs/symbol.extensions.js +256 -0
- package/dist/mjs/symbol.extensions.js.map +1 -0
- package/dist/mjs/{weakrefextensions.js → weakref.extensions.js} +1 -1
- package/dist/mjs/weakref.extensions.js.map +1 -0
- package/docs/index.html +24045 -5805
- package/package.json +6 -4
- package/repl.bootstrap.js +21 -0
- package/src/array.extensions.js +322 -0
- package/src/big.int.extension.js +163 -0
- package/src/{newClasses → classes}/descriptor.js +16 -12
- package/src/classes/index.js +51 -0
- package/src/classes/introspector.js +167 -0
- package/src/classes/param.parser.js +253 -0
- package/src/classes/pluggable.proxy.js +485 -0
- package/src/{newClasses → classes}/refmap.js +5 -3
- package/src/classes/symkeys.js +464 -0
- package/src/classes/type.js +427 -0
- package/src/function.extensions.js +818 -0
- package/src/global.this.js +304 -0
- package/src/index.js +56 -23
- package/src/json.extensions.js +108 -0
- package/src/map.extensions.js +144 -0
- package/src/number.extension.js +273 -0
- package/src/object.extensions.js +1222 -0
- package/src/proxy.extensions.js +229 -0
- package/src/reflect.extensions.js +346 -0
- package/src/regular.expression.extensions.js +451 -0
- package/src/{setextensions.js → set.extensions.js} +151 -2
- package/src/string.extensions.js +515 -0
- package/src/symbol.extensions.js +268 -0
- package/tests/newClasses/refmap.test.js +3 -2
- package/tsconfig.base.json +5 -3
- package/tsconfig.cjs.json +2 -2
- package/tsconfig.esm.json +2 -2
- package/dist/@nejs/basic-extensions.bundle.2.5.0.js +0 -8
- package/dist/@nejs/basic-extensions.bundle.2.5.0.js.map +0 -7
- package/dist/cjs/arrayextensions.d.ts +0 -10
- package/dist/cjs/arrayextensions.js +0 -73
- package/dist/cjs/arrayextensions.js.map +0 -1
- package/dist/cjs/functionextensions.js +0 -202
- package/dist/cjs/functionextensions.js.map +0 -1
- package/dist/cjs/globals.js +0 -166
- package/dist/cjs/globals.js.map +0 -1
- package/dist/cjs/mapextensions.js +0 -32
- package/dist/cjs/mapextensions.js.map +0 -1
- package/dist/cjs/newClasses/asyncIterable.js.map +0 -1
- package/dist/cjs/newClasses/deferred.js.map +0 -1
- package/dist/cjs/newClasses/descriptor.js.map +0 -1
- package/dist/cjs/newClasses/iterable.js.map +0 -1
- package/dist/cjs/newClasses/refmap.js.map +0 -1
- package/dist/cjs/newClasses/refset.js.map +0 -1
- package/dist/cjs/objectextensions.d.ts +0 -11
- package/dist/cjs/objectextensions.js +0 -232
- package/dist/cjs/objectextensions.js.map +0 -1
- package/dist/cjs/reflectextensions.js +0 -111
- package/dist/cjs/reflectextensions.js.map +0 -1
- package/dist/cjs/setextensions.d.ts +0 -2
- package/dist/cjs/setextensions.js.map +0 -1
- package/dist/cjs/stringextensions.js +0 -158
- package/dist/cjs/stringextensions.js.map +0 -1
- package/dist/cjs/symbolextensions.js +0 -69
- package/dist/cjs/symbolextensions.js.map +0 -1
- package/dist/cjs/weakrefextensions.js.map +0 -1
- package/dist/mjs/arrayextensions.d.ts +0 -10
- package/dist/mjs/arrayextensions.js +0 -70
- package/dist/mjs/arrayextensions.js.map +0 -1
- package/dist/mjs/functionextensions.js +0 -199
- package/dist/mjs/functionextensions.js.map +0 -1
- package/dist/mjs/globals.js +0 -163
- package/dist/mjs/globals.js.map +0 -1
- package/dist/mjs/mapextensions.js +0 -29
- package/dist/mjs/mapextensions.js.map +0 -1
- package/dist/mjs/newClasses/asyncIterable.js.map +0 -1
- package/dist/mjs/newClasses/deferred.js.map +0 -1
- package/dist/mjs/newClasses/descriptor.js.map +0 -1
- package/dist/mjs/newClasses/iterable.js.map +0 -1
- package/dist/mjs/newClasses/refmap.js.map +0 -1
- package/dist/mjs/newClasses/refset.js.map +0 -1
- package/dist/mjs/objectextensions.d.ts +0 -11
- package/dist/mjs/objectextensions.js +0 -229
- package/dist/mjs/objectextensions.js.map +0 -1
- package/dist/mjs/reflectextensions.js +0 -108
- package/dist/mjs/reflectextensions.js.map +0 -1
- package/dist/mjs/setextensions.d.ts +0 -2
- package/dist/mjs/setextensions.js.map +0 -1
- package/dist/mjs/stringextensions.js +0 -155
- package/dist/mjs/stringextensions.js.map +0 -1
- package/dist/mjs/symbolextensions.js +0 -66
- package/dist/mjs/symbolextensions.js.map +0 -1
- package/dist/mjs/weakrefextensions.js.map +0 -1
- package/src/arrayextensions.js +0 -75
- package/src/functionextensions.js +0 -225
- package/src/globals.js +0 -196
- package/src/mapextensions.js +0 -32
- package/src/objectextensions.js +0 -256
- package/src/reflectextensions.js +0 -118
- package/src/stringextensions.js +0 -166
- package/src/symbolextensions.js +0 -69
- /package/dist/cjs/{newClasses → classes}/asyncIterable.d.ts +0 -0
- /package/dist/cjs/{newClasses → classes}/deferred.d.ts +0 -0
- /package/dist/cjs/{newClasses → classes}/descriptor.d.ts +0 -0
- /package/dist/cjs/{newClasses → classes}/iterable.d.ts +0 -0
- /package/dist/cjs/{newClasses → classes}/refmap.d.ts +0 -0
- /package/dist/cjs/{newClasses → classes}/refset.d.ts +0 -0
- /package/dist/cjs/{functionextensions.d.ts → function.extensions.d.ts} +0 -0
- /package/dist/cjs/{globals.d.ts → global.this.d.ts} +0 -0
- /package/dist/cjs/{reflectextensions.d.ts → reflect.extensions.d.ts} +0 -0
- /package/dist/cjs/{stringextensions.d.ts → string.extensions.d.ts} +0 -0
- /package/dist/cjs/{weakrefextensions.d.ts → weakref.extensions.d.ts} +0 -0
- /package/dist/mjs/{newClasses → classes}/asyncIterable.d.ts +0 -0
- /package/dist/mjs/{newClasses → classes}/asyncIterable.js +0 -0
- /package/dist/mjs/{newClasses → classes}/deferred.d.ts +0 -0
- /package/dist/mjs/{newClasses → classes}/deferred.js +0 -0
- /package/dist/mjs/{newClasses → classes}/descriptor.d.ts +0 -0
- /package/dist/mjs/{newClasses → classes}/iterable.d.ts +0 -0
- /package/dist/mjs/{newClasses → classes}/iterable.js +0 -0
- /package/dist/mjs/{newClasses → classes}/refmap.d.ts +0 -0
- /package/dist/mjs/{newClasses → classes}/refset.d.ts +0 -0
- /package/dist/mjs/{newClasses → classes}/refset.js +0 -0
- /package/dist/mjs/{functionextensions.d.ts → function.extensions.d.ts} +0 -0
- /package/dist/mjs/{globals.d.ts → global.this.d.ts} +0 -0
- /package/dist/mjs/{reflectextensions.d.ts → reflect.extensions.d.ts} +0 -0
- /package/dist/mjs/{stringextensions.d.ts → string.extensions.d.ts} +0 -0
- /package/dist/mjs/{weakrefextensions.d.ts → weakref.extensions.d.ts} +0 -0
- /package/src/{newClasses → classes}/asyncIterable.js +0 -0
- /package/src/{newClasses → classes}/deferred.js +0 -0
- /package/src/{newClasses → classes}/iterable.js +0 -0
- /package/src/{newClasses → classes}/refset.js +0 -0
- /package/src/{weakrefextensions.js → weakref.extensions.js} +0 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SymbolPrototypeExtensions = exports.SymbolExtensions = void 0;
|
|
4
|
+
const extension_1 = require("@nejs/extension");
|
|
5
|
+
const symkeys_js_1 = require("./classes/symkeys.js");
|
|
6
|
+
const json_extensions_js_1 = require("./json.extensions.js");
|
|
7
|
+
const { extractFrom, mightContain } = json_extensions_js_1.JSONExtensions.patches;
|
|
8
|
+
/**
|
|
9
|
+
* `SymbolExtensions` is a patch for the JavaScript built-in `Symbol` class. It
|
|
10
|
+
* adds utility methods to the `Symbol` class without modifying the global namespace
|
|
11
|
+
* directly. This patch includes methods for key validation, object type checking,
|
|
12
|
+
* and retrieving the string tag of an object. These methods are useful for
|
|
13
|
+
* enhancing the capabilities of the standard `Symbol` class with additional
|
|
14
|
+
* utility functions.
|
|
15
|
+
*/
|
|
16
|
+
exports.SymbolExtensions = new extension_1.Patch(Symbol, {
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new Symbol with the given name and optional data. If data
|
|
19
|
+
* is provided, it will be stringified and appended to the symbol's
|
|
20
|
+
* name. This method is useful for creating unique symbols that carry
|
|
21
|
+
* additional metadata.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} name The name of the symbol.
|
|
24
|
+
* @param {*} [data] Optional data to be associated with the symbol.
|
|
25
|
+
* @returns {symbol} A new symbol created with Symbol.for(), using the
|
|
26
|
+
* provided name and stringified data (if provided).
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const symbolWithData = Symbol.withData('mySymbol', { foo: 'bar' })
|
|
30
|
+
* console.log(symbolWithData.toString())
|
|
31
|
+
* // Output: "Symbol(mySymbol {"foo":"bar"})"
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* const symbolWithoutData = Symbol.withData('mySymbol')
|
|
35
|
+
* console.log(symbolWithoutData.toString())
|
|
36
|
+
* // Output: "Symbol(mySymbol)"
|
|
37
|
+
*/
|
|
38
|
+
withData(name, data) {
|
|
39
|
+
return data !== undefined
|
|
40
|
+
? Symbol.for(`${name} ${JSON.stringify(data)}`)
|
|
41
|
+
: Symbol.for(name);
|
|
42
|
+
},
|
|
43
|
+
/**
|
|
44
|
+
* The `isSymbol` method does exactly what one would it expect. It returns
|
|
45
|
+
* true if the string matches typeof or instanceof as a symbol.
|
|
46
|
+
*
|
|
47
|
+
* @param {*} value checks to see if the `value` is a string
|
|
48
|
+
* @returns {boolean} `true` if it is a `Symbol`, `false` otherwise
|
|
49
|
+
*/
|
|
50
|
+
isSymbol(value) {
|
|
51
|
+
return value && (typeof value === 'symbol');
|
|
52
|
+
},
|
|
53
|
+
/**
|
|
54
|
+
* Returns true if the supplied value is a Symbol created using
|
|
55
|
+
* `Symbol.for()`.
|
|
56
|
+
*
|
|
57
|
+
* @param {any} value assumption is that the supplied value is of type
|
|
58
|
+
* 'symbol' however, unless `allowOnlySymbols` is set to `true`, `false`
|
|
59
|
+
* will be returned for any non-symbol values.
|
|
60
|
+
* @param {boolean} allowOnlySymbols true if an error should be thrown
|
|
61
|
+
* if the supplied value is not of type 'symbol'
|
|
62
|
+
* @returns true if the symbol is registered, meaning, none of the spec
|
|
63
|
+
* static symbols (`toStringTag`, `iterator`, etc...), and no symbols
|
|
64
|
+
* created by passing a value directly to the Symbol function, such as
|
|
65
|
+
* `Symbol('name')`
|
|
66
|
+
*/
|
|
67
|
+
isRegistered(value, allowOnlySymbols = false) {
|
|
68
|
+
if (!Symbol.isSymbol(value)) {
|
|
69
|
+
if (allowOnlySymbols) {
|
|
70
|
+
throw new TypeError('allowOnlySymbols specified; value is not a symbol');
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return Symbol.keyFor(value) !== undefined;
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* A function that returns true if the symbol is not registered, meaning,
|
|
78
|
+
* any of the spec static symbols (`toStringTag`, `iterator`, etc...), and
|
|
79
|
+
* any symbols created by passing a value directly to the `Symbol` function,
|
|
80
|
+
* such as `Symbol('name')`.
|
|
81
|
+
*
|
|
82
|
+
* @param {any} value assumption is that the supplied value is of type
|
|
83
|
+
* 'symbol' however, unless allowOnlySymbols is set to true, false will
|
|
84
|
+
* be returned for any non-symbol values.
|
|
85
|
+
* @param {boolean} allowOnlySymbols true if an error should be thrown
|
|
86
|
+
* if the supplied value is not of type 'symbol'
|
|
87
|
+
* @returns true if the symbol is not registered, meaning, any of the
|
|
88
|
+
* spec static symbols (`toStringTag`, `iterator`, etc...), and any symbols
|
|
89
|
+
* created by passing a value directly to the `Symbol` function, such as
|
|
90
|
+
* `Symbol('name')`
|
|
91
|
+
* @returns true if the `value` in question is both a `symbol` and has
|
|
92
|
+
* returns `undefined` if passed to `Symbol.keyFor`
|
|
93
|
+
*/
|
|
94
|
+
isNonRegistered(value, allowOnlySymbols = false) {
|
|
95
|
+
return !Symbol.isRegistered(value, allowOnlySymbols);
|
|
96
|
+
},
|
|
97
|
+
/**
|
|
98
|
+
* `keys` is an instance of the `Symkeys` class, initialized with the
|
|
99
|
+
* domain 'nejs'. The `Symkeys` class provides a way to easily generate
|
|
100
|
+
* Symbol.for elements that follow particular pattern. Symkeys also
|
|
101
|
+
* allows associated data storage with each generated key.
|
|
102
|
+
*
|
|
103
|
+
* @type {Symkeys}
|
|
104
|
+
* @see {@link SymKeys}
|
|
105
|
+
* @example
|
|
106
|
+
* // Returns something like Symbol.for('@nejs.prototype #rwiy2o905d')
|
|
107
|
+
* const kOriginal = Symbol.keys.add('prototypes')
|
|
108
|
+
*
|
|
109
|
+
* // Which can be used to retrieve and fetch data associated with that key
|
|
110
|
+
* // The value stored is an array by default, but can be anything. It can
|
|
111
|
+
* // be accessed one property at a time
|
|
112
|
+
* Symbol.keys[kOriginal].original = Object.prototype
|
|
113
|
+
* Symbol.keys[kOriginal].modified = Object.create(Object.prototype, ...)
|
|
114
|
+
*
|
|
115
|
+
* // Or wholesale replaced
|
|
116
|
+
* Symbol.keys[kOriginal] = [Object.prototype, Array.prototype]
|
|
117
|
+
*
|
|
118
|
+
* // But if all Symbol Extensions are in place, including prototype add-ons
|
|
119
|
+
* kOriginal.data.original = Object.prototype // ...and...
|
|
120
|
+
* kOriginal.data = [Object.prototype, Array.prototype] // ...both work
|
|
121
|
+
*/
|
|
122
|
+
keys: new symkeys_js_1.Symkeys('nejs'),
|
|
123
|
+
});
|
|
124
|
+
exports.SymbolPrototypeExtensions = new extension_1.Patch(Symbol.prototype, {
|
|
125
|
+
[extension_1.Patch.kMutablyHidden]: {
|
|
126
|
+
/**
|
|
127
|
+
* Returns an object representation of the symbol instance.
|
|
128
|
+
*
|
|
129
|
+
* This getter method creates and returns an object that wraps the
|
|
130
|
+
* symbol instance, allowing it to be treated as an object. The
|
|
131
|
+
* returned object is created using the `Object()` constructor,
|
|
132
|
+
* which takes the symbol instance as its argument.
|
|
133
|
+
*
|
|
134
|
+
* @type {Object}
|
|
135
|
+
* @readonly
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* const sym = Symbol('example')
|
|
139
|
+
* console.log(typeof sym) // 'symbol'
|
|
140
|
+
* console.log(typeof sym.instance) // 'object'
|
|
141
|
+
*/
|
|
142
|
+
get instance() {
|
|
143
|
+
return Object(this);
|
|
144
|
+
},
|
|
145
|
+
/**
|
|
146
|
+
* Getter method for retrieving the data associated with a symbol.
|
|
147
|
+
*
|
|
148
|
+
* This method first checks if the symbol is a Symkey created symbol
|
|
149
|
+
* by checking the existence of Symbol.keys and if the symbol's
|
|
150
|
+
* description matches the Symkey pattern. If it is a Symkey symbol,
|
|
151
|
+
* it attempts to fetch its associated data.
|
|
152
|
+
*
|
|
153
|
+
* NOTE: Symkey data is returned as its value directly, this is because
|
|
154
|
+
* it is stored in a {@link Map}. Embedded JSON data might be expensive
|
|
155
|
+
* to parse and as such a function is returned when data is accessed that
|
|
156
|
+
* needs to be invoked in order to decode its contents. See
|
|
157
|
+
* `{@link mightHaveEmbeddedJSON}` for more information.
|
|
158
|
+
*
|
|
159
|
+
* If the symbol is not a Symkey symbol or if no data is associated
|
|
160
|
+
* with it, the method attempts to parse the symbol's description as
|
|
161
|
+
* JSON and returns the first valid JSON object found.
|
|
162
|
+
*
|
|
163
|
+
* If no valid JSON object is found in the description, the method
|
|
164
|
+
* returns undefined.
|
|
165
|
+
*
|
|
166
|
+
* @type {Object|Function}
|
|
167
|
+
* @readonly
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* const keys = new Symkeys
|
|
171
|
+
* const key = keys.add('example', {count: 0})
|
|
172
|
+
* const data = key.data // note this isn't function!!
|
|
173
|
+
* const count = data.count
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* const sym = Symbol.for('fun {"name":"Brie"}')
|
|
177
|
+
* let json = sym.data() // {name: 'Brie'} JS object
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* const sym = Symbol('mySymbol')
|
|
181
|
+
* let data = sym.data() // undefined
|
|
182
|
+
*/
|
|
183
|
+
get data() {
|
|
184
|
+
if (Symbol?.keys && symkeys_js_1.Symkeys.isSymkey(this)) {
|
|
185
|
+
const possibleData = Symbol.keys[this];
|
|
186
|
+
if (possibleData) {
|
|
187
|
+
return possibleData;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return extractFrom(string);
|
|
191
|
+
},
|
|
192
|
+
/**
|
|
193
|
+
* Sets the data associated with a symbol.
|
|
194
|
+
*
|
|
195
|
+
* This setter method checks if the symbol is a Symkey and if it has
|
|
196
|
+
* associated data. If both conditions are met, it sets the data of the
|
|
197
|
+
* symbol to the provided value and returns true. If the conditions are
|
|
198
|
+
* not met, it simply returns false.
|
|
199
|
+
*
|
|
200
|
+
* While Symbols have been upgraded to also support embedded JSON data
|
|
201
|
+
* with this extension, symbol descriptions are static. Non Symkey symbols
|
|
202
|
+
* do not associated their data outside of a symbol, and cannot be changed,
|
|
203
|
+
* there new data cannot be set on them.
|
|
204
|
+
*
|
|
205
|
+
* @param {any} value - The value to be set as the symbol's data.
|
|
206
|
+
* @returns {boolean} - Returns true if the data was successfully set,
|
|
207
|
+
* false otherwise.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* const sym = Symbol.for('fun {"name":"Brie"}')
|
|
211
|
+
* Symkeys.isSymkey(sym) // false; not in Symkey format
|
|
212
|
+
* let json = sym.data() // {name: 'Brie'} JS object
|
|
213
|
+
* sym.data = JSON.stringify({name: 'Jane'}) // fails silently
|
|
214
|
+
* json = sym.data() // {name: 'Brie'} is hard-coded in description
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* const sym = Symbol('mySymbol')
|
|
218
|
+
* Symkeys.isSymkey(sym) // false; not in Symkey format
|
|
219
|
+
* Symkeys.hasData(sym) // false
|
|
220
|
+
* sym.data = { name: 'John', age: 30 } // will fail silently
|
|
221
|
+
* Symkeys.hasData(sym) // still false
|
|
222
|
+
*
|
|
223
|
+
* // Now let's create a Symkey with data
|
|
224
|
+
* const symWithData = Symkeys.create('mySymbolWithData',
|
|
225
|
+
* { name: 'Jane', age: 25 })
|
|
226
|
+
* Symkeys.isSymkey(symWithData) // true
|
|
227
|
+
* Symkeys.hasData(symWithData) // true
|
|
228
|
+
* symWithData.data = { name: 'Jane', age: 26 } // will succeed
|
|
229
|
+
* Symkeys.getData(symWithData) // returns { name: 'Jane', age: 26 }
|
|
230
|
+
*/
|
|
231
|
+
set data(value) {
|
|
232
|
+
if (symkeys_js_1.Symkeys.isSymkey(this) && symkeys_js_1.Symkeys.hasData(this)) {
|
|
233
|
+
Symbol.keys.setData(this, value);
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
/**
|
|
237
|
+
* Checks if the symbol might have embedded JSON data.
|
|
238
|
+
*
|
|
239
|
+
* This getter method checks if the symbol's description might contain
|
|
240
|
+
* JSON data and if the data property of the symbol is a function. If both
|
|
241
|
+
* conditions are met, it returns true, otherwise it returns false.
|
|
242
|
+
*
|
|
243
|
+
* @returns {boolean} - Returns true if the symbol might have embedded
|
|
244
|
+
* JSON, false otherwise.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* const sym = Symbol.for('fun {"name":"Brie"}')
|
|
248
|
+
* console.log(sym.mightHaveEmbeddedJSON) // Output: true
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* const sym = Symbol('mySymbol')
|
|
252
|
+
* console.log(sym.mightHaveEmbeddedJSON) // Output: false
|
|
253
|
+
*/
|
|
254
|
+
get mightHaveEmbeddedJSON() {
|
|
255
|
+
return mightContain(this.description) && typeof this.data === 'function';
|
|
256
|
+
},
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
//# sourceMappingURL=symbol.extensions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"symbol.extensions.js","sourceRoot":"","sources":["../../src/symbol.extensions.js"],"names":[],"mappings":";;;AAAA,+CAAwC;AAExC,qDAA8C;AAC9C,6DAAqD;AAErD,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE,GAAG,mCAAc,CAAC,OAAO,CAAA;AAE5D;;;;;;;GAOG;AACU,QAAA,gBAAgB,GAAG,IAAI,iBAAK,CAAC,MAAM,EAAE;IAChD;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ,CAAC,IAAI,EAAE,IAAI;QACjB,OAAO,IAAI,KAAK,SAAS;YACvB,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC;YAC/C,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACtB,CAAC;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,KAAK;QACZ,OAAO,KAAK,IAAI,CAAC,OAAO,KAAK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C5B,CAAC;QAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAsCG;QACH,IAAI,IAAI,CAAC,KAAK;YACZ,IAAI,oBAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,oBAAO,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;gBACpD,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;YAClC,CAAC;QACH,CAAC;QAED;;;;;;;;;;;;;;;;;WAiBG;QACH,IAAI,qBAAqB;YACvB,OAAO,YAAY,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,UAAU,CAAA;QAC1E,CAAC;KACF;CACF,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"weakref.extensions.js","sourceRoot":"","sources":["../../src/weakref.extensions.js"],"names":[],"mappings":";;;AAAA,+CAAuC;AAE1B,QAAA,iBAAiB,GAAG,IAAI,iBAAK,CAAC,OAAO,EAAE;IAClD;;;;;;OAMG;IACH,gBAAgB,CAAC,KAAK;QACpB,OAAO,CAAC,CACN,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC;YACjE,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC;YACxD,CAAC,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CACxC,CAAA;IACH,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ArrayExtensions` is a constant that applies a patch to the global
|
|
3
|
+
* `Array` constructor. This patch extends the `Array` with additional
|
|
4
|
+
* methods and properties, enhancing its functionality.
|
|
5
|
+
*
|
|
6
|
+
* The `Patch` function takes two arguments: the target object to be patched
|
|
7
|
+
* (in this case, `Array`), and an object containing the methods and
|
|
8
|
+
* properties to be added to the target object.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // Using a method added by ArrayExtensions
|
|
12
|
+
* const arr = [1, 2, 3];
|
|
13
|
+
* console.log(Array.ifArray(arr, 'Array', 'Not Array')); // Output: 'Array'
|
|
14
|
+
*
|
|
15
|
+
* @const
|
|
16
|
+
* @type {Patch}
|
|
17
|
+
* @memberof module:array.extensions
|
|
18
|
+
*/
|
|
19
|
+
export const ArrayExtensions: Patch;
|
|
20
|
+
/**
|
|
21
|
+
* `ArrayPrototypeExtensions` is a constant that applies a patch to the
|
|
22
|
+
* Array prototype. This patch extends the Array prototype with additional
|
|
23
|
+
* methods and properties, enhancing its functionality.
|
|
24
|
+
*
|
|
25
|
+
* The `Patch` function takes two arguments: the target object to be patched
|
|
26
|
+
* (in this case, `Array.prototype`), and an object containing the methods
|
|
27
|
+
* and properties to be added to the target object.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // Using a method added by ArrayPrototypeExtensions
|
|
31
|
+
* const arr = [1, 2, 3];
|
|
32
|
+
* console.log(arr.ifArray('Array', 'Not Array')); // Output: 'Array'
|
|
33
|
+
*
|
|
34
|
+
* @const
|
|
35
|
+
* @type {Patch}
|
|
36
|
+
* @memberof module:array.extensions
|
|
37
|
+
*/
|
|
38
|
+
export const ArrayPrototypeExtensions: Patch;
|
|
39
|
+
import { Patch } from '@nejs/extension';
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { Patch } from '@nejs/extension';
|
|
2
|
+
/**
|
|
3
|
+
* `ArrayExtensions` is a constant that applies a patch to the global
|
|
4
|
+
* `Array` constructor. This patch extends the `Array` with additional
|
|
5
|
+
* methods and properties, enhancing its functionality.
|
|
6
|
+
*
|
|
7
|
+
* The `Patch` function takes two arguments: the target object to be patched
|
|
8
|
+
* (in this case, `Array`), and an object containing the methods and
|
|
9
|
+
* properties to be added to the target object.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Using a method added by ArrayExtensions
|
|
13
|
+
* const arr = [1, 2, 3];
|
|
14
|
+
* console.log(Array.ifArray(arr, 'Array', 'Not Array')); // Output: 'Array'
|
|
15
|
+
*
|
|
16
|
+
* @const
|
|
17
|
+
* @type {Patch}
|
|
18
|
+
* @memberof module:array.extensions
|
|
19
|
+
*/
|
|
20
|
+
export const ArrayExtensions = new Patch(Array, {
|
|
21
|
+
/**
|
|
22
|
+
* Checks if the provided value is an array and returns one of two
|
|
23
|
+
* provided values based on the result. This function is a convenience
|
|
24
|
+
* method for performing conditional operations based on the type of
|
|
25
|
+
* the provided value.
|
|
26
|
+
*
|
|
27
|
+
* @name ifArray
|
|
28
|
+
* @type {function}
|
|
29
|
+
* @memberof ArrayExtensions
|
|
30
|
+
* @param {any} value - The value to be checked.
|
|
31
|
+
* @param {function | any} thenValue - The value to be returned if the
|
|
32
|
+
* provided value is an array.
|
|
33
|
+
* @param {function | any} elseValue - The value to be returned if the
|
|
34
|
+
* provided value is not an array.
|
|
35
|
+
* @returns {any} Returns `thenValue` if the provided value is an array,
|
|
36
|
+
* otherwise returns `elseValue`.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* const arr = [1, 2, 3];
|
|
40
|
+
* console.log(ArrayExtensions.ifArray(arr, 'Array', 'Not Array'));
|
|
41
|
+
* // Output: 'Array'
|
|
42
|
+
*
|
|
43
|
+
* const notArr = "I'm not an array";
|
|
44
|
+
* console.log(ArrayExtensions.ifArray(notArr, 'Array', 'Not Array'));
|
|
45
|
+
* // Output: 'Not Array'
|
|
46
|
+
*/
|
|
47
|
+
ifArray(value, thenValue, elseValue) {
|
|
48
|
+
return isThenElse(Array.isArray(value), thenValue, elseValue);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
const { ifArray: pIfArray } = ArrayExtensions.patches;
|
|
52
|
+
/**
|
|
53
|
+
* `ArrayPrototypeExtensions` is a constant that applies a patch to the
|
|
54
|
+
* Array prototype. This patch extends the Array prototype with additional
|
|
55
|
+
* methods and properties, enhancing its functionality.
|
|
56
|
+
*
|
|
57
|
+
* The `Patch` function takes two arguments: the target object to be patched
|
|
58
|
+
* (in this case, `Array.prototype`), and an object containing the methods
|
|
59
|
+
* and properties to be added to the target object.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // Using a method added by ArrayPrototypeExtensions
|
|
63
|
+
* const arr = [1, 2, 3];
|
|
64
|
+
* console.log(arr.ifArray('Array', 'Not Array')); // Output: 'Array'
|
|
65
|
+
*
|
|
66
|
+
* @const
|
|
67
|
+
* @type {Patch}
|
|
68
|
+
* @memberof module:array.extensions
|
|
69
|
+
*/
|
|
70
|
+
export const ArrayPrototypeExtensions = new Patch(Array.prototype, {
|
|
71
|
+
[Patch.kMutablyHidden]: {
|
|
72
|
+
/**
|
|
73
|
+
* Sometimes defining even a short function for the invocation of `find`
|
|
74
|
+
* can be troublesome. This helper function performs that job for you. If
|
|
75
|
+
* the specified element is in the array, `true` will be returned.
|
|
76
|
+
*
|
|
77
|
+
* @param {*} value the value to search for. This value must triple equals
|
|
78
|
+
* the array element in order to return true.
|
|
79
|
+
* @returns true if the exact element exists in the array, false otherwise
|
|
80
|
+
*/
|
|
81
|
+
contains(value) {
|
|
82
|
+
return !!this.find(entry => entry === value);
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* The `findEntry` function searches the entries of the object and returns
|
|
86
|
+
* the `[index, value]` entry array for the first matching value found.
|
|
87
|
+
*
|
|
88
|
+
* @param {function} findFn a function that takes the element to be checked
|
|
89
|
+
* and returns a boolean value
|
|
90
|
+
* @returns if `findFn` returns `true`, an array with two elements, the first
|
|
91
|
+
* being the index, the second being the value, is returned.
|
|
92
|
+
*/
|
|
93
|
+
findEntry(findFn) {
|
|
94
|
+
const entries = this.entries();
|
|
95
|
+
const VALUE = 1;
|
|
96
|
+
for (let entry of entries) {
|
|
97
|
+
if (findFn(entry[VALUE])) {
|
|
98
|
+
return entry;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
},
|
|
103
|
+
/**
|
|
104
|
+
* A getter property that returns the first element of the array. If the
|
|
105
|
+
* array is empty, it returns `undefined`. This property is useful for
|
|
106
|
+
* scenarios where you need to quickly access the first item of an array
|
|
107
|
+
* without the need for additional checks or method calls.
|
|
108
|
+
*
|
|
109
|
+
* @returns {*} The first element of the array or `undefined` if the array
|
|
110
|
+
* is empty.
|
|
111
|
+
*/
|
|
112
|
+
get first() {
|
|
113
|
+
return this[0];
|
|
114
|
+
},
|
|
115
|
+
/**
|
|
116
|
+
* A getter property that checks if the current context (`this`) is an
|
|
117
|
+
* array. This is a convenience method that wraps the native
|
|
118
|
+
* `Array.isArray` function.
|
|
119
|
+
*
|
|
120
|
+
* @name isArray
|
|
121
|
+
* @type {function}
|
|
122
|
+
* @memberof Array.prototype
|
|
123
|
+
* @returns {boolean} `true` if the current context is an array,
|
|
124
|
+
* `false` otherwise.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* const arr = [1, 2, 3];
|
|
128
|
+
* console.log(arr.isArray); // Output: true
|
|
129
|
+
*
|
|
130
|
+
* const notArr = "I'm not an array";
|
|
131
|
+
* console.log(notArr.isArray); // Output: false
|
|
132
|
+
*/
|
|
133
|
+
get isArray() {
|
|
134
|
+
return Array.isArray(this);
|
|
135
|
+
},
|
|
136
|
+
/**
|
|
137
|
+
* Checks if the current context (`this`) is an array and returns one of
|
|
138
|
+
* two provided values based on the result. This function is a convenience
|
|
139
|
+
* method for performing conditional operations based on the type of
|
|
140
|
+
* the current context.
|
|
141
|
+
*
|
|
142
|
+
* @name ifArray
|
|
143
|
+
* @type {function}
|
|
144
|
+
* @memberof Array.prototype
|
|
145
|
+
* @param {function | any} thenValue - The value to be returned if the
|
|
146
|
+
* current context is an array.
|
|
147
|
+
* @param {function | any} elseValue - The value to be returned if the
|
|
148
|
+
* current context is not an array.
|
|
149
|
+
* @returns {*} Returns `thenValue` if the current context is an array,
|
|
150
|
+
* otherwise returns `elseValue`.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* const arr = [1, 2, 3];
|
|
154
|
+
* console.log(arr.ifArray('Array', 'Not Array')); // Output: 'Array'
|
|
155
|
+
*
|
|
156
|
+
* const notArr = "I'm not an array";
|
|
157
|
+
* console.log(notArr.ifArray('Array', 'Not Array')); // Output: 'Not Array'
|
|
158
|
+
*/
|
|
159
|
+
ifArray(thenValue, elseValue) {
|
|
160
|
+
return pIfArray(this, thenValue, elseValue);
|
|
161
|
+
},
|
|
162
|
+
/**
|
|
163
|
+
* Checks if at least one element in the array is equal to the provided
|
|
164
|
+
* value. This method uses the `Array.prototype.some` function to iterate
|
|
165
|
+
* over the array and compare each element with the provided value.
|
|
166
|
+
*
|
|
167
|
+
* @name oneIs
|
|
168
|
+
* @type {function}
|
|
169
|
+
* @memberof Array.prototype
|
|
170
|
+
* @param {*} value - The value to be compared with the array elements.
|
|
171
|
+
* @param {boolean} [doubleEqualsOkay=true] - A flag indicating whether to
|
|
172
|
+
* use loose equality (`==`) or strict equality (`===`) for the comparison.
|
|
173
|
+
* If `true`, loose equality is used. If `false`, strict equality is used.
|
|
174
|
+
* @returns {boolean} Returns `true` if at least one element in the array
|
|
175
|
+
* is equal to the provided value, otherwise `false`.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* const arr = [1, 2, 3];
|
|
179
|
+
* console.log(arr.oneIs(2)); // Output: true
|
|
180
|
+
*
|
|
181
|
+
* const arr2 = ['1', '2', '3'];
|
|
182
|
+
* console.log(arr2.oneIs(2, false)); // Output: false
|
|
183
|
+
*/
|
|
184
|
+
oneIs(value, doubleEqualsOkay = true) {
|
|
185
|
+
return this.some(element => (doubleEqualsOkay ? element == value : element === value));
|
|
186
|
+
},
|
|
187
|
+
/**
|
|
188
|
+
* Checks if some elements in the array are included in the provided values.
|
|
189
|
+
* This method uses the `Array.prototype.some` function to iterate over the
|
|
190
|
+
* array and checks if any of the elements are included in the provided values.
|
|
191
|
+
*
|
|
192
|
+
* @name someAre
|
|
193
|
+
* @type {function}
|
|
194
|
+
* @memberof Array.prototype
|
|
195
|
+
* @param {...*} values - The values to be checked against the array elements.
|
|
196
|
+
* @returns {boolean} Returns `true` if at least one element in the array
|
|
197
|
+
* is included in the provided values, otherwise `false`.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* const arr = [1, 2, 3];
|
|
201
|
+
* console.log(arr.someAre(2, 4)); // Output: true
|
|
202
|
+
*
|
|
203
|
+
* const arr2 = ['1', '2', '3'];
|
|
204
|
+
* console.log(arr2.someAre(4, 5)); // Output: false
|
|
205
|
+
*/
|
|
206
|
+
someAre(...values) {
|
|
207
|
+
return this.some(element => !!~values.indexOf(element));
|
|
208
|
+
},
|
|
209
|
+
/**
|
|
210
|
+
* Checks if all elements in the array are equal to the provided value.
|
|
211
|
+
* This method uses the `Array.prototype.every` function to iterate over
|
|
212
|
+
* the array and compare each element with the provided value.
|
|
213
|
+
*
|
|
214
|
+
* @name allAre
|
|
215
|
+
* @type {function}
|
|
216
|
+
* @memberof Array.prototype
|
|
217
|
+
* @param {*} value - The value to be compared with the array elements.
|
|
218
|
+
* @param {boolean} [doubleEqualsOkay=true] - A flag indicating whether to
|
|
219
|
+
* use loose equality (`==`) or strict equality (`===`) for the comparison.
|
|
220
|
+
* If `true`, loose equality is used. If `false`, strict equality is used.
|
|
221
|
+
* @returns {boolean} Returns `true` if all elements in the array are equal
|
|
222
|
+
* to the provided value, otherwise `false`.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* const arr = [2, 2, 2];
|
|
226
|
+
* console.log(arr.allAre(2)); // Output: true
|
|
227
|
+
*
|
|
228
|
+
* const arr2 = ['2', '2', '2'];
|
|
229
|
+
* console.log(arr2.allAre(2, false)); // Output: false
|
|
230
|
+
*/
|
|
231
|
+
allAre(value, doubleEqualsOkay = true) {
|
|
232
|
+
return this.every(element => (doubleEqualsOkay ? element == value : element === value));
|
|
233
|
+
},
|
|
234
|
+
/**
|
|
235
|
+
* A getter property that returns the last element of the array. It
|
|
236
|
+
* calculates the last index based on the array's length. If the array is
|
|
237
|
+
* empty, it returns `undefined`. This property is beneficial when you need
|
|
238
|
+
* to access the last item in an array, improving code readability and
|
|
239
|
+
* avoiding manual index calculation.
|
|
240
|
+
*
|
|
241
|
+
* @returns {*} The last element of the array or `undefined` if the
|
|
242
|
+
* array is empty.
|
|
243
|
+
*/
|
|
244
|
+
get last() {
|
|
245
|
+
return this[this.length - 1];
|
|
246
|
+
},
|
|
247
|
+
// expected usage:
|
|
248
|
+
// function example(name, age) {
|
|
249
|
+
// const variants = [{name}, {age}].variants()
|
|
250
|
+
// if (typeof name === 'object')
|
|
251
|
+
// }
|
|
252
|
+
variants() {
|
|
253
|
+
const keys = this.map(o => Object.keys(o)?.[0]);
|
|
254
|
+
const entries = this.map(o => Object.entries(o)?.[0]);
|
|
255
|
+
const object = entries.reduce((acc, [key, value]) => {
|
|
256
|
+
acc[key] = value;
|
|
257
|
+
return acc;
|
|
258
|
+
}, {});
|
|
259
|
+
const result = {
|
|
260
|
+
order: keys,
|
|
261
|
+
entries: entries,
|
|
262
|
+
object: object,
|
|
263
|
+
};
|
|
264
|
+
Object.defineProperty(result, 'check', {
|
|
265
|
+
value(position) {
|
|
266
|
+
if (typeof position !== 'number' &&
|
|
267
|
+
position >= 0 &&
|
|
268
|
+
position < this.order.length) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
const value = this.entries[position][1];
|
|
272
|
+
if (typeof value === 'object' && value) {
|
|
273
|
+
if (Object.keys(value).every(key => ~this.order.indexOf(key))) {
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return false;
|
|
278
|
+
},
|
|
279
|
+
enumerable: false,
|
|
280
|
+
configurable: true
|
|
281
|
+
});
|
|
282
|
+
return result;
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
// NOTE to self; this is repeated here otherwise a circular reference from
|
|
287
|
+
// Object<->Function<->Global occurs. See original source in global.this.js
|
|
288
|
+
// {@see globalThis.isThenElse}
|
|
289
|
+
function isThenElse(bv, tv, ev) {
|
|
290
|
+
if (arguments.length > 1) {
|
|
291
|
+
var _then = isFunction(tv) ? tv(bv) : tv;
|
|
292
|
+
if (arguments.length > 2) {
|
|
293
|
+
var _else = isFunction(ev) ? tv(bv) : ev;
|
|
294
|
+
return bv ? _then : _else;
|
|
295
|
+
}
|
|
296
|
+
return bv || _then;
|
|
297
|
+
}
|
|
298
|
+
return bv;
|
|
299
|
+
}
|
|
300
|
+
//# sourceMappingURL=array.extensions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.extensions.js","sourceRoot":"","sources":["../../src/array.extensions.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAEvC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,KAAK,CAAC,KAAK,EAAE;IAC9C;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,OAAO,CAAC,KAAK,EAAE,SAAS,EAAE,SAAS;QACjC,OAAO,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,SAAS,CAAC,CAAA;IAC/D,CAAC;CACF,CAAC,CAAA;AAEF,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,eAAe,CAAC,OAAO,CAAA;AAErD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE;IACjE,CAAC,KAAK,CAAC,cAAc,CAAC,EAAE;QACtB;;;;;;;;WAQG;QACH,QAAQ,CAAC,KAAK;YACZ,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,C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@@
|
|
|
1
|
+
/**
|
|
2
|
+
* `BigIntExtensions` is a patch for the JavaScript built-in `BigInt` class.
|
|
3
|
+
* It adds utility methods to the `BigInt` class without modifying the global
|
|
4
|
+
* namespace directly. This patch includes methods for checking if a value is
|
|
5
|
+
* a `BigInt` and conditionally returning a value based on whether the supplied
|
|
6
|
+
* value is a `BigInt` or not.
|
|
7
|
+
*
|
|
8
|
+
* @type {Patch}
|
|
9
|
+
* @example
|
|
10
|
+
* import { BigIntExtensions } from 'big.int.extension.js'
|
|
11
|
+
*
|
|
12
|
+
* BigIntExtensions.apply()
|
|
13
|
+
* // Now the `BigInt` class has additional methods available
|
|
14
|
+
*/
|
|
15
|
+
export const BigIntExtensions: Patch;
|
|
16
|
+
/**
|
|
17
|
+
* `BigIntPrototypeExtensions` is a patch for the JavaScript built-in
|
|
18
|
+
* `BigInt.prototype`. It adds utility methods to the `BigInt` prototype
|
|
19
|
+
* without modifying the global namespace directly. This patch includes
|
|
20
|
+
* methods for checking if a value is a BigInt and conditionally returning
|
|
21
|
+
* a value based on whether the supplied value is a BigInt or not.
|
|
22
|
+
*
|
|
23
|
+
* @type {Patch}
|
|
24
|
+
* @example
|
|
25
|
+
* import { BigIntPrototypeExtensions } from 'big.int.extension.js'
|
|
26
|
+
*
|
|
27
|
+
* BigIntPrototypeExtensions.apply()
|
|
28
|
+
* // Now the `BigInt` prototype has additional methods available
|
|
29
|
+
*/
|
|
30
|
+
export const BigIntPrototypeExtensions: Patch;
|
|
31
|
+
import { Patch } from '@nejs/extension';
|