@revolugo/common 6.9.7-beta.4 → 6.9.7-beta.6
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/package.json +1 -1
- package/src/utils/images.ts +58 -0
- package/src/utils/index.ts +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Hotel, HotelImage, HotelImages } from '../types/index.ts'
|
|
2
|
+
|
|
3
|
+
export interface ImageUrls {
|
|
4
|
+
highres?: string[]
|
|
5
|
+
lowres?: string[]
|
|
6
|
+
thumbnails?: string[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function generateImageUrls(
|
|
10
|
+
images: HotelImages | undefined,
|
|
11
|
+
sizePriorityList: string[],
|
|
12
|
+
): string[] {
|
|
13
|
+
if (!images || !images.count) {
|
|
14
|
+
return []
|
|
15
|
+
}
|
|
16
|
+
// Choose the first available size
|
|
17
|
+
const existingSizePriority = sizePriorityList.find(
|
|
18
|
+
sizePriority => (images as { [key: string]: boolean })[sizePriority],
|
|
19
|
+
)
|
|
20
|
+
const size = existingSizePriority ? `${existingSizePriority}/` : ''
|
|
21
|
+
|
|
22
|
+
// Generate array of URLs
|
|
23
|
+
return Array.from(
|
|
24
|
+
{ length: images?.count || 0 },
|
|
25
|
+
(_, n) => `${images?.prefix}${size}${n}${images?.suffix}`,
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type ImageSize = 'm' | 'l' | 'xl'
|
|
30
|
+
|
|
31
|
+
function selectBestSize(
|
|
32
|
+
hotelImage: HotelImage,
|
|
33
|
+
sizePriorityList: ImageSize[],
|
|
34
|
+
): string {
|
|
35
|
+
return sizePriorityList
|
|
36
|
+
.map(sizePriority => hotelImage[sizePriority])
|
|
37
|
+
.find(Boolean) as string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const mapImagesUrls = (
|
|
41
|
+
hotelImages?: Hotel['hotelImages'],
|
|
42
|
+
): ImageUrls | null => {
|
|
43
|
+
if (hotelImages) {
|
|
44
|
+
return {
|
|
45
|
+
highres: hotelImages.map(hotelImage =>
|
|
46
|
+
selectBestSize(hotelImage, ['xl', 'l', 'm']),
|
|
47
|
+
),
|
|
48
|
+
lowres: hotelImages.map(hotelImage =>
|
|
49
|
+
selectBestSize(hotelImage, ['l', 'xl', 'm']),
|
|
50
|
+
),
|
|
51
|
+
thumbnails: hotelImages.map(hotelImage =>
|
|
52
|
+
selectBestSize(hotelImage, ['m', 'l', 'xl']),
|
|
53
|
+
),
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return null
|
|
58
|
+
}
|
package/src/utils/index.ts
CHANGED