-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathiterator.ts
More file actions
164 lines (140 loc) · 3.85 KB
/
Copy pathiterator.ts
File metadata and controls
164 lines (140 loc) · 3.85 KB
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
import type { PermissionRequest, PermissionsRequest } from '../types';
export type PermissionRequestIteratorItem = {
permissionRequest: PermissionRequest;
status: 'pending' | 'settled';
};
export type PermissionsRequestIterator = {
/**
* Moves to the first item in the collection.
*/
first: () => void;
/**
* Settles the current item and advances to the next item in the collection.
* If already at the last item, it does nothing.
*/
settleAndMoveNext: () => void;
/**
* Settles the current item and and moves to the previous item in the collection.
* If already at the first item, it does nothing.
*/
settleAndMovePrevious: () => void;
/**
* Checks if the iterator is at the first item.
*/
isFirst: () => boolean;
/**
* Checks if the iterator is at the last item.
*/
isLast: () => boolean;
/**
* Checks if all items in the collection are settled.
*/
areAllSettled: () => boolean;
/**
* Returns the entire collection of items.
*/
getItems: () => PermissionRequestIteratorItem[];
/**
* Returns the current item in the collection.
*/
currentItem: () => PermissionRequestIteratorItem | null;
/**
* Returns the index of the current item in the collection.
*/
currentIndex: () => number;
/**
* Updates the current item using the provided updater function.
* If there is not a current item, it does nothing.
*/
updateCurrentItem: (
updater: (
item: PermissionRequestIteratorItem,
) => PermissionRequestIteratorItem,
) => void;
/**
* The number of items in the collection.
*/
length: number;
};
/**
* Creates a new permissions request iterator. Supports bidirectional traversal of a collection of permission requests. Items default status is 'pending' on creation.
*
* @param permissionsRequest - The collection of permission requests to iterate over.
* @returns A new permissions request iterator.
* @example
* const permissionsRequest = [...];
* const iterator = createPermissionsRequestIterator(permissionsRequest);
* iterator.first();
* while(!iterator.areAllSettled()) {
* iterator.updateCurrentItem((item: PermissionRequestIteratorItem) => {
* return { ...item, status: 'settled' };
* });
*
* iterator.next();
* }
* console.log('iterator done:', iterator.getItems());
*/
export const createPermissionsRequestIterator = (
permissionsRequest: PermissionsRequest,
): PermissionsRequestIterator => {
let index = 0;
const items: PermissionRequestIteratorItem[] = permissionsRequest.map(
(permissionRequest) => ({
permissionRequest,
status: 'pending',
}),
);
return {
length: items.length,
getItems(): PermissionRequestIteratorItem[] {
return items;
},
currentItem(): PermissionRequestIteratorItem | null {
const item = items[index];
return item ?? null;
},
currentIndex() {
return index;
},
first() {
index = 0;
},
settleAndMoveNext(): void {
if (!this.isLast()) {
const item = items[index];
if (item) {
items[index] = { ...item, status: 'settled' };
}
index += 1;
}
},
settleAndMovePrevious(): void {
if (!this.isFirst()) {
const item = items[index];
if (item) {
items[index] = { ...item, status: 'settled' };
}
index -= 1;
}
},
isFirst(): boolean {
return index === 0;
},
isLast(): boolean {
return index === items.length - 1;
},
areAllSettled(): boolean {
return items.every((request) => request.status === 'settled');
},
updateCurrentItem(
updater: (
item: PermissionRequestIteratorItem,
) => PermissionRequestIteratorItem,
) {
const currentItem = items[index];
if (currentItem) {
items[index] = updater(currentItem);
}
},
};
};