-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
276 lines (244 loc) · 8.16 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { StyleProp, ViewStyle } from "react-native";
import { Item, List } from "./data/data";
import { ListType, CollectionViewCell, CollectionViewCellType } from "./types";
import { areTestsRunningOverride } from "./env.json";
import { NativeStackNavigationOptions } from "@react-navigation/native-stack";
import { TextSize } from "./components/core/CustomText";
// Colors
export enum Color {
LightBlue = "lightblue",
LightBlueButton = "#0097fb",
White = "white",
LightGray = "lightgray",
Gray = "gray",
Black = "black",
Red = "red",
}
export function getDeveloperModeListCellStyles(
isActive: boolean
): StyleProp<ViewStyle> {
return {
padding: 20,
borderBottomWidth: 1,
borderBottomColor: Color.Gray,
backgroundColor: isActive ? Color.LightBlue : Color.White,
};
}
/* * * * * * * * * *
* Utility methods *
* * * * * * * * * */
export function pluralize(
value: number,
singular: string,
plural: string
): string {
return value === 1 ? singular : plural;
}
export function insertAt<T>(index: number, value: T, collection: T[]): T[] {
// To add an item to the end of a list, use the length of the list as the index
return [...collection.slice(0, index), value, ...collection.slice(index)];
}
export function removeAt<T>(index: number, collection: T[]): T[] {
return [...collection.slice(0, index), ...collection.slice(index + 1)];
}
export function updateAt<T>(
value: T,
collection: T[],
currentIndex: number,
newIndex?: number
): T[] {
if (currentIndex >= collection.length || currentIndex < 0)
throw Error("Current index out of range");
const actualNewIndex: number = newIndex ?? currentIndex;
if (actualNewIndex >= collection.length || actualNewIndex < 0)
throw Error("New index out of range");
const listWithValueRemoved: T[] = removeAt(currentIndex, collection);
return insertAt(actualNewIndex, value, listWithValueRemoved);
}
/**
* Return the total number of items in a list that are not marked as complete.
*
* @param listType type of list items are in
* @param items list of Item objects
*
* @returns total number of items based on filter criteria (parameter values).
*/
export function getNumItemsIncomplete(
listType: ListType,
items: Item[]
): number {
// Only shopping lists should use the quantity for the items count. All other types can use
// the number of items that are not complete.
return listType === "Shopping"
? items
.map((item) => (item.isComplete ? 0 : item.quantity))
.reduce<number>((prev, curr) => prev + curr, 0)
: items.filter((item) => !item.isComplete).length;
}
export function getNumItemsTotal(listType: ListType, items: Item[]): number {
return listType === "Shopping"
? items
.map((item) => item.quantity)
.reduce<number>((prev, curr) => prev + curr, 0)
: items.length;
}
/* * * * * * * * * * * * * * * * * * * * * *
* Edit collections (lists of lists/items) *
* * * * * * * * * * * * * * * * * * * * * */
export function getSelectedCells(
items: CollectionViewCell[]
): CollectionViewCell[] {
return items.filter((item) => item.isSelected);
}
export function getNumberOfSelectedCells(cells: CollectionViewCell[]): number {
return getSelectedCells(cells).length;
}
export function areCellsSelected(cells: CollectionViewCell[]): boolean {
return getNumberOfSelectedCells(cells) > 0;
}
/**
* Get the index of the first cell being edited.
*
* @param items list of cells (lists or items)
* @returns the index of the first cell being edited.
*/
export function getCellBeingEdited(cells: CollectionViewCell[]): number {
return cells.findIndex((cell) => cell.isSelected);
}
export function isAllSelected(items: CollectionViewCell[]): boolean {
return (
items.length > 0 &&
items.filter((l) => l.isSelected).length == items.length
);
}
export function cellsCountDisplay(
cellsType: CollectionViewCellType,
count: number
): string {
switch (cellsType) {
case "Item": {
const label: string = pluralize(count, "item", "items");
return `${count} ${label}`;
}
case "List": {
const label: string = pluralize(count, "list", "lists");
return `${count} ${label}`;
}
}
}
export function getList(lists: List[], listIndex: number): List {
const list: List | undefined = lists[listIndex];
if (list === undefined) throw Error(`Index out of range: ${listIndex}`);
return list;
}
export function getListItems(lists: List[], listIndex: number): Item[] {
return getList(lists, listIndex).items;
}
export function updateLists(
lists: List[],
listIndex: number,
items: Item[]
): List[] {
const listBeingEdited: List = getList(lists, listIndex);
return lists.map((list, index) =>
index === listIndex ? listBeingEdited.updateItems(items) : list
);
}
/**
* Split lists into two sections: the current list and all other lists.
*
* @param currentListIndex index of the current list
* @param lists all lists
* @returns a tuple where the first element is the current list and the second element
* is all other lists.
*/
export function partitionLists(
currentListIndex: number,
lists: List[]
): [List, List[]] {
const currentList: List = getList(lists, currentListIndex);
const otherLists: List[] = [
...lists.slice(0, currentListIndex),
...lists.slice(currentListIndex + 1),
];
return [currentList, otherLists];
}
/**
* Checks if the app is being run by the tests.
*
* Solution for detecting when tests are running was found here: https://stackoverflow.com/a/52231746. Another
* way to check if the tests are running is with `process.env.JEST_WORKER_ID === 1`
*
* @returns `true` if the tests are running the app; `false` otherwise.
*/
export function areTestsRunning(): boolean {
return areTestsRunningOverride || process.env.NODE_ENV === "test";
}
/**
* If the user invokes the primary action (non-alternate) while adding a new
* list or item, the modal will close.
*
* If the user invokes the alternate action while adding a new list or item, the modal
* will remain visible, and the form values will reset so they can add another list.
*
* If the user invokes the primary action (not alternate) while editing a list or item,
* the modal will close.
*
* If the user invokes the alternate action while editing a list or item, the modal will
* reset to the next list or item, allowing the user to continually update subsequent
* lists or items. If the user is on the last list or item and invokes the alternate action, the
* modal will close.
*
* When updating, the index is reset to -1 after going beyond the end of the list.
*/
export function getCellModalVisibleAndNextIndex(
currentIndex: number,
numLists: number,
isAddingList: boolean,
isAltAction: boolean
): [boolean, number] {
const isModalVisible: boolean = isAddingList
? isAltAction
: isAltAction
? currentIndex + 1 < numLists
: false;
const nextIndex: number = isAddingList
? -1
: isAltAction
? currentIndex + 1 < numLists
? currentIndex + 1
: -1
: -1;
return [isModalVisible, nextIndex];
}
export function listTypePredicateFactory(
listType: ListType
): (list: List) => boolean {
return (list: List) => list.listType === listType;
}
export function listFilterIndices(
lists: List[],
predicate: (list: List) => boolean
): number[] {
return lists
.map((list, index) => [list, index] as [List, number])
.filter(([list, _]) => predicate(list))
.map(([_, index]) => index);
}
export function itemFilterIndices(
items: Item[],
predicate: (item: Item) => boolean
): number[] {
return items
.map((item, index) => [item, index] as [Item, number])
.filter(([item, _]) => predicate(item))
.map(([_, index]) => index);
}
export function navigationTitleOptions(
title: string
): Partial<NativeStackNavigationOptions> {
return {
title: title,
headerTitleStyle: { fontSize: TextSize.Medium },
};
}