@applicaster/zapp-react-native-utils 16.0.0-alpha.9530606740 → 16.0.0-alpha.9572544619

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.
Files changed (50) hide show
  1. package/actionsExecutor/ActionExecutorContext.tsx +2 -2
  2. package/actionsExecutor/actions/index.ts +1 -1
  3. package/actionsExecutor/actions/showToast.ts +31 -0
  4. package/actionsExecutor/types.ts +1 -1
  5. package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +12 -6
  6. package/appUtils/playerManager/conts.ts +6 -0
  7. package/arrayUtils/__tests__/{anyThruthy.test.ts → anyTruthy.test.ts} +8 -0
  8. package/arrayUtils/__tests__/arrayUtils.test.ts +165 -108
  9. package/arrayUtils/__tests__/isEmptyArray.test.ts +11 -0
  10. package/arrayUtils/__tests__/isFilledArray.test.ts +12 -0
  11. package/arrayUtils/__tests__/isFirst.test.ts +17 -0
  12. package/arrayUtils/__tests__/isIndexInRange.test.ts +14 -0
  13. package/arrayUtils/__tests__/isLast.test.ts +31 -0
  14. package/arrayUtils/__tests__/makeListOf.test.ts +31 -0
  15. package/arrayUtils/__tests__/makeListOfIndexes.test.ts +20 -0
  16. package/arrayUtils/__tests__/sample.test.ts +61 -0
  17. package/arrayUtils/index.ts +57 -20
  18. package/bottomSheet/__tests__/scrollContext.test.tsx +35 -0
  19. package/bottomSheet/index.ts +17 -0
  20. package/manifestUtils/defaultManifestConfigurations/player.js +19 -19
  21. package/modalState/index.ts +74 -25
  22. package/numberUtils/__tests__/isMinusZero.test.ts +20 -0
  23. package/numberUtils/__tests__/isZero.test.ts +27 -0
  24. package/numberUtils/__tests__/requireNumber.test.ts +50 -0
  25. package/numberUtils/__tests__/toNumber.test.ts +27 -0
  26. package/numberUtils/__tests__/toNumberWithDefault.test.ts +64 -0
  27. package/numberUtils/__tests__/toNumberWithDefaultZero.test.ts +48 -0
  28. package/numberUtils/index.ts +27 -8
  29. package/package.json +2 -2
  30. package/reactHooks/feed/__tests__/useInflatedUrl.test.tsx +25 -0
  31. package/reactHooks/layout/index.ts +1 -1
  32. package/reactHooks/navigation/__mocks__/index.ts +4 -0
  33. package/reactHooks/navigation/__tests__/useIsScreenActive.test.ts +42 -0
  34. package/reactHooks/navigation/index.ts +1 -1
  35. package/reactHooks/navigation/useIsScreenActive.ts +29 -8
  36. package/reactHooks/state/useComponentScreenState.ts +1 -1
  37. package/stringUtils/__tests__/stringUtils.test.js +42 -0
  38. package/stringUtils/index.ts +2 -2
  39. package/zappFrameworkUtils/__tests__/localStorageHelper.test.ts +146 -0
  40. package/zappFrameworkUtils/localStorageHelper.ts +9 -13
  41. package/actionsExecutor/actions/openBottomSheet.ts +0 -177
  42. package/modalState/ContentViewModel.ts +0 -59
  43. package/modalState/ModalOrchestrator.ts +0 -204
  44. package/modalState/__tests__/ContentViewModel.test.ts +0 -107
  45. package/modalState/components/ActionItem.tsx +0 -155
  46. package/modalState/components/BottomSheetHeader.tsx +0 -245
  47. package/modalState/components/PrimaryButton.tsx +0 -54
  48. package/modalState/components/TrackItem.tsx +0 -241
  49. package/modalState/store.ts +0 -102
  50. package/modalState/types.ts +0 -55
@@ -22,7 +22,6 @@ import {
22
22
  localStorageRemoveAction,
23
23
  localStorageSetAction,
24
24
  localStorageToggleFlagAction,
25
- openBottomSheetAction,
26
25
  refreshComponentAction,
27
26
  screenSetVariableAction,
28
27
  screenToggleFlagAction,
@@ -31,6 +30,7 @@ import {
31
30
  sessionStorageSetAction,
32
31
  sessionStorageToggleFlagAction,
33
32
  switchLayoutAction,
33
+ showToastAction,
34
34
  } from "./actions";
35
35
 
36
36
  export const { log_error, log_info, log_debug } = createLogger({
@@ -90,7 +90,7 @@ const prepareDefaultActions = (actionExecutor) => {
90
90
 
91
91
  actionExecutor.registerAction("screenSetVariable", screenSetVariableAction);
92
92
  actionExecutor.registerAction("screenToggleFlag", screenToggleFlagAction);
93
- actionExecutor.registerAction("openBottomSheet", openBottomSheetAction);
93
+ actionExecutor.registerAction("showToast", showToastAction);
94
94
  };
95
95
 
96
96
  export const ActionExecutorContext =
@@ -27,4 +27,4 @@ export { screenToggleFlagAction } from "./screenToggleFlag";
27
27
 
28
28
  export { createNavigateToScreenAction } from "./navigateToScreen";
29
29
 
30
- export { openBottomSheetAction } from "./openBottomSheet";
30
+ export { showToastAction } from "./showToast";
@@ -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
+ };
@@ -33,7 +33,7 @@ export interface ActionExecutionContext {
33
33
  /** Callback function for action completion */
34
34
  callback?: (result: { success: boolean; error: any; abort: boolean }) => void;
35
35
 
36
- screenState: Record<any, any>;
36
+ screenState?: Record<any, any>;
37
37
  }
38
38
 
39
39
  /**
@@ -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 isCanceledByUser = false;
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
- handleUserCancelPlayNext = () => {
147
- this.isCanceledByUser = true;
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.isCanceledByUser ? this.playNextConfig?.entry : null;
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.isCanceledByUser) {
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.isCanceledByUser = false;
373
+ this.isPlayNextSuppressed = false;
368
374
  }
369
375
  };
370
376
 
@@ -67,3 +67,9 @@ export enum PlayerRole {
67
67
  Chromecast = "Chromecast",
68
68
  Unspecified = "Unspecified",
69
69
  }
70
+
71
+ export enum PLAYER_EVENTS {
72
+ PlayNextTriggered = "play_next_triggered",
73
+ }
74
+
75
+ export type PlayNextTriggeredEvent = { id: string };
@@ -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
- describe("removeItemFromList", () => {
90
- it("removes an item from a list", () => {
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
- const arrayOfObjects = [
95
- { prop: "value1" },
96
- { prop: "value2" },
97
- { prop: "value3" },
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
- expect(removeItemFromList({ prop: "value2" }, arrayOfObjects)).toEqual([
101
- { prop: "value1" },
102
- { prop: "value3" },
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
- it("leaves the list as is if the item is not found", () => {
107
- const arrayOfObjects = [
108
- { prop: "value1" },
109
- { prop: "value2" },
110
- { prop: "value3" },
111
- ];
157
+ await expect(mapPromises(failingFn, [1, 2, 3])).rejects.toThrow("fail");
158
+ });
159
+ });
112
160
 
113
- expect(removeItemFromList({ prop: "value4" }, arrayOfObjects)).toEqual(
114
- arrayOfObjects
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
- describe("mapPromises", () => {
120
- const promiseFn = jest.fn((num) => Promise.resolve(num + 1));
171
+ beforeEach(() => {
172
+ promiseFn.mockClear();
173
+ });
121
174
 
122
- beforeEach(() => {
123
- promiseFn.mockClear();
124
- });
175
+ it("is a curried function; and returns the reduced promise values", async () => {
176
+ const promiseReducer = reducePromises(promiseFn);
125
177
 
126
- it("is a curried function, and returns an array of resolved values", async () => {
127
- const promiseMapper = mapPromises(promiseFn);
128
- const values = [1, 2, 3];
178
+ const initialValue = 0;
179
+ const values = [1, 2, 3];
180
+ const sumOfValues = initialValue + values.reduce((a, b) => a + b, 0);
129
181
 
130
- expect(typeof promiseMapper).toBe("function");
131
- await expect(promiseMapper(values)).resolves.toEqual([2, 3, 4]);
132
- });
182
+ expect(typeof promiseReducer).toBe("function");
133
183
 
134
- it("calls all the promises immediately", () => {
135
- const values = [1, 2, 3];
184
+ await expect(promiseReducer(initialValue, values)).resolves.toEqual(
185
+ sumOfValues
186
+ );
187
+ });
136
188
 
137
- mapPromises(promiseFn, values);
138
- expect(promiseFn).toHaveBeenCalledTimes(values.length);
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
- values.forEach((value) => {
141
- expect(promiseFn).toHaveBeenNthCalledWith(value, value, value - 1);
142
- });
212
+ expect.assertions(values.length * 2);
213
+ });
143
214
 
144
- expect.assertions(values.length + 1);
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
- describe("reduce promises", () => {
149
- const promiseFn = jest.fn((current, previous, index, cb) => {
150
- return new Promise((resolve) => {
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
- beforeEach(() => {
159
- promiseFn.mockClear();
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
- it("is a curried function; and returns the reduced promise values", async () => {
163
- const promiseReducer = reducePromises(promiseFn);
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
- const initialValue = 0;
166
- const values = [1, 2, 3];
167
- const sumOfValues = initialValue + values.reduce((a, b) => a + b, 0);
245
+ const getLetterForInt = (num) => alphabet[num];
246
+ const chunkSize = 5;
168
247
 
169
- expect(typeof promiseReducer).toBe("function");
248
+ const result = mapAndSplit<number, string>(
249
+ getLetterForInt,
250
+ chunkSize
251
+ )(values);
170
252
 
171
- await expect(promiseReducer(initialValue, values)).resolves.toEqual(
172
- sumOfValues
173
- );
174
- });
253
+ expect(result.length).toBe(values.length / chunkSize);
175
254
 
176
- it("calls promises one after the other", (done) => {
177
- const values = [1, 2, 3];
178
- const previousValues = [0, 1, 3];
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
- describe("mapAndSplit", () => {
204
- it("maps over a list of values and split it in chunks", () => {
205
- const values = new Array(20).fill(0).map((_, i) => i);
262
+ it("is a curried function", () => {
263
+ const mapper = (n: number) => n * 2;
264
+ const splitBy2 = mapAndSplit(mapper, 2);
206
265
 
207
- const alphabet = Array.from(Array(26)).map((_, i) =>
208
- String.fromCharCode(i + 65)
209
- );
266
+ expect(typeof splitBy2).toBe("function");
210
267
 
211
- const getLetterForInt = (num) => alphabet[num];
212
- const chunkSize = 5;
268
+ expect(splitBy2([1, 2, 3, 4])).toEqual([
269
+ [2, 4],
270
+ [6, 8],
271
+ ]);
272
+ });
213
273
 
214
- const result = mapAndSplit<number, string>(
215
- getLetterForInt,
216
- chunkSize
217
- )(values);
274
+ it("returns an empty list for an empty input", () => {
275
+ expect(mapAndSplit((n: number) => n, 2)([])).toEqual([]);
276
+ });
218
277
 
219
- expect(result.length).toBe(values.length / chunkSize);
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
- result.forEach((chunk, chunkIndex) => {
222
- chunk.forEach((value, index) => {
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
+ });