Skip to content

Commit 0899b3e

Browse files
committed
from array to array
1 parent b30fb9c commit 0899b3e

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

dist/index.cjs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ var arrayBack = require('array-back');
2121

2222
/* TODO: rename to extractFromTo? Rename `options.remove` to `extract`. */
2323
function fromTo (arr, options = {}) {
24-
const fromIndex = getFromIndex(arr, options.from);
24+
/* step 1: compute from and to index */
25+
const fromIndex = arr.findIndex(item => {
26+
return arrayBack(options.from).some(fr => {
27+
if (typeof fr === 'function') {
28+
return fr(item)
29+
} else if (fr instanceof RegExp) {
30+
return fr.test(item)
31+
} else {
32+
return fr === item
33+
}
34+
})
35+
});
2536

2637
const toFn = arrayBack(options.to).map(convertToFunction);
2738
let toIndex = -1;
@@ -42,10 +53,12 @@ function fromTo (arr, options = {}) {
4253
}
4354
}
4455

56+
/* step 2: extract items between the from and to indices */
4557
const output = toIndex === -1
4658
? arr.slice(fromIndex) /* Return all to the end */
4759
: arr.slice(fromIndex, toIndex);
4860

61+
/* step 3: optionally remove from input array */
4962
if (options.remove) {
5063
if (toIndex === -1) {
5164
arr.splice(fromIndex);

lib/from-to.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ import arrayBack from 'array-back'
1919

2020
/* TODO: rename to extractFromTo? Rename `options.remove` to `extract`. */
2121
function fromTo (arr, options = {}) {
22-
const fromIndex = getFromIndex(arr, options.from)
22+
/* step 1: compute from and to index */
23+
const fromIndex = arr.findIndex(item => {
24+
return arrayBack(options.from).some(fr => {
25+
if (typeof fr === 'function') {
26+
return fr(item)
27+
} else if (fr instanceof RegExp) {
28+
return fr.test(item)
29+
} else {
30+
return fr === item
31+
}
32+
})
33+
})
2334

2435
const toFn = arrayBack(options.to).map(convertToFunction)
2536
let toIndex = -1
@@ -40,10 +51,12 @@ function fromTo (arr, options = {}) {
4051
}
4152
}
4253

54+
/* step 2: extract items between the from and to indices */
4355
const output = toIndex === -1
4456
? arr.slice(fromIndex) /* Return all to the end */
4557
: arr.slice(fromIndex, toIndex)
4658

59+
/* step 3: optionally remove from input array */
4760
if (options.remove) {
4861
if (toIndex === -1) {
4962
arr.splice(fromIndex)

test/from-to.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as a } from 'assert'
2-
import { fromTo, single, positional } from '../lib/from-to.js'
2+
import { fromTo, single } from '../lib/from-to.js'
33

44
const [test, only, skip] = [new Map(), new Map(), new Map()]
55

@@ -88,7 +88,7 @@ test.set('--option value value ...', async function () {
8888
a.deepEqual(arr, ['one', 'here'])
8989
})
9090

91-
skip.set('from many, to many', async function () {
91+
test.set('from many, to many', async function () {
9292
const validCommands = [
9393
'/help',
9494
'/users',
@@ -100,14 +100,21 @@ skip.set('from many, to many', async function () {
100100
'/join'
101101
]
102102

103-
const arr = ['/join', 'r', '/nick', 'lloyd']
103+
const arr = ['/join', 'roomA', '/nick', 'lloyd']
104104
const result = fromTo(arr, {
105105
from: validCommands,
106-
to: validCommands
106+
to: validCommands,
107+
remove: true
108+
})
109+
const result2 = fromTo(arr, {
110+
from: validCommands,
111+
to: validCommands,
112+
remove: true
107113
})
108114
/* Priority should be given to "first in the array", not "first in the from list". Is order in the argv more meaningful than order in the from list? */
109-
a.deepEqual(result, ['/join', 'r'])
110-
// a.deepEqual(arr, ['one', 'here'])
115+
a.deepEqual(result, ['/join', 'roomA'])
116+
a.deepEqual(result2, ['/nick', 'lloyd'])
117+
// this.data = { result, result2 }
111118
})
112119

113120
export { test, only, skip }

test/test.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
import { strict as a } from 'assert'
22
import CommandLineArgs from 'command-line-args'
3-
import { fromTo, single, positional } from '../lib/from-to.js'
43

54
const [test, only, skip] = [new Map(), new Map(), new Map()]
65

7-
function singleOptionValuePreset (arg, index, argv, valueIndex) {
8-
return valueIndex > 1 || arg.startsWith('--')
9-
}
10-
11-
function multipleOptionValuePreset (arg, index, argv, valueIndex) {
12-
return arg.startsWith('--')
13-
}
14-
156
test.set('Input argv is not mutated', async function () {
167
const argv = ['one', 'two', '--one', 'something', '--two']
178
const optionDefinitions = [
@@ -180,7 +171,7 @@ test.set('Magic arg values: file1 file2 extra-large verbose output final', async
180171
})
181172

182173
skip.set('Preprocessing', async function () {
183-
const argv = ['b25lIHR3byAtLW9uZSBzb21ldGhpbmcgLS10d28K'] // or a JWT
174+
// const argv = ['b25lIHR3byAtLW9uZSBzb21ldGhpbmcgLS10d28K'] // or a JWT
184175
// echo 'one two --one something --two' | base64
185176
})
186177

@@ -245,13 +236,13 @@ test.set('self-defining options', async function () {
245236
from: /^--server\.string\./,
246237
to: 'singleOptionValue',
247238
output: e => e[1]
248-
},
239+
}
249240
]
250241

251242
const cla = new CommandLineArgs(argv)
252243
const result = cla.parse(optionDefinitions)
253244
a.deepEqual(result, {
254-
one: [ 'one', 'uno', 'ein' ],
245+
one: ['one', 'uno', 'ein'],
255246
two: undefined,
256247
broke: 1,
257248
three: true,

0 commit comments

Comments
 (0)