@lumjs/core 1.18.0 → 1.19.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/lib/{types/console.js → console.js} +6 -6
- package/lib/index.js +7 -0
- package/lib/types/basics.js +110 -110
- package/lib/types/index.js +5 -2
- package/lib/types/needs.js +1 -1
- package/lib/types/owncount.js +50 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const def = require('./def');
|
|
2
|
-
const {F} = require('./js');
|
|
1
|
+
const def = require('./types/def');
|
|
2
|
+
const {F} = require('./types/js');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* A super simple singleton wrapper around the Javascript console.
|
|
@@ -7,7 +7,7 @@ const {F} = require('./js');
|
|
|
7
7
|
* By default includes `info`, `log`, `warn`, and `error` methods.
|
|
8
8
|
* It may be expanded further using the `addMethod` method.
|
|
9
9
|
*
|
|
10
|
-
* @exports module:@lumjs/core/
|
|
10
|
+
* @exports module:@lumjs/core/console
|
|
11
11
|
*/
|
|
12
12
|
const LC =
|
|
13
13
|
{
|
|
@@ -80,7 +80,7 @@ const LC =
|
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
82
|
* A reference to the real console object.
|
|
83
|
-
* @alias module:@lumjs/core/
|
|
83
|
+
* @alias module:@lumjs/core/console.real
|
|
84
84
|
*/
|
|
85
85
|
const RC = globalThis.console;
|
|
86
86
|
def(LC, 'real', RC);
|
|
@@ -97,14 +97,14 @@ const HANDLER_OPTIONS =
|
|
|
97
97
|
/**
|
|
98
98
|
* Add a wrapper method to the console wrapper object.
|
|
99
99
|
* @param {string} method - The console method to wrap.
|
|
100
|
-
* @alias module:@lumjs/core/
|
|
100
|
+
* @alias module:@lumjs/core/console.addMethod
|
|
101
101
|
*/
|
|
102
102
|
function addMethod(method)
|
|
103
103
|
{
|
|
104
104
|
def(LC, method, function(...args)
|
|
105
105
|
{
|
|
106
106
|
if (typeof LC.trigger === F)
|
|
107
|
-
{ // Support for core.observable(core.
|
|
107
|
+
{ // Support for core.observable(core.console);
|
|
108
108
|
LC.trigger(method, ...args);
|
|
109
109
|
}
|
|
110
110
|
|
package/lib/index.js
CHANGED
|
@@ -79,6 +79,13 @@ lazy(exports, 'flags', () => require('./flags'));
|
|
|
79
79
|
*/
|
|
80
80
|
lazy(exports, 'obj', () => require('./obj'));
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* A wrapper around the Javascript console.
|
|
84
|
+
* @name module:@lumjs/core.console
|
|
85
|
+
* @type {module:@lumjs/core/console}
|
|
86
|
+
*/
|
|
87
|
+
lazy(exports, 'console', () => require('./console'));
|
|
88
|
+
|
|
82
89
|
/**
|
|
83
90
|
* Functions for getting values and properties with fallback defaults «Lazy»
|
|
84
91
|
* @name module:@lumjs/core.opt
|
package/lib/types/basics.js
CHANGED
|
@@ -6,92 +6,92 @@ const {O, F, S, SY} = require('./js');
|
|
|
6
6
|
* @returns {boolean}
|
|
7
7
|
* @alias module:@lumjs/core/types.isObj
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
9
|
+
function isObj(v) { return (typeof v === O && v !== null); }
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* See if a value is *complex* (i.e. either `object` or `function`).
|
|
13
|
+
* Like `isObj()`, `null` does not count as an `object`.
|
|
14
|
+
* @param {*} v - The value we're testing.
|
|
15
|
+
* @returns {boolean}
|
|
16
|
+
* @alias module:@lumjs/core/types.isComplex
|
|
17
|
+
*/
|
|
18
|
+
function isComplex(v) { return (typeof v === F || isObj(v)); }
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* See if a value is *nil* (i.e. either `null` or `undefined`).
|
|
22
|
+
* @param {*} v - The value we're testing.
|
|
23
|
+
* @returns {boolean}
|
|
24
|
+
* @alias module:@lumjs/core/types.isNil
|
|
25
|
+
*/
|
|
26
|
+
function isNil(v) { return (v === undefined || v === null); }
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* See if a value is not *nil* (i.e. neither `null` nor `undefined`).
|
|
30
|
+
* @param {*} v - The value we're testing.
|
|
31
|
+
* @returns {boolean}
|
|
32
|
+
* @alias module:@lumjs/core/types.notNil
|
|
33
|
+
*/
|
|
34
|
+
function notNil(v) { return (v !== undefined && v !== null); }
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* See if a value is a scalar.
|
|
38
|
+
* For the purposes of this, a scalar is any value which
|
|
39
|
+
* is neither *nil* nor *complex*.
|
|
40
|
+
* @param {*} v - The value we're testing.
|
|
41
|
+
* @returns {boolean}
|
|
42
|
+
* @alias module:@lumjs/core/types.isScalar
|
|
43
|
+
*/
|
|
44
|
+
function isScalar(v) { return (notNil(v) && !isComplex(v)); }
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* See if a value is an `Array` object.
|
|
48
|
+
* This is literally just a copy of `Array.isArray`.
|
|
49
|
+
* @function
|
|
50
|
+
* @param {*} v - The value we're testing.
|
|
51
|
+
* @returns {boolean}
|
|
52
|
+
* @alias module:@lumjs/core/types.isArray
|
|
53
|
+
*/
|
|
54
|
+
const isArray = Array.isArray;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* See if a value is a `TypedArray` object.
|
|
58
|
+
* @param {*} v - The value we're testing.
|
|
59
|
+
* @returns {boolean}
|
|
60
|
+
* @alias module:@lumjs/core/types.isTypedArray
|
|
61
|
+
*/
|
|
62
|
+
function isTypedArray(v)
|
|
63
|
+
{
|
|
64
|
+
return (ArrayBuffer.isView(v) && !(v instanceof DataView));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* See if a value is a non-empty Array.
|
|
69
|
+
* @param {*} v - The value we're testing.
|
|
70
|
+
* @param {boolean} [typed=false] If `true` we want a `TypedArray`.
|
|
71
|
+
* If `false` (default) we want a regular `Array`.
|
|
72
|
+
* @returns {boolean}
|
|
73
|
+
* @alias module:@lumjs/core/types.nonEmptyArray
|
|
74
|
+
*/
|
|
75
|
+
function nonEmptyArray(v, typed=false)
|
|
76
|
+
{
|
|
77
|
+
if (typed)
|
|
78
|
+
return (isTypedArray(v) && v.length > 0);
|
|
79
|
+
else
|
|
80
|
+
return (isArray(v) && v.length > 0);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* See if a value is an `arguments` object.
|
|
85
|
+
* @param {*} v - The value we're testing.
|
|
86
|
+
* @returns {boolean}
|
|
87
|
+
* @alias module:@lumjs/core/types.isArguments
|
|
88
|
+
*/
|
|
89
|
+
function isArguments(v)
|
|
90
|
+
{
|
|
91
|
+
return Object.prototype.toString.call(v) === '[object Arguments]';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
95
|
* See if a value is a Property name.
|
|
96
96
|
* @param {*} v - The value we're testing.
|
|
97
97
|
* @returns {boolean}
|
|
@@ -122,28 +122,28 @@ function isProperty(v)
|
|
|
122
122
|
* @returns {boolean} - Is the object a valid descriptor?
|
|
123
123
|
* @alias module:@lumjs/core/types.doesDescriptor
|
|
124
124
|
*/
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
125
|
+
function doesDescriptor(obj)
|
|
126
|
+
{
|
|
127
|
+
if (isObj(obj))
|
|
128
|
+
{
|
|
129
|
+
const hasValue = (obj.value !== undefined);
|
|
130
|
+
const hasGetter = (typeof obj.get === F);
|
|
131
|
+
const hasSetter = (typeof obj.set === F);
|
|
132
|
+
const hasWritable = (obj.writable !== undefined);
|
|
133
|
+
|
|
134
|
+
if (hasValue && !hasGetter && !hasSetter)
|
|
135
|
+
{ // We have a value, and no getter or setter.
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
else if ((hasGetter || hasSetter) && !hasValue && !hasWritable)
|
|
139
|
+
{ // We have a getter or setter, and no value or writable properties.
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Nothing matched, not a valid descriptor rule.
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
147
|
|
|
148
148
|
/**
|
|
149
149
|
* See if an object can be used as a valid *descriptor template*.
|
|
@@ -195,6 +195,6 @@ function doesDescriptorTemplate(obj, accessor=false, strict=true)
|
|
|
195
195
|
// Now export those.
|
|
196
196
|
module.exports =
|
|
197
197
|
{
|
|
198
|
-
isObj, isComplex, isNil, notNil, isScalar, isArray, isTypedArray,
|
|
199
|
-
nonEmptyArray, isArguments, isProperty, doesDescriptor, doesDescriptorTemplate,
|
|
198
|
+
isObj, isComplex, isNil, notNil, isScalar, isArray, isTypedArray,
|
|
199
|
+
nonEmptyArray, isArguments, isProperty, doesDescriptor, doesDescriptorTemplate,
|
|
200
200
|
}
|
package/lib/types/index.js
CHANGED
|
@@ -42,8 +42,10 @@ const def = require('./def');
|
|
|
42
42
|
const lazy = require('./lazy');
|
|
43
43
|
const TYPES = require('./typelist');
|
|
44
44
|
const stringify = require('./stringify');
|
|
45
|
+
const ownCount = require('./owncount');
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
// An alias for legacy reasons.
|
|
48
|
+
const console = require('../console');
|
|
47
49
|
|
|
48
50
|
// Okay, add all those to our exports.
|
|
49
51
|
// Further tests can be added by `TYPES.add()` later.
|
|
@@ -53,7 +55,8 @@ module.exports =
|
|
|
53
55
|
isObj, isComplex, isNil, notNil, isScalar, isArray, isTypedArray,
|
|
54
56
|
nonEmptyArray, isArguments, isProperty, doesDescriptor,
|
|
55
57
|
isInstance, isType, isa, needObj, needType, needs, stringify,
|
|
56
|
-
doesDescriptorTemplate,
|
|
58
|
+
doesDescriptorTemplate, ownCount,
|
|
59
|
+
console,
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
// Last but not least, this will be the module for TYPES.add()
|
package/lib/types/needs.js
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return the number of _local_ properties in an object.
|
|
3
|
+
*
|
|
4
|
+
* @param {(object|function)} v - The value we want the property count of.
|
|
5
|
+
*
|
|
6
|
+
* This can be any kind of `object`, or it can be a `function` that has
|
|
7
|
+
* a `prototype` property which is a `object`. Anything else is invalid.
|
|
8
|
+
*
|
|
9
|
+
* @param {number} [level=0] Determine which properties to count.
|
|
10
|
+
*
|
|
11
|
+
* - `== 0` : Counts only enumerable properties.
|
|
12
|
+
* - `> 0` : Includes all properties with string names.
|
|
13
|
+
* - `> 1` : Also includes symbol properties.
|
|
14
|
+
*
|
|
15
|
+
* @returns {number} The number of matching properties.
|
|
16
|
+
*
|
|
17
|
+
* If `v` was an invalid value, this will return `-1`.
|
|
18
|
+
*
|
|
19
|
+
* @alias module:@lumjs/core/types.ownCount
|
|
20
|
+
*/
|
|
21
|
+
function ownCount(v, level=0)
|
|
22
|
+
{
|
|
23
|
+
if (typeof v === F && isObj(v.prototype))
|
|
24
|
+
{ // If a class constructor is passed, we use the prototype.
|
|
25
|
+
v = v.prototype;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!isObj(v))
|
|
29
|
+
{ // Nothing more to do here.
|
|
30
|
+
return -1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (level == 0)
|
|
34
|
+
{ // Enumerable properties only.
|
|
35
|
+
return Object.keys(v).length;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Include all properties with string names.
|
|
39
|
+
|
|
40
|
+
let count = Object.getOwnPropertyNames(v).length;
|
|
41
|
+
|
|
42
|
+
if (level > 1)
|
|
43
|
+
{ // Also include symbol properties.
|
|
44
|
+
count += Object.getOwnPropertySymbols(v).length;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return count;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = ownCount;
|