resources-f170fbe8.js 6.1 KB

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