Skip to content

Commit b254deb

Browse files
committed
more tests
1 parent 822bc54 commit b254deb

File tree

5 files changed

+102
-69
lines changed

5 files changed

+102
-69
lines changed

dist/index.cjs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,45 @@ function fromTo (arr, options = {}) {
3434
})
3535
});
3636

37-
const toFn = arrayBack(options.to).map(convertToFunction);
38-
let toIndex = -1;
39-
if (toFn.length) {
40-
for (const fn of toFn) {
41-
toIndex = arr.findIndex((item, index, arr) => {
42-
if (index > fromIndex) {
43-
const valueIndex = index - fromIndex;
44-
return fn(item, index, arr, valueIndex)
45-
} else {
46-
return false
37+
/* From item not found */
38+
if (fromIndex === -1) {
39+
return []
40+
} else {
41+
const toFn = arrayBack(options.to).map(convertToFunction);
42+
let toIndex = -1;
43+
if (toFn.length) {
44+
for (const fn of toFn) {
45+
toIndex = arr.findIndex((item, index, arr) => {
46+
if (index > fromIndex) {
47+
const valueIndex = index - fromIndex;
48+
return fn(item, index, arr, valueIndex)
49+
} else {
50+
return false
51+
}
52+
});
53+
/* Keep looping until a match is found. */
54+
if (toIndex > -1) {
55+
break
4756
}
48-
});
49-
/* Keep looping until a match is found. */
50-
if (toIndex > -1) {
51-
break
5257
}
5358
}
54-
}
5559

56-
/* step 2: extract items between the from and to indices */
57-
const output = toIndex === -1
58-
? arr.slice(fromIndex) /* Return all to the end */
59-
: arr.slice(fromIndex, toIndex);
60+
/* step 2: extract items between the from and to indices */
61+
const output = toIndex === -1
62+
? arr.slice(fromIndex) /* Return all to the end */
63+
: arr.slice(fromIndex, toIndex);
6064

61-
/* step 3: optionally remove from input array */
62-
if (options.remove) {
63-
if (toIndex === -1) {
64-
arr.splice(fromIndex);
65-
} else {
66-
arr.splice(fromIndex, toIndex - fromIndex);
65+
/* step 3: optionally remove from input array */
66+
if (options.remove) {
67+
if (toIndex === -1) {
68+
arr.splice(fromIndex);
69+
} else {
70+
arr.splice(fromIndex, toIndex - fromIndex);
71+
}
6772
}
68-
}
6973

70-
return output
74+
return output
75+
}
7176
}
7277

7378
function convertToFunction (fn) {

lib/from-to.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,45 @@ function fromTo (arr, options = {}) {
3232
})
3333
})
3434

35-
const toFn = arrayBack(options.to).map(convertToFunction)
36-
let toIndex = -1
37-
if (toFn.length) {
38-
for (const fn of toFn) {
39-
toIndex = arr.findIndex((item, index, arr) => {
40-
if (index > fromIndex) {
41-
const valueIndex = index - fromIndex
42-
return fn(item, index, arr, valueIndex)
43-
} else {
44-
return false
35+
/* From item not found */
36+
if (fromIndex === -1) {
37+
return []
38+
} else {
39+
const toFn = arrayBack(options.to).map(convertToFunction)
40+
let toIndex = -1
41+
if (toFn.length) {
42+
for (const fn of toFn) {
43+
toIndex = arr.findIndex((item, index, arr) => {
44+
if (index > fromIndex) {
45+
const valueIndex = index - fromIndex
46+
return fn(item, index, arr, valueIndex)
47+
} else {
48+
return false
49+
}
50+
})
51+
/* Keep looping until a match is found. */
52+
if (toIndex > -1) {
53+
break
4554
}
46-
})
47-
/* Keep looping until a match is found. */
48-
if (toIndex > -1) {
49-
break
5055
}
5156
}
52-
}
5357

54-
/* step 2: extract items between the from and to indices */
55-
const output = toIndex === -1
56-
? arr.slice(fromIndex) /* Return all to the end */
57-
: arr.slice(fromIndex, toIndex)
58+
/* step 2: extract items between the from and to indices */
59+
const output = toIndex === -1
60+
? arr.slice(fromIndex) /* Return all to the end */
61+
: arr.slice(fromIndex, toIndex)
5862

59-
/* step 3: optionally remove from input array */
60-
if (options.remove) {
61-
if (toIndex === -1) {
62-
arr.splice(fromIndex)
63-
} else {
64-
arr.splice(fromIndex, toIndex - fromIndex)
63+
/* step 3: optionally remove from input array */
64+
if (options.remove) {
65+
if (toIndex === -1) {
66+
arr.splice(fromIndex)
67+
} else {
68+
arr.splice(fromIndex, toIndex - fromIndex)
69+
}
6570
}
66-
}
6771

68-
return output
72+
return output
73+
}
6974
}
7075

7176
function convertToFunction (fn) {

package-lock.json

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

test/from-to.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ test.set('--option value', async function () {
6666
a.deepEqual(arr, ['one', 'here', 'more'])
6767
})
6868

69+
test.set('--option value, from not found with remove set', async function () {
70+
const arr = ['one', 'here', '--option', 'there', 'more']
71+
const result = fromTo(arr, {
72+
from: '--not-found',
73+
to: (val, i, a, valueIndex) => valueIndex > 1 || val.startsWith('--'),
74+
remove: true
75+
})
76+
a.deepEqual(result, [])
77+
a.deepEqual(arr, ['one', 'here', '--option', 'there', 'more'])
78+
})
79+
6980
test.set('--option value, no remove', async function () {
7081
const arr = ['one', 'here', '--option', 'there', 'more']
7182
const result = fromTo(arr, {

test/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@ test.set('Positional with one single', async function () {
137137
a.deepEqual(result, { symbol: 'AAPL', contract: true })
138138
})
139139

140+
test.set('Positional, one single, one from-to', async function () {
141+
const argv = ['AAPL', '--contract']
142+
const cla = new CommandLineArgs(argv)
143+
const result = cla.parse([
144+
{
145+
name: 'contract',
146+
extractor: 'single',
147+
single: '--contract',
148+
output: extraction => true
149+
},
150+
{
151+
name: 'symbol',
152+
extractor: 'positional',
153+
position: 1,
154+
output: extraction => extraction[0]
155+
},
156+
{
157+
name: 'exchange',
158+
extractor: 'fromTo',
159+
from: '--exchange',
160+
to: 'singleOptionValue',
161+
output: extraction => extraction[1]
162+
},
163+
])
164+
a.deepEqual(result, { symbol: 'AAPL', contract: true })
165+
})
166+
140167
test.set('Missing positional with one single', async function () {
141168
const argv = ['--contract']
142169
const cla = new CommandLineArgs(argv)

0 commit comments

Comments
 (0)