@applicaster/zapp-react-native-utils 15.0.0-rc.85 → 15.0.0-rc.86

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.
@@ -31,4 +31,42 @@ describe("imageSrcFromMediaItem", () => {
31
31
 
32
32
  expect(imageSrcFromMediaItem(badEntry, ["image_base"])).toBeUndefined();
33
33
  });
34
+
35
+ it("returns undefined when fallback is false and key is not found", () => {
36
+ const result = imageSrcFromMediaItem(entry as ZappEntry, [
37
+ "does_not_exist",
38
+ false,
39
+ ]);
40
+
41
+ expect(result).toBeUndefined();
42
+ });
43
+
44
+ it("returns src when fallback is false and key is found", () => {
45
+ const result = imageSrcFromMediaItem(entry as ZappEntry, [
46
+ "logo_thumbnail",
47
+ false,
48
+ ]);
49
+
50
+ expect(result).toEqual(entry.media_group[1].media_item[0].src);
51
+ });
52
+
53
+ it("returns image_base as fallback when fallback is explicitly true and key is not found", () => {
54
+ const result = imageSrcFromMediaItem(entry as ZappEntry, [
55
+ "does_not_exist",
56
+ true,
57
+ ]);
58
+
59
+ const fallback = entry.media_group[0].media_item[0];
60
+ expect(result).toEqual(fallback.src);
61
+ expect(fallback.key).toBe("image_base");
62
+ });
63
+
64
+ it("returns src when fallback is explicitly true and key is found", () => {
65
+ const result = imageSrcFromMediaItem(entry as ZappEntry, [
66
+ "logo_thumbnail",
67
+ true,
68
+ ]);
69
+
70
+ expect(result).toEqual(entry.media_group[1].media_item[0].src);
71
+ });
34
72
  });
@@ -155,9 +155,9 @@ export function getMediaItems(entry: ZappEntry): Option<ZappMediaItem[]> {
155
155
 
156
156
  /**
157
157
  * Retrieves the "src" value from a media item in the entry's media group,
158
- * based on a provided key, with fallback logic.
158
+ * based on a provided key, with optional fallback logic.
159
159
  *
160
- * Fallback order:
160
+ * Fallback order (when enabled):
161
161
  * 1. Attempts to find a media item with the specified key (or "image_base" if none provided).
162
162
  * 2. If not found, attempts to find a media item with the key "image_base".
163
163
  * 3. If still not found, falls back to the first available media item.
@@ -166,15 +166,19 @@ export function getMediaItems(entry: ZappEntry): Option<ZappMediaItem[]> {
166
166
  * since empty URIs are invalid in some platforms (e.g., React Native).
167
167
  *
168
168
  * @param {ZappEntry} entry - The entry object containing a media group.
169
- * @param {string[] | unknown} arg - A single-element array containing the key to look up, or any unknown value.
169
+ * @param {string[] | unknown} arg - Can be an array or any other value (treated as empty array).
170
+ * When an array:
171
+ * - First element: The key to look up. If omitted or undefined, defaults to "image_base".
172
+ * - Second element: Boolean to enable/disable fallback logic. If omitted or undefined, defaults to true.
170
173
  * @returns {?string} The "src" URI from the matched media item, or undefined if not found or empty.
171
174
  */
172
175
  export function imageSrcFromMediaItem(
173
176
  entry: ZappEntry,
174
177
  arg: string[] | unknown
175
178
  ): Option<string> {
176
- const args: unknown = R.unless(Array.isArray, Array)(arg || []);
179
+ const args: any = R.unless(Array.isArray, Array)(arg || []);
177
180
  const imageKey: string = args?.[0] || "image_base"; // always a single key in this function
181
+ const fallback: boolean = args?.[1] !== false;
178
182
 
179
183
  const mediaItems = getMediaItems(entry);
180
184
 
@@ -185,14 +189,16 @@ export function imageSrcFromMediaItem(
185
189
  // Try to find the item with the given key
186
190
  let foundItem = mediaItems.find((item) => item.key === imageKey);
187
191
 
188
- // If not found and key was not "image_base", try to find "image_base"
189
- if (!foundItem && imageKey !== "image_base") {
190
- foundItem = mediaItems.find((item) => item.key === "image_base");
191
- }
192
+ if (fallback) {
193
+ // If not found and key was not "image_base", try to find "image_base"
194
+ if (!foundItem && imageKey !== "image_base") {
195
+ foundItem = mediaItems.find((item) => item.key === "image_base");
196
+ }
192
197
 
193
- // If still not found, default to first item
194
- if (!foundItem) {
195
- foundItem = mediaItems[0];
198
+ // If still not found, default to first item
199
+ if (!foundItem) {
200
+ foundItem = mediaItems[0];
201
+ }
196
202
  }
197
203
 
198
204
  const src = foundItem?.src;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "15.0.0-rc.85",
3
+ "version": "15.0.0-rc.86",
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": "15.0.0-rc.85",
30
+ "@applicaster/applicaster-types": "15.0.0-rc.86",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",