@open-tender/utils 0.4.35 → 0.4.36

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 (246) hide show
  1. package/LICENSE.md +21 -21
  2. package/README.md +2 -2
  3. package/dist/.DS_Store +0 -0
  4. package/dist/cjs/__tests__/utils/helpers.test.d.ts +1 -1
  5. package/dist/cjs/__tests__/utils/helpers.test.js +96 -96
  6. package/dist/cjs/__tests__/utils/theme.test.d.ts +1 -1
  7. package/dist/cjs/__tests__/utils/theme.test.js +37 -37
  8. package/dist/cjs/hooks/index.d.ts +35 -35
  9. package/dist/cjs/hooks/index.js +38 -38
  10. package/dist/cjs/hooks/types.d.ts +4 -4
  11. package/dist/cjs/hooks/types.js +2 -2
  12. package/dist/cjs/hooks/useAccordion.d.ts +22 -22
  13. package/dist/cjs/hooks/useAccordion.js +56 -56
  14. package/dist/cjs/hooks/useAddressForm.d.ts +12 -12
  15. package/dist/cjs/hooks/useAddressForm.js +83 -83
  16. package/dist/cjs/hooks/useAllergenForm.d.ts +10 -10
  17. package/dist/cjs/hooks/useAllergenForm.js +58 -58
  18. package/dist/cjs/hooks/useBuilder.d.ts +14 -14
  19. package/dist/cjs/hooks/useBuilder.js +144 -144
  20. package/dist/cjs/hooks/useCartGuestForm.d.ts +12 -12
  21. package/dist/cjs/hooks/useCartGuestForm.js +58 -58
  22. package/dist/cjs/hooks/useCartPoints.d.ts +13 -13
  23. package/dist/cjs/hooks/useCartPoints.js +74 -74
  24. package/dist/cjs/hooks/useCommunicationPreferences.js +1 -1
  25. package/dist/cjs/hooks/useCreditCard.d.ts +13 -13
  26. package/dist/cjs/hooks/useCreditCard.js +63 -63
  27. package/dist/cjs/hooks/useCreditCardForm.d.ts +10 -10
  28. package/dist/cjs/hooks/useCreditCardForm.js +71 -71
  29. package/dist/cjs/hooks/useCurbside.d.ts +9 -9
  30. package/dist/cjs/hooks/useCurbside.js +39 -39
  31. package/dist/cjs/hooks/useDineInForm.d.ts +11 -11
  32. package/dist/cjs/hooks/useDineInForm.js +56 -56
  33. package/dist/cjs/hooks/useDonationForm.d.ts +28 -28
  34. package/dist/cjs/hooks/useDonationForm.js +172 -172
  35. package/dist/cjs/hooks/useFirstTimes.d.ts +18 -18
  36. package/dist/cjs/hooks/useFirstTimes.js +81 -81
  37. package/dist/cjs/hooks/useGiftCardAssignForm.d.ts +14 -14
  38. package/dist/cjs/hooks/useGiftCardAssignForm.js +57 -57
  39. package/dist/cjs/hooks/useGiftCardAssignOtherForm.d.ts +14 -14
  40. package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +50 -50
  41. package/dist/cjs/hooks/useGiftCardForm.d.ts +12 -12
  42. package/dist/cjs/hooks/useGiftCardForm.js +74 -74
  43. package/dist/cjs/hooks/useGiftCardsForm.d.ts +31 -31
  44. package/dist/cjs/hooks/useGiftCardsForm.js +195 -195
  45. package/dist/cjs/hooks/useGuestCheckoutForm.d.ts +21 -21
  46. package/dist/cjs/hooks/useGuestCheckoutForm.js +89 -89
  47. package/dist/cjs/hooks/useGuestForm.d.ts +12 -12
  48. package/dist/cjs/hooks/useGuestForm.js +60 -60
  49. package/dist/cjs/hooks/useImage.d.ts +5 -5
  50. package/dist/cjs/hooks/useImage.js +33 -33
  51. package/dist/cjs/hooks/useLoginForm.d.ts +12 -12
  52. package/dist/cjs/hooks/useLoginForm.js +67 -67
  53. package/dist/cjs/hooks/useMenuItem.d.ts +57 -57
  54. package/dist/cjs/hooks/useMenuItem.js +67 -67
  55. package/dist/cjs/hooks/useModifier.d.ts +53 -53
  56. package/dist/cjs/hooks/useModifier.js +72 -72
  57. package/dist/cjs/hooks/useModifierGroup.d.ts +15 -15
  58. package/dist/cjs/hooks/useModifierGroup.js +17 -17
  59. package/dist/cjs/hooks/useOneTimePasscodeForm.d.ts +16 -16
  60. package/dist/cjs/hooks/useOneTimePasscodeForm.js +87 -87
  61. package/dist/cjs/hooks/useOrderFulfillmentForm.d.ts +11 -11
  62. package/dist/cjs/hooks/useOrderFulfillmentForm.js +65 -65
  63. package/dist/cjs/hooks/useOrderItem.d.ts +48 -48
  64. package/dist/cjs/hooks/useOrderItem.js +54 -54
  65. package/dist/cjs/hooks/useOrderRating.d.ts +10 -10
  66. package/dist/cjs/hooks/useOrderRating.js +34 -34
  67. package/dist/cjs/hooks/useOrderRatingForm.d.ts +11 -11
  68. package/dist/cjs/hooks/useOrderRatingForm.js +35 -35
  69. package/dist/cjs/hooks/useProfileForm.d.ts +15 -15
  70. package/dist/cjs/hooks/useProfileForm.js +116 -116
  71. package/dist/cjs/hooks/useResetPasswordForm.d.ts +12 -12
  72. package/dist/cjs/hooks/useResetPasswordForm.js +77 -77
  73. package/dist/cjs/hooks/useSendResetForm.d.ts +14 -14
  74. package/dist/cjs/hooks/useSendResetForm.js +57 -57
  75. package/dist/cjs/hooks/useSignUpForm.d.ts +20 -20
  76. package/dist/cjs/hooks/useSignUpForm.js +151 -157
  77. package/dist/cjs/hooks/useSignUpGuestForm.d.ts +23 -23
  78. package/dist/cjs/hooks/useSignUpGuestForm.js +113 -124
  79. package/dist/cjs/hooks/useSigninForm.d.ts +12 -12
  80. package/dist/cjs/hooks/useSigninForm.js +66 -66
  81. package/dist/cjs/index.d.ts +2 -2
  82. package/dist/cjs/index.js +5 -5
  83. package/dist/cjs/utils/cards.d.ts +20 -20
  84. package/dist/cjs/utils/cards.js +155 -155
  85. package/dist/cjs/utils/cart.d.ts +202 -202
  86. package/dist/cjs/utils/cart.js +909 -909
  87. package/dist/cjs/utils/checkout.d.ts +55 -55
  88. package/dist/cjs/utils/checkout.js +125 -125
  89. package/dist/cjs/utils/constants.d.ts +43 -43
  90. package/dist/cjs/utils/constants.js +58 -58
  91. package/dist/cjs/utils/customer.d.ts +4 -4
  92. package/dist/cjs/utils/customer.js +34 -34
  93. package/dist/cjs/utils/datetimes.d.ts +99 -99
  94. package/dist/cjs/utils/datetimes.js +601 -601
  95. package/dist/cjs/utils/errors.d.ts +51 -51
  96. package/dist/cjs/utils/errors.js +191 -191
  97. package/dist/cjs/utils/favorites.d.ts +10 -10
  98. package/dist/cjs/utils/favorites.js +119 -119
  99. package/dist/cjs/utils/googleMaps.d.ts +14 -14
  100. package/dist/cjs/utils/googleMaps.js +30 -30
  101. package/dist/cjs/utils/helpers.d.ts +63 -63
  102. package/dist/cjs/utils/helpers.js +244 -244
  103. package/dist/cjs/utils/index.d.ts +19 -19
  104. package/dist/cjs/utils/index.js +23 -23
  105. package/dist/cjs/utils/keypad.d.ts +9 -9
  106. package/dist/cjs/utils/keypad.js +105 -105
  107. package/dist/cjs/utils/maps.d.ts +226 -226
  108. package/dist/cjs/utils/maps.js +334 -334
  109. package/dist/cjs/utils/menu.d.ts +117 -117
  110. package/dist/cjs/utils/menu.js +82 -82
  111. package/dist/cjs/utils/orders.d.ts +21 -21
  112. package/dist/cjs/utils/orders.js +202 -202
  113. package/dist/cjs/utils/pizzaBuilder.d.ts +10 -10
  114. package/dist/cjs/utils/pizzaBuilder.js +43 -43
  115. package/dist/cjs/utils/refunds.d.ts +35 -35
  116. package/dist/cjs/utils/refunds.js +98 -98
  117. package/dist/cjs/utils/revenueCenters.d.ts +11 -11
  118. package/dist/cjs/utils/revenueCenters.js +125 -125
  119. package/dist/cjs/utils/rewards.d.ts +4 -4
  120. package/dist/cjs/utils/rewards.js +100 -100
  121. package/dist/cjs/utils/theme.d.ts +27 -27
  122. package/dist/cjs/utils/theme.js +266 -266
  123. package/dist/cjs/utils/version.d.ts +4 -4
  124. package/dist/cjs/utils/version.js +38 -38
  125. package/dist/esm/__tests__/utils/helpers.test.d.ts +1 -1
  126. package/dist/esm/__tests__/utils/helpers.test.js +94 -94
  127. package/dist/esm/__tests__/utils/theme.test.d.ts +1 -1
  128. package/dist/esm/__tests__/utils/theme.test.js +35 -35
  129. package/dist/esm/hooks/index.d.ts +35 -35
  130. package/dist/esm/hooks/index.js +35 -35
  131. package/dist/esm/hooks/types.d.ts +4 -4
  132. package/dist/esm/hooks/types.js +1 -1
  133. package/dist/esm/hooks/useAccordion.d.ts +22 -22
  134. package/dist/esm/hooks/useAccordion.js +52 -52
  135. package/dist/esm/hooks/useAddressForm.d.ts +12 -12
  136. package/dist/esm/hooks/useAddressForm.js +79 -79
  137. package/dist/esm/hooks/useAllergenForm.d.ts +10 -10
  138. package/dist/esm/hooks/useAllergenForm.js +54 -54
  139. package/dist/esm/hooks/useBuilder.d.ts +14 -14
  140. package/dist/esm/hooks/useBuilder.js +140 -140
  141. package/dist/esm/hooks/useCartGuestForm.d.ts +12 -12
  142. package/dist/esm/hooks/useCartGuestForm.js +54 -54
  143. package/dist/esm/hooks/useCartPoints.d.ts +13 -13
  144. package/dist/esm/hooks/useCartPoints.js +70 -70
  145. package/dist/esm/hooks/useCommunicationPreferences.js +1 -1
  146. package/dist/esm/hooks/useCreditCard.d.ts +13 -13
  147. package/dist/esm/hooks/useCreditCard.js +59 -59
  148. package/dist/esm/hooks/useCreditCardForm.d.ts +10 -10
  149. package/dist/esm/hooks/useCreditCardForm.js +67 -67
  150. package/dist/esm/hooks/useCurbside.d.ts +9 -9
  151. package/dist/esm/hooks/useCurbside.js +35 -35
  152. package/dist/esm/hooks/useDineInForm.d.ts +11 -11
  153. package/dist/esm/hooks/useDineInForm.js +52 -52
  154. package/dist/esm/hooks/useDonationForm.d.ts +28 -28
  155. package/dist/esm/hooks/useDonationForm.js +168 -168
  156. package/dist/esm/hooks/useFirstTimes.d.ts +18 -18
  157. package/dist/esm/hooks/useFirstTimes.js +77 -77
  158. package/dist/esm/hooks/useGiftCardAssignForm.d.ts +14 -14
  159. package/dist/esm/hooks/useGiftCardAssignForm.js +53 -53
  160. package/dist/esm/hooks/useGiftCardAssignOtherForm.d.ts +14 -14
  161. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +46 -46
  162. package/dist/esm/hooks/useGiftCardForm.d.ts +12 -12
  163. package/dist/esm/hooks/useGiftCardForm.js +70 -70
  164. package/dist/esm/hooks/useGiftCardsForm.d.ts +31 -31
  165. package/dist/esm/hooks/useGiftCardsForm.js +191 -191
  166. package/dist/esm/hooks/useGuestCheckoutForm.d.ts +21 -21
  167. package/dist/esm/hooks/useGuestCheckoutForm.js +85 -85
  168. package/dist/esm/hooks/useGuestForm.d.ts +12 -12
  169. package/dist/esm/hooks/useGuestForm.js +56 -56
  170. package/dist/esm/hooks/useImage.d.ts +5 -5
  171. package/dist/esm/hooks/useImage.js +29 -29
  172. package/dist/esm/hooks/useLoginForm.d.ts +12 -12
  173. package/dist/esm/hooks/useLoginForm.js +63 -63
  174. package/dist/esm/hooks/useMenuItem.d.ts +57 -57
  175. package/dist/esm/hooks/useMenuItem.js +63 -63
  176. package/dist/esm/hooks/useModifier.d.ts +53 -53
  177. package/dist/esm/hooks/useModifier.js +68 -68
  178. package/dist/esm/hooks/useModifierGroup.d.ts +15 -15
  179. package/dist/esm/hooks/useModifierGroup.js +13 -13
  180. package/dist/esm/hooks/useOneTimePasscodeForm.d.ts +16 -16
  181. package/dist/esm/hooks/useOneTimePasscodeForm.js +83 -83
  182. package/dist/esm/hooks/useOrderFulfillmentForm.d.ts +11 -11
  183. package/dist/esm/hooks/useOrderFulfillmentForm.js +61 -61
  184. package/dist/esm/hooks/useOrderItem.d.ts +48 -48
  185. package/dist/esm/hooks/useOrderItem.js +50 -50
  186. package/dist/esm/hooks/useOrderRating.d.ts +10 -10
  187. package/dist/esm/hooks/useOrderRating.js +30 -30
  188. package/dist/esm/hooks/useOrderRatingForm.d.ts +11 -11
  189. package/dist/esm/hooks/useOrderRatingForm.js +31 -31
  190. package/dist/esm/hooks/useProfileForm.d.ts +15 -15
  191. package/dist/esm/hooks/useProfileForm.js +112 -112
  192. package/dist/esm/hooks/useResetPasswordForm.d.ts +12 -12
  193. package/dist/esm/hooks/useResetPasswordForm.js +73 -73
  194. package/dist/esm/hooks/useSendResetForm.d.ts +14 -14
  195. package/dist/esm/hooks/useSendResetForm.js +53 -53
  196. package/dist/esm/hooks/useSignUpForm.d.ts +20 -20
  197. package/dist/esm/hooks/useSignUpForm.js +147 -153
  198. package/dist/esm/hooks/useSignUpGuestForm.d.ts +23 -23
  199. package/dist/esm/hooks/useSignUpGuestForm.js +109 -120
  200. package/dist/esm/hooks/useSigninForm.d.ts +12 -12
  201. package/dist/esm/hooks/useSigninForm.js +62 -62
  202. package/dist/esm/index.d.ts +2 -2
  203. package/dist/esm/index.js +2 -2
  204. package/dist/esm/utils/cards.d.ts +20 -20
  205. package/dist/esm/utils/cards.js +146 -146
  206. package/dist/esm/utils/cart.d.ts +202 -202
  207. package/dist/esm/utils/cart.js +868 -868
  208. package/dist/esm/utils/checkout.d.ts +55 -55
  209. package/dist/esm/utils/checkout.js +117 -117
  210. package/dist/esm/utils/constants.d.ts +43 -43
  211. package/dist/esm/utils/constants.js +55 -55
  212. package/dist/esm/utils/customer.d.ts +4 -4
  213. package/dist/esm/utils/customer.js +29 -29
  214. package/dist/esm/utils/datetimes.d.ts +99 -99
  215. package/dist/esm/utils/datetimes.js +531 -531
  216. package/dist/esm/utils/errors.d.ts +51 -51
  217. package/dist/esm/utils/errors.js +180 -180
  218. package/dist/esm/utils/favorites.d.ts +10 -10
  219. package/dist/esm/utils/favorites.js +109 -109
  220. package/dist/esm/utils/googleMaps.d.ts +14 -14
  221. package/dist/esm/utils/googleMaps.js +26 -26
  222. package/dist/esm/utils/helpers.d.ts +63 -63
  223. package/dist/esm/utils/helpers.js +205 -205
  224. package/dist/esm/utils/index.d.ts +19 -19
  225. package/dist/esm/utils/index.js +20 -20
  226. package/dist/esm/utils/keypad.d.ts +9 -9
  227. package/dist/esm/utils/keypad.js +97 -97
  228. package/dist/esm/utils/maps.d.ts +226 -226
  229. package/dist/esm/utils/maps.js +316 -316
  230. package/dist/esm/utils/menu.d.ts +117 -117
  231. package/dist/esm/utils/menu.js +74 -74
  232. package/dist/esm/utils/orders.d.ts +21 -21
  233. package/dist/esm/utils/orders.js +185 -185
  234. package/dist/esm/utils/pizzaBuilder.d.ts +10 -10
  235. package/dist/esm/utils/pizzaBuilder.js +38 -38
  236. package/dist/esm/utils/refunds.d.ts +35 -35
  237. package/dist/esm/utils/refunds.js +90 -90
  238. package/dist/esm/utils/revenueCenters.d.ts +11 -11
  239. package/dist/esm/utils/revenueCenters.js +115 -115
  240. package/dist/esm/utils/rewards.d.ts +4 -4
  241. package/dist/esm/utils/rewards.js +94 -94
  242. package/dist/esm/utils/theme.d.ts +27 -27
  243. package/dist/esm/utils/theme.js +254 -254
  244. package/dist/esm/utils/version.d.ts +4 -4
  245. package/dist/esm/utils/version.js +34 -34
  246. package/package.json +69 -69
@@ -1,531 +1,531 @@
1
- import { add, differenceInMinutes, differenceInSeconds, parseISO, sub } from 'date-fns';
2
- import { format, toDate, zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz';
3
- /* CONSTANTS */
4
- export const DATE = 'yyyy-MM-dd';
5
- export const TIME = 'h:mma';
6
- export const DATETIME = 'yyyy-MM-dd h:mma';
7
- export const HUMAN_DATE = 'MMM d, yyyy';
8
- export const HUMAN_TIME = 'h:mma';
9
- export const HUMAN_DATETIME = 'MMM d, h:mma';
10
- export const timezoneMap = {
11
- 'US/Eastern': 'America/New_York',
12
- 'US/Central': 'America/Chicago',
13
- 'US/Mountain': 'America/Denver',
14
- 'US/Pacific': 'America/Los_Angeles'
15
- };
16
- export const weekdays = [
17
- 'Sunday',
18
- 'Monday',
19
- 'Tuesday',
20
- 'Wednesday',
21
- 'Thursday',
22
- 'Friday',
23
- 'Saturday'
24
- ];
25
- export const weekdaysUpper = weekdays.map(weekday => weekday.toUpperCase());
26
- export const weekdaysLower = weekdays.map(weekday => weekday.toLowerCase());
27
- export const weekdayOptions = weekdays.map(weekday => ({
28
- value: weekday.toUpperCase(),
29
- name: weekday
30
- }));
31
- export const makeWeekday = (date = new Date()) => {
32
- return format(date, 'EEEE').toUpperCase();
33
- };
34
- export const minutesLeft = (start, end) => {
35
- return Math.max(differenceInMinutes(start, end), 0);
36
- };
37
- export const secondsLeft = (start, end) => {
38
- return Math.max(differenceInSeconds(start, end), 0);
39
- };
40
- export const secondsToTime = (seconds) => {
41
- const mins = Math.floor(seconds / 60);
42
- const secs = `${seconds % 60}`.padStart(2, '0');
43
- return `${mins}:${secs}`;
44
- };
45
- export const timeLeft = (start, end) => {
46
- const seconds = secondsLeft(start, end);
47
- return secondsToTime(seconds);
48
- };
49
- export const dateForWeekday = (weekday) => {
50
- const currentWeekday = makeWeekday();
51
- const currentIndex = weekdaysUpper.findIndex(i => i === currentWeekday);
52
- const index = weekdaysUpper.findIndex(i => i === weekday.toUpperCase());
53
- const offset = index - currentIndex + (index >= currentIndex ? 0 : 7);
54
- return add(new Date(), { days: offset });
55
- };
56
- export const weekdayAndTimeToDate = (weekday, timeStr) => {
57
- const date = dateForWeekday(weekday);
58
- const [hours, minutes] = timeStr.split(':').map(i => parseInt(i));
59
- return setTimeForDate(date, hours, minutes);
60
- };
61
- /* HELPERS */
62
- export const parseIsoToDate = (iso) => parseISO(iso);
63
- export const fmtDate = (date, fmt) => {
64
- return format(date, fmt);
65
- };
66
- // https://stackoverflow.com/questions/54555491/how-to-guess-users-timezone-using-date-fns-in-a-vuejs-app
67
- export const getUserTimezone = () => {
68
- try {
69
- return Intl.DateTimeFormat().resolvedOptions().timeZone;
70
- }
71
- catch (err) {
72
- return 'America/New_York';
73
- }
74
- };
75
- export const makeLocalDate = (dateStr) => {
76
- const tz = getUserTimezone();
77
- return toDate(dateStr, { timeZone: tz });
78
- };
79
- export const zonedTimeToDate = (str, timezone) => {
80
- const tz = timezoneMap[timezone];
81
- return toDate(str.split('.')[0], { timeZone: tz });
82
- };
83
- export const zonedTimeToDateStr = (str, timezone, fmt = DATE) => {
84
- const date = zonedTimeToDate(str, timezone);
85
- return format(date, fmt);
86
- };
87
- // returns a date string in a user's local time
88
- export const makeLocalDateStr = (date, days = 0, fmt = DATE) => {
89
- return format(add(date || new Date(), { days: days }), fmt);
90
- };
91
- export const todayDate = () => makeLocalDateStr();
92
- export const tomorrowDate = () => makeLocalDateStr(null, 1);
93
- export const isoToDate = (iso, tz) => {
94
- return tz ? utcToZonedTime(parseISO(iso), tz) : parseISO(iso);
95
- };
96
- export const isoToDateStr = (iso, tz, fmt = DATETIME) => {
97
- return format(isoToDate(iso, tz), fmt);
98
- };
99
- export const cleanISOString = (date) => {
100
- return (date.toISOString().split('.')[0] + 'Z');
101
- };
102
- export const dateToIso = (date, tz) => {
103
- return cleanISOString(zonedTimeToUtc(date, tz));
104
- };
105
- export const adjustIso = (iso, tz, adjustment) => {
106
- const date = isoToDate(iso, tz);
107
- return dateToIso(add(date, adjustment), tz);
108
- };
109
- export const adjustZonedIso = (zonedIso, tz, adjustment) => {
110
- const adjusted = add(toDate(zonedIso), adjustment);
111
- const zoned = utcToZonedTime(adjusted, tz);
112
- const dateStr = format(zoned, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone: tz });
113
- return dateStr;
114
- };
115
- export const dateToZonedDateStr = (date, tz, fmt = "yyyy-MM-dd'T'HH:mm:ssXXX", days = 0) => {
116
- const adjustedDate = add(date, { days: days });
117
- const zoned = utcToZonedTime(adjustedDate, tz);
118
- return format(zoned, fmt, { timeZone: tz });
119
- };
120
- export const dateToZonedIso = (date, tz) => {
121
- const zoned = utcToZonedTime(date, tz);
122
- return format(zoned, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone: tz });
123
- };
124
- export const currentLocalDate = (tz) => {
125
- const date = new Date();
126
- return isoToDate(date.toISOString(), tz);
127
- };
128
- export const currentLocalDateStr = (tz, fmt = DATE) => {
129
- const date = new Date();
130
- return format(isoToDate(date.toISOString(), tz), fmt);
131
- };
132
- export const dateStrToDate = (str) => toDate(str);
133
- export const dateStrToZonedDate = (str, tz) => {
134
- return toDate(str, { timeZone: tz });
135
- };
136
- export const dateStrToZonedWeekday = (str) => {
137
- return makeWeekday(toDate(str));
138
- };
139
- export const replaceAmPm = (str) => str.replace('AM', 'am').replace('PM', 'pm');
140
- export const formatDate = (date, fmt = HUMAN_DATETIME, amPm = true) => {
141
- const str = format(date, fmt);
142
- return amPm ? replaceAmPm(str) : str;
143
- };
144
- export const formatDateStr = (str, fmt = HUMAN_DATE) => {
145
- return str ? format(toDate(str), fmt) : null;
146
- };
147
- export const formatTimeStr = (str) => {
148
- const clean = str.replace(/\s/g, '').toLowerCase();
149
- const parts = clean.split('-');
150
- if (parts.length === 1)
151
- return clean;
152
- const [part1, part2] = parts;
153
- const end1 = part1.substr(part1.length - 2);
154
- const end2 = part2.substr(part1.length - 2);
155
- const newPart1 = ['am', 'pm'].includes(end1) && end1 === end2
156
- ? part1.slice(0, part1.length - 2)
157
- : part1;
158
- return [newPart1, part2].join('-');
159
- };
160
- export const dateStrMinutesToIso = (dateStr, minutes, tz) => {
161
- const hours = Math.floor(minutes / 60)
162
- .toString()
163
- .padStart(2, '0');
164
- const mins = (minutes % 60).toString().padStart(2, '0');
165
- const timeStr = `${hours}:${mins}:00`;
166
- const dateTimeStr = `${dateStr} ${timeStr}`;
167
- const dateUtc = zonedTimeToUtc(dateTimeStr, tz);
168
- return cleanISOString(dateUtc);
169
- };
170
- export const isoToDateStrMinutes = (iso, tz) => {
171
- const dateObj = isoToDate(iso, tz);
172
- const minutes = getMinutesfromDate(dateObj);
173
- const date = format(dateObj, DATE);
174
- return { date, minutes };
175
- };
176
- export const makeReadableDateStrFromIso = (iso, tz, verbose = false) => {
177
- if (!iso || iso.toLowerCase() === 'asap')
178
- return 'ASAP';
179
- const date = utcToZonedTime(parseISO(iso), tz);
180
- const timeString = format(date, TIME).toLowerCase();
181
- const dateString = makeLocalDateStr(date);
182
- if (dateString === todayDate()) {
183
- return verbose ? `Today @ ${timeString}` : timeString;
184
- }
185
- else if (dateString === tomorrowDate()) {
186
- return `${verbose ? 'Tomorrow' : 'Tmrw'} @ ${timeString}`;
187
- }
188
- else {
189
- return `${verbose ? format(date, 'EEE, MMM d') : format(date, 'M/d')} @ ${timeString}`;
190
- }
191
- };
192
- export const makeRequestedIso = (requestedAt) => {
193
- return !requestedAt || requestedAt === 'asap'
194
- ? cleanISOString(new Date())
195
- : requestedAt;
196
- };
197
- export const makeRequestedAtStr = (requestedAt, tz, verbose = false) => {
198
- if (!requestedAt || requestedAt.toLowerCase() === 'asap')
199
- return 'ASAP';
200
- return makeReadableDateStrFromIso(requestedAt, tz, verbose);
201
- };
202
- export const makeEstimatedTime = (requestedAt, revenueCenter, serviceType, verbose = false) => {
203
- var _a, _b;
204
- if (requestedAt !== 'asap' || !serviceType || !revenueCenter)
205
- return null;
206
- const settings = revenueCenter;
207
- const { first_times } = settings;
208
- if (!first_times)
209
- return null;
210
- const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
211
- const firstTime = first_times[st];
212
- if ((firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) === todayDate()) {
213
- return `around ${firstTime.time}`;
214
- }
215
- else if ((firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) === tomorrowDate()) {
216
- return `tomorrow @ ${formatTimeStr(firstTime === null || firstTime === void 0 ? void 0 : firstTime.time)}`;
217
- }
218
- else {
219
- const tz = timezoneMap[(_a = revenueCenter.timezone) !== null && _a !== void 0 ? _a : 'US/Eastern'];
220
- const date = toDate((_b = firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) !== null && _b !== void 0 ? _b : new Date(), { timeZone: tz });
221
- return `${verbose ? format(date, 'EEEE, MMMM d') : format(date, 'M/d')} @ ${firstTime === null || firstTime === void 0 ? void 0 : firstTime.time}`;
222
- }
223
- };
224
- function* range(start, end, step) {
225
- for (let i = start; i <= end; i += step) {
226
- yield i;
227
- }
228
- }
229
- export const makeOppositeTimes = (times, interval, min = 0, max = 1440) => {
230
- return [...range(min, max - interval, interval)].filter(i => !times.includes(i));
231
- };
232
- export const time24ToMinutes = (str) => {
233
- const [hours, minutes] = str.split(':');
234
- return parseInt(hours) * 60 + parseInt(minutes);
235
- };
236
- export const setTimeForDate = (date, hours, minutes, seconds = 0) => {
237
- date.setHours(hours);
238
- date.setMinutes(minutes);
239
- date.setSeconds(seconds);
240
- date.setMilliseconds(0);
241
- return date;
242
- };
243
- export const minutesToDate = (minutes, date) => {
244
- const hours = Math.floor(minutes / 60);
245
- const mins = minutes % 60;
246
- const dateCloned = date ? new Date(date.getTime()) : new Date();
247
- return setTimeForDate(dateCloned, hours, mins);
248
- };
249
- export const time24ToDate = (str) => {
250
- const minutes = time24ToMinutes(str);
251
- return minutesToDate(minutes);
252
- };
253
- export const time24ToDateStr = (str, fmt = TIME) => {
254
- const minutes = time24ToMinutes(str);
255
- return format(minutesToDate(minutes), fmt).replace(':00', '').toLowerCase();
256
- };
257
- export const minutesToDates = (minutes, date) => {
258
- return minutes.map(minute => {
259
- return minutesToDate(minute, date);
260
- });
261
- };
262
- export const getMinutesfromDate = (date) => {
263
- return date.getHours() * 60 + date.getMinutes();
264
- };
265
- export const getNextIntervalMinutes = (minutes, interval) => {
266
- const hours = Math.floor(minutes / 60);
267
- const mins = minutes % 60;
268
- const intervals = interval === 15 ? [0, 15, 30, 45, 60] : [0, 30, 60];
269
- const nextMinute = intervals.filter(i => i >= mins)[0];
270
- return hours * 60 + nextMinute;
271
- };
272
- export const makeDates = (startDateStr, days, fmt = HUMAN_DATE) => {
273
- const startDate = dateStrToDate(startDateStr);
274
- const dateArray = [];
275
- for (let step = 0; step < days + 1; step++) {
276
- const nextDate = add(startDate, { days: step });
277
- const value = format(nextDate, DATE);
278
- const label = value === todayDate()
279
- ? 'Today'
280
- : value === tomorrowDate()
281
- ? 'Tomorrow'
282
- : format(nextDate, fmt);
283
- dateArray.push({ label, value });
284
- }
285
- return dateArray;
286
- };
287
- export const makeTimes = (date, firstTime, validTimes, holidays, serviceType, leadTime = 0) => {
288
- var _a;
289
- const weekday = dateStrToZonedWeekday(date);
290
- let times = null;
291
- const holiday = holidays ? holidays[date] : null;
292
- if (holiday) {
293
- times = holiday[serviceType] ? (_a = holiday[serviceType]) === null || _a === void 0 ? void 0 : _a.valid_times : null;
294
- }
295
- else {
296
- const validTime = validTimes[serviceType];
297
- times = validTime ? validTime[weekday] : null;
298
- }
299
- if (!times)
300
- return null;
301
- // if first available date, remove times before first available time
302
- if (date === firstTime.date) {
303
- times = times.filter(t => t.minutes >= firstTime.minutes + leadTime);
304
- }
305
- return times.map(t => ({
306
- name: t.time,
307
- value: t.minutes,
308
- disabled: !t.is_orderable
309
- }));
310
- };
311
- export const makeOrderTimes = (orderTimes, tz) => {
312
- var _a;
313
- const currentDate = new Date();
314
- const withDates = orderTimes === null || orderTimes === void 0 ? void 0 : orderTimes.map(i => {
315
- const { weekday, time } = i.order_by;
316
- let orderByDate = weekdayAndTimeToDate(weekday, time);
317
- let startDate = weekdayAndTimeToDate(i.weekday, i.start_time);
318
- // if (orderByDate < currentDate || startDate < currentDate) {
319
- // startDate = add(startDate, { days: 7 })
320
- // }
321
- if (orderByDate < currentDate) {
322
- orderByDate = add(orderByDate, { days: 7 });
323
- }
324
- if (startDate < orderByDate) {
325
- startDate = add(startDate, { days: 7 });
326
- }
327
- const orderBy = Object.assign(Object.assign({}, i.order_by), { date: orderByDate });
328
- return Object.assign(Object.assign({}, i), { date: startDate, iso: dateToIso(startDate, tz), order_by: orderBy });
329
- });
330
- return (_a = withDates === null || withDates === void 0 ? void 0 : withDates.sort((a, b) => a.date.getTime() - b.date.getTime())) !== null && _a !== void 0 ? _a : [];
331
- };
332
- export const findOrderTime = (orderTimes, tz, requestedAt) => {
333
- const sortedTimes = makeOrderTimes(orderTimes, tz);
334
- const selected = requestedAt
335
- ? sortedTimes.find(i => i.iso === requestedAt)
336
- : null;
337
- return selected || sortedTimes[0];
338
- };
339
- export const makeFirstTime = (revenueCenter, tz, serviceType, requestedAt) => {
340
- var _a;
341
- const { first_times, order_times } = revenueCenter;
342
- const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
343
- if (!first_times || !first_times[st]) {
344
- if (!order_times || !order_times[st] || !((_a = order_times[st]) === null || _a === void 0 ? void 0 : _a.length))
345
- return null;
346
- const selected = findOrderTime(order_times[st], tz, requestedAt);
347
- return selected.iso;
348
- }
349
- const firstTime = first_times[st];
350
- const firstDate = firstTime ? isoToDate(firstTime === null || firstTime === void 0 ? void 0 : firstTime.utc, tz) : new Date();
351
- const hasAsap = firstTime === null || firstTime === void 0 ? void 0 : firstTime.has_asap;
352
- if (requestedAt === 'asap' && hasAsap) {
353
- return 'asap';
354
- }
355
- const requestedDate = requestedAt && requestedAt !== 'asap'
356
- ? isoToDate(requestedAt, tz)
357
- : null;
358
- if (requestedDate && requestedDate > firstDate) {
359
- return requestedAt;
360
- }
361
- if (hasAsap && firstTime.minutes % 15 !== 0) {
362
- return 'asap';
363
- }
364
- return firstTime.utc;
365
- };
366
- export const makeFirstRequestedAt = (revenueCenter, serviceType, requestedAt) => {
367
- const { timezone, revenue_center_type } = revenueCenter;
368
- const tz = timezoneMap[timezone];
369
- const newRequestedAt = requestedAt || (revenue_center_type === 'OLO' ? 'asap' : null);
370
- return makeFirstTime(revenueCenter, tz, serviceType, newRequestedAt);
371
- };
372
- export const makeFirstTimes = (revenueCenter, serviceType, requestedAt) => {
373
- const { timezone } = revenueCenter;
374
- const settings = revenueCenter;
375
- const tz = timezoneMap[timezone];
376
- const otherServiceType = serviceType === 'DELIVERY' ? 'PICKUP' : 'DELIVERY';
377
- const current = makeFirstTime(settings, tz, serviceType, requestedAt);
378
- const other = makeFirstTime(settings, tz, otherServiceType, requestedAt);
379
- if (!current && !other)
380
- return null;
381
- return [
382
- current ? { serviceType: serviceType, requestedAt: current } : null,
383
- other ? { serviceType: otherServiceType, requestedAt: other } : null
384
- ];
385
- // return { [serviceType]: current, [otherServiceType]: other }
386
- };
387
- export const getNextInterval = (requestedIso, tz, interval) => {
388
- const date = isoToDate(requestedIso, tz);
389
- const intervals = interval === 15 ? [0, 15, 30, 45, 60] : [0, 30, 60];
390
- const nextInterval = intervals.filter(i => i >= date.getMinutes())[0];
391
- const hours = nextInterval === 60 ? date.getHours() + 1 : date.getHours();
392
- const minutes = nextInterval === 60 ? 0 : nextInterval;
393
- date.setHours(hours);
394
- date.setMinutes(minutes);
395
- date.setSeconds(0);
396
- date.setMilliseconds(0);
397
- return date;
398
- };
399
- export const adjustRequestedAt = (requestedIso, tz, interval, leadTime) => {
400
- const nextDate = getNextInterval(requestedIso, tz, interval);
401
- const adjusted = add(nextDate, { minutes: leadTime });
402
- return dateToIso(adjusted, tz);
403
- };
404
- export const getFirstTime = (revenueCenter, serviceType, tz) => {
405
- var _a;
406
- const { first_times, order_times } = revenueCenter;
407
- const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
408
- if (!first_times || !first_times[st]) {
409
- if (!order_times || !order_times[st])
410
- return null;
411
- const orderTimes = makeOrderTimes((_a = order_times[st]) !== null && _a !== void 0 ? _a : [], tz);
412
- return orderTimes[0];
413
- }
414
- return first_times[st];
415
- };
416
- export const makeGroupOrderTime = (revenueCenter, serviceType, requestedAt) => {
417
- const { timezone } = revenueCenter || {};
418
- const settings = revenueCenter;
419
- const tz = timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Eastern'];
420
- const { first_times, order_times, wait_times, group_ordering } = settings;
421
- const { prep_time, lead_time } = group_ordering || {};
422
- const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
423
- const waitTime = wait_times && wait_times[st] ? wait_times[st] : 0;
424
- const prepTime = `${(waitTime !== null && waitTime !== void 0 ? waitTime : 0) + (prep_time !== null && prep_time !== void 0 ? prep_time : 0)}`;
425
- if (requestedAt === 'asap')
426
- return { prepTime };
427
- const firstTime = first_times && first_times[st] ? first_times[st] : null;
428
- const orderTimes = order_times && order_times[st] ? order_times[st] : null;
429
- if (!firstTime && !orderTimes)
430
- return null;
431
- let adjustedIso, adjustedDate, cutoffDate, firstIso;
432
- if (orderTimes) {
433
- const orderTime = findOrderTime(orderTimes, tz, requestedAt);
434
- adjustedIso = orderTime.iso;
435
- adjustedDate = isoToDate(adjustedIso, tz);
436
- cutoffDate = orderTime.order_by.date;
437
- const allOrderTimes = makeOrderTimes(orderTimes, tz);
438
- firstIso = allOrderTimes[0].iso;
439
- }
440
- else {
441
- // first available time depends on both suggested lead time and extra prep time
442
- // lead_time = how much time cart guests have to place their orders
443
- // prep_time = how much extra prep time is required for group orders
444
- // over and above the pickup or delivery wait time
445
- if (!firstTime)
446
- return null;
447
- const leadTime = (prep_time !== null && prep_time !== void 0 ? prep_time : 0) + (lead_time !== null && lead_time !== void 0 ? lead_time : 0);
448
- const interval = 15;
449
- firstIso = adjustRequestedAt(firstTime.utc, tz, interval, leadTime);
450
- const firstDate = isoToDate(firstIso, tz);
451
- const requestedDate = isoToDate(requestedAt, tz);
452
- adjustedIso =
453
- requestedDate && requestedDate > firstDate ? requestedAt : firstIso;
454
- adjustedDate = isoToDate(adjustedIso, tz);
455
- cutoffDate = sub(adjustedDate, { minutes: parseFloat(prepTime) });
456
- }
457
- const cutoffIso = dateToIso(cutoffDate, tz);
458
- return {
459
- isAdjusted: requestedAt !== adjustedIso,
460
- firstIso,
461
- iso: adjustedIso,
462
- date: adjustedDate,
463
- dateStr: makeReadableDateStrFromIso(adjustedIso, tz, true),
464
- cutoffIso,
465
- cutoffDate,
466
- cutoffDateStr: makeReadableDateStrFromIso(cutoffIso, tz, true)
467
- };
468
- };
469
- export const formatTime = (time) => {
470
- return time
471
- ? time.replace('Today', 'today').replace('Tomorrow', 'tomorrow')
472
- : '';
473
- };
474
- export const makeGroupOrderTimeStr = (requestedAt, tz) => {
475
- if (!requestedAt || requestedAt.toLowerCase() === 'asap')
476
- return 'ASAP';
477
- const orderTime = makeReadableDateStrFromIso(requestedAt, tz, true);
478
- return orderTime ? formatTime(orderTime) : null;
479
- };
480
- export const getLastInterval = (tz) => {
481
- const date = isoToDate(new Date().toISOString(), tz);
482
- date.setHours(23);
483
- date.setMinutes(45);
484
- date.setSeconds(0);
485
- date.setMilliseconds(0);
486
- return date;
487
- };
488
- export const makeIntervals = (tz) => {
489
- const nextInterval = getNextInterval(new Date().toISOString(), tz, 15);
490
- const lastInterval = getLastInterval(tz);
491
- const diff = Math.max(differenceInMinutes(lastInterval, nextInterval), 0);
492
- const steps = 24 * 4 + (diff ? Math.ceil(diff / 15) : 0);
493
- let start = sub(nextInterval, { minutes: 24 * 60 });
494
- const intervals = [];
495
- for (let step = 0; step < steps; step++) {
496
- const end = add(start, { minutes: 15 });
497
- intervals.push({ start, end, orders: [] });
498
- start = end;
499
- }
500
- return intervals;
501
- };
502
- export const formatTimeList = (dateStr, tz, includeDate) => {
503
- const date = isoToDate(dateStr, tz);
504
- const fmt = includeDate ? 'MMM d, h:mma' : 'h:mma';
505
- return format(date, fmt).replace('AM', 'am').replace('PM', 'pm');
506
- };
507
- export const makeCartDateStr = (requestedAt, tz, waitTime) => {
508
- if (requestedAt === 'asap')
509
- return `Ready in about ${waitTime} minutes`;
510
- const requestedDateStr = isoToDateStr(requestedAt, tz, 'yyyy-MM-dd');
511
- const now = new Date();
512
- const today = dateToZonedDateStr(now, tz, 'yyyy-MM-dd');
513
- const tomorrow = dateToZonedDateStr(now, tz, 'yyyy-MM-dd', 1);
514
- const isToday = requestedDateStr === today;
515
- const isTomorrow = requestedDateStr === tomorrow;
516
- const timeStr = isoToDateStr(requestedAt, tz, 'h:mma').toLowerCase();
517
- const dateStr = isoToDateStr(requestedAt, tz, 'EEE, MMM dd');
518
- if (isToday)
519
- return `Ready by ${timeStr}`;
520
- if (isTomorrow)
521
- return `Ready by ${timeStr} tomorrow`;
522
- return `Ready by ${timeStr} on ${dateStr}`;
523
- };
524
- export const makeOrderWindow = (orderTime) => {
525
- if (orderTime.start_time === orderTime.end_time) {
526
- return `@ ${time24ToDateStr(orderTime.start_time)}`;
527
- }
528
- else {
529
- return `from ${time24ToDateStr(orderTime.start_time)} to ${time24ToDateStr(orderTime.end_time)}`;
530
- }
531
- };
1
+ import { add, differenceInMinutes, differenceInSeconds, parseISO, sub } from 'date-fns';
2
+ import { format, toDate, zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz';
3
+ /* CONSTANTS */
4
+ export const DATE = 'yyyy-MM-dd';
5
+ export const TIME = 'h:mma';
6
+ export const DATETIME = 'yyyy-MM-dd h:mma';
7
+ export const HUMAN_DATE = 'MMM d, yyyy';
8
+ export const HUMAN_TIME = 'h:mma';
9
+ export const HUMAN_DATETIME = 'MMM d, h:mma';
10
+ export const timezoneMap = {
11
+ 'US/Eastern': 'America/New_York',
12
+ 'US/Central': 'America/Chicago',
13
+ 'US/Mountain': 'America/Denver',
14
+ 'US/Pacific': 'America/Los_Angeles'
15
+ };
16
+ export const weekdays = [
17
+ 'Sunday',
18
+ 'Monday',
19
+ 'Tuesday',
20
+ 'Wednesday',
21
+ 'Thursday',
22
+ 'Friday',
23
+ 'Saturday'
24
+ ];
25
+ export const weekdaysUpper = weekdays.map(weekday => weekday.toUpperCase());
26
+ export const weekdaysLower = weekdays.map(weekday => weekday.toLowerCase());
27
+ export const weekdayOptions = weekdays.map(weekday => ({
28
+ value: weekday.toUpperCase(),
29
+ name: weekday
30
+ }));
31
+ export const makeWeekday = (date = new Date()) => {
32
+ return format(date, 'EEEE').toUpperCase();
33
+ };
34
+ export const minutesLeft = (start, end) => {
35
+ return Math.max(differenceInMinutes(start, end), 0);
36
+ };
37
+ export const secondsLeft = (start, end) => {
38
+ return Math.max(differenceInSeconds(start, end), 0);
39
+ };
40
+ export const secondsToTime = (seconds) => {
41
+ const mins = Math.floor(seconds / 60);
42
+ const secs = `${seconds % 60}`.padStart(2, '0');
43
+ return `${mins}:${secs}`;
44
+ };
45
+ export const timeLeft = (start, end) => {
46
+ const seconds = secondsLeft(start, end);
47
+ return secondsToTime(seconds);
48
+ };
49
+ export const dateForWeekday = (weekday) => {
50
+ const currentWeekday = makeWeekday();
51
+ const currentIndex = weekdaysUpper.findIndex(i => i === currentWeekday);
52
+ const index = weekdaysUpper.findIndex(i => i === weekday.toUpperCase());
53
+ const offset = index - currentIndex + (index >= currentIndex ? 0 : 7);
54
+ return add(new Date(), { days: offset });
55
+ };
56
+ export const weekdayAndTimeToDate = (weekday, timeStr) => {
57
+ const date = dateForWeekday(weekday);
58
+ const [hours, minutes] = timeStr.split(':').map(i => parseInt(i));
59
+ return setTimeForDate(date, hours, minutes);
60
+ };
61
+ /* HELPERS */
62
+ export const parseIsoToDate = (iso) => parseISO(iso);
63
+ export const fmtDate = (date, fmt) => {
64
+ return format(date, fmt);
65
+ };
66
+ // https://stackoverflow.com/questions/54555491/how-to-guess-users-timezone-using-date-fns-in-a-vuejs-app
67
+ export const getUserTimezone = () => {
68
+ try {
69
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
70
+ }
71
+ catch (err) {
72
+ return 'America/New_York';
73
+ }
74
+ };
75
+ export const makeLocalDate = (dateStr) => {
76
+ const tz = getUserTimezone();
77
+ return toDate(dateStr, { timeZone: tz });
78
+ };
79
+ export const zonedTimeToDate = (str, timezone) => {
80
+ const tz = timezoneMap[timezone];
81
+ return toDate(str.split('.')[0], { timeZone: tz });
82
+ };
83
+ export const zonedTimeToDateStr = (str, timezone, fmt = DATE) => {
84
+ const date = zonedTimeToDate(str, timezone);
85
+ return format(date, fmt);
86
+ };
87
+ // returns a date string in a user's local time
88
+ export const makeLocalDateStr = (date, days = 0, fmt = DATE) => {
89
+ return format(add(date || new Date(), { days: days }), fmt);
90
+ };
91
+ export const todayDate = () => makeLocalDateStr();
92
+ export const tomorrowDate = () => makeLocalDateStr(null, 1);
93
+ export const isoToDate = (iso, tz) => {
94
+ return tz ? utcToZonedTime(parseISO(iso), tz) : parseISO(iso);
95
+ };
96
+ export const isoToDateStr = (iso, tz, fmt = DATETIME) => {
97
+ return format(isoToDate(iso, tz), fmt);
98
+ };
99
+ export const cleanISOString = (date) => {
100
+ return (date.toISOString().split('.')[0] + 'Z');
101
+ };
102
+ export const dateToIso = (date, tz) => {
103
+ return cleanISOString(zonedTimeToUtc(date, tz));
104
+ };
105
+ export const adjustIso = (iso, tz, adjustment) => {
106
+ const date = isoToDate(iso, tz);
107
+ return dateToIso(add(date, adjustment), tz);
108
+ };
109
+ export const adjustZonedIso = (zonedIso, tz, adjustment) => {
110
+ const adjusted = add(toDate(zonedIso), adjustment);
111
+ const zoned = utcToZonedTime(adjusted, tz);
112
+ const dateStr = format(zoned, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone: tz });
113
+ return dateStr;
114
+ };
115
+ export const dateToZonedDateStr = (date, tz, fmt = "yyyy-MM-dd'T'HH:mm:ssXXX", days = 0) => {
116
+ const adjustedDate = add(date, { days: days });
117
+ const zoned = utcToZonedTime(adjustedDate, tz);
118
+ return format(zoned, fmt, { timeZone: tz });
119
+ };
120
+ export const dateToZonedIso = (date, tz) => {
121
+ const zoned = utcToZonedTime(date, tz);
122
+ return format(zoned, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone: tz });
123
+ };
124
+ export const currentLocalDate = (tz) => {
125
+ const date = new Date();
126
+ return isoToDate(date.toISOString(), tz);
127
+ };
128
+ export const currentLocalDateStr = (tz, fmt = DATE) => {
129
+ const date = new Date();
130
+ return format(isoToDate(date.toISOString(), tz), fmt);
131
+ };
132
+ export const dateStrToDate = (str) => toDate(str);
133
+ export const dateStrToZonedDate = (str, tz) => {
134
+ return toDate(str, { timeZone: tz });
135
+ };
136
+ export const dateStrToZonedWeekday = (str) => {
137
+ return makeWeekday(toDate(str));
138
+ };
139
+ export const replaceAmPm = (str) => str.replace('AM', 'am').replace('PM', 'pm');
140
+ export const formatDate = (date, fmt = HUMAN_DATETIME, amPm = true) => {
141
+ const str = format(date, fmt);
142
+ return amPm ? replaceAmPm(str) : str;
143
+ };
144
+ export const formatDateStr = (str, fmt = HUMAN_DATE) => {
145
+ return str ? format(toDate(str), fmt) : null;
146
+ };
147
+ export const formatTimeStr = (str) => {
148
+ const clean = str.replace(/\s/g, '').toLowerCase();
149
+ const parts = clean.split('-');
150
+ if (parts.length === 1)
151
+ return clean;
152
+ const [part1, part2] = parts;
153
+ const end1 = part1.substr(part1.length - 2);
154
+ const end2 = part2.substr(part1.length - 2);
155
+ const newPart1 = ['am', 'pm'].includes(end1) && end1 === end2
156
+ ? part1.slice(0, part1.length - 2)
157
+ : part1;
158
+ return [newPart1, part2].join('-');
159
+ };
160
+ export const dateStrMinutesToIso = (dateStr, minutes, tz) => {
161
+ const hours = Math.floor(minutes / 60)
162
+ .toString()
163
+ .padStart(2, '0');
164
+ const mins = (minutes % 60).toString().padStart(2, '0');
165
+ const timeStr = `${hours}:${mins}:00`;
166
+ const dateTimeStr = `${dateStr} ${timeStr}`;
167
+ const dateUtc = zonedTimeToUtc(dateTimeStr, tz);
168
+ return cleanISOString(dateUtc);
169
+ };
170
+ export const isoToDateStrMinutes = (iso, tz) => {
171
+ const dateObj = isoToDate(iso, tz);
172
+ const minutes = getMinutesfromDate(dateObj);
173
+ const date = format(dateObj, DATE);
174
+ return { date, minutes };
175
+ };
176
+ export const makeReadableDateStrFromIso = (iso, tz, verbose = false) => {
177
+ if (!iso || iso.toLowerCase() === 'asap')
178
+ return 'ASAP';
179
+ const date = utcToZonedTime(parseISO(iso), tz);
180
+ const timeString = format(date, TIME).toLowerCase();
181
+ const dateString = makeLocalDateStr(date);
182
+ if (dateString === todayDate()) {
183
+ return verbose ? `Today @ ${timeString}` : timeString;
184
+ }
185
+ else if (dateString === tomorrowDate()) {
186
+ return `${verbose ? 'Tomorrow' : 'Tmrw'} @ ${timeString}`;
187
+ }
188
+ else {
189
+ return `${verbose ? format(date, 'EEE, MMM d') : format(date, 'M/d')} @ ${timeString}`;
190
+ }
191
+ };
192
+ export const makeRequestedIso = (requestedAt) => {
193
+ return !requestedAt || requestedAt === 'asap'
194
+ ? cleanISOString(new Date())
195
+ : requestedAt;
196
+ };
197
+ export const makeRequestedAtStr = (requestedAt, tz, verbose = false) => {
198
+ if (!requestedAt || requestedAt.toLowerCase() === 'asap')
199
+ return 'ASAP';
200
+ return makeReadableDateStrFromIso(requestedAt, tz, verbose);
201
+ };
202
+ export const makeEstimatedTime = (requestedAt, revenueCenter, serviceType, verbose = false) => {
203
+ var _a, _b;
204
+ if (requestedAt !== 'asap' || !serviceType || !revenueCenter)
205
+ return null;
206
+ const settings = revenueCenter;
207
+ const { first_times } = settings;
208
+ if (!first_times)
209
+ return null;
210
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
211
+ const firstTime = first_times[st];
212
+ if ((firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) === todayDate()) {
213
+ return `around ${firstTime.time}`;
214
+ }
215
+ else if ((firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) === tomorrowDate()) {
216
+ return `tomorrow @ ${formatTimeStr(firstTime === null || firstTime === void 0 ? void 0 : firstTime.time)}`;
217
+ }
218
+ else {
219
+ const tz = timezoneMap[(_a = revenueCenter.timezone) !== null && _a !== void 0 ? _a : 'US/Eastern'];
220
+ const date = toDate((_b = firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) !== null && _b !== void 0 ? _b : new Date(), { timeZone: tz });
221
+ return `${verbose ? format(date, 'EEEE, MMMM d') : format(date, 'M/d')} @ ${firstTime === null || firstTime === void 0 ? void 0 : firstTime.time}`;
222
+ }
223
+ };
224
+ function* range(start, end, step) {
225
+ for (let i = start; i <= end; i += step) {
226
+ yield i;
227
+ }
228
+ }
229
+ export const makeOppositeTimes = (times, interval, min = 0, max = 1440) => {
230
+ return [...range(min, max - interval, interval)].filter(i => !times.includes(i));
231
+ };
232
+ export const time24ToMinutes = (str) => {
233
+ const [hours, minutes] = str.split(':');
234
+ return parseInt(hours) * 60 + parseInt(minutes);
235
+ };
236
+ export const setTimeForDate = (date, hours, minutes, seconds = 0) => {
237
+ date.setHours(hours);
238
+ date.setMinutes(minutes);
239
+ date.setSeconds(seconds);
240
+ date.setMilliseconds(0);
241
+ return date;
242
+ };
243
+ export const minutesToDate = (minutes, date) => {
244
+ const hours = Math.floor(minutes / 60);
245
+ const mins = minutes % 60;
246
+ const dateCloned = date ? new Date(date.getTime()) : new Date();
247
+ return setTimeForDate(dateCloned, hours, mins);
248
+ };
249
+ export const time24ToDate = (str) => {
250
+ const minutes = time24ToMinutes(str);
251
+ return minutesToDate(minutes);
252
+ };
253
+ export const time24ToDateStr = (str, fmt = TIME) => {
254
+ const minutes = time24ToMinutes(str);
255
+ return format(minutesToDate(minutes), fmt).replace(':00', '').toLowerCase();
256
+ };
257
+ export const minutesToDates = (minutes, date) => {
258
+ return minutes.map(minute => {
259
+ return minutesToDate(minute, date);
260
+ });
261
+ };
262
+ export const getMinutesfromDate = (date) => {
263
+ return date.getHours() * 60 + date.getMinutes();
264
+ };
265
+ export const getNextIntervalMinutes = (minutes, interval) => {
266
+ const hours = Math.floor(minutes / 60);
267
+ const mins = minutes % 60;
268
+ const intervals = interval === 15 ? [0, 15, 30, 45, 60] : [0, 30, 60];
269
+ const nextMinute = intervals.filter(i => i >= mins)[0];
270
+ return hours * 60 + nextMinute;
271
+ };
272
+ export const makeDates = (startDateStr, days, fmt = HUMAN_DATE) => {
273
+ const startDate = dateStrToDate(startDateStr);
274
+ const dateArray = [];
275
+ for (let step = 0; step < days + 1; step++) {
276
+ const nextDate = add(startDate, { days: step });
277
+ const value = format(nextDate, DATE);
278
+ const label = value === todayDate()
279
+ ? 'Today'
280
+ : value === tomorrowDate()
281
+ ? 'Tomorrow'
282
+ : format(nextDate, fmt);
283
+ dateArray.push({ label, value });
284
+ }
285
+ return dateArray;
286
+ };
287
+ export const makeTimes = (date, firstTime, validTimes, holidays, serviceType, leadTime = 0) => {
288
+ var _a;
289
+ const weekday = dateStrToZonedWeekday(date);
290
+ let times = null;
291
+ const holiday = holidays ? holidays[date] : null;
292
+ if (holiday) {
293
+ times = holiday[serviceType] ? (_a = holiday[serviceType]) === null || _a === void 0 ? void 0 : _a.valid_times : null;
294
+ }
295
+ else {
296
+ const validTime = validTimes[serviceType];
297
+ times = validTime ? validTime[weekday] : null;
298
+ }
299
+ if (!times)
300
+ return null;
301
+ // if first available date, remove times before first available time
302
+ if (date === firstTime.date) {
303
+ times = times.filter(t => t.minutes >= firstTime.minutes + leadTime);
304
+ }
305
+ return times.map(t => ({
306
+ name: t.time,
307
+ value: t.minutes,
308
+ disabled: !t.is_orderable
309
+ }));
310
+ };
311
+ export const makeOrderTimes = (orderTimes, tz) => {
312
+ var _a;
313
+ const currentDate = new Date();
314
+ const withDates = orderTimes === null || orderTimes === void 0 ? void 0 : orderTimes.map(i => {
315
+ const { weekday, time } = i.order_by;
316
+ let orderByDate = weekdayAndTimeToDate(weekday, time);
317
+ let startDate = weekdayAndTimeToDate(i.weekday, i.start_time);
318
+ // if (orderByDate < currentDate || startDate < currentDate) {
319
+ // startDate = add(startDate, { days: 7 })
320
+ // }
321
+ if (orderByDate < currentDate) {
322
+ orderByDate = add(orderByDate, { days: 7 });
323
+ }
324
+ if (startDate < orderByDate) {
325
+ startDate = add(startDate, { days: 7 });
326
+ }
327
+ const orderBy = Object.assign(Object.assign({}, i.order_by), { date: orderByDate });
328
+ return Object.assign(Object.assign({}, i), { date: startDate, iso: dateToIso(startDate, tz), order_by: orderBy });
329
+ });
330
+ return (_a = withDates === null || withDates === void 0 ? void 0 : withDates.sort((a, b) => a.date.getTime() - b.date.getTime())) !== null && _a !== void 0 ? _a : [];
331
+ };
332
+ export const findOrderTime = (orderTimes, tz, requestedAt) => {
333
+ const sortedTimes = makeOrderTimes(orderTimes, tz);
334
+ const selected = requestedAt
335
+ ? sortedTimes.find(i => i.iso === requestedAt)
336
+ : null;
337
+ return selected || sortedTimes[0];
338
+ };
339
+ export const makeFirstTime = (revenueCenter, tz, serviceType, requestedAt) => {
340
+ var _a;
341
+ const { first_times, order_times } = revenueCenter;
342
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
343
+ if (!first_times || !first_times[st]) {
344
+ if (!order_times || !order_times[st] || !((_a = order_times[st]) === null || _a === void 0 ? void 0 : _a.length))
345
+ return null;
346
+ const selected = findOrderTime(order_times[st], tz, requestedAt);
347
+ return selected.iso;
348
+ }
349
+ const firstTime = first_times[st];
350
+ const firstDate = firstTime ? isoToDate(firstTime === null || firstTime === void 0 ? void 0 : firstTime.utc, tz) : new Date();
351
+ const hasAsap = firstTime === null || firstTime === void 0 ? void 0 : firstTime.has_asap;
352
+ if (requestedAt === 'asap' && hasAsap) {
353
+ return 'asap';
354
+ }
355
+ const requestedDate = requestedAt && requestedAt !== 'asap'
356
+ ? isoToDate(requestedAt, tz)
357
+ : null;
358
+ if (requestedDate && requestedDate > firstDate) {
359
+ return requestedAt;
360
+ }
361
+ if (hasAsap && firstTime.minutes % 15 !== 0) {
362
+ return 'asap';
363
+ }
364
+ return firstTime.utc;
365
+ };
366
+ export const makeFirstRequestedAt = (revenueCenter, serviceType, requestedAt) => {
367
+ const { timezone, revenue_center_type } = revenueCenter;
368
+ const tz = timezoneMap[timezone];
369
+ const newRequestedAt = requestedAt || (revenue_center_type === 'OLO' ? 'asap' : null);
370
+ return makeFirstTime(revenueCenter, tz, serviceType, newRequestedAt);
371
+ };
372
+ export const makeFirstTimes = (revenueCenter, serviceType, requestedAt) => {
373
+ const { timezone } = revenueCenter;
374
+ const settings = revenueCenter;
375
+ const tz = timezoneMap[timezone];
376
+ const otherServiceType = serviceType === 'DELIVERY' ? 'PICKUP' : 'DELIVERY';
377
+ const current = makeFirstTime(settings, tz, serviceType, requestedAt);
378
+ const other = makeFirstTime(settings, tz, otherServiceType, requestedAt);
379
+ if (!current && !other)
380
+ return null;
381
+ return [
382
+ current ? { serviceType: serviceType, requestedAt: current } : null,
383
+ other ? { serviceType: otherServiceType, requestedAt: other } : null
384
+ ];
385
+ // return { [serviceType]: current, [otherServiceType]: other }
386
+ };
387
+ export const getNextInterval = (requestedIso, tz, interval) => {
388
+ const date = isoToDate(requestedIso, tz);
389
+ const intervals = interval === 15 ? [0, 15, 30, 45, 60] : [0, 30, 60];
390
+ const nextInterval = intervals.filter(i => i >= date.getMinutes())[0];
391
+ const hours = nextInterval === 60 ? date.getHours() + 1 : date.getHours();
392
+ const minutes = nextInterval === 60 ? 0 : nextInterval;
393
+ date.setHours(hours);
394
+ date.setMinutes(minutes);
395
+ date.setSeconds(0);
396
+ date.setMilliseconds(0);
397
+ return date;
398
+ };
399
+ export const adjustRequestedAt = (requestedIso, tz, interval, leadTime) => {
400
+ const nextDate = getNextInterval(requestedIso, tz, interval);
401
+ const adjusted = add(nextDate, { minutes: leadTime });
402
+ return dateToIso(adjusted, tz);
403
+ };
404
+ export const getFirstTime = (revenueCenter, serviceType, tz) => {
405
+ var _a;
406
+ const { first_times, order_times } = revenueCenter;
407
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
408
+ if (!first_times || !first_times[st]) {
409
+ if (!order_times || !order_times[st])
410
+ return null;
411
+ const orderTimes = makeOrderTimes((_a = order_times[st]) !== null && _a !== void 0 ? _a : [], tz);
412
+ return orderTimes[0];
413
+ }
414
+ return first_times[st];
415
+ };
416
+ export const makeGroupOrderTime = (revenueCenter, serviceType, requestedAt) => {
417
+ const { timezone } = revenueCenter || {};
418
+ const settings = revenueCenter;
419
+ const tz = timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Eastern'];
420
+ const { first_times, order_times, wait_times, group_ordering } = settings;
421
+ const { prep_time, lead_time } = group_ordering || {};
422
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
423
+ const waitTime = wait_times && wait_times[st] ? wait_times[st] : 0;
424
+ const prepTime = `${(waitTime !== null && waitTime !== void 0 ? waitTime : 0) + (prep_time !== null && prep_time !== void 0 ? prep_time : 0)}`;
425
+ if (requestedAt === 'asap')
426
+ return { prepTime };
427
+ const firstTime = first_times && first_times[st] ? first_times[st] : null;
428
+ const orderTimes = order_times && order_times[st] ? order_times[st] : null;
429
+ if (!firstTime && !orderTimes)
430
+ return null;
431
+ let adjustedIso, adjustedDate, cutoffDate, firstIso;
432
+ if (orderTimes) {
433
+ const orderTime = findOrderTime(orderTimes, tz, requestedAt);
434
+ adjustedIso = orderTime.iso;
435
+ adjustedDate = isoToDate(adjustedIso, tz);
436
+ cutoffDate = orderTime.order_by.date;
437
+ const allOrderTimes = makeOrderTimes(orderTimes, tz);
438
+ firstIso = allOrderTimes[0].iso;
439
+ }
440
+ else {
441
+ // first available time depends on both suggested lead time and extra prep time
442
+ // lead_time = how much time cart guests have to place their orders
443
+ // prep_time = how much extra prep time is required for group orders
444
+ // over and above the pickup or delivery wait time
445
+ if (!firstTime)
446
+ return null;
447
+ const leadTime = (prep_time !== null && prep_time !== void 0 ? prep_time : 0) + (lead_time !== null && lead_time !== void 0 ? lead_time : 0);
448
+ const interval = 15;
449
+ firstIso = adjustRequestedAt(firstTime.utc, tz, interval, leadTime);
450
+ const firstDate = isoToDate(firstIso, tz);
451
+ const requestedDate = isoToDate(requestedAt, tz);
452
+ adjustedIso =
453
+ requestedDate && requestedDate > firstDate ? requestedAt : firstIso;
454
+ adjustedDate = isoToDate(adjustedIso, tz);
455
+ cutoffDate = sub(adjustedDate, { minutes: parseFloat(prepTime) });
456
+ }
457
+ const cutoffIso = dateToIso(cutoffDate, tz);
458
+ return {
459
+ isAdjusted: requestedAt !== adjustedIso,
460
+ firstIso,
461
+ iso: adjustedIso,
462
+ date: adjustedDate,
463
+ dateStr: makeReadableDateStrFromIso(adjustedIso, tz, true),
464
+ cutoffIso,
465
+ cutoffDate,
466
+ cutoffDateStr: makeReadableDateStrFromIso(cutoffIso, tz, true)
467
+ };
468
+ };
469
+ export const formatTime = (time) => {
470
+ return time
471
+ ? time.replace('Today', 'today').replace('Tomorrow', 'tomorrow')
472
+ : '';
473
+ };
474
+ export const makeGroupOrderTimeStr = (requestedAt, tz) => {
475
+ if (!requestedAt || requestedAt.toLowerCase() === 'asap')
476
+ return 'ASAP';
477
+ const orderTime = makeReadableDateStrFromIso(requestedAt, tz, true);
478
+ return orderTime ? formatTime(orderTime) : null;
479
+ };
480
+ export const getLastInterval = (tz) => {
481
+ const date = isoToDate(new Date().toISOString(), tz);
482
+ date.setHours(23);
483
+ date.setMinutes(45);
484
+ date.setSeconds(0);
485
+ date.setMilliseconds(0);
486
+ return date;
487
+ };
488
+ export const makeIntervals = (tz) => {
489
+ const nextInterval = getNextInterval(new Date().toISOString(), tz, 15);
490
+ const lastInterval = getLastInterval(tz);
491
+ const diff = Math.max(differenceInMinutes(lastInterval, nextInterval), 0);
492
+ const steps = 24 * 4 + (diff ? Math.ceil(diff / 15) : 0);
493
+ let start = sub(nextInterval, { minutes: 24 * 60 });
494
+ const intervals = [];
495
+ for (let step = 0; step < steps; step++) {
496
+ const end = add(start, { minutes: 15 });
497
+ intervals.push({ start, end, orders: [] });
498
+ start = end;
499
+ }
500
+ return intervals;
501
+ };
502
+ export const formatTimeList = (dateStr, tz, includeDate) => {
503
+ const date = isoToDate(dateStr, tz);
504
+ const fmt = includeDate ? 'MMM d, h:mma' : 'h:mma';
505
+ return format(date, fmt).replace('AM', 'am').replace('PM', 'pm');
506
+ };
507
+ export const makeCartDateStr = (requestedAt, tz, waitTime) => {
508
+ if (requestedAt === 'asap')
509
+ return `Ready in about ${waitTime} minutes`;
510
+ const requestedDateStr = isoToDateStr(requestedAt, tz, 'yyyy-MM-dd');
511
+ const now = new Date();
512
+ const today = dateToZonedDateStr(now, tz, 'yyyy-MM-dd');
513
+ const tomorrow = dateToZonedDateStr(now, tz, 'yyyy-MM-dd', 1);
514
+ const isToday = requestedDateStr === today;
515
+ const isTomorrow = requestedDateStr === tomorrow;
516
+ const timeStr = isoToDateStr(requestedAt, tz, 'h:mma').toLowerCase();
517
+ const dateStr = isoToDateStr(requestedAt, tz, 'EEE, MMM dd');
518
+ if (isToday)
519
+ return `Ready by ${timeStr}`;
520
+ if (isTomorrow)
521
+ return `Ready by ${timeStr} tomorrow`;
522
+ return `Ready by ${timeStr} on ${dateStr}`;
523
+ };
524
+ export const makeOrderWindow = (orderTime) => {
525
+ if (orderTime.start_time === orderTime.end_time) {
526
+ return `@ ${time24ToDateStr(orderTime.start_time)}`;
527
+ }
528
+ else {
529
+ return `from ${time24ToDateStr(orderTime.start_time)} to ${time24ToDateStr(orderTime.end_time)}`;
530
+ }
531
+ };