@applicaster/zapp-react-native-utils 16.0.0-rc.46 → 16.0.0-rc.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/arrayUtils/__tests__/{anyThruthy.test.ts → anyTruthy.test.ts} +8 -0
- package/arrayUtils/__tests__/arrayUtils.test.ts +165 -108
- package/arrayUtils/__tests__/isEmptyArray.test.ts +11 -0
- package/arrayUtils/__tests__/isFilledArray.test.ts +12 -0
- package/arrayUtils/__tests__/isFirst.test.ts +17 -0
- package/arrayUtils/__tests__/isIndexInRange.test.ts +14 -0
- package/arrayUtils/__tests__/isLast.test.ts +31 -0
- package/arrayUtils/__tests__/makeListOf.test.ts +31 -0
- package/arrayUtils/__tests__/makeListOfIndexes.test.ts +20 -0
- package/arrayUtils/__tests__/sample.test.ts +61 -0
- package/arrayUtils/index.ts +57 -20
- package/package.json +2 -2
- package/zappFrameworkUtils/__tests__/localStorageHelper.test.ts +146 -0
- package/zappFrameworkUtils/localStorageHelper.ts +9 -13
|
@@ -21,4 +21,12 @@ describe("anyTruthy", () => {
|
|
|
21
21
|
expect(anyTruthy([true])).toBe(true);
|
|
22
22
|
expect(anyTruthy([false])).toBe(false);
|
|
23
23
|
});
|
|
24
|
+
|
|
25
|
+
it("should return true when the first value is true", () => {
|
|
26
|
+
expect(anyTruthy([true, false, false])).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return true when only the last value is true", () => {
|
|
30
|
+
expect(anyTruthy([false, false, true])).toBe(true);
|
|
31
|
+
});
|
|
24
32
|
});
|
|
@@ -28,6 +28,30 @@ describe("shiftArray", () => {
|
|
|
28
28
|
expect(shiftArray(negativeOffset, array)).toMatchSnapshot();
|
|
29
29
|
expect(shiftArray(2 * negativeOffset, array)).toMatchSnapshot();
|
|
30
30
|
});
|
|
31
|
+
|
|
32
|
+
it("returns a new array and does not mutate the original", () => {
|
|
33
|
+
const original = ["a", "b", "c"];
|
|
34
|
+
const result = shiftArray(1, original);
|
|
35
|
+
|
|
36
|
+
expect(result).toEqual(["b", "c", "a"]);
|
|
37
|
+
expect(result).not.toBe(original);
|
|
38
|
+
expect(original).toEqual(["a", "b", "c"]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("returns an empty array when shifting an empty array", () => {
|
|
42
|
+
expect(shiftArray(1, [])).toEqual([]);
|
|
43
|
+
expect(shiftArray(-1, [])).toEqual([]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("returns the same order for a single-element array", () => {
|
|
47
|
+
expect(shiftArray(1, [1])).toEqual([1]);
|
|
48
|
+
expect(shiftArray(-1, [1])).toEqual([1]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("handles an offset equal to the array length", () => {
|
|
52
|
+
expect(shiftArray(4, array)).toEqual(array);
|
|
53
|
+
expect(shiftArray(-4, array)).toEqual(array);
|
|
54
|
+
});
|
|
31
55
|
});
|
|
32
56
|
|
|
33
57
|
describe("removeItemFromList", () => {
|
|
@@ -58,6 +82,38 @@ describe("removeItemFromList", () => {
|
|
|
58
82
|
arrayOfObjects
|
|
59
83
|
);
|
|
60
84
|
});
|
|
85
|
+
|
|
86
|
+
it("returns the same reference when the item is not found", () => {
|
|
87
|
+
const list = [1, 2, 3];
|
|
88
|
+
|
|
89
|
+
expect(removeItemFromList(4, list)).toBe(list);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("returns a new array when the item is removed", () => {
|
|
93
|
+
const list = [1, 2, 3];
|
|
94
|
+
const result = removeItemFromList(2, list);
|
|
95
|
+
|
|
96
|
+
expect(result).toEqual([1, 3]);
|
|
97
|
+
expect(result).not.toBe(list);
|
|
98
|
+
expect(list).toEqual([1, 2, 3]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("removes only the first occurrence of a duplicate item", () => {
|
|
102
|
+
expect(removeItemFromList(2, [1, 2, 2, 3])).toEqual([1, 2, 3]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("removes the first and last items", () => {
|
|
106
|
+
expect(removeItemFromList(1, [1, 2, 3])).toEqual([2, 3]);
|
|
107
|
+
expect(removeItemFromList(3, [1, 2, 3])).toEqual([1, 2]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("returns an empty list when removing from an empty list", () => {
|
|
111
|
+
expect(removeItemFromList(1, [])).toEqual([]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("returns an empty list when removing the only item", () => {
|
|
115
|
+
expect(removeItemFromList(1, [1])).toEqual([]);
|
|
116
|
+
});
|
|
61
117
|
});
|
|
62
118
|
|
|
63
119
|
describe("mapPromises", () => {
|
|
@@ -84,145 +140,146 @@ describe("mapPromises", () => {
|
|
|
84
140
|
values.forEach((value) => {
|
|
85
141
|
expect(promiseFn).toHaveBeenNthCalledWith(value, value, value - 1);
|
|
86
142
|
});
|
|
87
|
-
});
|
|
88
143
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const arrayOfPrimitives = [1, 2, 3, 4];
|
|
92
|
-
expect(removeItemFromList(1, arrayOfPrimitives)).toEqual([2, 3, 4]);
|
|
144
|
+
expect.assertions(values.length + 1);
|
|
145
|
+
});
|
|
93
146
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
];
|
|
147
|
+
it("resolves to an empty array for an empty input", async () => {
|
|
148
|
+
await expect(mapPromises(promiseFn, [])).resolves.toEqual([]);
|
|
149
|
+
expect(promiseFn).not.toHaveBeenCalled();
|
|
150
|
+
});
|
|
99
151
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
});
|
|
152
|
+
it("rejects when any promise rejects", async () => {
|
|
153
|
+
const failingFn = jest.fn((num) =>
|
|
154
|
+
num === 2 ? Promise.reject(new Error("fail")) : Promise.resolve(num)
|
|
155
|
+
);
|
|
105
156
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
{ prop: "value2" },
|
|
110
|
-
{ prop: "value3" },
|
|
111
|
-
];
|
|
157
|
+
await expect(mapPromises(failingFn, [1, 2, 3])).rejects.toThrow("fail");
|
|
158
|
+
});
|
|
159
|
+
});
|
|
112
160
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
161
|
+
describe("reducePromises", () => {
|
|
162
|
+
const promiseFn = jest.fn((current, previous, index, cb) => {
|
|
163
|
+
return new Promise((resolve) => {
|
|
164
|
+
setTimeout(() => {
|
|
165
|
+
resolve(previous + current);
|
|
166
|
+
cb?.(index);
|
|
167
|
+
}, 1);
|
|
116
168
|
});
|
|
117
169
|
});
|
|
118
170
|
|
|
119
|
-
|
|
120
|
-
|
|
171
|
+
beforeEach(() => {
|
|
172
|
+
promiseFn.mockClear();
|
|
173
|
+
});
|
|
121
174
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
175
|
+
it("is a curried function; and returns the reduced promise values", async () => {
|
|
176
|
+
const promiseReducer = reducePromises(promiseFn);
|
|
125
177
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
178
|
+
const initialValue = 0;
|
|
179
|
+
const values = [1, 2, 3];
|
|
180
|
+
const sumOfValues = initialValue + values.reduce((a, b) => a + b, 0);
|
|
129
181
|
|
|
130
|
-
|
|
131
|
-
await expect(promiseMapper(values)).resolves.toEqual([2, 3, 4]);
|
|
132
|
-
});
|
|
182
|
+
expect(typeof promiseReducer).toBe("function");
|
|
133
183
|
|
|
134
|
-
|
|
135
|
-
|
|
184
|
+
await expect(promiseReducer(initialValue, values)).resolves.toEqual(
|
|
185
|
+
sumOfValues
|
|
186
|
+
);
|
|
187
|
+
});
|
|
136
188
|
|
|
137
|
-
|
|
138
|
-
|
|
189
|
+
it("calls promises one after the other", (done) => {
|
|
190
|
+
const values = [1, 2, 3];
|
|
191
|
+
const previousValues = [0, 1, 3];
|
|
192
|
+
|
|
193
|
+
reducePromises(
|
|
194
|
+
(current, previous, index) =>
|
|
195
|
+
promiseFn(current, previous, index, (index) => {
|
|
196
|
+
expect(promiseFn).toHaveBeenCalledTimes(index + 1);
|
|
197
|
+
|
|
198
|
+
expect(promiseFn).toHaveBeenNthCalledWith(
|
|
199
|
+
index + 1,
|
|
200
|
+
values[index],
|
|
201
|
+
previousValues[index],
|
|
202
|
+
index,
|
|
203
|
+
expect.any(Function)
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
if (index === 2) done();
|
|
207
|
+
}),
|
|
208
|
+
0,
|
|
209
|
+
values
|
|
210
|
+
);
|
|
139
211
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
});
|
|
212
|
+
expect.assertions(values.length * 2);
|
|
213
|
+
});
|
|
143
214
|
|
|
144
|
-
|
|
145
|
-
|
|
215
|
+
it("returns the initial value for an empty list", async () => {
|
|
216
|
+
await expect(reducePromises(promiseFn, 10, [])).resolves.toBe(10);
|
|
217
|
+
expect(promiseFn).not.toHaveBeenCalled();
|
|
146
218
|
});
|
|
147
219
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
setTimeout(() => {
|
|
152
|
-
resolve(previous + current);
|
|
153
|
-
cb?.(index);
|
|
154
|
-
}, 1);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
220
|
+
it("reduces a single-element list", async () => {
|
|
221
|
+
await expect(reducePromises(promiseFn, 5, [2])).resolves.toBe(7);
|
|
222
|
+
});
|
|
157
223
|
|
|
158
|
-
|
|
159
|
-
|
|
224
|
+
it("rejects when a promise rejects", async () => {
|
|
225
|
+
const failingFn = jest.fn(async (current, previous) => {
|
|
226
|
+
if (current === 2) throw new Error("reduce fail");
|
|
227
|
+
|
|
228
|
+
return previous + current;
|
|
160
229
|
});
|
|
161
230
|
|
|
162
|
-
|
|
163
|
-
|
|
231
|
+
await expect(reducePromises(failingFn, 0, [1, 2, 3])).rejects.toThrow(
|
|
232
|
+
"reduce fail"
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe("mapAndSplit", () => {
|
|
238
|
+
it("maps over a list of values and split it in chunks", () => {
|
|
239
|
+
const values = new Array(20).fill(0).map((_, i) => i);
|
|
240
|
+
|
|
241
|
+
const alphabet = Array.from(Array(26)).map((_, i) =>
|
|
242
|
+
String.fromCharCode(i + 65)
|
|
243
|
+
);
|
|
164
244
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const sumOfValues = initialValue + values.reduce((a, b) => a + b, 0);
|
|
245
|
+
const getLetterForInt = (num) => alphabet[num];
|
|
246
|
+
const chunkSize = 5;
|
|
168
247
|
|
|
169
|
-
|
|
248
|
+
const result = mapAndSplit<number, string>(
|
|
249
|
+
getLetterForInt,
|
|
250
|
+
chunkSize
|
|
251
|
+
)(values);
|
|
170
252
|
|
|
171
|
-
|
|
172
|
-
sumOfValues
|
|
173
|
-
);
|
|
174
|
-
});
|
|
253
|
+
expect(result.length).toBe(values.length / chunkSize);
|
|
175
254
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
reducePromises(
|
|
181
|
-
(current, previous, index) =>
|
|
182
|
-
promiseFn(current, previous, index, (index) => {
|
|
183
|
-
expect(promiseFn).toHaveBeenCalledTimes(index + 1);
|
|
184
|
-
|
|
185
|
-
expect(promiseFn).toHaveBeenNthCalledWith(
|
|
186
|
-
index + 1,
|
|
187
|
-
values[index],
|
|
188
|
-
previousValues[index],
|
|
189
|
-
index,
|
|
190
|
-
expect.any(Function)
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
if (index === 2) done();
|
|
194
|
-
}),
|
|
195
|
-
0,
|
|
196
|
-
values
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
expect.assertions(values.length * 2);
|
|
255
|
+
result.forEach((chunk, chunkIndex) => {
|
|
256
|
+
chunk.forEach((value, index) => {
|
|
257
|
+
expect(value).toBe(alphabet[chunkIndex * chunkSize + index]);
|
|
258
|
+
});
|
|
200
259
|
});
|
|
201
260
|
});
|
|
202
261
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
262
|
+
it("is a curried function", () => {
|
|
263
|
+
const mapper = (n: number) => n * 2;
|
|
264
|
+
const splitBy2 = mapAndSplit(mapper, 2);
|
|
206
265
|
|
|
207
|
-
|
|
208
|
-
String.fromCharCode(i + 65)
|
|
209
|
-
);
|
|
266
|
+
expect(typeof splitBy2).toBe("function");
|
|
210
267
|
|
|
211
|
-
|
|
212
|
-
|
|
268
|
+
expect(splitBy2([1, 2, 3, 4])).toEqual([
|
|
269
|
+
[2, 4],
|
|
270
|
+
[6, 8],
|
|
271
|
+
]);
|
|
272
|
+
});
|
|
213
273
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
)(values);
|
|
274
|
+
it("returns an empty list for an empty input", () => {
|
|
275
|
+
expect(mapAndSplit((n: number) => n, 2)([])).toEqual([]);
|
|
276
|
+
});
|
|
218
277
|
|
|
219
|
-
|
|
278
|
+
it("puts remaining items in a final smaller chunk", () => {
|
|
279
|
+
expect(mapAndSplit((n: number) => n, 2)([1, 2, 3])).toEqual([[1, 2], [3]]);
|
|
280
|
+
});
|
|
220
281
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
expect(value).toBe(alphabet[chunkIndex * chunkSize + index]);
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
});
|
|
282
|
+
it("returns a single chunk when chunk size exceeds the list length", () => {
|
|
283
|
+
expect(mapAndSplit((n: number) => n * 10, 10)([1, 2])).toEqual([[10, 20]]);
|
|
227
284
|
});
|
|
228
285
|
});
|
|
@@ -60,4 +60,15 @@ describe("isEmptyArray", () => {
|
|
|
60
60
|
|
|
61
61
|
expect(isEmptyArray(value)).toBe(false);
|
|
62
62
|
});
|
|
63
|
+
|
|
64
|
+
it("array with falsy values is not empty", () => {
|
|
65
|
+
expect(isEmptyArray([0])).toBe(false);
|
|
66
|
+
expect(isEmptyArray([false])).toBe(false);
|
|
67
|
+
expect(isEmptyArray([null])).toBe(false);
|
|
68
|
+
expect(isEmptyArray([undefined])).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("nested empty array is not empty", () => {
|
|
72
|
+
expect(isEmptyArray([[]])).toBe(false);
|
|
73
|
+
});
|
|
63
74
|
});
|
|
@@ -60,4 +60,16 @@ describe("isFilledArray", () => {
|
|
|
60
60
|
|
|
61
61
|
expect(isFilledArray(value)).toBe(false);
|
|
62
62
|
});
|
|
63
|
+
|
|
64
|
+
it("array with falsy values is filled", () => {
|
|
65
|
+
expect(isFilledArray([0])).toBe(true);
|
|
66
|
+
expect(isFilledArray([false])).toBe(true);
|
|
67
|
+
expect(isFilledArray([null])).toBe(true);
|
|
68
|
+
expect(isFilledArray([undefined])).toBe(true);
|
|
69
|
+
expect(isFilledArray([""])).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("nested empty array is filled", () => {
|
|
73
|
+
expect(isFilledArray([[]])).toBe(true);
|
|
74
|
+
});
|
|
63
75
|
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { isFirst } from "..";
|
|
2
|
+
|
|
3
|
+
describe("isFirst", () => {
|
|
4
|
+
it("returns true for index 0", () => {
|
|
5
|
+
expect(isFirst(0)).toBe(true);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it("returns false for positive indexes", () => {
|
|
9
|
+
expect(isFirst(1)).toBe(false);
|
|
10
|
+
expect(isFirst(10)).toBe(false);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("returns true for negative indexes", () => {
|
|
14
|
+
expect(isFirst(-1)).toBe(true);
|
|
15
|
+
expect(isFirst(-100)).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -56,4 +56,18 @@ describe("isIndexInRange", () => {
|
|
|
56
56
|
|
|
57
57
|
expect(isIndexInRange(index, length)).toBe(false);
|
|
58
58
|
});
|
|
59
|
+
|
|
60
|
+
it("single-element list", () => {
|
|
61
|
+
expect(isIndexInRange(0, 1)).toBe(true);
|
|
62
|
+
expect(isIndexInRange(1, 1)).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("negative length is out of range", () => {
|
|
66
|
+
expect(isIndexInRange(0, -1)).toBe(false);
|
|
67
|
+
expect(isIndexInRange(0, -5)).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("middle index is in range", () => {
|
|
71
|
+
expect(isIndexInRange(1, 3)).toBe(true);
|
|
72
|
+
});
|
|
59
73
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isLast } from "..";
|
|
2
|
+
|
|
3
|
+
describe("isLast", () => {
|
|
4
|
+
it("returns true for the last index", () => {
|
|
5
|
+
expect(isLast(2, 3)).toBe(true);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it("returns false for indexes before the last", () => {
|
|
9
|
+
expect(isLast(0, 3)).toBe(false);
|
|
10
|
+
expect(isLast(1, 3)).toBe(false);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("returns true for indexes at or past the last", () => {
|
|
14
|
+
expect(isLast(3, 3)).toBe(true);
|
|
15
|
+
expect(isLast(10, 3)).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("returns true for a single-element list", () => {
|
|
19
|
+
expect(isLast(0, 1)).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("treats empty length as last for indexes >= -1", () => {
|
|
23
|
+
expect(isLast(0, 0)).toBe(true);
|
|
24
|
+
expect(isLast(-1, 0)).toBe(true);
|
|
25
|
+
expect(isLast(-2, 0)).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("returns false for negative indexes that are before length - 1", () => {
|
|
29
|
+
expect(isLast(-1, 3)).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { makeListOf } from "..";
|
|
2
|
+
|
|
3
|
+
describe("makeListOf", () => {
|
|
4
|
+
it("creates a list filled with the given value", () => {
|
|
5
|
+
expect(makeListOf("x", 3)).toEqual(["x", "x", "x"]);
|
|
6
|
+
expect(makeListOf(0, 4)).toEqual([0, 0, 0, 0]);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("returns an empty list for size 0", () => {
|
|
10
|
+
expect(makeListOf("x", 0)).toEqual([]);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("returns a single-element list for size 1", () => {
|
|
14
|
+
expect(makeListOf(42, 1)).toEqual([42]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("fills with null, undefined, and objects by reference", () => {
|
|
18
|
+
expect(makeListOf(null, 2)).toEqual([null, null]);
|
|
19
|
+
expect(makeListOf(undefined, 2)).toEqual([undefined, undefined]);
|
|
20
|
+
|
|
21
|
+
const obj = { a: 1 };
|
|
22
|
+
const result = makeListOf(obj, 2);
|
|
23
|
+
|
|
24
|
+
expect(result).toEqual([obj, obj]);
|
|
25
|
+
expect(result[0]).toBe(result[1]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("throws for negative size", () => {
|
|
29
|
+
expect(() => makeListOf("x", -1)).toThrow(RangeError);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { makeListOfIndexes } from "..";
|
|
2
|
+
|
|
3
|
+
describe("makeListOfIndexes", () => {
|
|
4
|
+
it("returns a list of indexes for the given size", () => {
|
|
5
|
+
expect(makeListOfIndexes(5)).toEqual([0, 1, 2, 3, 4]);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it("returns an empty list for size 0", () => {
|
|
9
|
+
expect(makeListOfIndexes(0)).toEqual([]);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("returns a single-element list for size 1", () => {
|
|
13
|
+
expect(makeListOfIndexes(1)).toEqual([0]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("returns an empty list for negative size", () => {
|
|
17
|
+
expect(makeListOfIndexes(-1)).toEqual([]);
|
|
18
|
+
expect(makeListOfIndexes(-10)).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { sample } from "..";
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
var __DEV__: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
describe("sample", () => {
|
|
8
|
+
const originalDev = global.__DEV__;
|
|
9
|
+
const originalRandom = Math.random;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
global.__DEV__ = true;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
global.__DEV__ = originalDev;
|
|
17
|
+
Math.random = originalRandom;
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("returns an item from the list", () => {
|
|
21
|
+
Math.random = () => 0.5;
|
|
22
|
+
|
|
23
|
+
expect(sample(["a", "b", "c"])).toBe("b");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("returns the first item when random is near 0", () => {
|
|
27
|
+
Math.random = () => 0;
|
|
28
|
+
|
|
29
|
+
expect(sample(["a", "b", "c"])).toBe("a");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("returns the last item when random is near 1", () => {
|
|
33
|
+
Math.random = () => 0.999;
|
|
34
|
+
|
|
35
|
+
expect(sample(["a", "b", "c"])).toBe("c");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("returns the only item for a single-element list", () => {
|
|
39
|
+
Math.random = () => 0.42;
|
|
40
|
+
|
|
41
|
+
expect(sample([42])).toBe(42);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("throws when the input is not an array", () => {
|
|
45
|
+
expect(() => sample("not-an-array" as unknown as unknown[])).toThrow(
|
|
46
|
+
/input value is not an array/
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
expect(() => sample(null as unknown as unknown[])).toThrow(
|
|
50
|
+
/input value is not an array/
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
expect(() => sample(undefined as unknown as unknown[])).toThrow(
|
|
54
|
+
/input value is not an array/
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("throws when the array is empty", () => {
|
|
59
|
+
expect(() => sample([])).toThrow(/input array is empty/);
|
|
60
|
+
});
|
|
61
|
+
});
|
package/arrayUtils/index.ts
CHANGED
|
@@ -3,17 +3,16 @@ import * as R from "ramda";
|
|
|
3
3
|
import { invariant } from "@applicaster/zapp-react-native-utils/errorUtils";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @returns {Array<T>}
|
|
6
|
+
* Shifts an array by the given offset and returns a new array (does not mutate).
|
|
7
|
+
* A positive offset moves items from the head to the tail;
|
|
8
|
+
* a negative offset moves items from the tail to the head.
|
|
9
|
+
* An offset of `0` leaves the order unchanged.
|
|
10
|
+
*
|
|
11
|
+
* Curried (Ramda): `shiftArray(offset)(array)` or `shiftArray(offset, array)`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* shiftArray(-1, [1, 2, 3, 4]) // => [4, 1, 2, 3]
|
|
15
|
+
* shiftArray(1, [1, 2, 3, 4]) // => [2, 3, 4, 1]
|
|
17
16
|
*/
|
|
18
17
|
export const shiftArray = R.curry(
|
|
19
18
|
<T extends unknown>(offset: number, array: T[]): T[] => {
|
|
@@ -25,10 +24,9 @@ export const shiftArray = R.curry(
|
|
|
25
24
|
);
|
|
26
25
|
|
|
27
26
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* @returns {Array<any>}
|
|
27
|
+
* Removes the first item equal to `item` from `list` (Ramda `equals` / deep equality).
|
|
28
|
+
* If the item is not found, returns the same list reference; otherwise returns a new
|
|
29
|
+
* array without that element.
|
|
32
30
|
*/
|
|
33
31
|
export function removeItemFromList<T extends unknown>(item: T, list: T[]): T[] {
|
|
34
32
|
return R.compose(
|
|
@@ -37,6 +35,12 @@ export function removeItemFromList<T extends unknown>(item: T, list: T[]): T[] {
|
|
|
37
35
|
)(list);
|
|
38
36
|
}
|
|
39
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Maps each value to a promise and resolves them all in parallel via `Promise.all`.
|
|
40
|
+
* The mapper receives `(value, index)`.
|
|
41
|
+
*
|
|
42
|
+
* Curried (Ramda): `mapPromises(fn)(values)` or `mapPromises(fn, values)`.
|
|
43
|
+
*/
|
|
40
44
|
export const mapPromises = R.curry(
|
|
41
45
|
<T extends unknown, S extends unknown>(
|
|
42
46
|
fn: (value: T, index?: number) => Promise<S>,
|
|
@@ -44,6 +48,10 @@ export const mapPromises = R.curry(
|
|
|
44
48
|
): Promise<S[]> => Promise.all(R.addIndex(R.map)(R.nAry(2, fn), values))
|
|
45
49
|
);
|
|
46
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Awaits `previousPromise`, then runs `fn(currentValue, previousValue, index)`.
|
|
53
|
+
* Used internally by `reducePromises` as a Ramda-indexed reducer step.
|
|
54
|
+
*/
|
|
47
55
|
const waitForPromiseAndRun = R.curryN(
|
|
48
56
|
3,
|
|
49
57
|
async <T extends unknown, S extends unknown>(
|
|
@@ -58,6 +66,14 @@ const waitForPromiseAndRun = R.curryN(
|
|
|
58
66
|
}
|
|
59
67
|
);
|
|
60
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Reduces a list by applying an async function sequentially.
|
|
71
|
+
* Each step awaits the previous result, then calls `fn(current, previous, index)`.
|
|
72
|
+
* An empty list resolves to `initialValue` without calling `fn`.
|
|
73
|
+
*
|
|
74
|
+
* Curried (Ramda): `reducePromises(fn)(initialValue)(values)` or
|
|
75
|
+
* `reducePromises(fn, initialValue, values)`.
|
|
76
|
+
*/
|
|
61
77
|
export const reducePromises = R.curry(
|
|
62
78
|
<T extends unknown, S extends unknown>(
|
|
63
79
|
fn: (current: T, previous: S, index?: number) => Promise<S>,
|
|
@@ -71,18 +87,32 @@ export const reducePromises = R.curry(
|
|
|
71
87
|
)
|
|
72
88
|
);
|
|
73
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Returns a function that maps a list with `mapperFn`, then splits the result
|
|
92
|
+
* into chunks of size `chunks` (`R.splitEvery`).
|
|
93
|
+
*
|
|
94
|
+
* Curried (Ramda): `mapAndSplit(mapperFn, chunks)(values)`.
|
|
95
|
+
*/
|
|
74
96
|
export const mapAndSplit = R.curry(
|
|
75
97
|
<T extends unknown, S extends unknown>(
|
|
76
|
-
mapperFn: (T) => S,
|
|
98
|
+
mapperFn: (value: T) => S,
|
|
77
99
|
chunks: number
|
|
78
|
-
): S[][] =>
|
|
100
|
+
): ((values: T[]) => S[][]) =>
|
|
101
|
+
(values: T[]) =>
|
|
102
|
+
R.splitEvery(chunks, R.map(mapperFn, values))
|
|
79
103
|
);
|
|
80
104
|
|
|
105
|
+
/** Returns `true` when `index` is at or past the last position (`length - 1`). */
|
|
81
106
|
export const isLast = (index: number, length: number): boolean =>
|
|
82
107
|
index >= length - 1;
|
|
83
108
|
|
|
109
|
+
/** Returns `true` when `index` is `0` or negative. */
|
|
84
110
|
export const isFirst = (index: number): boolean => index <= 0;
|
|
85
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Returns `true` when `index` is a valid zero-based index for a list of the
|
|
114
|
+
* given `length`. Returns `false` for empty/negative lengths or negative indexes.
|
|
115
|
+
*/
|
|
86
116
|
export const isIndexInRange = (index: number, length: number): boolean => {
|
|
87
117
|
if (length <= 0) return false;
|
|
88
118
|
if (index < 0) return false;
|
|
@@ -90,24 +120,29 @@ export const isIndexInRange = (index: number, length: number): boolean => {
|
|
|
90
120
|
return index + 1 <= length;
|
|
91
121
|
};
|
|
92
122
|
|
|
123
|
+
/** Builds `[0, 1, ..., size - 1]`. Negative or zero `size` yields `[]`. */
|
|
93
124
|
export const makeListOfIndexes = (size: number): number[] =>
|
|
94
125
|
Array.from({ length: size }, (_, index) => index);
|
|
95
126
|
|
|
127
|
+
/** Builds an array of the given `size` filled with `value`. */
|
|
96
128
|
export const makeListOf = <T>(value: T, size: number): T[] => {
|
|
97
129
|
return Array(size).fill(value);
|
|
98
130
|
};
|
|
99
131
|
|
|
100
|
-
/** Checks if a value is a non-empty array */
|
|
132
|
+
/** Checks if a value is a non-empty array. */
|
|
101
133
|
export function isFilledArray(value: unknown): boolean {
|
|
102
134
|
return Array.isArray(value) && value.length > 0;
|
|
103
135
|
}
|
|
104
136
|
|
|
105
|
-
/** Checks if a value is
|
|
137
|
+
/** Checks if a value is an empty array. */
|
|
106
138
|
export function isEmptyArray(value: unknown): boolean {
|
|
107
139
|
return Array.isArray(value) && value.length === 0;
|
|
108
140
|
}
|
|
109
141
|
|
|
110
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Returns a random item from a non-empty array.
|
|
144
|
+
* Throws (in development via `invariant`) if the input is not an array or is empty.
|
|
145
|
+
*/
|
|
111
146
|
export const sample = (xs: unknown[]): unknown => {
|
|
112
147
|
invariant(Array.isArray(xs), `input value is not an array: ${xs}`);
|
|
113
148
|
invariant(isFilledArray(xs), `input array is empty: ${xs}`);
|
|
@@ -117,7 +152,9 @@ export const sample = (xs: unknown[]): unknown => {
|
|
|
117
152
|
return xs[index];
|
|
118
153
|
};
|
|
119
154
|
|
|
155
|
+
/** Returns `true` when `xs` is non-empty and every value is truthy. */
|
|
120
156
|
export const allTruthy = (xs: boolean[]) =>
|
|
121
157
|
isFilledArray(xs) && xs.every(Boolean);
|
|
122
158
|
|
|
159
|
+
/** Returns `true` when at least one value in `xs` is truthy. */
|
|
123
160
|
export const anyTruthy = (xs: boolean[]) => xs.some(Boolean);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "16.0.0-rc.
|
|
3
|
+
"version": "16.0.0-rc.48",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@applicaster/applicaster-types": "16.0.0-rc.
|
|
30
|
+
"@applicaster/applicaster-types": "16.0.0-rc.48",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {
|
|
2
|
+
batchSaveOwnedValues,
|
|
3
|
+
batchRemoveOwnedValues,
|
|
4
|
+
} from "../localStorageHelper";
|
|
5
|
+
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
6
|
+
|
|
7
|
+
// Mock localStorage
|
|
8
|
+
jest.mock(
|
|
9
|
+
"@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage",
|
|
10
|
+
() => ({
|
|
11
|
+
localStorage: {
|
|
12
|
+
getItem: jest.fn(),
|
|
13
|
+
setItem: jest.fn(),
|
|
14
|
+
removeItem: jest.fn(),
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
describe("localStorageHelper", () => {
|
|
20
|
+
const ownershipKey = "test_ownership_key";
|
|
21
|
+
const ownershipNamespace = "test_namespace";
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
jest.clearAllMocks();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("addOwnedKeys via batchSaveOwnedValues", () => {
|
|
28
|
+
it("should add new keys to existing owned keys", async () => {
|
|
29
|
+
// Setup existing keys in storage
|
|
30
|
+
const existingKeys = {
|
|
31
|
+
namespace1: ["key1"],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
35
|
+
JSON.stringify(existingKeys)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const newValues = {
|
|
39
|
+
namespace1: { key2: "value2" },
|
|
40
|
+
namespace2: { key3: "value3" },
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
await batchSaveOwnedValues({
|
|
44
|
+
storageValues: newValues,
|
|
45
|
+
ownershipKey,
|
|
46
|
+
ownershipNamespace,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Verify localStorage.setItem was called with correct updated keys
|
|
50
|
+
// Note: order of keys in array might vary depending on Set implementation, but usually insertion order
|
|
51
|
+
// However, to be safe, we can parse the argument and check contents
|
|
52
|
+
|
|
53
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
54
|
+
(call) => call[0] === ownershipKey
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
expect(lastCall).toBeDefined();
|
|
58
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
59
|
+
|
|
60
|
+
expect(savedData.namespace1).toContain("key1");
|
|
61
|
+
expect(savedData.namespace1).toContain("key2");
|
|
62
|
+
expect(savedData.namespace2).toContain("key3");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should handle empty existing keys", async () => {
|
|
66
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(null);
|
|
67
|
+
|
|
68
|
+
const newValues = {
|
|
69
|
+
namespace1: { key1: "value1" },
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
await batchSaveOwnedValues({
|
|
73
|
+
storageValues: newValues,
|
|
74
|
+
ownershipKey,
|
|
75
|
+
ownershipNamespace,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
79
|
+
(call) => call[0] === ownershipKey
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(lastCall).toBeDefined();
|
|
83
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
84
|
+
|
|
85
|
+
expect(savedData.namespace1).toEqual(["key1"]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("removeOwnedKeys via batchRemoveOwnedValues", () => {
|
|
90
|
+
it("should remove keys from owned keys", async () => {
|
|
91
|
+
const existingKeys = {
|
|
92
|
+
namespace1: ["key1", "key2"],
|
|
93
|
+
namespace2: ["key3"],
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
97
|
+
JSON.stringify(existingKeys)
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const keysToRemove = {
|
|
101
|
+
namespace1: ["key1"],
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
await batchRemoveOwnedValues({
|
|
105
|
+
storageValues: keysToRemove,
|
|
106
|
+
ownershipKey,
|
|
107
|
+
ownershipNamespace,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
111
|
+
(call) => call[0] === ownershipKey
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
expect(lastCall).toBeDefined();
|
|
115
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
116
|
+
|
|
117
|
+
expect(savedData.namespace1).toEqual(["key2"]);
|
|
118
|
+
expect(savedData.namespace2).toEqual(["key3"]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should remove namespace if all keys removed", async () => {
|
|
122
|
+
const existingKeys = {
|
|
123
|
+
namespace1: ["key1"],
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
127
|
+
JSON.stringify(existingKeys)
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const keysToRemove = {
|
|
131
|
+
namespace1: ["key1"],
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
await batchRemoveOwnedValues({
|
|
135
|
+
storageValues: keysToRemove,
|
|
136
|
+
ownershipKey,
|
|
137
|
+
ownershipNamespace,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
expect(localStorage.removeItem).toHaveBeenCalledWith(
|
|
141
|
+
ownershipKey,
|
|
142
|
+
ownershipNamespace
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -2,10 +2,10 @@ import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/
|
|
|
2
2
|
import { isNilOrEmpty } from "../reactUtils/helpers";
|
|
3
3
|
import { parseJsonIfNeeded } from "@applicaster/zapp-react-native-utils/functionUtils";
|
|
4
4
|
import {
|
|
5
|
-
StorageOwnedValues,
|
|
6
5
|
NamespaceValues,
|
|
7
|
-
|
|
6
|
+
StorageOwnedValues,
|
|
8
7
|
StorageValuesToAdd,
|
|
8
|
+
StorageValuesToRemove,
|
|
9
9
|
} from "./types";
|
|
10
10
|
import { Storage } from "@applicaster/zapp-react-native-bridge/ZappStorage/Storage";
|
|
11
11
|
|
|
@@ -43,10 +43,10 @@ function mapOwnedKeysToAdd(
|
|
|
43
43
|
): StorageOwnedValues {
|
|
44
44
|
const mappedData = {};
|
|
45
45
|
|
|
46
|
-
Object.keys(storageValues)
|
|
46
|
+
for (const namespace of Object.keys(storageValues)) {
|
|
47
47
|
const data = storageValues[namespace];
|
|
48
48
|
mappedData[namespace] = Object.keys(data);
|
|
49
|
-
}
|
|
49
|
+
}
|
|
50
50
|
|
|
51
51
|
return mappedData;
|
|
52
52
|
}
|
|
@@ -111,12 +111,12 @@ async function addOwnedKeys({
|
|
|
111
111
|
ownershipNamespace
|
|
112
112
|
);
|
|
113
113
|
|
|
114
|
-
Object.keys(newKeys)
|
|
114
|
+
for (const namespace of Object.keys(newKeys)) {
|
|
115
115
|
const newOwnedKeys: string[] = newKeys[namespace];
|
|
116
116
|
const currentKeys = allStoragedOwnedKeys[namespace] || [];
|
|
117
117
|
const combinedSet = new Set([...currentKeys, ...newOwnedKeys]);
|
|
118
118
|
allStoragedOwnedKeys[namespace] = Array.from(combinedSet);
|
|
119
|
-
}
|
|
119
|
+
}
|
|
120
120
|
|
|
121
121
|
const data = JSON.stringify(allStoragedOwnedKeys);
|
|
122
122
|
await localStorage.setItem(ownershipKey, data, ownershipNamespace);
|
|
@@ -136,18 +136,14 @@ async function removeOwnedKeys({
|
|
|
136
136
|
ownershipNamespace
|
|
137
137
|
);
|
|
138
138
|
|
|
139
|
-
Object.keys(toRemoveKeys)
|
|
139
|
+
for (const namespace of Object.keys(toRemoveKeys)) {
|
|
140
140
|
const keysToRemove: string[] = toRemoveKeys[namespace] || [];
|
|
141
141
|
const currentKeys: string[] = currentKeysList[namespace] || [];
|
|
142
142
|
|
|
143
143
|
const storageOwnedSet = new Set(currentKeys);
|
|
144
144
|
const keysToRemoveSet = new Set(keysToRemove);
|
|
145
145
|
|
|
146
|
-
|
|
147
|
-
toBeRemovedSet.forEach(Set.prototype.delete, originalSet);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
removeAll(storageOwnedSet, keysToRemoveSet);
|
|
146
|
+
keysToRemoveSet.forEach((key) => storageOwnedSet.delete(key));
|
|
151
147
|
const newLoginKeys = Array.from(storageOwnedSet);
|
|
152
148
|
|
|
153
149
|
if (isNilOrEmpty(newLoginKeys)) {
|
|
@@ -155,7 +151,7 @@ async function removeOwnedKeys({
|
|
|
155
151
|
} else {
|
|
156
152
|
currentKeysList[namespace] = newLoginKeys;
|
|
157
153
|
}
|
|
158
|
-
}
|
|
154
|
+
}
|
|
159
155
|
|
|
160
156
|
if (isNilOrEmpty(currentKeysList)) {
|
|
161
157
|
await localStorage.removeItem(ownershipKey, ownershipNamespace);
|