11
11
<dd ></dd >
12
12
<dt ><a href =" #module_optionist/assign/underscore " >optionist/assign/underscore</a ></dt >
13
13
<dd ></dd >
14
- <dt ><a href =" #module_optionist/process/get " >optionist/process/get</a ></dt >
15
- <dd ></dd >
16
14
<dt ><a href =" #module_optionist/process " >optionist/process</a ></dt >
17
15
<dd ></dd >
16
+ <dt ><a href =" #module_optionist/process/get " >optionist/process/get</a ></dt >
17
+ <dd ></dd >
18
18
</dl >
19
19
20
20
<a name =" module_optionist " ></a >
24
24
25
25
### module.exports([ options] , [ defaultOptions] ) ⇒ <code >Object</code > ⏏
26
26
Copies and merges options and default options into a new object.
27
- If no options and defaultOptions were passed, or they are both null, or falsy,
28
- an empty Object will be returned (e.g.: {} ).
27
+ If no ` options ` and ` defaultOptions ` were passed, or they are both ` null ` , or falsy,
28
+ an empty Object will be returned (e.g.: ` {} ` ).
29
29
30
30
** Kind** : Exported function
31
31
** Returns** : <code >Object</code > - The new object with the merged options.
@@ -162,19 +162,20 @@ function myFunction (options = null) {
162
162
options = process (options, DEFAULTS )
163
163
164
164
// handle complex, deeply nested Objects without 'Uncaught ReferenceError'
165
- // and constant "key within object" checking, e.g.:
166
- // note: key existence checking isn't needed (e.g.: if (options.settings && 'data' in options.settings && ...))
165
+ // and constant "key within object" checking
166
+ // note: key existence checking isn't needed (e.g.:
167
+ // if (options.settings && 'data' in options.settings && ...) { ... })
167
168
this ._value = get (options .settings .deeply .nested .data .value )
168
169
// note: per-option default option is also possible, when the specific option doesn't exist
169
- this ._flag = get (options .settings .burried .deeply .flag , true )
170
+ this ._flag = get (options .settings .burried .deeply .yet . doesnt . exist . flag , true )
170
171
171
172
// also possible in conditions...
172
173
if (get (options .deeply .nested .settings .value ) > 0 ) {
173
174
// handle value
174
175
}
175
176
176
177
// ...with per-option default option, when the specific option doesn't exist
177
- if (get (options .some .other .deeply .nested .flag , false )) {
178
+ if (get (options .some .other .deeply .nested .non . existent . flag , false )) {
178
179
// handle flag
179
180
}
180
181
}
@@ -281,20 +282,19 @@ Processes the given options and assigns them by setting the underscored properti
281
282
</tr> </tbody>
282
283
</table >
283
284
284
- <a name =" module_optionist/process/get " ></a >
285
+ <a name =" module_optionist/process " ></a >
285
286
286
- ## optionist/process/get
287
- <a name =" exp_module_optionist/process/get --module.exports " ></a >
287
+ ## optionist/process
288
+ <a name =" exp_module_optionist/process--module.exports " ></a >
288
289
289
- ### module.exports([ option] , [ defaultOption] ) ⇒ <code >\* </code > ⏏
290
- Returns the specific option of the processed options from a recursive Proxy.
290
+ ### module.exports([ options] , [ defaultOptions] ) ⇒ <code >Object</code > ⏏
291
+ Processes the options and default options and merges them into a new recursive Proxy.
292
+ This recursive Proxy provides a convenient and short way to handle complex, deeply nested options
293
+ without getting ` Uncaught ReferenceError ` and constantly checking whether keys exist in objects
294
+ (e.g.: ` if (options.settings && 'data' in options.settings && ...) { ... } ` ).
291
295
292
296
** Kind** : Exported function
293
- ** Returns** : <code >\* </code > - The specific option.
294
- ** Throws** :
295
-
296
- - <code >TypeError</code > In case the option was not previously processed with ".process()".
297
-
297
+ ** Returns** : <code >Object</code > - The new recursive Proxy with the processed options.
298
298
<table >
299
299
<thead >
300
300
<tr>
@@ -303,24 +303,81 @@ Returns the specific option of the processed options from a recursive Proxy.
303
303
</thead >
304
304
<tbody >
305
305
<tr >
306
- <td>[option ]</td><td><code>Object</code></td><td><p>The specific option to return .</p>
306
+ <td>[options ]</td><td><code>Object</code></td><td><p>The options to use to merge into a new recursive Proxy .</p>
307
307
</td >
308
308
</tr><tr>
309
- <td>[defaultOption ]</td><td><code>Object</code></td><td><p>The default option to return in case the specific option is not present .</p>
309
+ <td>[defaultOptions ]</td><td><code>Object</code></td><td><p>The default options to use to merge into a new recursive Proxy .</p>
310
310
</td >
311
311
</tr> </tbody>
312
312
</table >
313
313
314
- <a name =" module_optionist/process " ></a >
314
+ ** Example** * (complex/convenient options object handling with default options)*
315
+ ``` js
316
+ // note: when using 'optionist/process', you also need 'optionist/process/get' too
317
+ const process = require (' optionist/process' )
318
+ const get = require (' optionist/process/get' )
315
319
316
- ## optionist/process
317
- < a name = " exp_module_optionist/ process--module.exports " ></ a >
320
+ function myFunction ( options = null ) {
321
+ options = process (options, DEFAULTS ) // best practice: use a constant, when storing your defaults
318
322
319
- ### module.exports([ options] , [ defaultOptions] ) ⇒ <code >Object</code > ⏏
320
- Processes the options and default options into a new recursive Proxy.
323
+ // handle complex, deeply nested Objects without 'Uncaught ReferenceError'
324
+ // and constant "key within object" checking
325
+ // note: key existence checking isn't needed (e.g.:
326
+ // if (options.settings && 'data' in options.settings && ...) { ... })
327
+ this ._value = get (options .settings .deeply .nested .data .value )
328
+ // note: per-option default option is also possible, when the specific option doesn't exist
329
+ this ._flag = get (options .settings .burried .deeply .yet .doesnt .exist .flag , true )
330
+
331
+ // also possible in conditions...
332
+ if (get (options .deeply .nested .settings .value ) > 0 ) {
333
+ // handle value
334
+ }
335
+
336
+ // ...with per-option default option, when the specific option doesn't exist
337
+ if (get (options .some .other .deeply .nested .non .existent .flag , false )) {
338
+ // handle flag
339
+ }
340
+ }
341
+
342
+ // or with a class
343
+ class MyClass {
344
+ constructor (options = null ) {
345
+ options = process (options, DEFAULTS ) // best practice: use a constant, when storing your defaults
346
+
347
+ // handle complex, deeply nested Objects without 'Uncaught ReferenceError'
348
+ // and constant "key within object" checking
349
+ // note: key existence checking isn't needed (e.g.:
350
+ // if (options.settings && 'data' in options.settings && ...) { ... })
351
+ this ._value = get (options .settings .deeply .nested .data .value )
352
+ // note: per-option default option is also possible, when the specific option doesn't exist
353
+ this ._flag = get (options .settings .burried .deeply .yet .doesnt .exist .flag , true )
354
+
355
+ // also possible in conditions...
356
+ if (get (options .deeply .nested .settings .value ) > 0 ) {
357
+ // handle value
358
+ }
359
+
360
+ // ...with per-option default option, when the specific option doesn't exist
361
+ if (! get (options .some .other .deeply .nested .non .existent .flag , false )) {
362
+ // handle flag
363
+ }
364
+ }
365
+ }
366
+ ```
367
+ <a name =" module_optionist/process/get " ></a >
368
+
369
+ ## optionist/process/get
370
+ <a name =" exp_module_optionist/process/get--module.exports " ></a >
371
+
372
+ ### module.exports([ option] , [ defaultOption] ) ⇒ <code >\* </code > ⏏
373
+ Returns the specific option of the processed options from a recursive Proxy.
321
374
322
375
** Kind** : Exported function
323
- ** Returns** : <code >Object</code > - The new recursive Proxy with the processed options.
376
+ ** Returns** : <code >\* </code > - The specific option.
377
+ ** Throws** :
378
+
379
+ - <code >TypeError</code > In case the option was not previously processed with ".process()".
380
+
324
381
<table >
325
382
<thead >
326
383
<tr>
@@ -329,11 +386,40 @@ Processes the options and default options into a new recursive Proxy.
329
386
</thead >
330
387
<tbody >
331
388
<tr >
332
- <td>[options ]</td><td><code>Object</code></td><td><p>The options to use to merge into a new recursive Proxy .</p>
389
+ <td>[option ]</td><td><code>Object</code></td><td><p>The specific option to return .</p>
333
390
</td >
334
391
</tr><tr>
335
- <td>[defaultOptions ]</td><td><code>Object</code></td><td><p>The default options to use to merge into a new recursive Proxy .</p>
392
+ <td>[defaultOption ]</td><td><code>Object</code></td><td><p>The default option to return in case the specific option is not present .</p>
336
393
</td >
337
394
</tr> </tbody>
338
395
</table >
339
396
397
+ ** Example** * (get the processed options - for more examples, check: ** optionist/process** )*
398
+ ``` js
399
+ const process = require (' optionist/process' )
400
+ const get = require (' optionist/process/get' )
401
+
402
+ class MyClass {
403
+ constructor (options = null ) {
404
+ options = process (options, DEFAULTS )
405
+
406
+ // handle complex, deeply nested Objects without 'Uncaught ReferenceError'
407
+ // and constant "key within object" checking
408
+ // note: key existence checking isn't needed (e.g.:
409
+ // if (options.settings && 'data' in options.settings && ...) { ... })
410
+ this ._value = get (options .settings .deeply .nested .data .value )
411
+ // note: per-option default option is also possible, when the specific option doesn't exist
412
+ this ._flag = get (options .settings .burried .deeply .yet .doesnt .exist .flag , true )
413
+
414
+ // also possible in conditions...
415
+ if (get (options .deeply .nested .settings .value ) > 0 ) {
416
+ // handle value
417
+ }
418
+
419
+ // ...with per-option default option, when the specific option doesn't exist
420
+ if (! get (options .some .other .deeply .nested .non .existent .flag , false )) {
421
+ // handle flag
422
+ }
423
+ }
424
+ }
425
+ ```
0 commit comments