@odynn/awayz-hotels 0.2.5 → 0.2.7

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.
@@ -1,47 +1,47 @@
1
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;
2
+ import { EHotelSortOption as a, ESortingOrder as l } from "./useHotelSort.types.js";
3
+ const S = (r) => {
4
+ var n;
5
5
  return Math.round(
6
- ((o = n.cashBookingOffers) == null ? void 0 : o.cheapestRateTotalAmount) || n.cashValue.amount
6
+ ((n = r.cashBookingOffers) == null ? void 0 : n.cheapestRateTotalAmount) || r.cashValue.amount
7
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)
8
+ }, g = (r, t, n) => {
9
+ const u = S(r), e = S(t);
10
+ return u <= 0 ? 1 : n === l.DESCENDING ? e - u : u - e;
11
+ }, E = (r, t, n) => {
12
+ const u = r.hotelGroup == "other", e = t.hotelGroup == "other", p = r.chainRating != null, c = t.chainRating != null;
13
+ return !p && c ? 1 : p && !c ? -1 : u && !e ? 1 : !u && e ? -1 : n === l.DESCENDING ? t.chainRating - r.chainRating : r.chainRating - t.chainRating;
14
+ }, I = (r, t, n) => n === l.DESCENDING ? t.distance - r.distance : r.distance - t.distance, O = (r, t, n) => {
15
+ const u = r.points <= 0 ? Number.MAX_VALUE : r.points, e = t.points <= 0 ? Number.MAX_VALUE : t.points;
16
+ return n === l.DESCENDING ? e * t.hotelProgram.valueCent - u * r.hotelProgram.valueCent : u * r.hotelProgram.valueCent - e * t.hotelProgram.valueCent;
17
+ }, d = (r, t, n) => r.chainRating !== t.chainRating ? E(r, t, n) : I(r, t, l.ASCENDING), j = (r) => {
18
+ if (!r.hotelData)
19
19
  throw new Error("hotelData is required");
20
- if (!n.orderBy)
20
+ if (!r.orderBy)
21
21
  throw new Error("orderBy is required");
22
- if (!n.sortOrder)
22
+ if (!r.sortOrder)
23
23
  throw new Error("sortOrder is required");
24
- if (!Object.values(m).includes(n.orderBy))
24
+ if (!Object.values(a).includes(r.orderBy))
25
25
  throw new Error(
26
- `Invalid orderBy: ${n.orderBy}. Please use one of the following: ${Object.values(
27
- m
26
+ `Invalid orderBy: ${r.orderBy}. Please use one of the following: ${Object.values(
27
+ a
28
28
  ).join(", ")}`
29
29
  );
30
- if (!Object.values(a).includes(n.sortOrder))
30
+ if (!Object.values(l).includes(r.sortOrder))
31
31
  throw new Error(
32
- `Invalid sortOrder: ${n.sortOrder}. Please use one of the following: ${Object.values(a).join(
32
+ `Invalid sortOrder: ${r.sortOrder}. Please use one of the following: ${Object.values(l).join(
33
33
  ", "
34
34
  )}`
35
35
  );
36
36
  }, P = () => ({
37
37
  sortHotelResults: ({
38
- hotelData: o,
38
+ hotelData: n,
39
39
  orderBy: u,
40
- sortOrder: t
40
+ sortOrder: e
41
41
  }) => {
42
- O({ hotelData: o, orderBy: u, sortOrder: t });
43
- const l = () => {
44
- const { chainHotels: r, nonChainHotels: h } = o.reduce(
42
+ j({ hotelData: n, orderBy: u, sortOrder: e });
43
+ const p = () => {
44
+ const { chainHotels: o, nonChainHotels: h } = n.reduce(
45
45
  (s, i) => (N.isMajorChain(i.hotelGroup) ? s.chainHotels.push(i) : s.nonChainHotels.push(i), s),
46
46
  {
47
47
  chainHotels: [],
@@ -50,10 +50,10 @@ const H = (n) => {
50
50
  ), {
51
51
  majorChainsWithAccommodationIds: f,
52
52
  majorChainsWithoutAccommodationIds: C
53
- } = r.reduce(
53
+ } = o.reduce(
54
54
  (s, i) => {
55
- var E;
56
- return (E = i.cashBookingOffers) != null && E.accommodationId ? s.majorChainsWithAccommodationIds.push(i) : s.majorChainsWithoutAccommodationIds.push(i), s;
55
+ var A;
56
+ return (A = i.cashBookingOffers) != null && A.accommodationId ? s.majorChainsWithAccommodationIds.push(i) : s.majorChainsWithoutAccommodationIds.push(i), s;
57
57
  },
58
58
  {
59
59
  majorChainsWithAccommodationIds: [],
@@ -61,61 +61,67 @@ const H = (n) => {
61
61
  }
62
62
  );
63
63
  f.sort(
64
- (s, i) => p(s, i, t)
64
+ (s, i) => d(s, i, e)
65
65
  ), C.sort(
66
- (s, i) => p(s, i, t)
66
+ (s, i) => d(s, i, e)
67
67
  );
68
- const { luxuryHotels: A, otherProperties: S } = h.reduce(
68
+ const { luxuryHotels: H, otherProperties: R } = h.reduce(
69
69
  (s, i) => (i.hotelGroup !== "other" ? s.luxuryHotels.push(i) : s.otherProperties.push(i), s),
70
70
  {
71
71
  luxuryHotels: [],
72
72
  otherProperties: []
73
73
  }
74
74
  );
75
- return A.sort((s, i) => p(s, i, t)), S.sort((s, i) => p(s, i, t)), [
75
+ return H.sort((s, i) => d(s, i, e)), R.sort((s, i) => d(s, i, e)), [
76
76
  ...f,
77
77
  // available to book on Duffel
78
78
  ...C,
79
79
  // unavailable to book on Duffel
80
- ...A,
80
+ ...H,
81
81
  // luxury hotels
82
- ...S
82
+ ...R
83
83
  // other properties
84
84
  ];
85
85
  };
86
- let c = [], d = [];
86
+ let c = [], m = [];
87
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();
88
+ case a.CASH:
89
+ return c = n == null ? void 0 : n.filter((o) => o.cashValue.amount > 0).sort((o, h) => g(o, h, e)), m = n == null ? void 0 : n.filter(
90
+ (o) => o.cashValue.amount <= 0
91
+ ), c == null ? void 0 : c.concat(m);
92
+ case a.POINTS:
93
+ return c = n == null ? void 0 : n.filter((o) => o.points > 0).sort((o, h) => O(o, h, e)), m = n == null ? void 0 : n.filter((o) => o.points <= 0).sort((o, h) => g(o, h, e)), c == null ? void 0 : c.concat(m);
94
+ case a.STAR_RATING:
95
+ return c = n == null ? void 0 : n.filter(
96
+ (o) => o.cashValue.amount > 0 || o.points > 0
97
+ ).sort((o, h) => o.cashValue.amount <= 0 && o.points <= 0 ? 1 : o.chainRating === h.chainRating ? I(o, h, e) : E(o, h, e)), m = n == null ? void 0 : n.filter(
98
+ (o) => o.cashValue.amount <= 0 && o.points <= 0
99
+ ), c == null ? void 0 : c.concat(m);
100
+ case a.BOOKABLE:
101
+ return p();
102
102
  default:
103
- return l();
103
+ return p();
104
104
  }
105
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
- }
106
+ hotelSortOptions: () => {
107
+ const n = (u) => {
108
+ switch (u) {
109
+ case a.CASH:
110
+ return "Cash";
111
+ case a.POINTS:
112
+ return "Points";
113
+ case a.STAR_RATING:
114
+ return "Star Rating";
115
+ case a.BOOKABLE:
116
+ return "Bookable";
117
+ default:
118
+ return "Not specified";
119
+ }
120
+ };
121
+ return Object.values(a).map((u) => ({
122
+ value: u,
123
+ label: n(u)
124
+ }));
119
125
  }
120
126
  });
121
127
  export {
@@ -1,6 +1,45 @@
1
1
  import { IHotelDetails } from '../../main';
2
2
  import { EHotelSortOption, ISortHotelResultsParams } from './useHotelSort.types';
3
+ /**
4
+ * Custom hook for sorting hotel search results based on various criteria.
5
+ *
6
+ * @returns An object containing utility functions for hotel sorting:
7
+ * - sortHotelResults: Sorts an array of hotel data based on specified criteria
8
+ * - hotelSortOptionsToString: Converts sort option enum values to readable strings
9
+ *
10
+ * @example
11
+ * ```tsx
12
+ * const { sortHotelResults, hotelSortOptionsToString } = useHotelSort();
13
+ *
14
+ * // Sort hotels by price
15
+ * const sortedHotels = sortHotelResults({
16
+ * hotelData: hotels,
17
+ * orderBy: EHotelSortOption.CASH,
18
+ * sortOrder: ESortOrder.ASC
19
+ * });
20
+ *
21
+ * // Get display name for sort option
22
+ * const sortOptionName = hotelSortOptionsToString(EHotelSortOption.CASH); // "Cash"
23
+ * ```
24
+ *
25
+ * @remarks
26
+ * The `sortHotelResults` function supports multiple sorting strategies:
27
+ * - CASH: Sorts by cash value, placing unavailable hotels at the end
28
+ * - POINTS: Sorts by points value, placing unavailable hotels at the end
29
+ * - STAR_RATING: Sorts by chain rating and distance from city center
30
+ * - BOOKABLE: Uses a specialized algorithm that prioritizes:
31
+ * 1. Major chain hotels with accommodation IDs (bookable)
32
+ * 2. Major chain hotels without accommodation IDs (not bookable)
33
+ * 3. Luxury non-chain hotels
34
+ * 4. Other properties
35
+ *
36
+ * Within each category, additional tie-breakers like chain rating and
37
+ * distance to city center are applied based on the sort order.
38
+ */
3
39
  export declare const useHotelSort: () => {
4
40
  sortHotelResults: ({ hotelData, orderBy, sortOrder }: ISortHotelResultsParams) => IHotelDetails[];
5
- hotelSortOptionsToString: (type: EHotelSortOption) => "Bookable" | "Cash" | "Points" | "Star Rating" | "Not specified";
41
+ hotelSortOptions: () => {
42
+ value: EHotelSortOption;
43
+ label: string;
44
+ }[];
6
45
  };
@@ -2,6 +2,7 @@ import { IRoom, IRate } from './services/booking/BookingService.types';
2
2
  import { IHotelDetails } from './components/HotelResult/HotelResult.types';
3
3
  import { IPointsAsCash } from './utilities/pointsAsCashUtils';
4
4
  export { ESearchType } from './services/hotel/HotelService.types';
5
+ export { EHotelSortOption, ESortingOrder } from './hooks/useHotelSort/useHotelSort.types';
5
6
  export * from './components';
6
7
  export * from './hooks';
7
8
  export type { IRoom, IRate, IHotelDetails, IPointsAsCash };
package/dist/main.js CHANGED
@@ -1,16 +1,19 @@
1
1
  import { ESearchType as r } from "./services/hotel/HotelService.types.js";
2
- import { HotelResult as l } from "./components/HotelResult/HotelResult.js";
3
- import { HotelResults as s } from "./components/HotelResults/HotelResults.js";
4
- import { useHotelSearch as m } from "./hooks/useHotelSearch/useHotelSearch.js";
5
- import { useLocationSearch as u } from "./hooks/useLocationSearch/useLocationSearch.js";
6
- import { useHotelDetails as H } from "./hooks/useHotelDetails/useHotelDetails.js";
7
- import { useHotelSort as S } from "./hooks/useHotelSort/useHotelSort.js";
2
+ import { EHotelSortOption as p, ESortingOrder as l } from "./hooks/useHotelSort/useHotelSort.types.js";
3
+ import { HotelResult as m } from "./components/HotelResult/HotelResult.js";
4
+ import { HotelResults as x } from "./components/HotelResults/HotelResults.js";
5
+ import { useHotelSearch as H } from "./hooks/useHotelSearch/useHotelSearch.js";
6
+ import { useLocationSearch as a } from "./hooks/useLocationSearch/useLocationSearch.js";
7
+ import { useHotelDetails as i } from "./hooks/useHotelDetails/useHotelDetails.js";
8
+ import { useHotelSort as n } from "./hooks/useHotelSort/useHotelSort.js";
8
9
  export {
10
+ p as EHotelSortOption,
9
11
  r as ESearchType,
10
- l as HotelResult,
11
- s as HotelResults,
12
- H as useHotelDetails,
13
- m as useHotelSearch,
14
- S as useHotelSort,
15
- u as useLocationSearch
12
+ l as ESortingOrder,
13
+ m as HotelResult,
14
+ x as HotelResults,
15
+ i as useHotelDetails,
16
+ H as useHotelSearch,
17
+ n as useHotelSort,
18
+ a as useLocationSearch
16
19
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@odynn/awayz-hotels",
3
3
  "private": false,
4
- "version": "0.2.5",
4
+ "version": "0.2.7",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"