cno-bedrock 0.0.1

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/src/lib.js ADDED
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ # [lib.js](src/lib.js)
4
+ > `cno-bedrock`: micropackage: a test bed for various small pieces of reusable code.
5
+
6
+ Author: Anadian
7
+
8
+ Code license: MIT
9
+ ```
10
+ Copyright 2024 Anadian
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
12
+ software and associated documentation files (the "Software"), to deal in the Software
13
+ without restriction, including without limitation the rights to use, copy, modify,
14
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
15
+ permit persons to whom the Software is furnished to do so, subject to the following
16
+ conditions:
17
+ The above copyright notice and this permission notice shall be included in all copies
18
+ or substantial portions of the Software.
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
20
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
21
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23
+ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
24
+ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ ```
26
+ Documentation License: [![Creative Commons License](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-sa/4.0/)
27
+ > The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
28
+
29
+ */
30
+
31
+ //# Dependencies
32
+ //## Internal
33
+ import Time from './cno-time.js';
34
+ import Utility from './cno-utility.js';
35
+ import NOOP from './cno-noop.js';
36
+ import deriveOptions from './cno-options.js';
37
+ import Assert from './cno-assert.js';
38
+ //## Standard
39
+ //## External
40
+
41
+ //# Constants
42
+ const FILENAME = 'lib.js';
43
+ //## Errors
44
+
45
+ //# Global Variables
46
+ /**## Functions*/
47
+ // Utility functions
48
+
49
+ /**
50
+ ### isStrictlyNotEqual
51
+ > Returns `true` if the a give property `received` is the same type but not the same value as `invalid`.
52
+
53
+ #### Parametres
54
+ | name | type | description |
55
+ | --- | --- | --- |
56
+ | input_options | object | Run-time options. \[default: {}\] |
57
+
58
+ ##### `options` Properties
59
+ | name | type | default | description |
60
+ | --- | --- | --- | --- |
61
+ | noop | boolean | false | Skip primary functionality. |
62
+ | noDefaults | boolean | false | Don't apply static default options. |
63
+ | noDynamic | boolean | false | Don't apply dynamic default options. |
64
+ | received | object | null | The value to be checked against `invalid`. |
65
+ | invalid | object | null | The test condition; same type as that of the expected value in `received` but a different value. |
66
+ | code | string | null | The sort of variable/property being checked, used to give more descriptive error messages; should be either 'ARG', 'PROPERTY', or 'RETURN'. |
67
+ | name | string | null | The name of the variable/property being checked, used to give more descriptive error messages; dynamically defaults to either 'value' or 'type' depending on which part of operation failed. |
68
+ | value | string | null | A string representation of `invalid`, used to give more descriptive error messages; dynamically defaults to `invalid` if not specified. |
69
+ | expectedType | string | null | A string representation of the type expected, used to give more descriptive error messages; dynamically defaults to `typeof(invalid)` if not specified. |
70
+
71
+ #### Returns
72
+ | type | description |
73
+ | --- | --- |
74
+ | boolean | Returns `true` if `received` is the same type but not the same value as `invalid`; throws an descriptive error message otherwise. |
75
+
76
+ #### Throws
77
+ | code | type | condition |
78
+ | --- | --- | --- |
79
+ | 'ERR_INVALID_TYPE' | TypeError | Thrown if `received` and `invalid` aren't of the same type and `code` is not provided. |
80
+ | 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if `received` and `invalid` aren't of the same type and `code` is given as 'ARG'. |
81
+ | 'ERR_INVALID_PROPERTY_TYPE' | TypeError | Thrown if `received` and `invalid` aren't of the same type and `code` is given as 'PROPERTY'. |
82
+ | 'ERR_INVALID_RETURN_TYPE' | TypeError | Thrown if `received` and `invalid` aren't of the same type and `code` is given as 'RETURN'. |
83
+ | 'ERR_INVALID_VALUE' | Error | Thrown if `received` and `invalid` are an equal value and `code` is not provided. |
84
+ | 'ERR_INVALID_ARG_VALUE' | Error | Thrown if `received` and `invalid` are an equal value and `code` is given as 'ARG'. |
85
+ | 'ERR_INVALID_PROPERTY_VALUE' | Error | Thrown if `received` and `invalid` are an equal value and `code` is given as 'PROPERTY'. |
86
+ | 'ERR_INVALID_RETURN_VALUE' | Error | Thrown if `received` and `invalid` are an equal value and `code` is given as 'RETURN'. |
87
+
88
+ #### History
89
+ | version | change |
90
+ | --- | --- |
91
+ | 0.0.1 | WIP |
92
+ */
93
+ function isStrictlyNotEqual( input_options = {} ){
94
+ const FUNCTION_NAME = 'isStrictlyNotEqual';
95
+ const DEFAULT_OPTIONS = {
96
+ noop: false, // Skip primary functionality.
97
+ noDefaults: false, // Don't apply static default options.
98
+ noDynamic: false, // Don't apply dynamic default options.
99
+ received: null, // The value to be checked against `invalid`.
100
+ invalid: null, // The test condition; same type as that of the expected value in `received` but a different value.
101
+ code: null, // The sort of variable/property being checked, used to give more descriptive error messages; should be either 'ARG', 'PROPERTY', or 'RETURN'.
102
+ name: null, // The name of the variable/property being checked, used to give more descriptive error messages; dynamically defaults to either 'value' or 'type' depending on which part of operation failed.
103
+ value: null, // A string representation of `invalid`, used to give more descriptive error messages; dynamically defaults to `invalid` if not specified.
104
+ expectedType: null, // A string representation of the type expected, used to give more descriptive error messages; dynamically defaults to `typeof(invalid)` if not specified.
105
+ errorCallback: NOOP.returnUndefined
106
+ };// Variables
107
+ //var arguments_array = Array.from(arguments);
108
+ var _return = false;
109
+ var return_error = null;
110
+ this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received.`});
111
+ // Parametre checks
112
+ if( typeof(input_options) !== 'object' ){
113
+ return_error = new TypeError('Param "input_options" is not of type object.');
114
+ return_error.code = 'ERR_INVALID_ARG_TYPE';
115
+ throw return_error;
116
+ }
117
+
118
+ // Options
119
+ const { logFunction: log_function = this?.logger?.log, validationFunction: validation_function, ...options } = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
120
+ if( options.noop !== true ){
121
+ // Function
122
+ try{
123
+ Assert.assertStrictlyNotEqual( options.received, options.invalid, options.name, options.value, options.expectedType );
124
+ _return = true;
125
+ } catch( error ){
126
+ options.errorCallback( error );
127
+ _return = false;
128
+ }
129
+ } // noop
130
+ // Return
131
+ this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
132
+ return _return;
133
+ } // isStrictlyNotEqual
134
+
135
+ // Exports
136
+
137
+ const NAMESPACE = {};
138
+ Object.defineProperties( NAMESPACE, {
139
+ time: {
140
+ value: Time,
141
+ enumerable: true
142
+ },
143
+ utility: {
144
+ value: Utility,
145
+ enumerable: true
146
+ },
147
+ noop: {
148
+ value: NOOP,
149
+ enumerable: true
150
+ },
151
+ assert: {
152
+ value: Assert,
153
+ enumerable: true
154
+ },
155
+ deriveOptions: {
156
+ value: deriveOptions,
157
+ enumerable: true
158
+ },
159
+ isStrictlyNotEqual: {
160
+ value: isStrictlyNotEqual,
161
+ enumerable: true
162
+ }
163
+ } );
164
+
165
+ export { NAMESPACE as default, isStrictlyNotEqual };
166
+ export * from './cno-time.js';
167
+ export * from './cno-utility.js';
168
+ export * from './cno-noop.js';
169
+ export * from './cno-options.js';
170
+ export * from './cno-assert.js';
171
+
172
+ // lib.js EOF
173
+
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ # [lib.test.js](src/lib.test.js)
4
+ > Simple practical test for CNO-Bedrock.
5
+
6
+ Author: Anadian
7
+
8
+ Code license: MIT
9
+ ```
10
+ Copyright 2024 Anadian
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
12
+ software and associated documentation files (the "Software"), to deal in the Software
13
+ without restriction, including without limitation the rights to use, copy, modify,
14
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
15
+ permit persons to whom the Software is furnished to do so, subject to the following
16
+ conditions:
17
+ The above copyright notice and this permission notice shall be included in all copies
18
+ or substantial portions of the Software.
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
20
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
21
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23
+ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
24
+ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ ```
26
+ Documentation License: [![Creative Commons License](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-sa/4.0/)
27
+ > The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
28
+
29
+ */
30
+
31
+ //# Dependencies
32
+ //## Internal
33
+ import Bedrock from './lib.js';
34
+ //## Standard
35
+ import { AssertionError } from 'node:assert/strict';
36
+ //## External
37
+ //# Constants
38
+ const FILENAME = 'lib.test.js';
39
+ //## Errors
40
+
41
+ //# Global Variables
42
+ /**## Functions*/
43
+ function main_test(){
44
+ console.log( 'Testing starting at %s', Bedrock.time.getNowISO() );
45
+ var benchmark = Bedrock.time.Benchmark( { clock: Bedrock.time.Clock() } );
46
+ Bedrock.utility.annotateThis.call( benchmark );
47
+ benchmark.begin();
48
+ Bedrock.assert.deepStrictEqual( Bedrock.noop.returnNull(), null );
49
+ Bedrock.assert.deepStrictEqual( Bedrock.noop.returnTrue(), true );
50
+ Bedrock.assert.deepStrictEqual( Bedrock.noop.returnFalse(), false );
51
+ Bedrock.assert.deepStrictEqual( Bedrock.noop.returnUndefined(), undefined );
52
+ benchmark.mark();
53
+ Bedrock.assert.assertSameType( 1, 0 );
54
+ try{
55
+ Bedrock.assert.assertSameType( '1', 0 );
56
+ } catch( error ){
57
+ Bedrock.assert.assertExpectedError( error, { constructor: AssertionError, code: 'ERR_ASSERTION_SAMETYPE' } );
58
+ }
59
+ benchmark.mark();
60
+ Bedrock.assert.assertNonstrictNotEqual( '1', 0 );
61
+ try{
62
+ Bedrock.assert.assertNonstrictNotEqual( false, 0 );
63
+ } catch( error ){
64
+ Bedrock.assert.assertExpectedError( error, { constructor: AssertionError, code: 'ERR_ASSERTION_NONSTRICTNOTEQUAL' } );
65
+ }
66
+ benchmark.mark();
67
+ Bedrock.assert.assertStrictlyNotEqual( 1, 0 );
68
+ try{
69
+ Bedrock.assert.assertStrictlyNotEqual( 1, 1 );
70
+ } catch( error ){
71
+ Bedrock.assert.assertExpectedError( error, { constructor: AssertionError, code: 'ERR_ASSERTION_STRICTLYNOTEQUAL' } );
72
+ }
73
+ try{
74
+ Bedrock.assert.assertStrictlyNotEqual( 1, false );
75
+ } catch( error ){
76
+ Bedrock.assert.assertExpectedError( error, { constructor: AssertionError, code: 'ERR_ASSERTION_STRICTLYNOTEQUAL' } );
77
+ }
78
+ benchmark.mark();
79
+ Bedrock.assert.assertInstanceof( new TypeError(), Error ); // TypeError is an instance of Error
80
+ try{
81
+ Bedrock.assert.assertInstanceof( new Error(), TypeError );
82
+ } catch( error ){
83
+ Bedrock.assert.assertExpectedError( error, { constructor: AssertionError, code: 'ERR_ASSERTION_INSTANCEOF' } );
84
+ }
85
+ benchmark.mark();
86
+ Bedrock.assert.assertObjectsQuantitativelyEqual( { noop: false, noDefaults: false, noDynamic: false, logFunction: Bedrock.noop.returnNull, validationFunction: Bedrock.noop.returnTrue }, Bedrock.deriveOptions.STANDARD_OPTIONS );
87
+ try{
88
+ Bedrock.assert.assertObjectsQuantitativelyEqual( { a: 3 }, { b: 3 } );
89
+ } catch( error ){
90
+ Bedrock.assert.assertExpectedError( error, { constructor: AssertionError, code: 'ERR_ASSERTION_OBJECTSQUANTITAIVELYEQUAL' } );
91
+ }
92
+ benchmark.mark();
93
+ try{
94
+ var error = new Error();
95
+ error.code = 'NONE';
96
+ Bedrock.assert.assertExpectedError( error, { constructor: TypeError, code: 'NONE' } );
97
+ } catch( error ){
98
+ Bedrock.assert.assertExpectedError( error, { constructor: AssertionError, code: 'ERR_ASSERTION_EXPECTEDERROR' } );
99
+ }
100
+ try{
101
+ error = new Error();
102
+ error.code = 'WRONG';
103
+ Bedrock.assert.assertExpectedError( error, { constructor: Error, code: 'NONE' } );
104
+ } catch( error ){
105
+ Bedrock.assert.assertExpectedError( error, { constructor: AssertionError, code: 'ERR_ASSERTION_EXPECTEDERROR' } );
106
+ }
107
+ benchmark.end();
108
+ console.log( benchmark.getIntervals() );
109
+ console.log( 'Benchmark end: %s', Bedrock.time.getISOStringFromUnixTimestamp( Number(benchmark.duration) * benchmark.granularity ) );
110
+ console.log( Bedrock.utility.inspThis.call( benchmark ) );
111
+ // Improving coverage
112
+ try{
113
+ Bedrock.isStrictlyNotEqual( false );
114
+ } catch( error ){
115
+ Bedrock.assert.assertExpectedError( error, { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } );
116
+ }
117
+ Bedrock.assert.deepStrictEqual( Bedrock.isStrictlyNotEqual( { received: 1, invalid: 0 } ), true );
118
+ Bedrock.assert.deepStrictEqual( Bedrock.isStrictlyNotEqual( { received: 0, invalid: 0 } ), false );
119
+ var current_options = { a: 0 };
120
+ var default_options = { a: 1 };
121
+ var dynamic_function = ( options ) => {
122
+ options.a = 5;
123
+ return options;
124
+ };
125
+ Bedrock.assert.assertObjectsQuantitativelyEqual( Bedrock.deriveOptions( current_options, default_options, dynamic_function, false ), { a: 0 } );
126
+ current_options = { noDefaults: true };
127
+ Bedrock.assert.assertObjectsQuantitativelyEqual( Bedrock.deriveOptions( current_options, null ), { noDefaults: true } );
128
+ try{
129
+ Bedrock.utility.getAnnotatedObject( false );
130
+ } catch( error ){
131
+ Bedrock.assert.assertExpectedError( error, { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } );
132
+ }
133
+ var annotated_object = Bedrock.utility.getAnnotatedObject( current_options );
134
+ Bedrock.assert.assertObjectsQuantitativelyEqual( current_options, { noDefaults: true } );
135
+ Bedrock.assert.deepStrictEqual( Bedrock.time.getStringFromDurationOptions( { duration: 109210 } ), "30\u205fh\u200520\u205fmin\u200510\u205fs" );
136
+ try{
137
+ Bedrock.time.getStringFromDurationOptions( false );
138
+ } catch( error ){
139
+ Bedrock.assert.assertExpectedError( error, { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } );
140
+ }
141
+ //var new_benchmark = Bedrock.time.Benchmark( { clock: Bedrock.time.Clock() } );
142
+ try{
143
+ Bedrock.time.Clock( false );
144
+ } catch( error ){
145
+ Bedrock.assert.assertExpectedError( error, { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } );
146
+ }
147
+ try{
148
+ Bedrock.time.Benchmark( false );
149
+ } catch( error ){
150
+ Bedrock.assert.assertExpectedError( error, { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } );
151
+ }
152
+ }
153
+ main_test();
154
+
155
+ // lib.test.js EOF
156
+