@nejs/basic-extensions 2.9.0 → 2.10.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/dist/@nejs/basic-extensions.bundle.2.10.0.js +19 -0
- package/dist/@nejs/basic-extensions.bundle.2.10.0.js.map +7 -0
- package/dist/cjs/array.extensions.js +174 -0
- package/dist/cjs/array.extensions.js.map +1 -1
- package/dist/cjs/big.int.extension.js +1 -0
- package/dist/cjs/big.int.extension.js.map +1 -1
- package/dist/cjs/classes/descriptor.js +1 -1
- package/dist/cjs/classes/descriptor.js.map +1 -1
- package/dist/cjs/classes/index.d.ts +1 -0
- package/dist/cjs/classes/index.js +3 -0
- package/dist/cjs/classes/index.js.map +1 -1
- package/dist/cjs/classes/iterable.d.ts +44 -0
- package/dist/cjs/classes/iterable.js +64 -0
- package/dist/cjs/classes/iterable.js.map +1 -1
- package/dist/cjs/classes/param.parser.d.ts +10 -10
- package/dist/cjs/classes/property.d.ts +86 -0
- package/dist/cjs/classes/property.js +284 -0
- package/dist/cjs/classes/property.js.map +1 -0
- package/dist/cjs/classes/symkeys.d.ts +68 -11
- package/dist/cjs/classes/symkeys.js +103 -17
- package/dist/cjs/classes/symkeys.js.map +1 -1
- package/dist/cjs/classes/type.d.ts +4 -4
- package/dist/cjs/function.extensions.js +1 -0
- package/dist/cjs/function.extensions.js.map +1 -1
- package/dist/cjs/global.this.js +29 -0
- package/dist/cjs/global.this.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/json.extensions.js +19 -18
- package/dist/cjs/json.extensions.js.map +1 -1
- package/dist/cjs/map.extensions.js +1 -0
- package/dist/cjs/map.extensions.js.map +1 -1
- package/dist/cjs/number.extension.js +1 -0
- package/dist/cjs/number.extension.js.map +1 -1
- package/dist/cjs/object.extensions.d.ts +0 -29
- package/dist/cjs/object.extensions.js +218 -255
- package/dist/cjs/object.extensions.js.map +1 -1
- package/dist/cjs/set.extensions.js +1 -0
- package/dist/cjs/set.extensions.js.map +1 -1
- package/dist/cjs/string.extensions.js +474 -469
- package/dist/cjs/string.extensions.js.map +1 -1
- package/dist/cjs/symbol.extensions.js +386 -31
- package/dist/cjs/symbol.extensions.js.map +1 -1
- package/dist/cjs/utils/copy.object.d.ts +408 -0
- package/dist/cjs/utils/copy.object.js +720 -0
- package/dist/cjs/utils/copy.object.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.js +19 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/utils/toolkit.d.ts +1897 -0
- package/dist/cjs/utils/toolkit.js +1377 -0
- package/dist/cjs/utils/toolkit.js.map +1 -0
- package/dist/mjs/array.extensions.js +174 -0
- package/dist/mjs/array.extensions.js.map +1 -1
- package/dist/mjs/big.int.extension.js +1 -0
- package/dist/mjs/big.int.extension.js.map +1 -1
- package/dist/mjs/classes/descriptor.js +1 -1
- package/dist/mjs/classes/descriptor.js.map +1 -1
- package/dist/mjs/classes/index.d.ts +1 -0
- package/dist/mjs/classes/index.js +3 -0
- package/dist/mjs/classes/index.js.map +1 -1
- package/dist/mjs/classes/iterable.d.ts +44 -0
- package/dist/mjs/classes/iterable.js +64 -0
- package/dist/mjs/classes/iterable.js.map +1 -1
- package/dist/mjs/classes/param.parser.d.ts +10 -10
- package/dist/mjs/classes/property.d.ts +86 -0
- package/dist/mjs/classes/property.js +280 -0
- package/dist/mjs/classes/property.js.map +1 -0
- package/dist/mjs/classes/symkeys.d.ts +68 -11
- package/dist/mjs/classes/symkeys.js +103 -17
- package/dist/mjs/classes/symkeys.js.map +1 -1
- package/dist/mjs/classes/type.d.ts +4 -4
- package/dist/mjs/function.extensions.js +1 -0
- package/dist/mjs/function.extensions.js.map +1 -1
- package/dist/mjs/global.this.js +6 -0
- package/dist/mjs/global.this.js.map +1 -1
- package/dist/mjs/index.d.ts +2 -0
- package/dist/mjs/index.js +4 -0
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/json.extensions.js +19 -18
- package/dist/mjs/json.extensions.js.map +1 -1
- package/dist/mjs/map.extensions.js +1 -0
- package/dist/mjs/map.extensions.js.map +1 -1
- package/dist/mjs/number.extension.js +1 -0
- package/dist/mjs/number.extension.js.map +1 -1
- package/dist/mjs/object.extensions.d.ts +0 -29
- package/dist/mjs/object.extensions.js +215 -251
- package/dist/mjs/object.extensions.js.map +1 -1
- package/dist/mjs/set.extensions.js +1 -0
- package/dist/mjs/set.extensions.js.map +1 -1
- package/dist/mjs/string.extensions.js +474 -469
- package/dist/mjs/string.extensions.js.map +1 -1
- package/dist/mjs/symbol.extensions.js +386 -31
- package/dist/mjs/symbol.extensions.js.map +1 -1
- package/dist/mjs/utils/copy.object.d.ts +408 -0
- package/dist/mjs/utils/copy.object.js +702 -0
- package/dist/mjs/utils/copy.object.js.map +1 -0
- package/dist/mjs/utils/index.d.ts +1 -0
- package/dist/mjs/utils/index.js +3 -0
- package/dist/mjs/utils/index.js.map +1 -0
- package/dist/mjs/utils/toolkit.d.ts +1897 -0
- package/dist/mjs/utils/toolkit.js +1372 -0
- package/dist/mjs/utils/toolkit.js.map +1 -0
- package/package.json +29 -37
- package/repl.bootstrap.js +12 -1
- package/src/array.extensions.js +191 -1
- package/src/big.int.extension.js +3 -1
- package/src/classes/descriptor.js +1 -1
- package/src/classes/index.js +4 -0
- package/src/classes/iterable.js +74 -0
- package/src/classes/property.js +333 -0
- package/src/classes/symkeys.js +120 -19
- package/src/function.extensions.js +2 -0
- package/src/global.this.js +8 -0
- package/src/index.js +5 -0
- package/src/json.extensions.js +18 -19
- package/src/map.extensions.js +3 -1
- package/src/number.extension.js +3 -1
- package/src/object.extensions.js +240 -277
- package/src/set.extensions.js +3 -1
- package/src/string.extensions.js +512 -506
- package/src/symbol.extensions.js +412 -29
- package/src/utils/copy.object.js +780 -0
- package/src/utils/index.js +2 -0
- package/src/utils/toolkit.js +1471 -0
- package/tests/arrayextensions.test.js +2 -0
- package/tests/index.test.js +1 -0
- package/tests/newClasses/asyncIterable.test.js +2 -0
- package/tests/newClasses/deferred.test.js +5 -3
- package/tests/newClasses/descriptor.test.js +2 -0
- package/tests/newClasses/iterable.test.js +2 -0
- package/tests/newClasses/refmap.test.js +2 -1
- package/tests/newClasses/refset.test.js +2 -0
- package/tests/objectextensions.test.js +2 -0
- package/tests/setextensions.test.js +2 -0
- package/tests/stringextensions.test.js +1 -0
- package/tests/utils/toolkit.test.js +223 -0
- package/tsconfig.base.json +1 -1
- package/vitest.config.js +7 -0
- package/dist/@nejs/basic-extensions.bundle.2.8.0.js +0 -19
- package/dist/@nejs/basic-extensions.bundle.2.8.0.js.map +0 -7
- package/docs/assets/anchor.js +0 -350
- package/docs/assets/bass-addons.css +0 -12
- package/docs/assets/bass.css +0 -544
- package/docs/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/docs/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/docs/assets/fonts/LICENSE.txt +0 -93
- package/docs/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/docs/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/docs/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/docs/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/docs/assets/fonts/source-code-pro.css +0 -23
- package/docs/assets/github.css +0 -123
- package/docs/assets/site.js +0 -168
- package/docs/assets/split.css +0 -15
- package/docs/assets/split.js +0 -782
- package/docs/assets/style.css +0 -147
- package/docs/index.html +0 -35485
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.ObjectPrototypeExtensions = exports.ObjectExtensions = void 0;
|
|
4
4
|
const extension_1 = require("@nejs/extension");
|
|
5
5
|
const symbol_extensions_js_1 = require("./symbol.extensions.js");
|
|
6
6
|
const descriptor_js_1 = require("./classes/descriptor.js");
|
|
7
|
+
const property_js_1 = require("./classes/property.js");
|
|
8
|
+
const toolkit_js_1 = require("./utils/toolkit.js");
|
|
9
|
+
const copy_object_js_1 = require("./utils/copy.object.js");
|
|
7
10
|
const { keys: symkeys } = symbol_extensions_js_1.SymbolExtensions.patches;
|
|
8
|
-
// Avoid circular dependencies; rewrite here for brevity
|
|
9
|
-
const isFn = o => typeof o === 'function' || o instanceof Function;
|
|
10
|
-
const isStr = o => typeof o === 'string' || o instanceof String;
|
|
11
|
-
const isBool = o => typeof o === 'boolean';
|
|
12
|
-
const isTrue = o => isBool(o) && o === true;
|
|
13
|
-
const isTruthy = o => isTrue(!!o);
|
|
14
|
-
const isFalse = o => isBool(o) && o === false;
|
|
15
|
-
const isFalsy = o => isFalse(!!o);
|
|
16
11
|
/**
|
|
17
12
|
* `ObjectExtensions` is a constant that applies a patch to the global
|
|
18
13
|
* `Object` constructor. This patch extends the `Object` with additional
|
|
@@ -27,6 +22,112 @@ const isFalsy = o => isFalse(!!o);
|
|
|
27
22
|
*/
|
|
28
23
|
exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
29
24
|
[extension_1.Patch.kMutablyHidden]: {
|
|
25
|
+
add(...args) {
|
|
26
|
+
const { isDescriptor } = descriptor_js_1.Descriptor;
|
|
27
|
+
const { isObject: isObj } = this;
|
|
28
|
+
const { kDescriptorStore } = this;
|
|
29
|
+
let obj, key, value, _get, _set, storage, storageKey;
|
|
30
|
+
let _type, _flag, _desc;
|
|
31
|
+
// Check to see if we received multiple arguments or an object
|
|
32
|
+
if (args.length && isObj(args[0])) {
|
|
33
|
+
({
|
|
34
|
+
to: obj,
|
|
35
|
+
key,
|
|
36
|
+
value,
|
|
37
|
+
get: _get,
|
|
38
|
+
set: _set,
|
|
39
|
+
storage,
|
|
40
|
+
storageKey,
|
|
41
|
+
type: _type = ['accessor', 'data'][1],
|
|
42
|
+
flag: _flag = undefined,
|
|
43
|
+
descriptorBase: _desc = undefined,
|
|
44
|
+
} = args[0]);
|
|
45
|
+
}
|
|
46
|
+
else if (args.length > 1) {
|
|
47
|
+
([
|
|
48
|
+
to,
|
|
49
|
+
_type,
|
|
50
|
+
key,
|
|
51
|
+
getOrValue,
|
|
52
|
+
_set,
|
|
53
|
+
storage,
|
|
54
|
+
storageKey,
|
|
55
|
+
_flag,
|
|
56
|
+
_desc,
|
|
57
|
+
] = args);
|
|
58
|
+
obj = to;
|
|
59
|
+
_type = (['accessor', 'data'].includes(_type.toLowerCase())
|
|
60
|
+
? _type.toLowerCase() : 'data');
|
|
61
|
+
_get = _type === 'accessor' ? getOrValue : undefined;
|
|
62
|
+
_value = _type === 'data' ? getOrValue : undefined;
|
|
63
|
+
}
|
|
64
|
+
if (!this.isObject(obj)) {
|
|
65
|
+
console.warn('Object.add() must receive an object for `toObject`');
|
|
66
|
+
return obj;
|
|
67
|
+
}
|
|
68
|
+
const more = isDescriptor(_desc) ? _desc : {};
|
|
69
|
+
const flag = _flag || Object.definitionType.mutablyVisible;
|
|
70
|
+
const props = { ...extension_1.Patch.getDescriptorOverridesFromSymbol(flag), ...more };
|
|
71
|
+
const type = (['accessor', 'data'].includes(_type)
|
|
72
|
+
? String(_type).toLowerCase() : 'data');
|
|
73
|
+
switch (type) {
|
|
74
|
+
case 'accessor':
|
|
75
|
+
let store = storage;
|
|
76
|
+
let storeKey = storageKey || key;
|
|
77
|
+
let makeStore = false;
|
|
78
|
+
let get = _get;
|
|
79
|
+
let set = _set;
|
|
80
|
+
if (!toolkit_js_1.is.truthy(get) && !toolkit_js_1.is.function(get)) {
|
|
81
|
+
get = undefined;
|
|
82
|
+
}
|
|
83
|
+
if (!toolkit_js_1.is.truthy(set) && !toolkit_js_1.is.function(set)) {
|
|
84
|
+
set = undefined;
|
|
85
|
+
}
|
|
86
|
+
if (isObj(store) || toolkit_js_1.is.true(store) || toolkit_js_1.is.function(store)) {
|
|
87
|
+
makeStore = toolkit_js_1.is.true(store);
|
|
88
|
+
store = toolkit_js_1.is.fn(store) ? store() : store;
|
|
89
|
+
store = toolkit_js_1.is.object(store) ? store : (makeStore && {} || undefined);
|
|
90
|
+
}
|
|
91
|
+
// store should be defined by here: object or undefined
|
|
92
|
+
if ((!get && !set) && makeStore) {
|
|
93
|
+
// being lazy here, someone has defined we make an accessor but
|
|
94
|
+
// wants the default accessor behaviors with an associated store
|
|
95
|
+
// made by us.
|
|
96
|
+
Object.defineProperty(obj, kDescriptorStore, {
|
|
97
|
+
value: symkeys.add('descriptor.store', store),
|
|
98
|
+
configurable: true,
|
|
99
|
+
enumerable: false,
|
|
100
|
+
writable: true,
|
|
101
|
+
});
|
|
102
|
+
get = () => this[kDescriptorStore]?.data?.[storeKey];
|
|
103
|
+
set = (value) => { this[kDescriptorStore].data[storeKey] = value; };
|
|
104
|
+
}
|
|
105
|
+
else if (get?.length && set?.length > 1 && store) {
|
|
106
|
+
// if we received a get or set that takes more arguments than
|
|
107
|
+
// expected, assume the last argument should be the store variable
|
|
108
|
+
// so we execute the supplied function with the storage and its
|
|
109
|
+
// results or byproducts are the result to the get/set we define
|
|
110
|
+
const innerGet = get;
|
|
111
|
+
const innerSet = set;
|
|
112
|
+
get = () => innerGet(store);
|
|
113
|
+
set = (value) => innerSet(value, store);
|
|
114
|
+
}
|
|
115
|
+
// get and set should be in their final state by here
|
|
116
|
+
Object.defineProperty(obj, key, { ...props, get, set });
|
|
117
|
+
break;
|
|
118
|
+
case 'data':
|
|
119
|
+
Object.defineProperty(obj, key, { ...props, value });
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
return obj;
|
|
123
|
+
},
|
|
124
|
+
addAccessor(to, key, getter, setter, storage) {
|
|
125
|
+
const store = storage ?? (!getter && !setter) ? true : undefined;
|
|
126
|
+
return this.add({ to, key, get: getter, set: setter, storage: store });
|
|
127
|
+
},
|
|
128
|
+
addData(to, key, value) {
|
|
129
|
+
return this.add({ to, key, value });
|
|
130
|
+
},
|
|
30
131
|
/**
|
|
31
132
|
* Creates a shallow copy of the provided object(s).
|
|
32
133
|
*
|
|
@@ -53,7 +154,7 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
53
154
|
* console.log(result); // Output: { a: 1, b: 3, c: 4 }
|
|
54
155
|
*/
|
|
55
156
|
copy(destination, ...sources) {
|
|
56
|
-
return copyObject(false, destination, ...sources);
|
|
157
|
+
return (0, copy_object_js_1.copyObject)(false, destination, ...sources);
|
|
57
158
|
},
|
|
58
159
|
/**
|
|
59
160
|
* Creates a deep copy of the provided object(s).
|
|
@@ -80,7 +181,34 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
80
181
|
* console.log(result); // Output: { a: 1, b: { d: 3 }, e: 4 }
|
|
81
182
|
*/
|
|
82
183
|
deepCopy(destination, ...sources) {
|
|
83
|
-
return copyObject(true, destination, ...sources);
|
|
184
|
+
return (0, copy_object_js_1.copyObject)(true, destination, ...sources);
|
|
185
|
+
},
|
|
186
|
+
/**
|
|
187
|
+
* Defines a new property on an object with a specified value and
|
|
188
|
+
* visibility/mutability flag. The flag determines the visibility and
|
|
189
|
+
* mutability of the property. By default, the property is defined as
|
|
190
|
+
* mutably hidden.
|
|
191
|
+
*
|
|
192
|
+
* @param {object} object - The object on which to define the property.
|
|
193
|
+
* @param {string} key - The name of the property to be defined.
|
|
194
|
+
* @param {any} value - The value of the property to be defined.
|
|
195
|
+
* @param {symbol} [flag=Object.definitionType.mutablyHidden] - The
|
|
196
|
+
* visibility/mutability flag for the property. This should be one of the
|
|
197
|
+
* symbols available in `ObjectExtensions.definitionType`.
|
|
198
|
+
* @returns {object} The object with the newly defined property.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* // Define a new mutably hidden property on an object
|
|
202
|
+
* const myObject = {};
|
|
203
|
+
* const myValue = 'Hello, world!';
|
|
204
|
+
* const hiddenSymbol = Object.definitionType.mutablyHidden;
|
|
205
|
+
* Object.define(myObject, 'myProperty', myValue, hiddenSymbol);
|
|
206
|
+
* // myObject now has a mutably hidden property 'myProperty' with value
|
|
207
|
+
* // 'Hello, world!'
|
|
208
|
+
*/
|
|
209
|
+
define(object, key, value, flag = Object.definitionType.mutablyHidden) {
|
|
210
|
+
const properties = extension_1.Patch.getDescriptorOverridesFromSymbol(flag);
|
|
211
|
+
return Object.defineProperty(object, key, { ...properties, value });
|
|
84
212
|
},
|
|
85
213
|
/**
|
|
86
214
|
* A getter property that provides access to the definition types used
|
|
@@ -113,33 +241,6 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
113
241
|
get immutablyVisible() { return extension_1.Patch.kImmutablyVisible; },
|
|
114
242
|
};
|
|
115
243
|
},
|
|
116
|
-
/**
|
|
117
|
-
* Defines a new property on an object with a specified value and
|
|
118
|
-
* visibility/mutability flag. The flag determines the visibility and
|
|
119
|
-
* mutability of the property. By default, the property is defined as
|
|
120
|
-
* mutably hidden.
|
|
121
|
-
*
|
|
122
|
-
* @param {object} object - The object on which to define the property.
|
|
123
|
-
* @param {string} key - The name of the property to be defined.
|
|
124
|
-
* @param {any} value - The value of the property to be defined.
|
|
125
|
-
* @param {symbol} [flag=Object.definitionType.mutablyHidden] - The
|
|
126
|
-
* visibility/mutability flag for the property. This should be one of the
|
|
127
|
-
* symbols available in `ObjectExtensions.definitionType`.
|
|
128
|
-
* @returns {object} The object with the newly defined property.
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* // Define a new mutably hidden property on an object
|
|
132
|
-
* const myObject = {};
|
|
133
|
-
* const myValue = 'Hello, world!';
|
|
134
|
-
* const hiddenSymbol = Object.definitionType.mutablyHidden;
|
|
135
|
-
* Object.define(myObject, 'myProperty', myValue, hiddenSymbol);
|
|
136
|
-
* // myObject now has a mutably hidden property 'myProperty' with value
|
|
137
|
-
* // 'Hello, world!'
|
|
138
|
-
*/
|
|
139
|
-
define(object, key, value, flag = Object.definitionType.mutablyHidden) {
|
|
140
|
-
const properties = extension_1.Patch.getDescriptorOverridesFromSymbol(flag);
|
|
141
|
-
return Object.defineProperty(object, key, { ...properties, value });
|
|
142
|
-
},
|
|
143
244
|
/**
|
|
144
245
|
* Defines a new accessor property on an object with specified getter and
|
|
145
246
|
* setter functions and a visibility/mutability flag. The flag determines
|
|
@@ -173,112 +274,6 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
173
274
|
const properties = extension_1.Patch.getDescriptorOverridesFromSymbol(flag);
|
|
174
275
|
return Object.defineProperty(object, key, { ...properties, get, set });
|
|
175
276
|
},
|
|
176
|
-
addAccessor(to, key, getter, setter, storage) {
|
|
177
|
-
const store = storage ?? (!getter && !setter) ? true : undefined;
|
|
178
|
-
return this.add({ to, key, get: getter, set: setter, storage: store });
|
|
179
|
-
},
|
|
180
|
-
addData(to, key, value) {
|
|
181
|
-
return this.add({ to, key, value });
|
|
182
|
-
},
|
|
183
|
-
add(...args) {
|
|
184
|
-
const { isDescriptor } = descriptor_js_1.Descriptor;
|
|
185
|
-
const { isObject: isObj } = this;
|
|
186
|
-
const { kDescriptorStore } = this;
|
|
187
|
-
let obj, key, value, _get, _set, storage, storageKey;
|
|
188
|
-
let _type, _flag, _desc;
|
|
189
|
-
// Check to see if we received multiple arguments or an object
|
|
190
|
-
if (args.length && isObj(args[0])) {
|
|
191
|
-
({
|
|
192
|
-
to: obj,
|
|
193
|
-
key,
|
|
194
|
-
value,
|
|
195
|
-
get: _get,
|
|
196
|
-
set: _set,
|
|
197
|
-
storage,
|
|
198
|
-
storageKey,
|
|
199
|
-
type: _type = ['accessor', 'data'][1],
|
|
200
|
-
flag: _flag = undefined,
|
|
201
|
-
descriptorBase: _desc = undefined,
|
|
202
|
-
} = args[0]);
|
|
203
|
-
}
|
|
204
|
-
else if (args.length > 1) {
|
|
205
|
-
([
|
|
206
|
-
to,
|
|
207
|
-
_type,
|
|
208
|
-
key,
|
|
209
|
-
getOrValue,
|
|
210
|
-
_set,
|
|
211
|
-
storage,
|
|
212
|
-
storageKey,
|
|
213
|
-
_flag,
|
|
214
|
-
_desc,
|
|
215
|
-
] = args);
|
|
216
|
-
obj = to;
|
|
217
|
-
_type = (['accessor', 'data'].includes(_type.toLowerCase())
|
|
218
|
-
? _type.toLowerCase() : 'data');
|
|
219
|
-
_get = _type === 'accessor' ? getOrValue : undefined;
|
|
220
|
-
_value = _type === 'data' ? getOrValue : undefined;
|
|
221
|
-
}
|
|
222
|
-
if (!this.isObject(obj)) {
|
|
223
|
-
console.warn('Object.add() must receive an object for `toObject`');
|
|
224
|
-
return obj;
|
|
225
|
-
}
|
|
226
|
-
const more = isDescriptor(_desc) ? _desc : {};
|
|
227
|
-
const flag = _flag || Object.definitionType.mutablyVisible;
|
|
228
|
-
const props = { ...extension_1.Patch.getDescriptorOverridesFromSymbol(flag), ...more };
|
|
229
|
-
const type = (['accessor', 'data'].includes(_type)
|
|
230
|
-
? String(_type).toLowerCase() : 'data');
|
|
231
|
-
switch (type) {
|
|
232
|
-
case 'accessor':
|
|
233
|
-
let store = storage;
|
|
234
|
-
let storeKey = storageKey || key;
|
|
235
|
-
let makeStore = false;
|
|
236
|
-
let get = _get;
|
|
237
|
-
let set = _set;
|
|
238
|
-
if (!isTruthy(get) && !isFn(get)) {
|
|
239
|
-
get = undefined;
|
|
240
|
-
}
|
|
241
|
-
if (!isTruthy(set) && !isFn(set)) {
|
|
242
|
-
set = undefined;
|
|
243
|
-
}
|
|
244
|
-
if (isObj(store) || isTrue(store) || isFn(store)) {
|
|
245
|
-
makeStore = isTrue(store);
|
|
246
|
-
store = isFn(store) ? store() : store;
|
|
247
|
-
store = isObj(store) ? store : (makeStore && {} || undefined);
|
|
248
|
-
}
|
|
249
|
-
// store should be defined by here: object or undefined
|
|
250
|
-
if ((!get && !set) && makeStore) {
|
|
251
|
-
// being lazy here, someone has defined we make an accessor but
|
|
252
|
-
// wants the default accessor behaviors with an associated store
|
|
253
|
-
// made by us.
|
|
254
|
-
Object.defineProperty(obj, kDescriptorStore, {
|
|
255
|
-
value: symkeys.add('descriptor.store', store),
|
|
256
|
-
configurable: true,
|
|
257
|
-
enumerable: false,
|
|
258
|
-
writable: true,
|
|
259
|
-
});
|
|
260
|
-
get = () => this[kDescriptorStore]?.data?.[storeKey];
|
|
261
|
-
set = (value) => { this[kDescriptorStore].data[storeKey] = value; };
|
|
262
|
-
}
|
|
263
|
-
else if (get?.length && set?.length > 1 && store) {
|
|
264
|
-
// if we received a get or set that takes more arguments than
|
|
265
|
-
// expected, assume the last argument should be the store variable
|
|
266
|
-
// so we execute the supplied function with the storage and its
|
|
267
|
-
// results or byproducts are the result to the get/set we define
|
|
268
|
-
const innerGet = get;
|
|
269
|
-
const innerSet = set;
|
|
270
|
-
get = () => innerGet(store);
|
|
271
|
-
set = (value) => innerSet(value, store);
|
|
272
|
-
}
|
|
273
|
-
// get and set should be in their final state by here
|
|
274
|
-
Object.defineProperty(obj, key, { ...props, get, set });
|
|
275
|
-
break;
|
|
276
|
-
case 'data':
|
|
277
|
-
Object.defineProperty(obj, key, { ...props, value });
|
|
278
|
-
break;
|
|
279
|
-
}
|
|
280
|
-
return obj;
|
|
281
|
-
},
|
|
282
277
|
/**
|
|
283
278
|
* Creates a new object from an array of key-value pairs (entries), with an
|
|
284
279
|
* optional prototype and reducer function. If no prototype is provided,
|
|
@@ -313,10 +308,10 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
313
308
|
* // with prototype { foo: 'bar' }
|
|
314
309
|
*/
|
|
315
310
|
fromEntriesUsing(entries, prototype = Object.prototype, reducer = undefined) {
|
|
316
|
-
if (!
|
|
311
|
+
if (!toolkit_js_1.is.array(entries)) {
|
|
317
312
|
return undefined;
|
|
318
313
|
}
|
|
319
|
-
const entriesToUse = entries.filter(entry =>
|
|
314
|
+
const entriesToUse = entries.filter(entry => toolkit_js_1.is.array(entry) && entry.length >= 2);
|
|
320
315
|
if (!entriesToUse.length) {
|
|
321
316
|
return undefined;
|
|
322
317
|
}
|
|
@@ -376,7 +371,7 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
376
371
|
* @returns {string} - The string tag of the object, indicating its type.
|
|
377
372
|
*/
|
|
378
373
|
getStringTag(value, strict = false) {
|
|
379
|
-
if (
|
|
374
|
+
if (toolkit_js_1.has.stringTag(value)) {
|
|
380
375
|
return value[Symbol.toStringTag];
|
|
381
376
|
}
|
|
382
377
|
if (strict) {
|
|
@@ -425,7 +420,7 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
425
420
|
* @returns true if the symbol is defined, false otherwise
|
|
426
421
|
*/
|
|
427
422
|
hasStringTag(value) {
|
|
428
|
-
return
|
|
423
|
+
return toolkit_js_1.has.stringTag(value);
|
|
429
424
|
},
|
|
430
425
|
/**
|
|
431
426
|
* The function checks if a value is either `undefined` or `null`.
|
|
@@ -436,7 +431,7 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
436
431
|
* and `false` otherwise.
|
|
437
432
|
*/
|
|
438
433
|
isNullDefined(value) {
|
|
439
|
-
return value
|
|
434
|
+
return toolkit_js_1.is.nullish(value);
|
|
440
435
|
},
|
|
441
436
|
/**
|
|
442
437
|
* The `ifNullDefined` function checks if a given value is either `null` or
|
|
@@ -471,7 +466,7 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
471
466
|
* );
|
|
472
467
|
*/
|
|
473
468
|
ifNullDefined(value, thenValue, elseValue) {
|
|
474
|
-
return isThenElse(
|
|
469
|
+
return isThenElse(toolkit_js_1.is.nullish(value), thenValue, elseValue);
|
|
475
470
|
},
|
|
476
471
|
/**
|
|
477
472
|
* Checks if the provided value is an object.
|
|
@@ -495,22 +490,31 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
495
490
|
* console.log(isObject(null)); // Output: false
|
|
496
491
|
*/
|
|
497
492
|
isObject(value) {
|
|
498
|
-
return value
|
|
493
|
+
return toolkit_js_1.is.object(value);
|
|
499
494
|
},
|
|
500
495
|
/**
|
|
501
|
-
*
|
|
502
|
-
*
|
|
503
|
-
*
|
|
504
|
-
*
|
|
496
|
+
* Executes a conditional function based on whether the provided value
|
|
497
|
+
* is an object or not. This method first checks if the value is an
|
|
498
|
+
* object using the `is.object` method from the toolkit. If it is, it
|
|
499
|
+
* returns the `thenValue`, otherwise it returns the `elseValue`.
|
|
505
500
|
*
|
|
506
|
-
* @param {
|
|
507
|
-
* @
|
|
508
|
-
*
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
501
|
+
* @param {any} value - The value to be checked.
|
|
502
|
+
* @param {function | any} thenValue - The value to return if `value`
|
|
503
|
+
* is an object.
|
|
504
|
+
* @param {function | any} elseValue - The value to return if `value`
|
|
505
|
+
* is not an object.
|
|
506
|
+
* @returns {*} - Returns `thenValue` if the value is an object,
|
|
507
|
+
* otherwise `elseValue`.
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* // returns 'Is Object'
|
|
511
|
+
* ifObject({}, 'Is Object', 'Not Object')
|
|
512
|
+
* // returns 'Not Object'
|
|
513
|
+
* ifObject(42, 'Is Object', 'Not Object')
|
|
514
|
+
*/
|
|
515
|
+
ifObject(value, thenValue, elseValue) {
|
|
516
|
+
return isThenElse(toolkit_js_1.is.object(value), thenValue, elseValue);
|
|
512
517
|
},
|
|
513
|
-
|
|
514
518
|
/**
|
|
515
519
|
* Checks to see if the supplied value is a primitive value.
|
|
516
520
|
*
|
|
@@ -519,23 +523,7 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
519
523
|
* false otherwise.
|
|
520
524
|
*/
|
|
521
525
|
isPrimitive(value) {
|
|
522
|
-
|
|
523
|
-
// is 'object'
|
|
524
|
-
if (value === null) {
|
|
525
|
-
return true;
|
|
526
|
-
}
|
|
527
|
-
// Check for other primitives
|
|
528
|
-
switch (typeof value) {
|
|
529
|
-
case 'string':
|
|
530
|
-
case 'number':
|
|
531
|
-
case 'bigint':
|
|
532
|
-
case 'boolean':
|
|
533
|
-
case 'undefined':
|
|
534
|
-
case 'symbol':
|
|
535
|
-
return true;
|
|
536
|
-
default:
|
|
537
|
-
return false;
|
|
538
|
-
}
|
|
526
|
+
return toolkit_js_1.is.primitive(value);
|
|
539
527
|
},
|
|
540
528
|
/**
|
|
541
529
|
* Executes a conditional function based on whether the provided value is
|
|
@@ -558,7 +546,7 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
558
546
|
* ifPrimitive({a: 'hello'}, 1, 2)
|
|
559
547
|
*/
|
|
560
548
|
ifPrimitive(value, thenValue, elseValue) {
|
|
561
|
-
return isThenElse(
|
|
549
|
+
return isThenElse(toolkit_js_1.is.primitive(value), thenValue, elseValue);
|
|
562
550
|
},
|
|
563
551
|
/**
|
|
564
552
|
* Checks if the given value is a valid key for an object. In JavaScript, a
|
|
@@ -730,6 +718,54 @@ exports.ObjectExtensions = new extension_1.Patch(Object, {
|
|
|
730
718
|
}
|
|
731
719
|
return result;
|
|
732
720
|
},
|
|
721
|
+
withProperties(prototype, ...properties) {
|
|
722
|
+
const props = properties.filter(p => p instanceof property_js_1.Property);
|
|
723
|
+
const possible = properties.filter(p => Array.isArray(p));
|
|
724
|
+
if (possible.length) {
|
|
725
|
+
for (const [key, descriptorOrDataOrAccessorArgs, ...rest] of possible) {
|
|
726
|
+
// if arg[1] is a descriptor
|
|
727
|
+
if (property_js_1.Property.is.descriptor(descriptorOrDataOrAccessorArgs)) {
|
|
728
|
+
props.push(new property_js_1.Property(key, descriptorOrDataOrAccessorArgs));
|
|
729
|
+
}
|
|
730
|
+
else {
|
|
731
|
+
const { get, set } = descriptorOrDataOrAccessorArgs;
|
|
732
|
+
const args = [key, descriptorOrDataOrAccessorArgs, ...rest];
|
|
733
|
+
if (get || set) {
|
|
734
|
+
props.push(property_js_1.Property.accessor(...args));
|
|
735
|
+
}
|
|
736
|
+
else {
|
|
737
|
+
props.push(property_js_1.Property.data(...args));
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
const object = Object.create(prototype ?? Object.prototype);
|
|
743
|
+
const sorted = props.toSorted((a, b) => a.key < b.key ? -1 : (a.key > b.key ? 1 : 0));
|
|
744
|
+
for (const property of sorted) {
|
|
745
|
+
property.apply(object);
|
|
746
|
+
}
|
|
747
|
+
property_js_1.Property.data(Symbol.for('properties'), sorted).apply(object);
|
|
748
|
+
return object;
|
|
749
|
+
},
|
|
750
|
+
/**
|
|
751
|
+
* Retrieves a toolkit object containing various utility functions
|
|
752
|
+
* for type checking and introspection.
|
|
753
|
+
*
|
|
754
|
+
* The toolkit includes many functions. It was designed to read as
|
|
755
|
+
* though the toolkit were assigned to the variable `it`.
|
|
756
|
+
*
|
|
757
|
+
* @example
|
|
758
|
+
* const is = ObjectExtensions.patches.toolkit
|
|
759
|
+
* console.log(is.object({})) // Output: true
|
|
760
|
+
* console.log(is.string('hello')) // Output: true
|
|
761
|
+
* console.log(is.number(42)) // Output: true
|
|
762
|
+
*
|
|
763
|
+
* @returns {object} The toolkit object containing various utility
|
|
764
|
+
* functions for type checking and introspection.
|
|
765
|
+
*/
|
|
766
|
+
get toolkit() {
|
|
767
|
+
return { as: toolkit_js_1.as, has: toolkit_js_1.has, is: toolkit_js_1.is, si: toolkit_js_1.si };
|
|
768
|
+
},
|
|
733
769
|
},
|
|
734
770
|
});
|
|
735
771
|
const { isObject: pIsObject, ifObject: pIfObject, isNullDefined: pIsNullDefined, ifNullDefined: pIfNullDefined, isPrimitive: pIsPrimitive, ifPrimitive: pIfPrimitive, isValidKey: pIsValidKey, ifValidKey: pIfValidKey, hasStringTag: pHasStringTag, getStringTag: pGetStringTag, stripTo: pStripTo, } = exports.ObjectExtensions.patches;
|
|
@@ -1043,86 +1079,13 @@ exports.ObjectPrototypeExtensions = new extension_1.Patch(Object.prototype, {
|
|
|
1043
1079
|
// {@see globalThis.isThenElse}
|
|
1044
1080
|
function isThenElse(bv, tv, ev) {
|
|
1045
1081
|
if (arguments.length > 1) {
|
|
1046
|
-
var _then =
|
|
1082
|
+
var _then = toolkit_js_1.is.function(tv) ? tv(bv) : tv;
|
|
1047
1083
|
if (arguments.length > 2) {
|
|
1048
|
-
var _else =
|
|
1084
|
+
var _else = toolkit_js_1.is.function(ev) ? tv(bv) : ev;
|
|
1049
1085
|
return bv ? _then : _else;
|
|
1050
1086
|
}
|
|
1051
1087
|
return bv || _then;
|
|
1052
1088
|
}
|
|
1053
1089
|
return bv;
|
|
1054
1090
|
}
|
|
1055
|
-
/**
|
|
1056
|
-
* Creates a deep or shallow copy of the provided source objects and merges
|
|
1057
|
-
* them into the destination object. The function uses a Set to keep track
|
|
1058
|
-
* of visited objects to avoid circular references.
|
|
1059
|
-
*
|
|
1060
|
-
* @function
|
|
1061
|
-
* @name copyObject
|
|
1062
|
-
* @param {boolean} deep - If true, performs a deep copy, otherwise performs
|
|
1063
|
-
* a shallow copy.
|
|
1064
|
-
* @param {object} destination - The object to which properties will be copied.
|
|
1065
|
-
* @param {...object} sources - The source object(s) from which properties
|
|
1066
|
-
* will be copied.
|
|
1067
|
-
* @returns {object} The destination object with the copied properties.
|
|
1068
|
-
*
|
|
1069
|
-
* @example
|
|
1070
|
-
* // Shallow copy
|
|
1071
|
-
* const obj1 = { a: 1, b: { c: 2 } };
|
|
1072
|
-
* const obj2 = { b: { d: 3 }, e: 4 };
|
|
1073
|
-
* const result = copyObject(false, obj1, obj2);
|
|
1074
|
-
* console.log(result); // Output: { a: 1, b: { d: 3 }, e: 4 }
|
|
1075
|
-
*
|
|
1076
|
-
* @example
|
|
1077
|
-
* // Deep copy
|
|
1078
|
-
* const obj1 = { a: 1, b: { c: 2 } };
|
|
1079
|
-
* const obj2 = { b: { d: 3 }, e: 4 };
|
|
1080
|
-
* const result = copyObject(true, obj1, obj2);
|
|
1081
|
-
* console.log(result); // Output: { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
1082
|
-
*/
|
|
1083
|
-
function copyObject(deep, destination, ...sources) {
|
|
1084
|
-
const visited = new Set();
|
|
1085
|
-
for (const source of sources) {
|
|
1086
|
-
if (source === null || typeof source !== 'object' || visited.has(source)) {
|
|
1087
|
-
continue;
|
|
1088
|
-
}
|
|
1089
|
-
visited.add(source);
|
|
1090
|
-
const keys = Reflect.ownKeys(source);
|
|
1091
|
-
for (const key of keys) {
|
|
1092
|
-
let descriptor;
|
|
1093
|
-
try {
|
|
1094
|
-
descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
1095
|
-
}
|
|
1096
|
-
catch (err) {
|
|
1097
|
-
console.warn(`Failed to get descriptor for key "${key}": ${err}`);
|
|
1098
|
-
continue;
|
|
1099
|
-
}
|
|
1100
|
-
const isDataDesc = Reflect.has(descriptor, 'value');
|
|
1101
|
-
const keyedValue = descriptor?.value;
|
|
1102
|
-
const conditionsMet = [
|
|
1103
|
-
isDataDesc,
|
|
1104
|
-
keyedValue,
|
|
1105
|
-
typeof keyedValue === 'object',
|
|
1106
|
-
!visited.has(keyedValue)
|
|
1107
|
-
].every(condition => condition);
|
|
1108
|
-
if (conditionsMet) {
|
|
1109
|
-
visited.add(keyedValue);
|
|
1110
|
-
const prototype = Object.getPrototypeOf(keyedValue);
|
|
1111
|
-
const descriptors = Object.getOwnPropertyDescriptors(keyedValue);
|
|
1112
|
-
const replacement = Object.create(prototype, descriptors);
|
|
1113
|
-
descriptor.value = deep
|
|
1114
|
-
? copyObject(deep, replacement, keyedValue)
|
|
1115
|
-
: replacement;
|
|
1116
|
-
}
|
|
1117
|
-
try {
|
|
1118
|
-
Object.defineProperty(destination, key, descriptor);
|
|
1119
|
-
}
|
|
1120
|
-
catch (err) {
|
|
1121
|
-
console.error(`Failed to define property "${key}": ${err}`);
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
return destination;
|
|
1126
|
-
}
|
|
1127
|
-
exports.copyObject = copyObject;
|
|
1128
1091
|
//# sourceMappingURL=object.extensions.js.map
|