1- import { difference , head , last , take , union , without } from "ramda" ;
1+ import { head , last , take , union , without } from "ramda" ;
22import { findAdjacentToPivotInSortedArray , findNextPivot } from "./arrayUtils" ;
33
44export type Context = {
5- list : string [ ] ,
5+ index : string [ ] ,
66 selected : string [ ] ,
77 adjacentPivot : string | undefined ,
88}
@@ -15,15 +15,15 @@ export type Command =
1515 | { type : "SELECT NEXT" }
1616 | { type : "SELECT PREVIOUS" }
1717
18- function listIncludesAndIsNotEmpty ( list : string [ ] , item : string ) {
19- return list . length > 0 && list . includes ( item )
18+ function listIncludesAndIsNotEmpty ( index : string [ ] , key : string ) {
19+ return index . length > 0 && index . includes ( key )
2020}
2121
2222export function multiselect ( context : Context , command : Command ) : Context {
2323
2424 if (
2525 command . type === "SELECT ONE" &&
26- listIncludesAndIsNotEmpty ( context . list , command . id )
26+ listIncludesAndIsNotEmpty ( context . index , command . id )
2727 ) {
2828 return {
2929 ...context ,
@@ -32,23 +32,23 @@ export function multiselect(context: Context, command: Command): Context {
3232 } ;
3333 } else if (
3434 command . type === 'TOGGLE SELECTION' &&
35- listIncludesAndIsNotEmpty ( context . list , command . id ) &&
35+ listIncludesAndIsNotEmpty ( context . index , command . id ) &&
3636 context . selected . includes ( command . id ) &&
3737 context . selected . length === 1
3838 ) {
3939 return {
4040 ...context ,
4141 selected : [ ] ,
42- adjacentPivot : head ( context . list ) ,
42+ adjacentPivot : head ( context . index ) ,
4343 } ;
4444 } else if (
4545 command . type === 'TOGGLE SELECTION' &&
46- listIncludesAndIsNotEmpty ( context . list , command . id ) &&
46+ listIncludesAndIsNotEmpty ( context . index , command . id ) &&
4747 context . selected . includes ( command . id )
4848 ) {
4949 const selected = context . selected . filter ( x => x !== command . id ) ;
5050 const adjacentPivot = findNextPivot (
51- context . list ,
51+ context . index ,
5252 selected ,
5353 command . id
5454 ) ;
@@ -60,7 +60,7 @@ export function multiselect(context: Context, command: Command): Context {
6060 } ;
6161 } else if (
6262 command . type === 'TOGGLE SELECTION' &&
63- context . list . includes ( command . id )
63+ context . index . includes ( command . id )
6464 ) {
6565 return {
6666 ...context ,
@@ -71,41 +71,41 @@ export function multiselect(context: Context, command: Command): Context {
7171 return {
7272 ...context ,
7373 selected : [ ] ,
74- adjacentPivot : head ( context . list ) ! ,
74+ adjacentPivot : head ( context . index ) ! ,
7575 }
7676 } else if (
7777 command . type === "SELECT ADJACENT" &&
78- listIncludesAndIsNotEmpty ( context . list , command . id ) &&
78+ listIncludesAndIsNotEmpty ( context . index , command . id ) &&
7979 context . selected . length === 0
8080 ) {
81- const n = context . list . indexOf ( command . id ) + 1 ;
81+ const n = context . index . indexOf ( command . id ) + 1 ;
8282 return {
8383 ...context ,
84- selected : take ( n , context . list ) ,
85- adjacentPivot : head ( context . list ) ,
84+ selected : take ( n , context . index ) ,
85+ adjacentPivot : head ( context . index ) ,
8686 }
8787 } else if (
8888 command . type === "SELECT ADJACENT" &&
89- listIncludesAndIsNotEmpty ( context . list , command . id ) &&
89+ listIncludesAndIsNotEmpty ( context . index , command . id ) &&
9090 context . adjacentPivot !== undefined
9191 ) {
9292
93- const pivotIndex = context . list . indexOf ( context . adjacentPivot ) ;
94- const selectionIndex = context . list . indexOf ( command . id ) ;
93+ const pivotIndex = context . index . indexOf ( context . adjacentPivot ) ;
94+ const selectionIndex = context . index . indexOf ( command . id ) ;
9595
9696 const adjacentToStart = findAdjacentToPivotInSortedArray (
97- context . list ,
97+ context . index ,
9898 context . selected ,
9999 context . adjacentPivot
100100 ) ;
101101
102102 const adjacentToEnd = findAdjacentToPivotInSortedArray (
103- context . list ,
103+ context . index ,
104104 context . selected ,
105105 command . id ,
106106 ) ;
107107
108- const nextSelection = context . list . slice (
108+ const nextSelection = context . index . slice (
109109 Math . min ( pivotIndex , selectionIndex ) ,
110110 Math . max ( pivotIndex , selectionIndex ) + 1
111111 ) ;
@@ -122,36 +122,36 @@ export function multiselect(context: Context, command: Command): Context {
122122 }
123123 } else if (
124124 command . type === "SELECT NEXT" &&
125- context . list . length &&
125+ context . index . length &&
126126 context . selected . length === 0
127127 ) {
128128 return {
129129 ...context ,
130- selected : [ context . list [ 0 ] ] ,
131- adjacentPivot : context . list [ 0 ] ,
130+ selected : [ context . index [ 0 ] ] ,
131+ adjacentPivot : context . index [ 0 ] ,
132132 }
133133 } else if (
134134 command . type === "SELECT NEXT" &&
135- context . list . length &&
135+ context . index . length &&
136136 context . selected . length
137137 ) {
138- const pivotIndex = context . list . indexOf ( last ( context . selected ) ! )
138+ const pivotIndex = context . index . indexOf ( last ( context . selected ) ! )
139139
140- if ( pivotIndex < context . list . length - 1 ) {
141- const nextItem = context . list [ pivotIndex + 1 ] ;
140+ if ( pivotIndex < context . index . length - 1 ) {
141+ const nextKey = context . index [ pivotIndex + 1 ] ;
142142
143143 return {
144144 ...context ,
145- selected : [ nextItem ] ,
146- adjacentPivot : nextItem
145+ selected : [ nextKey ] ,
146+ adjacentPivot : nextKey
147147 }
148148 } else if (
149149 ! (
150150 context . selected . length === 1 &&
151- context . selected [ 0 ] === last ( context . list )
151+ context . selected [ 0 ] === last ( context . index )
152152 )
153153 ) {
154- const pivot = context . list [ pivotIndex ] ;
154+ const pivot = context . index [ pivotIndex ] ;
155155 return {
156156 ...context ,
157157 selected : [ pivot ] ,
@@ -162,36 +162,36 @@ export function multiselect(context: Context, command: Command): Context {
162162 }
163163 } else if (
164164 command . type === "SELECT PREVIOUS" &&
165- context . list . length &&
165+ context . index . length &&
166166 context . selected . length === 0
167167 ) {
168- const pivot = last ( context . list ) ! ;
168+ const pivot = last ( context . index ) ! ;
169169 return {
170170 ...context ,
171171 selected : [ pivot ] ,
172172 adjacentPivot : pivot ,
173173 }
174174 } else if (
175175 command . type === "SELECT PREVIOUS" &&
176- context . list . length &&
176+ context . index . length &&
177177 context . selected . length
178178 ) {
179- const pivotIndex = context . list . indexOf ( last ( context . selected ) ! )
179+ const pivotIndex = context . index . indexOf ( last ( context . selected ) ! )
180180
181181 if ( pivotIndex > 0 ) {
182- const prevItem = context . list [ pivotIndex - 1 ] ;
182+ const prevKey = context . index [ pivotIndex - 1 ] ;
183183 return {
184184 ...context ,
185- selected : [ prevItem ] ,
186- adjacentPivot : prevItem
185+ selected : [ prevKey ] ,
186+ adjacentPivot : prevKey
187187 }
188188 } else if (
189189 ! (
190190 context . selected . length === 1 &&
191- context . selected [ 0 ] === head ( context . list )
191+ context . selected [ 0 ] === head ( context . index )
192192 )
193193 ) {
194- const pivot = head ( context . list ) ! ;
194+ const pivot = head ( context . index ) ! ;
195195 return {
196196 ...context ,
197197 selected : [ pivot ] ,
0 commit comments