Skip to content

Commit d76f788

Browse files
committed
select adjacent should do a minus on adjacent end #17
1 parent 1eef476 commit d76f788

File tree

3 files changed

+193
-4
lines changed

3 files changed

+193
-4
lines changed

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,18 @@ export function multiselect(context: Context, command: Command): Context {
9393
const pivotIndex = context.list.indexOf(context.adjacentPivot);
9494
const selectionIndex = context.list.indexOf(command.id);
9595

96-
const adjacent = findAdjacentToPivotInSortedArray(
96+
const adjacentToStart = findAdjacentToPivotInSortedArray(
9797
context.list,
9898
context.selected,
9999
context.adjacentPivot
100100
);
101101

102+
const adjacentToEnd = findAdjacentToPivotInSortedArray(
103+
context.list,
104+
context.selected,
105+
command.id,
106+
);
107+
102108
const nextSelection = context.list.slice(
103109
Math.min(pivotIndex, selectionIndex),
104110
Math.max(pivotIndex, selectionIndex) + 1
@@ -108,7 +114,7 @@ export function multiselect(context: Context, command: Command): Context {
108114
nextSelection.reverse()
109115
}
110116

111-
const toRemove = difference(adjacent, nextSelection);
117+
const toRemove = union(adjacentToStart, adjacentToEnd);
112118

113119
return {
114120
...context,

src/spec/arbitraries.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fc from 'fast-check';
2+
import { sort } from 'ramda';
23

34
export function subsequentSubarray(arr: string[]) {
45
return fc.tuple(fc.nat(arr.length), fc.nat(arr.length))
@@ -15,3 +16,70 @@ export function nonEmptySubsequentSubarray(nonEmptyArray: string[]) {
1516
export function list() {
1617
return fc.set(fc.string())
1718
}
19+
20+
export function ascending (a: number, b:number): number {
21+
return a - b;
22+
}
23+
24+
export function descending (a:number, b:number): number {
25+
return b - a;
26+
}
27+
28+
export function takeFromTo<T>(array: T[], start: T, end: T) {
29+
const startIndex = array.indexOf(start);
30+
const endIndex = array.indexOf(end) + 1;
31+
32+
return array.slice(startIndex, endIndex);
33+
}
34+
35+
export function indexWithAdjacentConnections() {
36+
return fc
37+
.tuple(
38+
fc.set(
39+
fc.string(),
40+
{ minLength: 1 }
41+
),
42+
fc.nat(),
43+
fc.nat(),
44+
fc.nat(),
45+
fc.nat(),
46+
fc.nat(),
47+
fc.nat(),
48+
)
49+
.map(([list, startStart, startEnd, endStart, endEnd, start, end]) =>
50+
[
51+
list,
52+
...sort(ascending, [
53+
startStart % list.length,
54+
startEnd % list.length,
55+
endStart % list.length,
56+
endEnd % list.length,
57+
]),
58+
start,
59+
end,
60+
] as const
61+
)
62+
.map(([list, startStart, startEnd, endStart, endEnd, start, end]) =>
63+
[
64+
list,
65+
startStart,
66+
startEnd,
67+
endStart,
68+
endEnd,
69+
(start % (startEnd - startStart + 1)) + startStart,
70+
(end % (endEnd - endStart + 1)) + endStart,
71+
] as const
72+
)
73+
.map(([list, startStart, startEnd, endStart, endEnd, start, end]) => ({
74+
list,
75+
adjacentGroup1: list.slice(startStart, start),
76+
adjacentGroup2: list.slice(start + 1, startEnd),
77+
// if startEnd is the same as endStart
78+
// end can be a member of group3
79+
adjacentGroup3: list.slice(endStart + ((startEnd === endStart) ? 1 : 0), end),
80+
adjacentGroup4: list.slice(end + 1, endEnd),
81+
end: list[end],
82+
start: list[start],
83+
})
84+
)
85+
}

src/spec/selectAdjacent.spec.ts

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fc, { array } from 'fast-check';
2-
import { take, last, head, startsWith } from "ramda";
3-
import { multiselect } from '../index';
2+
import { take, last, head, reverse } from "ramda";
3+
import { Context, multiselect } from '../index';
4+
import { indexWithAdjacentConnections, takeFromTo } from './arbitraries';
45

56
describe('Select Adjacent', () => {
67
test('Should select from top to bottom in an empty list', () => {
@@ -188,4 +189,118 @@ describe('Select Adjacent', () => {
188189
),
189190
)
190191
})
192+
193+
test('Should perform a minus between the old and new end selection group, when selection is ascendent', () => {
194+
fc.assert(
195+
fc.property(
196+
indexWithAdjacentConnections()
197+
,
198+
({
199+
list,
200+
adjacentGroup1,
201+
adjacentGroup2,
202+
adjacentGroup3,
203+
adjacentGroup4,
204+
start,
205+
end,
206+
}) => {
207+
208+
const expectedSelection = takeFromTo(list, start, end);
209+
210+
let context: Context = {
211+
adjacentPivot: undefined,
212+
list,
213+
selected: []
214+
}
215+
216+
context = [
217+
...adjacentGroup1,
218+
...adjacentGroup2,
219+
...adjacentGroup3,
220+
...adjacentGroup4,
221+
start
222+
].reduce(
223+
(ctx, id) => multiselect(
224+
ctx,
225+
{
226+
type: "TOGGLE SELECTION",
227+
id
228+
}
229+
),
230+
context
231+
);
232+
233+
const result = multiselect(
234+
context,
235+
{
236+
type: 'SELECT ADJACENT',
237+
id: end
238+
}
239+
)
240+
expect(result).toEqual({
241+
list,
242+
adjacentPivot: start,
243+
selected: expectedSelection,
244+
})
245+
}
246+
),
247+
)
248+
})
249+
250+
test('Should perform a minus between the old and new end selection group, when selection is descendent', () => {
251+
fc.assert(
252+
fc.property(
253+
indexWithAdjacentConnections()
254+
,
255+
({
256+
list,
257+
adjacentGroup1,
258+
adjacentGroup2,
259+
adjacentGroup3,
260+
adjacentGroup4,
261+
start,
262+
end,
263+
}) => {
264+
265+
const expectedSelection = reverse(takeFromTo(list, start, end));
266+
267+
let context: Context = {
268+
adjacentPivot: undefined,
269+
list,
270+
selected: []
271+
}
272+
273+
context = [
274+
...adjacentGroup1,
275+
...adjacentGroup2,
276+
...adjacentGroup3,
277+
...adjacentGroup4,
278+
end,
279+
].reduce(
280+
(ctx, id) => multiselect(
281+
ctx,
282+
{
283+
type: "TOGGLE SELECTION",
284+
id
285+
}
286+
),
287+
context
288+
);
289+
290+
const result = multiselect(
291+
context,
292+
{
293+
type: 'SELECT ADJACENT',
294+
id: start
295+
}
296+
)
297+
expect(result).toEqual({
298+
list,
299+
adjacentPivot: end,
300+
selected: expectedSelection,
301+
})
302+
}
303+
),
304+
)
305+
})
191306
})

0 commit comments

Comments
 (0)