resources-8812a15c.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { a as getAssetPath } from './index-1f9b54dc.js';
  7. import { n as numberStringFormatter, g as getSupportedLocale } from './locale-35f81208.js';
  8. /**
  9. * Check if date is within a min and max
  10. *
  11. * @param date
  12. * @param min
  13. * @param max
  14. */
  15. function inRange(date, min, max) {
  16. const time = date.getTime();
  17. const afterMin = !(min instanceof Date) || time >= min.getTime();
  18. const beforeMax = !(max instanceof Date) || time <= max.getTime();
  19. return afterMin && beforeMax;
  20. }
  21. /**
  22. * Ensures date is within range,
  23. * returns min or max if out of bounds
  24. *
  25. * @param date
  26. * @param min
  27. * @param max
  28. */
  29. function dateFromRange(date, min, max) {
  30. if (!(date instanceof Date)) {
  31. return null;
  32. }
  33. const time = date.getTime();
  34. const beforeMin = min instanceof Date && time < min.getTime();
  35. const afterMax = max instanceof Date && time > max.getTime();
  36. if (beforeMin) {
  37. return min;
  38. }
  39. if (afterMax) {
  40. return max;
  41. }
  42. return date;
  43. }
  44. /**
  45. * Parse an iso8601 string (YYYY-mm-dd) into a valid date.
  46. * TODO: handle time when time of day UI is added
  47. *
  48. * @param iso8601
  49. * @param isEndDate
  50. */
  51. function dateFromISO(iso8601, isEndDate = false) {
  52. if (iso8601 instanceof Date) {
  53. return iso8601;
  54. }
  55. if (!iso8601 || typeof iso8601 !== "string") {
  56. return null;
  57. }
  58. const d = iso8601.split(/[: T-]/).map(parseFloat);
  59. const date = new Date(d[0], (d[1] || 1) - 1, d[2] || 1);
  60. date.setFullYear(d[0]);
  61. if (isNaN(date.getTime())) {
  62. throw new Error(`Invalid ISO 8601 date: "${iso8601}"`);
  63. }
  64. if (isEndDate) {
  65. return setEndOfDay(date);
  66. }
  67. return date;
  68. }
  69. /**
  70. * Return first portion of ISO string (YYYY-mm-dd)
  71. *
  72. * @param date
  73. */
  74. function dateToISO(date) {
  75. if (typeof date === "string") {
  76. return date;
  77. }
  78. if (date instanceof Date) {
  79. return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().split("T")[0];
  80. }
  81. return "";
  82. }
  83. /**
  84. * Check if two dates are the same day, month, year
  85. *
  86. * @param d1
  87. * @param d2
  88. */
  89. function sameDate(d1, d2) {
  90. return (d1 instanceof Date &&
  91. d2 instanceof Date &&
  92. d1.getDate() === d2.getDate() &&
  93. d1.getMonth() === d2.getMonth() &&
  94. d1.getFullYear() === d2.getFullYear());
  95. }
  96. /**
  97. * Get a date one month in the past
  98. *
  99. * @param date
  100. */
  101. function prevMonth(date) {
  102. const month = date.getMonth();
  103. const nextDate = new Date(date);
  104. nextDate.setMonth(month - 1);
  105. // date doesn't exist in new month, use last day
  106. if (month === nextDate.getMonth()) {
  107. return new Date(date.getFullYear(), month, 0);
  108. }
  109. return nextDate;
  110. }
  111. /**
  112. * Get a date one month in the future
  113. *
  114. * @param date
  115. */
  116. function nextMonth(date) {
  117. const month = date.getMonth();
  118. const nextDate = new Date(date);
  119. nextDate.setMonth(month + 1);
  120. // date doesn't exist in new month, use last day
  121. if ((month + 2) % 7 === nextDate.getMonth() % 7) {
  122. return new Date(date.getFullYear(), month + 2, 0);
  123. }
  124. return nextDate;
  125. }
  126. /**
  127. * Parse numeric units for day, month, and year from a localized string
  128. * month starts at 0 (can pass to date constructor)
  129. *
  130. * @param str
  131. * @param localeData
  132. */
  133. function parseDateString(str, localeData) {
  134. const { separator, unitOrder } = localeData;
  135. const order = getOrder(unitOrder);
  136. const values = str.split(separator).map((part) => numberStringFormatter.delocalize(part));
  137. return {
  138. day: parseInt(values[order.indexOf("d")]),
  139. month: parseInt(values[order.indexOf("m")]) - 1,
  140. year: parseInt(values[order.indexOf("y")])
  141. };
  142. }
  143. /**
  144. * Based on the unitOrder string, find order of month, day, and year for locale
  145. *
  146. * @param unitOrder
  147. */
  148. function getOrder(unitOrder) {
  149. const signifiers = ["d", "m", "y"];
  150. const order = unitOrder.toLowerCase();
  151. return signifiers.sort((a, b) => order.indexOf(a) - order.indexOf(b));
  152. }
  153. /**
  154. * Get number of days between two dates
  155. *
  156. * @param date1
  157. * @param date2
  158. */
  159. function getDaysDiff(date1, date2) {
  160. const ts1 = date1.getTime();
  161. const ts2 = date2.getTime();
  162. return (ts1 - ts2) / (1000 * 3600 * 24);
  163. }
  164. /**
  165. * Set time of the day to the end.
  166. *
  167. * @param {Date} date Date.
  168. * @returns {Date} Date with time set to end of day .
  169. */
  170. function setEndOfDay(date) {
  171. date.setHours(23, 59, 59, 999);
  172. return date;
  173. }
  174. /**
  175. * CLDR cache.
  176. * Exported for testing purposes.
  177. *
  178. * @private
  179. */
  180. const translationCache = {};
  181. /**
  182. * CLDR request cache.
  183. * Exported for testing purposes.
  184. *
  185. * @private
  186. */
  187. const requestCache = {};
  188. /**
  189. * Fetch calendar data for a given locale from list of supported languages
  190. *
  191. * @param lang
  192. * @public
  193. */
  194. async function getLocaleData(lang) {
  195. const locale = getSupportedLocale(lang);
  196. if (translationCache[locale]) {
  197. return translationCache[locale];
  198. }
  199. if (!requestCache[locale]) {
  200. requestCache[locale] = fetch(getAssetPath(`./assets/date-picker/nls/${locale}.json`))
  201. .then((resp) => resp.json())
  202. .catch(() => {
  203. console.error(`Translations for "${locale}" not found or invalid, falling back to english`);
  204. return getLocaleData("en");
  205. });
  206. }
  207. const data = await requestCache[locale];
  208. translationCache[locale] = data;
  209. return data;
  210. }
  211. /**
  212. * Maps value to valueAsDate
  213. *
  214. * @param value
  215. */
  216. function getValueAsDateRange(value) {
  217. return value.map((v, index) => dateFromISO(v, index === 1));
  218. }
  219. const HEADING_LEVEL = 2;
  220. const TEXT = {
  221. nextMonth: "Next month",
  222. prevMonth: "Previous month",
  223. year: "Year"
  224. };
  225. export { HEADING_LEVEL as H, TEXT as T, dateFromISO as a, dateFromRange as b, getLocaleData as c, dateToISO as d, sameDate as e, getDaysDiff as f, getValueAsDateRange as g, getOrder as h, inRange as i, prevMonth as j, nextMonth as n, parseDateString as p, setEndOfDay as s };