vuetify.mjs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. // Utilities
  2. import { createRange, padStart } from "../../../util/index.mjs"; // Types
  3. const firstDay = {
  4. '001': 1,
  5. AD: 1,
  6. AE: 6,
  7. AF: 6,
  8. AG: 0,
  9. AI: 1,
  10. AL: 1,
  11. AM: 1,
  12. AN: 1,
  13. AR: 1,
  14. AS: 0,
  15. AT: 1,
  16. AU: 1,
  17. AX: 1,
  18. AZ: 1,
  19. BA: 1,
  20. BD: 0,
  21. BE: 1,
  22. BG: 1,
  23. BH: 6,
  24. BM: 1,
  25. BN: 1,
  26. BR: 0,
  27. BS: 0,
  28. BT: 0,
  29. BW: 0,
  30. BY: 1,
  31. BZ: 0,
  32. CA: 0,
  33. CH: 1,
  34. CL: 1,
  35. CM: 1,
  36. CN: 1,
  37. CO: 0,
  38. CR: 1,
  39. CY: 1,
  40. CZ: 1,
  41. DE: 1,
  42. DJ: 6,
  43. DK: 1,
  44. DM: 0,
  45. DO: 0,
  46. DZ: 6,
  47. EC: 1,
  48. EE: 1,
  49. EG: 6,
  50. ES: 1,
  51. ET: 0,
  52. FI: 1,
  53. FJ: 1,
  54. FO: 1,
  55. FR: 1,
  56. GB: 1,
  57. 'GB-alt-variant': 0,
  58. GE: 1,
  59. GF: 1,
  60. GP: 1,
  61. GR: 1,
  62. GT: 0,
  63. GU: 0,
  64. HK: 0,
  65. HN: 0,
  66. HR: 1,
  67. HU: 1,
  68. ID: 0,
  69. IE: 1,
  70. IL: 0,
  71. IN: 0,
  72. IQ: 6,
  73. IR: 6,
  74. IS: 1,
  75. IT: 1,
  76. JM: 0,
  77. JO: 6,
  78. JP: 0,
  79. KE: 0,
  80. KG: 1,
  81. KH: 0,
  82. KR: 0,
  83. KW: 6,
  84. KZ: 1,
  85. LA: 0,
  86. LB: 1,
  87. LI: 1,
  88. LK: 1,
  89. LT: 1,
  90. LU: 1,
  91. LV: 1,
  92. LY: 6,
  93. MC: 1,
  94. MD: 1,
  95. ME: 1,
  96. MH: 0,
  97. MK: 1,
  98. MM: 0,
  99. MN: 1,
  100. MO: 0,
  101. MQ: 1,
  102. MT: 0,
  103. MV: 5,
  104. MX: 0,
  105. MY: 1,
  106. MZ: 0,
  107. NI: 0,
  108. NL: 1,
  109. NO: 1,
  110. NP: 0,
  111. NZ: 1,
  112. OM: 6,
  113. PA: 0,
  114. PE: 0,
  115. PH: 0,
  116. PK: 0,
  117. PL: 1,
  118. PR: 0,
  119. PT: 0,
  120. PY: 0,
  121. QA: 6,
  122. RE: 1,
  123. RO: 1,
  124. RS: 1,
  125. RU: 1,
  126. SA: 0,
  127. SD: 6,
  128. SE: 1,
  129. SG: 0,
  130. SI: 1,
  131. SK: 1,
  132. SM: 1,
  133. SV: 0,
  134. SY: 6,
  135. TH: 0,
  136. TJ: 1,
  137. TM: 1,
  138. TR: 1,
  139. TT: 0,
  140. TW: 0,
  141. UA: 1,
  142. UM: 0,
  143. US: 0,
  144. UY: 1,
  145. UZ: 1,
  146. VA: 1,
  147. VE: 0,
  148. VI: 0,
  149. VN: 1,
  150. WS: 0,
  151. XK: 1,
  152. YE: 0,
  153. ZA: 0,
  154. ZW: 0
  155. };
  156. function getWeekArray(date, locale, firstDayOfWeek) {
  157. const weeks = [];
  158. let currentWeek = [];
  159. const firstDayOfMonth = startOfMonth(date);
  160. const lastDayOfMonth = endOfMonth(date);
  161. const first = firstDayOfWeek ?? firstDay[locale.slice(-2).toUpperCase()] ?? 0;
  162. const firstDayWeekIndex = (firstDayOfMonth.getDay() - first + 7) % 7;
  163. const lastDayWeekIndex = (lastDayOfMonth.getDay() - first + 7) % 7;
  164. for (let i = 0; i < firstDayWeekIndex; i++) {
  165. const adjacentDay = new Date(firstDayOfMonth);
  166. adjacentDay.setDate(adjacentDay.getDate() - (firstDayWeekIndex - i));
  167. currentWeek.push(adjacentDay);
  168. }
  169. for (let i = 1; i <= lastDayOfMonth.getDate(); i++) {
  170. const day = new Date(date.getFullYear(), date.getMonth(), i);
  171. // Add the day to the current week
  172. currentWeek.push(day);
  173. // If the current week has 7 days, add it to the weeks array and start a new week
  174. if (currentWeek.length === 7) {
  175. weeks.push(currentWeek);
  176. currentWeek = [];
  177. }
  178. }
  179. for (let i = 1; i < 7 - lastDayWeekIndex; i++) {
  180. const adjacentDay = new Date(lastDayOfMonth);
  181. adjacentDay.setDate(adjacentDay.getDate() + i);
  182. currentWeek.push(adjacentDay);
  183. }
  184. if (currentWeek.length > 0) {
  185. weeks.push(currentWeek);
  186. }
  187. return weeks;
  188. }
  189. function startOfWeek(date, locale, firstDayOfWeek) {
  190. const day = firstDayOfWeek ?? firstDay[locale.slice(-2).toUpperCase()] ?? 0;
  191. const d = new Date(date);
  192. while (d.getDay() !== day) {
  193. d.setDate(d.getDate() - 1);
  194. }
  195. return d;
  196. }
  197. function endOfWeek(date, locale) {
  198. const d = new Date(date);
  199. const lastDay = ((firstDay[locale.slice(-2).toUpperCase()] ?? 0) + 6) % 7;
  200. while (d.getDay() !== lastDay) {
  201. d.setDate(d.getDate() + 1);
  202. }
  203. return d;
  204. }
  205. function startOfMonth(date) {
  206. return new Date(date.getFullYear(), date.getMonth(), 1);
  207. }
  208. function endOfMonth(date) {
  209. return new Date(date.getFullYear(), date.getMonth() + 1, 0);
  210. }
  211. function parseLocalDate(value) {
  212. const parts = value.split('-').map(Number);
  213. // new Date() uses local time zone when passing individual date component values
  214. return new Date(parts[0], parts[1] - 1, parts[2]);
  215. }
  216. const _YYYMMDD = /^([12]\d{3}-([1-9]|0[1-9]|1[0-2])-([1-9]|0[1-9]|[12]\d|3[01]))$/;
  217. function date(value) {
  218. if (value == null) return new Date();
  219. if (value instanceof Date) return value;
  220. if (typeof value === 'string') {
  221. let parsed;
  222. if (_YYYMMDD.test(value)) {
  223. return parseLocalDate(value);
  224. } else {
  225. parsed = Date.parse(value);
  226. }
  227. if (!isNaN(parsed)) return new Date(parsed);
  228. }
  229. return null;
  230. }
  231. const sundayJanuarySecond2000 = new Date(2000, 0, 2);
  232. function getWeekdays(locale, firstDayOfWeek) {
  233. const daysFromSunday = firstDayOfWeek ?? firstDay[locale.slice(-2).toUpperCase()] ?? 0;
  234. return createRange(7).map(i => {
  235. const weekday = new Date(sundayJanuarySecond2000);
  236. weekday.setDate(sundayJanuarySecond2000.getDate() + daysFromSunday + i);
  237. return new Intl.DateTimeFormat(locale, {
  238. weekday: 'narrow'
  239. }).format(weekday);
  240. });
  241. }
  242. function format(value, formatString, locale, formats) {
  243. const newDate = date(value) ?? new Date();
  244. const customFormat = formats?.[formatString];
  245. if (typeof customFormat === 'function') {
  246. return customFormat(newDate, formatString, locale);
  247. }
  248. let options = {};
  249. switch (formatString) {
  250. case 'fullDate':
  251. options = {
  252. year: 'numeric',
  253. month: 'long',
  254. day: 'numeric'
  255. };
  256. break;
  257. case 'fullDateWithWeekday':
  258. options = {
  259. weekday: 'long',
  260. year: 'numeric',
  261. month: 'long',
  262. day: 'numeric'
  263. };
  264. break;
  265. case 'normalDate':
  266. const day = newDate.getDate();
  267. const month = new Intl.DateTimeFormat(locale, {
  268. month: 'long'
  269. }).format(newDate);
  270. return `${day} ${month}`;
  271. case 'normalDateWithWeekday':
  272. options = {
  273. weekday: 'short',
  274. day: 'numeric',
  275. month: 'short'
  276. };
  277. break;
  278. case 'shortDate':
  279. options = {
  280. month: 'short',
  281. day: 'numeric'
  282. };
  283. break;
  284. case 'year':
  285. options = {
  286. year: 'numeric'
  287. };
  288. break;
  289. case 'month':
  290. options = {
  291. month: 'long'
  292. };
  293. break;
  294. case 'monthShort':
  295. options = {
  296. month: 'short'
  297. };
  298. break;
  299. case 'monthAndYear':
  300. options = {
  301. month: 'long',
  302. year: 'numeric'
  303. };
  304. break;
  305. case 'monthAndDate':
  306. options = {
  307. month: 'long',
  308. day: 'numeric'
  309. };
  310. break;
  311. case 'weekday':
  312. options = {
  313. weekday: 'long'
  314. };
  315. break;
  316. case 'weekdayShort':
  317. options = {
  318. weekday: 'short'
  319. };
  320. break;
  321. case 'dayOfMonth':
  322. return new Intl.NumberFormat(locale).format(newDate.getDate());
  323. case 'hours12h':
  324. options = {
  325. hour: 'numeric',
  326. hour12: true
  327. };
  328. break;
  329. case 'hours24h':
  330. options = {
  331. hour: 'numeric',
  332. hour12: false
  333. };
  334. break;
  335. case 'minutes':
  336. options = {
  337. minute: 'numeric'
  338. };
  339. break;
  340. case 'seconds':
  341. options = {
  342. second: 'numeric'
  343. };
  344. break;
  345. case 'fullTime':
  346. options = {
  347. hour: 'numeric',
  348. minute: 'numeric',
  349. second: 'numeric',
  350. hour12: true
  351. };
  352. break;
  353. case 'fullTime12h':
  354. options = {
  355. hour: 'numeric',
  356. minute: 'numeric',
  357. second: 'numeric',
  358. hour12: true
  359. };
  360. break;
  361. case 'fullTime24h':
  362. options = {
  363. hour: 'numeric',
  364. minute: 'numeric',
  365. second: 'numeric',
  366. hour12: false
  367. };
  368. break;
  369. case 'fullDateTime':
  370. options = {
  371. year: 'numeric',
  372. month: 'long',
  373. day: 'numeric',
  374. hour: 'numeric',
  375. minute: 'numeric',
  376. second: 'numeric',
  377. hour12: true
  378. };
  379. break;
  380. case 'fullDateTime12h':
  381. options = {
  382. year: 'numeric',
  383. month: 'long',
  384. day: 'numeric',
  385. hour: 'numeric',
  386. minute: 'numeric',
  387. second: 'numeric',
  388. hour12: true
  389. };
  390. break;
  391. case 'fullDateTime24h':
  392. options = {
  393. year: 'numeric',
  394. month: 'long',
  395. day: 'numeric',
  396. hour: 'numeric',
  397. minute: 'numeric',
  398. second: 'numeric',
  399. hour12: false
  400. };
  401. break;
  402. case 'keyboardDate':
  403. options = {
  404. year: 'numeric',
  405. month: '2-digit',
  406. day: '2-digit'
  407. };
  408. break;
  409. case 'keyboardDateTime':
  410. options = {
  411. year: 'numeric',
  412. month: '2-digit',
  413. day: '2-digit',
  414. hour: 'numeric',
  415. minute: 'numeric',
  416. second: 'numeric',
  417. hour12: false
  418. };
  419. break;
  420. case 'keyboardDateTime12h':
  421. options = {
  422. year: 'numeric',
  423. month: '2-digit',
  424. day: '2-digit',
  425. hour: 'numeric',
  426. minute: 'numeric',
  427. second: 'numeric',
  428. hour12: true
  429. };
  430. break;
  431. case 'keyboardDateTime24h':
  432. options = {
  433. year: 'numeric',
  434. month: '2-digit',
  435. day: '2-digit',
  436. hour: 'numeric',
  437. minute: 'numeric',
  438. second: 'numeric',
  439. hour12: false
  440. };
  441. break;
  442. default:
  443. options = customFormat ?? {
  444. timeZone: 'UTC',
  445. timeZoneName: 'short'
  446. };
  447. }
  448. return new Intl.DateTimeFormat(locale, options).format(newDate);
  449. }
  450. function toISO(adapter, value) {
  451. const date = adapter.toJsDate(value);
  452. const year = date.getFullYear();
  453. const month = padStart(String(date.getMonth() + 1), 2, '0');
  454. const day = padStart(String(date.getDate()), 2, '0');
  455. return `${year}-${month}-${day}`;
  456. }
  457. function parseISO(value) {
  458. const [year, month, day] = value.split('-').map(Number);
  459. return new Date(year, month - 1, day);
  460. }
  461. function addMinutes(date, amount) {
  462. const d = new Date(date);
  463. d.setMinutes(d.getMinutes() + amount);
  464. return d;
  465. }
  466. function addHours(date, amount) {
  467. const d = new Date(date);
  468. d.setHours(d.getHours() + amount);
  469. return d;
  470. }
  471. function addDays(date, amount) {
  472. const d = new Date(date);
  473. d.setDate(d.getDate() + amount);
  474. return d;
  475. }
  476. function addWeeks(date, amount) {
  477. const d = new Date(date);
  478. d.setDate(d.getDate() + amount * 7);
  479. return d;
  480. }
  481. function addMonths(date, amount) {
  482. const d = new Date(date);
  483. d.setDate(1);
  484. d.setMonth(d.getMonth() + amount);
  485. return d;
  486. }
  487. function getYear(date) {
  488. return date.getFullYear();
  489. }
  490. function getMonth(date) {
  491. return date.getMonth();
  492. }
  493. function getDate(date) {
  494. return date.getDate();
  495. }
  496. function getNextMonth(date) {
  497. return new Date(date.getFullYear(), date.getMonth() + 1, 1);
  498. }
  499. function getPreviousMonth(date) {
  500. return new Date(date.getFullYear(), date.getMonth() - 1, 1);
  501. }
  502. function getHours(date) {
  503. return date.getHours();
  504. }
  505. function getMinutes(date) {
  506. return date.getMinutes();
  507. }
  508. function startOfYear(date) {
  509. return new Date(date.getFullYear(), 0, 1);
  510. }
  511. function endOfYear(date) {
  512. return new Date(date.getFullYear(), 11, 31);
  513. }
  514. function isWithinRange(date, range) {
  515. return isAfter(date, range[0]) && isBefore(date, range[1]);
  516. }
  517. function isValid(date) {
  518. const d = new Date(date);
  519. return d instanceof Date && !isNaN(d.getTime());
  520. }
  521. function isAfter(date, comparing) {
  522. return date.getTime() > comparing.getTime();
  523. }
  524. function isAfterDay(date, comparing) {
  525. return isAfter(startOfDay(date), startOfDay(comparing));
  526. }
  527. function isBefore(date, comparing) {
  528. return date.getTime() < comparing.getTime();
  529. }
  530. function isEqual(date, comparing) {
  531. return date.getTime() === comparing.getTime();
  532. }
  533. function isSameDay(date, comparing) {
  534. return date.getDate() === comparing.getDate() && date.getMonth() === comparing.getMonth() && date.getFullYear() === comparing.getFullYear();
  535. }
  536. function isSameMonth(date, comparing) {
  537. return date.getMonth() === comparing.getMonth() && date.getFullYear() === comparing.getFullYear();
  538. }
  539. function isSameYear(date, comparing) {
  540. return date.getFullYear() === comparing.getFullYear();
  541. }
  542. function getDiff(date, comparing, unit) {
  543. const d = new Date(date);
  544. const c = new Date(comparing);
  545. switch (unit) {
  546. case 'years':
  547. return d.getFullYear() - c.getFullYear();
  548. case 'quarters':
  549. return Math.floor((d.getMonth() - c.getMonth() + (d.getFullYear() - c.getFullYear()) * 12) / 4);
  550. case 'months':
  551. return d.getMonth() - c.getMonth() + (d.getFullYear() - c.getFullYear()) * 12;
  552. case 'weeks':
  553. return Math.floor((d.getTime() - c.getTime()) / (1000 * 60 * 60 * 24 * 7));
  554. case 'days':
  555. return Math.floor((d.getTime() - c.getTime()) / (1000 * 60 * 60 * 24));
  556. case 'hours':
  557. return Math.floor((d.getTime() - c.getTime()) / (1000 * 60 * 60));
  558. case 'minutes':
  559. return Math.floor((d.getTime() - c.getTime()) / (1000 * 60));
  560. case 'seconds':
  561. return Math.floor((d.getTime() - c.getTime()) / 1000);
  562. default:
  563. {
  564. return d.getTime() - c.getTime();
  565. }
  566. }
  567. }
  568. function setHours(date, count) {
  569. const d = new Date(date);
  570. d.setHours(count);
  571. return d;
  572. }
  573. function setMinutes(date, count) {
  574. const d = new Date(date);
  575. d.setMinutes(count);
  576. return d;
  577. }
  578. function setMonth(date, count) {
  579. const d = new Date(date);
  580. d.setMonth(count);
  581. return d;
  582. }
  583. function setDate(date, day) {
  584. const d = new Date(date);
  585. d.setDate(day);
  586. return d;
  587. }
  588. function setYear(date, year) {
  589. const d = new Date(date);
  590. d.setFullYear(year);
  591. return d;
  592. }
  593. function startOfDay(date) {
  594. return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0);
  595. }
  596. function endOfDay(date) {
  597. return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59, 999);
  598. }
  599. export class VuetifyDateAdapter {
  600. constructor(options) {
  601. this.locale = options.locale;
  602. this.formats = options.formats;
  603. }
  604. date(value) {
  605. return date(value);
  606. }
  607. toJsDate(date) {
  608. return date;
  609. }
  610. toISO(date) {
  611. return toISO(this, date);
  612. }
  613. parseISO(date) {
  614. return parseISO(date);
  615. }
  616. addMinutes(date, amount) {
  617. return addMinutes(date, amount);
  618. }
  619. addHours(date, amount) {
  620. return addHours(date, amount);
  621. }
  622. addDays(date, amount) {
  623. return addDays(date, amount);
  624. }
  625. addWeeks(date, amount) {
  626. return addWeeks(date, amount);
  627. }
  628. addMonths(date, amount) {
  629. return addMonths(date, amount);
  630. }
  631. getWeekArray(date, firstDayOfWeek) {
  632. return getWeekArray(date, this.locale, firstDayOfWeek ? Number(firstDayOfWeek) : undefined);
  633. }
  634. startOfWeek(date, firstDayOfWeek) {
  635. return startOfWeek(date, this.locale, firstDayOfWeek ? Number(firstDayOfWeek) : undefined);
  636. }
  637. endOfWeek(date) {
  638. return endOfWeek(date, this.locale);
  639. }
  640. startOfMonth(date) {
  641. return startOfMonth(date);
  642. }
  643. endOfMonth(date) {
  644. return endOfMonth(date);
  645. }
  646. format(date, formatString) {
  647. return format(date, formatString, this.locale, this.formats);
  648. }
  649. isEqual(date, comparing) {
  650. return isEqual(date, comparing);
  651. }
  652. isValid(date) {
  653. return isValid(date);
  654. }
  655. isWithinRange(date, range) {
  656. return isWithinRange(date, range);
  657. }
  658. isAfter(date, comparing) {
  659. return isAfter(date, comparing);
  660. }
  661. isAfterDay(date, comparing) {
  662. return isAfterDay(date, comparing);
  663. }
  664. isBefore(date, comparing) {
  665. return !isAfter(date, comparing) && !isEqual(date, comparing);
  666. }
  667. isSameDay(date, comparing) {
  668. return isSameDay(date, comparing);
  669. }
  670. isSameMonth(date, comparing) {
  671. return isSameMonth(date, comparing);
  672. }
  673. isSameYear(date, comparing) {
  674. return isSameYear(date, comparing);
  675. }
  676. setMinutes(date, count) {
  677. return setMinutes(date, count);
  678. }
  679. setHours(date, count) {
  680. return setHours(date, count);
  681. }
  682. setMonth(date, count) {
  683. return setMonth(date, count);
  684. }
  685. setDate(date, day) {
  686. return setDate(date, day);
  687. }
  688. setYear(date, year) {
  689. return setYear(date, year);
  690. }
  691. getDiff(date, comparing, unit) {
  692. return getDiff(date, comparing, unit);
  693. }
  694. getWeekdays(firstDayOfWeek) {
  695. return getWeekdays(this.locale, firstDayOfWeek ? Number(firstDayOfWeek) : undefined);
  696. }
  697. getYear(date) {
  698. return getYear(date);
  699. }
  700. getMonth(date) {
  701. return getMonth(date);
  702. }
  703. getDate(date) {
  704. return getDate(date);
  705. }
  706. getNextMonth(date) {
  707. return getNextMonth(date);
  708. }
  709. getPreviousMonth(date) {
  710. return getPreviousMonth(date);
  711. }
  712. getHours(date) {
  713. return getHours(date);
  714. }
  715. getMinutes(date) {
  716. return getMinutes(date);
  717. }
  718. startOfDay(date) {
  719. return startOfDay(date);
  720. }
  721. endOfDay(date) {
  722. return endOfDay(date);
  723. }
  724. startOfYear(date) {
  725. return startOfYear(date);
  726. }
  727. endOfYear(date) {
  728. return endOfYear(date);
  729. }
  730. }
  731. //# sourceMappingURL=vuetify.mjs.map