Skip to content

Commit 705145c

Browse files
committed
select next: should be from last selected not pivot #15
1 parent c1d05d0 commit 705145c

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

src/arrayUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export function findNextPivot(
3232
export function findAdjacentToPivotInSortedArray(
3333
sortedArray: string[],
3434
subarray: string[],
35-
item: string): string[] {
35+
item: string
36+
): string[] {
3637
const indexOfItem = sortedArray.indexOf(item);
3738
let leftIndex = indexOfItem;
3839
let rightIndex = indexOfItem;

src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ export function multiselect(context: Context, command: Command): Context {
123123
} else if (
124124
command.type === "SELECT NEXT" &&
125125
context.list.length &&
126-
context.adjacentPivot !== undefined
126+
context.selected.length
127127
) {
128-
const pivotIndex = context.list.indexOf(context.adjacentPivot)
128+
const pivotIndex = context.list.indexOf(last(context.selected)!)
129129

130130
if (pivotIndex < context.list.length - 1) {
131131
const nextItem = context.list[pivotIndex + 1];
132+
132133
return {
133134
...context,
134135
selected: [nextItem],
@@ -137,12 +138,14 @@ export function multiselect(context: Context, command: Command): Context {
137138
} else if (
138139
!(
139140
context.selected.length === 1 &&
140-
last(context.selected) === last(context.list)
141+
context.selected[0] === last(context.list)
141142
)
142143
) {
144+
const pivot = context.list[pivotIndex];
143145
return {
144146
...context,
145-
selected: [context.list[pivotIndex]],
147+
selected: [pivot],
148+
adjacentPivot: pivot,
146149
}
147150
} else {
148151
return context;

src/spec/selectNext.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,66 @@ describe('Select Next Item', () => {
103103
)
104104
});
105105

106+
test('Should select next from the last selected (not the pivot) given the last command were select adjacent', () => {
107+
fc.assert(
108+
fc.property(
109+
fc.tuple(
110+
fc.boolean(),
111+
fc.set(
112+
fc.string(), { minLength: 1 }
113+
)
114+
)
115+
.chain(([undefOrTop, list]) =>
116+
fc.nat(list.length - 1)
117+
.map(id => ({
118+
list,
119+
adjacentPivot: undefOrTop ? undefined : head(list),
120+
id: list[id],
121+
}))
122+
)
123+
,
124+
({
125+
adjacentPivot,
126+
list,
127+
id,
128+
}) => {
129+
130+
const prevContext = multiselect(
131+
{
132+
list,
133+
adjacentPivot: adjacentPivot,
134+
selected: []
135+
},
136+
{
137+
type: "SELECT ADJACENT",
138+
id,
139+
}
140+
)
141+
142+
const currentSelectionPivot = list.indexOf(id)
143+
const nextSelection = currentSelectionPivot < list.length - 1
144+
? currentSelectionPivot + 1
145+
: list.length - 1;
146+
147+
const nextPivot = list[nextSelection];
148+
149+
expect(
150+
multiselect(
151+
prevContext,
152+
{
153+
type: "SELECT NEXT",
154+
}
155+
)
156+
)
157+
.toEqual({
158+
list,
159+
selected: [nextPivot],
160+
adjacentPivot: nextPivot,
161+
})
162+
163+
}
164+
)
165+
)
166+
})
167+
106168
})

0 commit comments

Comments
 (0)