cno-bedrock 0.0.4 → 0.1.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.
@@ -30,7 +30,8 @@ Documentation License: [![Creative Commons License](https://i.creativecommons.or
30
30
 
31
31
  //# Dependencies
32
32
  //## Internal
33
- import NOOP from './cno-noop.js'
33
+ import NOOP from './cno-noop.js';
34
+ import { assertSameType } from './cno-assert.js';
34
35
  //## Standard
35
36
  //## External
36
37
  //# Constants
@@ -78,17 +79,85 @@ function deriveOptions( input_options, default_options, dynamic_function = null,
78
79
  if( standard_options !== false ){
79
80
  options = Object.assign( options, deriveOptions.STANDARD_OPTIONS, input_options );
80
81
  }
81
- if( input_options.noDefaults !== true && default_options != null ){
82
+ if( input_options?.noDefaults !== true && default_options != null ){
82
83
  options = Object.assign( options, default_options, input_options );
83
- if( input_options.noDynamic !== true && typeof(dynamic_function) === 'function' && dynamic_function != null ){
84
+ if( input_options?.noDynamic !== true && typeof(dynamic_function) === 'function' && dynamic_function != null ){
84
85
  options = Object.assign( options, dynamic_function.call( this, options ), input_options );
85
86
  } // noDynamic
86
87
  } else{
87
- options = Object.assign( {}, input_options );
88
+ options = Object.assign( options, input_options );
88
89
  } // noDefaults
89
90
  _return = options;
91
+ // try{
92
+ // deriveOptions.assertOptions( _return );
93
+ // } catch( error ){
94
+ // console.error( 'options=%o error=%o input_options=%o default_options=%o dynamic_function=%o STANDARD_OPTIONS=%o', options, error, input_options, default_options, dynamic_function, deriveOptions.STANDARD_OPTIONS );
95
+ // }
90
96
  return _return;
91
97
  } // deriveOptions
98
+ deriveOptions.assertOptions = function( options_object ){
99
+ var return_error = null;
100
+ var errors_array = [];
101
+ try{
102
+ assertSameType( options_object, deriveOptions.STANDARD_OPTIONS );
103
+ } catch( error ){
104
+ return_error = new Error( `Given "object" is not an object: ${error}` );
105
+ return_error.cause = error;
106
+ errors_array.push( return_error );
107
+ }
108
+ try{
109
+ assertSameType( options_object.noop, deriveOptions.STANDARD_OPTIONS.noop );
110
+ } catch( error ){
111
+ return_error = new Error( `For property "noop": ${error}` );
112
+ return_error.cause = error;
113
+ errors_array.push( return_error );
114
+ }
115
+ try{
116
+ assertSameType( options_object.noDefaults, deriveOptions.STANDARD_OPTIONS.noDefaults );
117
+ } catch( error ){
118
+ return_error = new Error( `For property "noDefaults": ${error}` );
119
+ return_error.cause = error;
120
+ errors_array.push( return_error );
121
+ }
122
+ try{
123
+ assertSameType( options_object.noDynamic, deriveOptions.STANDARD_OPTIONS.noDynamic );
124
+ } catch( error ){
125
+ return_error = new Error( `For property "noDynamic": ${error}` );
126
+ return_error.cause = error;
127
+ errors_array.push( return_error );
128
+ }
129
+ try{
130
+ assertSameType( options_object.logFunction, deriveOptions.STANDARD_OPTIONS.logFunction );
131
+ } catch( error ){
132
+ return_error = new Error( `For property "logFunction": ${error}` );
133
+ return_error.cause = error;
134
+ errors_array.push( return_error );
135
+ }
136
+ try{
137
+ assertSameType( options_object.validationFunction, deriveOptions.STANDARD_OPTIONS.validationFunction );
138
+ } catch( error ){
139
+ return_error = new Error( `For property "validationFunction": ${error}` );
140
+ return_error.cause = error;
141
+ errors_array.push( return_error );
142
+ }
143
+ if( errors_array.length > 1 ){
144
+ return_error = new AggregateError( errors_array );
145
+ throw return_error;
146
+ } else if( errors_array.length === 1 ){
147
+ return_error = errors_array[0];
148
+ throw return_error;
149
+ }
150
+ }
151
+ deriveOptions.isOptions = function( options_object ){
152
+ var _return = false;
153
+ try{
154
+ deriveOptions.assertOptions( options_object );
155
+ _return = true;
156
+ } catch( error ){
157
+ _return = false;
158
+ }
159
+ return _return;
160
+ }
92
161
  Object.defineProperties( deriveOptions, {
93
162
  STANDARD_OPTIONS: {
94
163
  value: {
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env node
2
+
3
+ import deriveOptions from './cno-options.js';
4
+ import TestNS from './cno-test-matrix.js';
5
+ import { assertIsEmpty } from './cno-assert.js';
6
+
7
+ TestNS.test( 'deriveOptions:success', async function( t ){
8
+ t.diagnostic( t.name );
9
+ var tm = new TestNS.TestMatrix( { testContext: t } );
10
+ tm.addFunction( { name: 'deriveOptions', func: deriveOptions } );
11
+ tm.addCondition( {
12
+ name: 'null-all',
13
+ success: true,
14
+ args: [ null ],
15
+ expected: deriveOptions.STANDARD_OPTIONS
16
+ } );
17
+ tm.addCondition( {
18
+ name: 'no-standard-options',
19
+ success: true,
20
+ args: [ null, null, null, false ],
21
+ expected: ( return_value ) => {
22
+ var _return = false;
23
+ assertIsEmpty( return_value );
24
+ _return = true;
25
+ return _return;
26
+ }
27
+ } );
28
+ tm.addCondition( {
29
+ name: 'noDefaults-customvalue',
30
+ success: true,
31
+ args: [ { noDefaults: 'customFromInput' }, null, null, true ],
32
+ expected: { ...deriveOptions.STANDARD_OPTIONS, noDefaults: 'customFromInput' }
33
+ } );
34
+ tm.addCondition( {
35
+ name: 'noDynamic-customvalueFromDefaults',
36
+ success: true,
37
+ args: [ { noDefaults: 'customFromInput' }, { noDynamic: 'customFromDefaults' }, null, true ],
38
+ expected: { ...deriveOptions.STANDARD_OPTIONS, noDefaults: 'customFromInput', noDynamic: 'customFromDefaults' }
39
+ } );
40
+ tm.addCondition( {
41
+ name: 'dynamicDefaults',
42
+ success: true,
43
+ args: [ { noDefaults: 'customFromInput' }, { noDynamic: 'customFromDefaults' }, ( options ) => {
44
+ return { ...options, noop: 'customFromDynamic' };
45
+ }, true ],
46
+ expected: { ...deriveOptions.STANDARD_OPTIONS, noDefaults: 'customFromInput', noDynamic: 'customFromDefaults', noop: 'customFromDynamic' }
47
+ } );
48
+ await tm.run();
49
+ tm.conditionsMap.clear();
50
+ tm.functionsMap.clear();
51
+ tm.addFunction( { name: 'isOptions', func: deriveOptions.isOptions } );
52
+ tm.addCondition( {
53
+ name: 'success',
54
+ success: true,
55
+ args: [ deriveOptions.STANDARD_OPTIONS ],
56
+ expected: true
57
+ } );
58
+ tm.addCondition( {
59
+ name: 'failure',
60
+ success: true,
61
+ args: [ false ],
62
+ expected: false
63
+ } );
64
+ await tm.run();
65
+ } );