@odynn/awayz-hotels 0.2.3 → 0.2.4

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.
@@ -0,0 +1,123 @@
1
+ import { HotelChains as N } from "../../definitions/HotelChains.js";
2
+ import { EHotelSortOption as m, ESortingOrder as a } from "./useHotelSort.types.js";
3
+ const H = (n) => {
4
+ var o;
5
+ return Math.round(
6
+ ((o = n.cashBookingOffers) == null ? void 0 : o.cheapestRateTotalAmount) || n.cashValue.amount
7
+ );
8
+ }, I = (n, e, o) => {
9
+ const u = H(n), t = H(e);
10
+ return u <= 0 ? 1 : o === a.DESCENDING ? t - u : u - t;
11
+ }, R = (n, e, o) => {
12
+ const u = n.hotelGroup == "other", t = e.hotelGroup == "other";
13
+ return u && !t ? 1 : !u && t ? -1 : o === a.DESCENDING ? e.chainRating - n.chainRating : n.chainRating - e.chainRating;
14
+ }, g = (n, e, o) => o === a.DESCENDING ? e.distance - n.distance : n.distance - e.distance, w = (n, e, o) => {
15
+ const u = n.points <= 0 ? Number.MAX_VALUE : n.points, t = e.points <= 0 ? Number.MAX_VALUE : e.points;
16
+ return o === a.DESCENDING ? t * e.hotelProgram.valueCent - u * n.hotelProgram.valueCent : u * n.hotelProgram.valueCent - t * e.hotelProgram.valueCent;
17
+ }, p = (n, e, o) => n.chainRating !== e.chainRating ? R(n, e, o) : g(n, e, a.ASCENDING), O = (n) => {
18
+ if (!n.hotelData)
19
+ throw new Error("hotelData is required");
20
+ if (!n.orderBy)
21
+ throw new Error("orderBy is required");
22
+ if (!n.sortOrder)
23
+ throw new Error("sortOrder is required");
24
+ if (!Object.values(m).includes(n.orderBy))
25
+ throw new Error(
26
+ `Invalid orderBy: ${n.orderBy}. Please use one of the following: ${Object.values(
27
+ m
28
+ ).join(", ")}`
29
+ );
30
+ if (!Object.values(a).includes(n.sortOrder))
31
+ throw new Error(
32
+ `Invalid sortOrder: ${n.sortOrder}. Please use one of the following: ${Object.values(a).join(
33
+ ", "
34
+ )}`
35
+ );
36
+ }, P = () => ({
37
+ sortHotelResults: ({
38
+ hotelData: o,
39
+ orderBy: u,
40
+ sortOrder: t
41
+ }) => {
42
+ O({ hotelData: o, orderBy: u, sortOrder: t });
43
+ const l = () => {
44
+ const { chainHotels: r, nonChainHotels: h } = o.reduce(
45
+ (s, i) => (N.isMajorChain(i.hotelGroup) ? s.chainHotels.push(i) : s.nonChainHotels.push(i), s),
46
+ {
47
+ chainHotels: [],
48
+ nonChainHotels: []
49
+ }
50
+ ), {
51
+ majorChainsWithAccommodationIds: f,
52
+ majorChainsWithoutAccommodationIds: C
53
+ } = r.reduce(
54
+ (s, i) => {
55
+ var E;
56
+ return (E = i.cashBookingOffers) != null && E.accommodationId ? s.majorChainsWithAccommodationIds.push(i) : s.majorChainsWithoutAccommodationIds.push(i), s;
57
+ },
58
+ {
59
+ majorChainsWithAccommodationIds: [],
60
+ majorChainsWithoutAccommodationIds: []
61
+ }
62
+ );
63
+ f.sort(
64
+ (s, i) => p(s, i, t)
65
+ ), C.sort(
66
+ (s, i) => p(s, i, t)
67
+ );
68
+ const { luxuryHotels: A, otherProperties: S } = h.reduce(
69
+ (s, i) => (i.hotelGroup !== "other" ? s.luxuryHotels.push(i) : s.otherProperties.push(i), s),
70
+ {
71
+ luxuryHotels: [],
72
+ otherProperties: []
73
+ }
74
+ );
75
+ return A.sort((s, i) => p(s, i, t)), S.sort((s, i) => p(s, i, t)), [
76
+ ...f,
77
+ // available to book on Duffel
78
+ ...C,
79
+ // unavailable to book on Duffel
80
+ ...A,
81
+ // luxury hotels
82
+ ...S
83
+ // other properties
84
+ ];
85
+ };
86
+ let c = [], d = [];
87
+ switch (u) {
88
+ case m.CASH:
89
+ return c = o == null ? void 0 : o.filter((r) => r.cashValue.amount > 0).sort((r, h) => I(r, h, t)), d = o == null ? void 0 : o.filter(
90
+ (r) => r.cashValue.amount <= 0
91
+ ), c == null ? void 0 : c.concat(d);
92
+ case m.POINTS:
93
+ return c = o == null ? void 0 : o.filter((r) => r.points > 0).sort((r, h) => w(r, h, t)), d = o == null ? void 0 : o.filter((r) => r.points <= 0).sort((r, h) => I(r, h, t)), c == null ? void 0 : c.concat(d);
94
+ case m.STAR_RATING:
95
+ return c = o == null ? void 0 : o.filter(
96
+ (r) => r.cashValue.amount > 0 || r.points > 0
97
+ ).sort((r, h) => r.cashValue.amount <= 0 && r.points <= 0 ? 1 : r.chainRating === h.chainRating ? g(r, h, t) : R(r, h, t)), d = o == null ? void 0 : o.filter(
98
+ (r) => r.cashValue.amount <= 0 && r.points <= 0
99
+ ), c == null ? void 0 : c.concat(d);
100
+ case m.BOOKABLE:
101
+ return l();
102
+ default:
103
+ return l();
104
+ }
105
+ },
106
+ hotelSortOptionsToString: (o) => {
107
+ switch (o) {
108
+ case m.CASH:
109
+ return "Cash";
110
+ case m.POINTS:
111
+ return "Points";
112
+ case m.STAR_RATING:
113
+ return "Star Rating";
114
+ case m.BOOKABLE:
115
+ return "Bookable";
116
+ default:
117
+ return "Not specified";
118
+ }
119
+ }
120
+ });
121
+ export {
122
+ P as useHotelSort
123
+ };
@@ -0,0 +1,5 @@
1
+ var N = /* @__PURE__ */ ((A) => (A.ASCENDING = "ASC", A.DESCENDING = "DESC", A))(N || {}), C = /* @__PURE__ */ ((A) => (A.CASH = "CASH", A.POINTS = "POINTS", A.STAR_RATING = "STAR_RATING", A.BOOKABLE = "BOOKABLE", A))(C || {});
2
+ export {
3
+ C as EHotelSortOption,
4
+ N as ESortingOrder
5
+ };
@@ -0,0 +1,6 @@
1
+ import { IHotelDetails } from '../../main';
2
+ import { EHotelSortOption, ISortHotelResultsParams } from './useHotelSort.types';
3
+ export declare const useHotelSort: () => {
4
+ sortHotelResults: ({ hotelData, orderBy, sortOrder }: ISortHotelResultsParams) => IHotelDetails[];
5
+ hotelSortOptionsToString: (type: EHotelSortOption) => "Bookable" | "Cash" | "Points" | "Star Rating" | "Not specified";
6
+ };
@@ -0,0 +1,16 @@
1
+ import { IHotelDetails } from '../../main';
2
+ export declare enum ESortingOrder {
3
+ ASCENDING = "ASC",
4
+ DESCENDING = "DESC"
5
+ }
6
+ export declare enum EHotelSortOption {
7
+ CASH = "CASH",
8
+ POINTS = "POINTS",
9
+ STAR_RATING = "STAR_RATING",
10
+ BOOKABLE = "BOOKABLE"
11
+ }
12
+ export interface ISortHotelResultsParams {
13
+ hotelData: IHotelDetails[];
14
+ orderBy: EHotelSortOption;
15
+ sortOrder: ESortingOrder;
16
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@odynn/awayz-hotels",
3
3
  "private": false,
4
- "version": "0.2.3",
4
+ "version": "0.2.4",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"