Skip to content

Commit fa0757f

Browse files
committed
docs(src): add examples
Also add several clarifications and regenerate API docs.
1 parent d315496 commit fa0757f

File tree

4 files changed

+205
-35
lines changed

4 files changed

+205
-35
lines changed

docs/API.md

+114-28
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
<dd></dd>
1212
<dt><a href="#module_optionist/assign/underscore">optionist/assign/underscore</a></dt>
1313
<dd></dd>
14-
<dt><a href="#module_optionist/process/get">optionist/process/get</a></dt>
15-
<dd></dd>
1614
<dt><a href="#module_optionist/process">optionist/process</a></dt>
1715
<dd></dd>
16+
<dt><a href="#module_optionist/process/get">optionist/process/get</a></dt>
17+
<dd></dd>
1818
</dl>
1919

2020
<a name="module_optionist"></a>
@@ -24,8 +24,8 @@
2424

2525
### module.exports([options], [defaultOptions]) ⇒ <code>Object</code> ⏏
2626
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.: `{}`).
2929

3030
**Kind**: Exported function
3131
**Returns**: <code>Object</code> - The new object with the merged options.
@@ -162,19 +162,20 @@ function myFunction (options = null) {
162162
options = process(options, DEFAULTS)
163163

164164
// 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 && ...) { ... })
167168
this._value = get(options.settings.deeply.nested.data.value)
168169
// 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)
170171

171172
// also possible in conditions...
172173
if (get(options.deeply.nested.settings.value) > 0) {
173174
// handle value
174175
}
175176

176177
// ...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)) {
178179
// handle flag
179180
}
180181
}
@@ -281,20 +282,19 @@ Processes the given options and assigns them by setting the underscored properti
281282
</tr> </tbody>
282283
</table>
283284

284-
<a name="module_optionist/process/get"></a>
285+
<a name="module_optionist/process"></a>
285286

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>
288289

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 && ...) { ... }`).
291295

292296
**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.
298298
<table>
299299
<thead>
300300
<tr>
@@ -303,24 +303,81 @@ Returns the specific option of the processed options from a recursive Proxy.
303303
</thead>
304304
<tbody>
305305
<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>
307307
</td>
308308
</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>
310310
</td>
311311
</tr> </tbody>
312312
</table>
313313

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')
315319

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
318322

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.
321374

322375
**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+
324381
<table>
325382
<thead>
326383
<tr>
@@ -329,11 +386,40 @@ Processes the options and default options into a new recursive Proxy.
329386
</thead>
330387
<tbody>
331388
<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>
333390
</td>
334391
</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>
336393
</td>
337394
</tr> </tbody>
338395
</table>
339396

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+
```

src/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const merge = require('./_utils/_merge')
88

99
/**
1010
* Copies and merges options and default options into a new object.
11-
* If no options and defaultOptions were passed, or they are both null, or falsy,
12-
* an empty Object will be returned (e.g.: {}).
11+
* If no `options` and `defaultOptions` were passed, or they are both `null`, or falsy,
12+
* an empty Object will be returned (e.g.: `{}`).
1313
*
1414
* @param {Object} [options] - The options to use to merge into a new object.
1515
* @param {Object} [defaultOptions] - The default options to use to merge into a new object.
@@ -128,19 +128,20 @@ const merge = require('./_utils/_merge')
128128
* options = process(options, DEFAULTS)
129129
*
130130
* // handle complex, deeply nested Objects without 'Uncaught ReferenceError'
131-
* // and constant "key within object" checking, e.g.:
132-
* // note: key existence checking isn't needed (e.g.: if (options.settings && 'data' in options.settings && ...))
131+
* // and constant "key within object" checking
132+
* // note: key existence checking isn't needed (e.g.:
133+
* // if (options.settings && 'data' in options.settings && ...) { ... })
133134
* this._value = get(options.settings.deeply.nested.data.value)
134135
* // note: per-option default option is also possible, when the specific option doesn't exist
135-
* this._flag = get(options.settings.burried.deeply.flag, true)
136+
* this._flag = get(options.settings.burried.deeply.yet.doesnt.exist.flag, true)
136137
*
137138
* // also possible in conditions...
138139
* if (get(options.deeply.nested.settings.value) > 0) {
139140
* // handle value
140141
* }
141142
*
142143
* // ...with per-option default option, when the specific option doesn't exist
143-
* if (get(options.some.other.deeply.nested.flag, false)) {
144+
* if (get(options.some.other.deeply.nested.non.existent.flag, false)) {
144145
* // handle flag
145146
* }
146147
* }

src/process/get.js

+28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ function resetKey (proxy) {
2626
* @returns {*} The specific option.
2727
*
2828
* @throws {TypeError} In case the option was not previously processed with ".process()".
29+
*
30+
* @example <caption>get the processed options - for more examples, check: **optionist/process**</caption>
31+
* const process = require('optionist/process')
32+
* const get = require('optionist/process/get')
33+
*
34+
* class MyClass {
35+
* constructor (options = null) {
36+
* options = process(options, DEFAULTS)
37+
*
38+
* // handle complex, deeply nested Objects without 'Uncaught ReferenceError'
39+
* // and constant "key within object" checking
40+
* // note: key existence checking isn't needed (e.g.:
41+
* // if (options.settings && 'data' in options.settings && ...) { ... })
42+
* this._value = get(options.settings.deeply.nested.data.value)
43+
* // note: per-option default option is also possible, when the specific option doesn't exist
44+
* this._flag = get(options.settings.burried.deeply.yet.doesnt.exist.flag, true)
45+
*
46+
* // also possible in conditions...
47+
* if (get(options.deeply.nested.settings.value) > 0) {
48+
* // handle value
49+
* }
50+
*
51+
* // ...with per-option default option, when the specific option doesn't exist
52+
* if (!get(options.some.other.deeply.nested.non.existent.flag, false)) {
53+
* // handle flag
54+
* }
55+
* }
56+
* }
2957
*/
3058
module.exports = (option = null, defaultOption = null) => {
3159
if (!option || typeof option !== 'object') {

src/process/index.js

+56-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,67 @@ const KEYS = require('../_utils/_KEYS')
88
const merge = require('../_utils/_merge')
99

1010
/**
11-
* Processes the options and default options into a new recursive Proxy.
11+
* Processes the options and default options and merges them into a new recursive Proxy.
12+
* This recursive Proxy provides a convenient and short way to handle complex, deeply nested options
13+
* without getting `Uncaught ReferenceError` and constantly checking whether keys exist in objects
14+
* (e.g.: `if (options.settings && 'data' in options.settings && ...) { ... }`).
1215
*
1316
* @param {Object} [options] - The options to use to merge into a new recursive Proxy.
1417
* @param {Object} [defaultOptions] - The default options to use to merge into a new recursive Proxy.
1518
*
1619
* @returns {Object} The new recursive Proxy with the processed options.
20+
*
21+
* @example <caption>complex/convenient options object handling with default options</caption>
22+
* // note: when using 'optionist/process', you also need 'optionist/process/get' too
23+
* const process = require('optionist/process')
24+
* const get = require('optionist/process/get')
25+
*
26+
* function myFunction (options = null) {
27+
* options = process(options, DEFAULTS) // best practice: use a constant, when storing your defaults
28+
*
29+
* // handle complex, deeply nested Objects without 'Uncaught ReferenceError'
30+
* // and constant "key within object" checking
31+
* // note: key existence checking isn't needed (e.g.:
32+
* // if (options.settings && 'data' in options.settings && ...) { ... })
33+
* this._value = get(options.settings.deeply.nested.data.value)
34+
* // note: per-option default option is also possible, when the specific option doesn't exist
35+
* this._flag = get(options.settings.burried.deeply.yet.doesnt.exist.flag, true)
36+
*
37+
* // also possible in conditions...
38+
* if (get(options.deeply.nested.settings.value) > 0) {
39+
* // handle value
40+
* }
41+
*
42+
* // ...with per-option default option, when the specific option doesn't exist
43+
* if (get(options.some.other.deeply.nested.non.existent.flag, false)) {
44+
* // handle flag
45+
* }
46+
* }
47+
*
48+
* // or with a class
49+
* class MyClass {
50+
* constructor (options = null) {
51+
* options = process(options, DEFAULTS) // best practice: use a constant, when storing your defaults
52+
*
53+
* // handle complex, deeply nested Objects without 'Uncaught ReferenceError'
54+
* // and constant "key within object" checking
55+
* // note: key existence checking isn't needed (e.g.:
56+
* // if (options.settings && 'data' in options.settings && ...) { ... })
57+
* this._value = get(options.settings.deeply.nested.data.value)
58+
* // note: per-option default option is also possible, when the specific option doesn't exist
59+
* this._flag = get(options.settings.burried.deeply.yet.doesnt.exist.flag, true)
60+
*
61+
* // also possible in conditions...
62+
* if (get(options.deeply.nested.settings.value) > 0) {
63+
* // handle value
64+
* }
65+
*
66+
* // ...with per-option default option, when the specific option doesn't exist
67+
* if (!get(options.some.other.deeply.nested.non.existent.flag, false)) {
68+
* // handle flag
69+
* }
70+
* }
71+
* }
1772
*/
1873
module.exports = (options = null, defaultOptions = null) => {
1974
// create a recursive Proxy with Symbol-based keys to store data

0 commit comments

Comments
 (0)