@applicaster/zapp-react-native-utils 16.0.0-alpha.8525514228 → 16.0.0-alpha.8716141508
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/actionsExecutor/ActionExecutorContext.tsx +2 -0
- package/actionsExecutor/actions/index.ts +2 -0
- package/actionsExecutor/actions/refreshComponent.ts +8 -23
- package/actionsExecutor/actions/showToast.ts +31 -0
- package/actionsExecutor/types.ts +5 -3
- package/appUtils/contextKeysManager/__tests__/getKey/failure.test.ts +1 -1
- package/appUtils/contextKeysManager/__tests__/removeKey/failure.test.ts +1 -1
- package/appUtils/contextKeysManager/__tests__/setKey/failure/invalidKey.test.ts +4 -4
- package/appUtils/contextKeysManager/index.ts +1 -1
- package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +12 -6
- package/appUtils/playerManager/conts.ts +6 -0
- 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/bottomSheet/__tests__/scrollContext.test.tsx +35 -0
- package/bottomSheet/index.ts +17 -0
- package/manifestUtils/defaultManifestConfigurations/player.js +19 -19
- package/numberUtils/__tests__/isMinusZero.test.ts +20 -0
- package/numberUtils/__tests__/isZero.test.ts +27 -0
- package/numberUtils/__tests__/requireNumber.test.ts +50 -0
- package/numberUtils/__tests__/toNumber.test.ts +27 -0
- package/numberUtils/__tests__/toNumberWithDefault.test.ts +64 -0
- package/numberUtils/__tests__/toNumberWithDefaultZero.test.ts +48 -0
- package/numberUtils/index.ts +27 -8
- package/package.json +2 -2
- package/reactHooks/cell-click/index.ts +3 -3
- package/reactHooks/feed/__tests__/useInflatedUrl.test.tsx +25 -0
- package/reactHooks/feed/useInflatedUrl.ts +0 -1
- package/reactHooks/layout/index.ts +1 -1
- package/reactHooks/navigation/__mocks__/index.ts +4 -0
- package/reactHooks/navigation/__tests__/useIsScreenActive.test.ts +42 -0
- package/reactHooks/navigation/index.ts +1 -1
- package/reactHooks/navigation/useIsScreenActive.ts +29 -8
- package/reactHooks/state/useComponentScreenState.ts +1 -1
- package/stringUtils/__tests__/stringUtils.test.js +42 -0
- package/stringUtils/index.ts +2 -2
- package/zappFrameworkUtils/__tests__/localStorageHelper.test.ts +146 -0
- package/zappFrameworkUtils/localStorageHelper.ts +9 -13
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
sessionStorageSetAction,
|
|
31
31
|
sessionStorageToggleFlagAction,
|
|
32
32
|
switchLayoutAction,
|
|
33
|
+
showToastAction,
|
|
33
34
|
} from "./actions";
|
|
34
35
|
|
|
35
36
|
export const { log_error, log_info, log_debug } = createLogger({
|
|
@@ -89,6 +90,7 @@ const prepareDefaultActions = (actionExecutor) => {
|
|
|
89
90
|
|
|
90
91
|
actionExecutor.registerAction("screenSetVariable", screenSetVariableAction);
|
|
91
92
|
actionExecutor.registerAction("screenToggleFlag", screenToggleFlagAction);
|
|
93
|
+
actionExecutor.registerAction("showToast", showToastAction);
|
|
92
94
|
};
|
|
93
95
|
|
|
94
96
|
export const ActionExecutorContext =
|
|
@@ -14,20 +14,14 @@ const { log_info } = createLogger({
|
|
|
14
14
|
category: "General",
|
|
15
15
|
});
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
parent: ZappRiver | ZappUIComponent
|
|
23
|
-
): ZappRiver | ZappUIComponent | null {
|
|
24
|
-
for (const child of parent.ui_components) {
|
|
25
|
-
if (child.id === childId) return parent;
|
|
17
|
+
/** Prefer component data.source, otherwise direct UIComponentContext parent. */
|
|
18
|
+
function getComponentFeedData(component) {
|
|
19
|
+
if (component?.data?.source) {
|
|
20
|
+
return component.data;
|
|
21
|
+
}
|
|
26
22
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (found) return found;
|
|
30
|
-
}
|
|
23
|
+
if (component?.parent?.data?.source) {
|
|
24
|
+
return component.parent.data;
|
|
31
25
|
}
|
|
32
26
|
|
|
33
27
|
return null;
|
|
@@ -43,16 +37,7 @@ export const refreshComponentAction: ActionHandler = async (
|
|
|
43
37
|
): Promise<ActionResult> => {
|
|
44
38
|
const dispatch = appStore.getDispatch();
|
|
45
39
|
|
|
46
|
-
const
|
|
47
|
-
context?.component?.id,
|
|
48
|
-
context?.screenData
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
const componentSource = context?.component?.data?.source;
|
|
52
|
-
|
|
53
|
-
const componentData = componentSource
|
|
54
|
-
? context.component.data
|
|
55
|
-
: parentComponent?.data;
|
|
40
|
+
const componentData = getComponentFeedData(context?.component);
|
|
56
41
|
|
|
57
42
|
const source = componentData?.source;
|
|
58
43
|
const mapping = componentData?.mapping;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="@applicaster/applicaster-types" />
|
|
2
|
+
import { ActionResult } from "../ActionExecutor";
|
|
3
|
+
import { ActionHandler } from "../types";
|
|
4
|
+
import { postEvent } from "../../reactHooks/useSubscriberFor";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Options for showToast action.
|
|
8
|
+
*/
|
|
9
|
+
export interface ShowToastActionOptions {
|
|
10
|
+
id: string;
|
|
11
|
+
message: string;
|
|
12
|
+
extraMessage?: string;
|
|
13
|
+
style?: any;
|
|
14
|
+
timeout?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const showToastAction: ActionHandler<ShowToastActionOptions> = async (
|
|
18
|
+
action
|
|
19
|
+
): Promise<ActionResult> => {
|
|
20
|
+
postEvent("showToast", [
|
|
21
|
+
{
|
|
22
|
+
id: action.options.id,
|
|
23
|
+
message: action.options.message,
|
|
24
|
+
extraMessage: action.options.extraMessage,
|
|
25
|
+
style: action.options.style,
|
|
26
|
+
timeout: action.options.timeout,
|
|
27
|
+
},
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
return ActionResult.Success;
|
|
31
|
+
};
|
package/actionsExecutor/types.ts
CHANGED
|
@@ -16,8 +16,10 @@ export interface ActionExecutionContext {
|
|
|
16
16
|
/** The entry that defines the screen (if screen was opened from an entry) */
|
|
17
17
|
screenEntry?: ZappEntry;
|
|
18
18
|
|
|
19
|
-
/** The UI component that triggered the action
|
|
20
|
-
|
|
19
|
+
/** The UI component that triggered the action.
|
|
20
|
+
* From UIComponentContext as `{ ...component, parent }`.
|
|
21
|
+
*/
|
|
22
|
+
component?: ZappUIComponent & { parent?: ZappUIComponent };
|
|
21
23
|
|
|
22
24
|
/** The route identifier for the current screen */
|
|
23
25
|
screenRoute?: string;
|
|
@@ -31,7 +33,7 @@ export interface ActionExecutionContext {
|
|
|
31
33
|
/** Callback function for action completion */
|
|
32
34
|
callback?: (result: { success: boolean; error: any; abort: boolean }) => void;
|
|
33
35
|
|
|
34
|
-
screenState
|
|
36
|
+
screenState?: Record<any, any>;
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
/**
|
|
@@ -46,7 +46,7 @@ describe("Context Keys Manager - getKey", () => {
|
|
|
46
46
|
|
|
47
47
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
48
48
|
message:
|
|
49
|
-
"Provided key is not valid -
|
|
49
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
50
50
|
data: { key: invalidKey },
|
|
51
51
|
});
|
|
52
52
|
|
|
@@ -47,7 +47,7 @@ describe("Context Keys Manager - removeKey", () => {
|
|
|
47
47
|
|
|
48
48
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
49
49
|
message:
|
|
50
|
-
"Provided key is not valid -
|
|
50
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
51
51
|
data: { key },
|
|
52
52
|
});
|
|
53
53
|
|
|
@@ -48,7 +48,7 @@ describe("Context Keys Manager - setKey", () => {
|
|
|
48
48
|
|
|
49
49
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
50
50
|
message:
|
|
51
|
-
"Provided key is not valid -
|
|
51
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
52
52
|
data: { key },
|
|
53
53
|
});
|
|
54
54
|
|
|
@@ -105,7 +105,7 @@ describe("Context Keys Manager - setKey", () => {
|
|
|
105
105
|
|
|
106
106
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
107
107
|
message:
|
|
108
|
-
"Provided key is not valid -
|
|
108
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
109
109
|
data: { key },
|
|
110
110
|
});
|
|
111
111
|
|
|
@@ -162,7 +162,7 @@ describe("Context Keys Manager - setKey", () => {
|
|
|
162
162
|
|
|
163
163
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
164
164
|
message:
|
|
165
|
-
"Provided key is not valid -
|
|
165
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
166
166
|
data: { key },
|
|
167
167
|
});
|
|
168
168
|
|
|
@@ -217,7 +217,7 @@ describe("Context Keys Manager - setKey", () => {
|
|
|
217
217
|
|
|
218
218
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
219
219
|
message:
|
|
220
|
-
"Provided key is not valid -
|
|
220
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
221
221
|
data: { key },
|
|
222
222
|
});
|
|
223
223
|
|
|
@@ -109,7 +109,7 @@ export class ContextKeysManager {
|
|
|
109
109
|
if (!keyIsValid(parsedKey)) {
|
|
110
110
|
this._logger.warn({
|
|
111
111
|
message:
|
|
112
|
-
"Provided key is not valid -
|
|
112
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
113
113
|
data: { key },
|
|
114
114
|
});
|
|
115
115
|
|
|
@@ -59,6 +59,7 @@ type PlayNextConfig = {
|
|
|
59
59
|
export type PlayNextState = PlayNextConfig & {
|
|
60
60
|
triggerTime: number;
|
|
61
61
|
handleUserCancelPlayNext: () => void;
|
|
62
|
+
hidePlayNext: () => void;
|
|
62
63
|
};
|
|
63
64
|
|
|
64
65
|
export class OverlaysObserver {
|
|
@@ -76,7 +77,7 @@ export class OverlaysObserver {
|
|
|
76
77
|
private chapterMarkerEvents: ChapterMarkerEvent[];
|
|
77
78
|
readonly player: Player;
|
|
78
79
|
private playNextConfig?: PlayNextConfig;
|
|
79
|
-
private
|
|
80
|
+
private isPlayNextSuppressed = false;
|
|
80
81
|
|
|
81
82
|
constructor({ player }: ChapterMarkersObserverProps) {
|
|
82
83
|
this.chapterSubject = new BehaviorSubject(null);
|
|
@@ -143,11 +144,15 @@ export class OverlaysObserver {
|
|
|
143
144
|
return this.titleSummarySubject.asObservable().pipe(distinctUntilChanged());
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
|
|
147
|
-
this.
|
|
147
|
+
hidePlayNext = () => {
|
|
148
|
+
this.isPlayNextSuppressed = true;
|
|
148
149
|
this.playNextSubject.next(null);
|
|
149
150
|
};
|
|
150
151
|
|
|
152
|
+
handleUserCancelPlayNext = () => {
|
|
153
|
+
this.hidePlayNext();
|
|
154
|
+
};
|
|
155
|
+
|
|
151
156
|
preparePlayNext = async () => {
|
|
152
157
|
try {
|
|
153
158
|
const plugins = appStore.get("plugins");
|
|
@@ -270,7 +275,7 @@ export class OverlaysObserver {
|
|
|
270
275
|
|
|
271
276
|
// TODO: Hack for video end, will be replaced with playlist prev/next in the future
|
|
272
277
|
getPlayNextEntry = () =>
|
|
273
|
-
!this.
|
|
278
|
+
!this.isPlayNextSuppressed ? this.playNextConfig?.entry : null;
|
|
274
279
|
|
|
275
280
|
prepareChapterMarkers = () => {
|
|
276
281
|
const chapterMarkers: ChapterMarkerOriginal[] =
|
|
@@ -353,7 +358,7 @@ export class OverlaysObserver {
|
|
|
353
358
|
const shouldPlayNextBeVisible = currentTime > triggerTime;
|
|
354
359
|
|
|
355
360
|
if (shouldPlayNextBeVisible) {
|
|
356
|
-
if (this.
|
|
361
|
+
if (this.isPlayNextSuppressed) {
|
|
357
362
|
return;
|
|
358
363
|
}
|
|
359
364
|
|
|
@@ -361,10 +366,11 @@ export class OverlaysObserver {
|
|
|
361
366
|
...this.playNextConfig,
|
|
362
367
|
triggerTime,
|
|
363
368
|
handleUserCancelPlayNext: this.handleUserCancelPlayNext,
|
|
369
|
+
hidePlayNext: this.hidePlayNext,
|
|
364
370
|
});
|
|
365
371
|
} else {
|
|
366
372
|
this.playNextSubject.next(null);
|
|
367
|
-
this.
|
|
373
|
+
this.isPlayNextSuppressed = false;
|
|
368
374
|
}
|
|
369
375
|
};
|
|
370
376
|
|
|
@@ -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
|
+
});
|