Skip to content

Commit f10f0a5

Browse files
committed
noFurtherThan
1 parent dbd28e8 commit f10f0a5

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
lines changed

dist/index.cjs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,32 @@
1616
* @returns string[]
1717
*/
1818
function fromTo (arr, options = {}) {
19-
const { from: fromFn, to: toFn, remove } = options;
19+
const { from: fromFn, to: toFn, noFurtherThan, remove } = options;
2020
const fromIndex = arr.findIndex(fromFn);
2121
let toIndex;
2222
if (toFn) {
2323
toIndex = arr.findIndex((item, index, arr) => {
24-
if ((index > fromIndex) && toFn) {
24+
if (index > fromIndex) {
2525
const valueIndex = index - fromIndex;
2626
return toFn(valueIndex, item, index, arr)
2727
} else {
2828
return false
2929
}
3030
});
31+
} else if (noFurtherThan) {
32+
toIndex = arr.findIndex((item, index, arr) => {
33+
if (index > fromIndex) {
34+
const valueIndex = index - fromIndex;
35+
return noFurtherThan(valueIndex, item, index, arr)
36+
} else {
37+
return false
38+
}
39+
});
40+
if (toIndex > 0) {
41+
toIndex -= 1;
42+
} else if (toIndex === -1) {
43+
toIndex = arr.length - 1;
44+
}
3145
} else {
3246
toIndex = fromIndex;
3347
}
@@ -40,7 +54,10 @@ function fromTo (arr, options = {}) {
4054
}
4155
}
4256

43-
/* TODO: add `noFurtherThan` function as an alternative, or replacement, for `to`.. Might result in easier code, e.g. "no further than a --option", rather than "stop here if the next item is an option or the end" */
57+
/*
58+
TODO: add `noFurtherThan` function as an additional alternative, or replacement, for `to`.. Might result in easier code, e.g. "no further than a --option", rather than "stop here if the next item is an option or the end". This is also how slice() works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
59+
60+
*/
4461

4562
class CommandLineArgs {
4663
constructor (args, optionDefinitions) {
@@ -79,6 +96,7 @@ class CommandLineArgs {
7996
result.push(fromTo(this.args, {
8097
from: dynamicDef.from,
8198
to: dynamicDef.to,
99+
noFurtherThan: dynamicDef.noFurtherThan,
82100
remove: true
83101
}));
84102
}

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import fromTo from './lib/from-to.js'
22

3-
/* TODO: add `noFurtherThan` function as an alternative, or replacement, for `to`.. Might result in easier code, e.g. "no further than a --option", rather than "stop here if the next item is an option or the end" */
4-
53
class CommandLineArgs {
64
constructor (args, optionDefinitions) {
75
this.origArgv = args.slice()
@@ -39,6 +37,7 @@ class CommandLineArgs {
3937
result.push(fromTo(this.args, {
4038
from: dynamicDef.from,
4139
to: dynamicDef.to,
40+
noFurtherThan: dynamicDef.noFurtherThan,
4241
remove: true
4342
}))
4443
}

lib/from-to.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,32 @@
1414
* @returns string[]
1515
*/
1616
function fromTo (arr, options = {}) {
17-
const { from: fromFn, to: toFn, remove } = options
17+
const { from: fromFn, to: toFn, noFurtherThan, remove } = options
1818
const fromIndex = arr.findIndex(fromFn)
1919
let toIndex
2020
if (toFn) {
2121
toIndex = arr.findIndex((item, index, arr) => {
22-
if ((index > fromIndex) && toFn) {
22+
if (index > fromIndex) {
2323
const valueIndex = index - fromIndex
2424
return toFn(valueIndex, item, index, arr)
2525
} else {
2626
return false
2727
}
2828
})
29+
} else if (noFurtherThan) {
30+
toIndex = arr.findIndex((item, index, arr) => {
31+
if (index > fromIndex) {
32+
const valueIndex = index - fromIndex
33+
return noFurtherThan(valueIndex, item, index, arr)
34+
} else {
35+
return false
36+
}
37+
})
38+
if (toIndex > 0) {
39+
toIndex -= 1
40+
} else if (toIndex === -1) {
41+
toIndex = arr.length - 1
42+
}
2943
} else {
3044
toIndex = fromIndex
3145
}

lib/option-definition.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class OptionDefinition {
2+
name
3+
from
4+
to
5+
6+
/**
7+
* An additional alternative, or replacement, for `to`. Might result in easier code, e.g. "no further than a --option", rather than "stop here if the next item is an option or the end". This is also how slice() works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
8+
*/
9+
noFurtherThan
10+
type
11+
def
12+
}
13+
14+
export default OptionDefinition

test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,54 @@ test.set('dynamic definition function receives fromArg', async function () {
146146
a.deepEqual(cla.args, ['one', 'two', '--two'])
147147
})
148148

149+
test.set('noFurtherThan', async function () {
150+
const argv = ['command1', 'arg', '--option', 'value', '--flag', 'command2', 'arg2']
151+
const commands = ['command1', 'command2']
152+
const optionDefinitions = [
153+
{
154+
from: arg => commands.includes(arg),
155+
noFurtherThan: (valueIndex, arg, index, argv) => commands.includes(arg)
156+
}
157+
]
158+
const cla = new CommandLineArgs(argv, optionDefinitions)
159+
const result = cla.parse()
160+
// this.data = result
161+
a.deepEqual(result, {
162+
command1: ['arg', '--option', 'value', '--flag'],
163+
command2: ['arg2']
164+
})
165+
})
166+
167+
test.set('to not found: no args matched', async function () {
168+
const argv = ['command1', 'arg', '--option', 'value', '--flag']
169+
const optionDefinitions = [
170+
{
171+
from: arg => arg === 'command1',
172+
to: () => false
173+
}
174+
]
175+
const cla = new CommandLineArgs(argv, optionDefinitions)
176+
const result = cla.parse()
177+
// this.data = result
178+
a.deepEqual(result, {
179+
command1: []
180+
})
181+
})
182+
183+
test.set('noFurtherThan not found: all args matched until the end', async function () {
184+
const argv = ['command1', 'arg', '--option', 'value', '--flag']
185+
const optionDefinitions = [
186+
{
187+
from: arg => arg === 'command1',
188+
noFurtherThan: () => false
189+
}
190+
]
191+
const cla = new CommandLineArgs(argv, optionDefinitions)
192+
const result = cla.parse()
193+
// this.data = result
194+
a.deepEqual(result, {
195+
command1: [ 'arg', '--option', 'value', '--flag' ]
196+
})
197+
})
198+
149199
export { test, only, skip }

0 commit comments

Comments
 (0)