Skip to content

Commit 0f2310c

Browse files
committed
optionDefinition.output is now async
1 parent 82ad647 commit 0f2310c

6 files changed

Lines changed: 76 additions & 40 deletions

File tree

LICENCE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT Licence (MIT)
22

3-
Copyright (c) 2014-25 Lloyd Brookes <opensource@75lb.com>
3+
Copyright (c) 2014-26 Lloyd Brookes <opensource@75lb.com>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fromTo, single, positional } from './lib/from-to.js'
22

3-
function defaultOutput (val) { return val }
3+
async function defaultOutput (val) { return val }
44

55
const toPresets = {
66
singleOptionValue (arg, index, argv, valueIndex) {
@@ -22,10 +22,7 @@ class CommandLineArgs {
2222
this.origArgv = this.argv.slice()
2323
}
2424

25-
/**
26-
* @param {OptionDefinition[]}
27-
*/
28-
parse (optionDefinitions) {
25+
async parse (optionDefinitions) {
2926
const result = {}
3027

3128
const notPositionals = optionDefinitions.filter(d => d.extractor !== 'positional')
@@ -41,7 +38,7 @@ class CommandLineArgs {
4138
throw new Error('Extractor not found: ' + def.extractor)
4239
}
4340
if (extraction.length) {
44-
result[def.name] = def.output(extraction)
41+
result[def.name] = await def.output(extraction)
4542
}
4643
}
4744

@@ -54,7 +51,7 @@ class CommandLineArgs {
5451
def.output ||= defaultOutput
5552
const extraction = positional(this.argv, def.position - 1, { remove: true })
5653
if (extraction.length) {
57-
result[def.name] = def.output(extraction)
54+
result[def.name] = await def.output(extraction)
5855
}
5956
}
6057
}

lib/from-to.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
import arrayBack from 'array-back'
22

3-
/**
4-
* Similar to find-replace with two exceptions:
5-
* - fromTo finds multiple items, find-replace finds single items
6-
* - fromTo offers the option to remove, find-replace offers option to remove and/or replace.
7-
*
8-
* Scenarios you can perform
9-
* - Find one or more items and return (all return values are arrays)
10-
* - Find one or more items, return them, remove them from the input array
11-
*
12-
* arr {string[]} - Input array. Only mutated if `options.remove` is set.
13-
* [options.remove] {boolean} - Remove from source array
14-
* [options.from] {string[]|function[]} - String literal or a [findIndex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) callback function.
15-
* [options.to] {string[]|function[]} - A "Stop Here" function. Set one or more strings as the terminating arg. Or, from the function `fn(arg, index, argv, valueIndex)`, return true for the first arg that is out of range. Set `inclusive` to also include it. To will always search to the end of the input array.
16-
* @returns string[]
17-
*/
3+
/*☭
4+
## from-to
5+
6+
Similar to find-replace with two exceptions:
7+
8+
- fromTo finds multiple items, find-replace finds single items
9+
- fromTo offers the option to remove, find-replace offers option to remove and/or replace.
10+
11+
Scenarios you can perform
12+
13+
- Find one or more items and return (all return values are arrays)
14+
- Find one or more items, return them, remove them from the input array
15+
16+
- **Type:** `function`
17+
- **Returns:** `string[]`
18+
19+
arr {string[]} - Input array. Only mutated if `options.remove` is set.
20+
[options.remove] {boolean} - Remove from source array
21+
[options.from] {string[]|function[]} - String literal or a [findIndex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) callback function.
22+
[options.to] {string[]|function[]} - A "Stop Here" function. Set one or more strings as the terminating arg. Or, from the function `fn(arg, index, argv, valueIndex)`, return true for the first arg that is out of range. Set `inclusive` to also include it. To will always search to the end of the input array.
23+
24+
¬
25+
Param
26+
Type
27+
Description
28+
¬
29+
[options]
30+
`object`
31+
Description
32+
¬
33+
*/
1834

1935
function fromTo (arr, options = {}) {
2036
/* step 1: compute from and to index */
@@ -84,8 +100,8 @@ function convertToFunction (fn) {
84100
/**
85101
* Find the first value which matches the supplied `find` definition (one or more string or functions).
86102
*/
87-
function getFromIndex (arr, find) {
88-
const fromFns = arrayBack(find).map(convertToFunction)
103+
function getFromIndex (arr, itemsToFind) {
104+
const fromFns = arrayBack(itemsToFind).map(convertToFunction)
89105

90106
if (fromFns.length === 0) {
91107
throw new Error('from/single required')
@@ -109,6 +125,7 @@ function getFromIndex (arr, find) {
109125
function single (arr, item, options = {}) {
110126
const fromIndex = getFromIndex(arr, item)
111127

128+
/* TODO: could this use array.find? */
112129
const output = arr.slice(fromIndex, fromIndex + 1)
113130
if (options.remove && fromIndex > -1) {
114131
arr.splice(fromIndex, 1)

lib/option-definition.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
1-
/**
2-
* @property name
3-
* @property extractor - from-to, single or positional.
4-
* @property from - used by extractor 'from-to'
5-
* @property to - used by extractor 'from-to'
6-
* @property single - used by extractor 'single'
7-
* @property position - used by extractor 'positional', not zero-indexed, starts with 1.
8-
* @property output - a function to run on the final value
9-
*/
1+
/*☭
2+
## OptionDefinition
3+
4+
- **Type:** `class`
5+
6+
¬
7+
Property
8+
Description
9+
¬
10+
name
11+
property name on the result
12+
¬
13+
extractor
14+
from-to, single or positional.
15+
¬
16+
from
17+
used by extractor 'from-to'
18+
¬
19+
toPreset
20+
used by extractor 'from-to'
21+
¬
22+
single
23+
used by extractor 'single'
24+
¬
25+
position
26+
used by extractor 'positional', not zero-indexed, starts with 1.
27+
¬
28+
output
29+
a function to cherry-pick the output value
30+
¬
31+
*/
1032
class OptionDefinition {
1133
name
1234
from
1335
to
1436
single
15-
output
37+
output // TODO: should this be named `value` as it is the output value.
1638
}
1739

1840
export default OptionDefinition

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@
5858
]
5959
},
6060
"dependencies": {
61-
"array-back": "^6.2.2"
61+
"array-back": "^6.2.3"
6262
}
6363
}

0 commit comments

Comments
 (0)