@webamoki/web-svelte 2.1.1 → 2.2.0

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.
@@ -766,3 +766,54 @@ describe('formatAbsolute', () => {
766
766
  expect(formatAbsolute(datetime)).toBe('15/05/2024 14:30:45');
767
767
  });
768
768
  });
769
+ describe('dateWithin', () => {
770
+ beforeEach(() => {
771
+ // Mock current date to 2024-05-15
772
+ const mockDate = new CalendarDate(2024, 5, 15);
773
+ vi.useFakeTimers();
774
+ vi.setSystemTime(mockDate.toDate(SERVER_TIME_ZONE));
775
+ });
776
+ afterEach(() => {
777
+ vi.useRealTimers();
778
+ });
779
+ it('returns true when date is today with zero duration', () => {
780
+ const today = new CalendarDate(2024, 5, 15);
781
+ expect(dt.dateWithin(today, { days: 0 })).toBe(true);
782
+ });
783
+ it('returns true when date is within the duration (days)', () => {
784
+ const twoDaysAgo = new CalendarDate(2024, 5, 13);
785
+ expect(dt.dateWithin(twoDaysAgo, { days: 2 })).toBe(true);
786
+ expect(dt.dateWithin(twoDaysAgo, { days: 3 })).toBe(true);
787
+ });
788
+ it('returns false when date is outside the duration (days)', () => {
789
+ const threeDaysAgo = new CalendarDate(2024, 5, 12);
790
+ expect(dt.dateWithin(threeDaysAgo, { days: 2 })).toBe(false);
791
+ });
792
+ it('returns false when date is in the future', () => {
793
+ const tomorrow = new CalendarDate(2024, 5, 16);
794
+ expect(dt.dateWithin(tomorrow, { days: 5 })).toBe(false);
795
+ });
796
+ it('returns true at the exact boundary', () => {
797
+ const exactBoundary = new CalendarDate(2024, 5, 8);
798
+ expect(dt.dateWithin(exactBoundary, { weeks: 1 })).toBe(true);
799
+ });
800
+ it('returns false just past the boundary', () => {
801
+ const pastBoundary = new CalendarDate(2024, 5, 7);
802
+ expect(dt.dateWithin(pastBoundary, { weeks: 1 })).toBe(false);
803
+ });
804
+ it('works with week durations', () => {
805
+ const twoWeeksAgo = new CalendarDate(2024, 5, 1);
806
+ expect(dt.dateWithin(twoWeeksAgo, { weeks: 2 })).toBe(true);
807
+ expect(dt.dateWithin(twoWeeksAgo, { weeks: 1 })).toBe(false);
808
+ });
809
+ it('works with month durations', () => {
810
+ const lastMonth = new CalendarDate(2024, 4, 15);
811
+ expect(dt.dateWithin(lastMonth, { months: 1 })).toBe(true);
812
+ expect(dt.dateWithin(lastMonth, { months: 0 })).toBe(false);
813
+ });
814
+ it('works with year durations', () => {
815
+ const lastYear = new CalendarDate(2023, 5, 15);
816
+ expect(dt.dateWithin(lastYear, { years: 1 })).toBe(true);
817
+ expect(dt.dateWithin(lastYear, { months: 11 })).toBe(false);
818
+ });
819
+ });
@@ -13,6 +13,13 @@ export declare class LocalDateF {
13
13
  * @throws Error if the date of birth is in the future.
14
14
  */
15
15
  ageFromDob(dob: CalendarDate): number;
16
+ /**
17
+ * Checks if a given date is within a specified duration before today (inclusive).
18
+ * @param date - The date to check.
19
+ * @param duration - The duration to check against.
20
+ * @returns True if the date is in the past and within the duration, false otherwise.
21
+ */
22
+ dateWithin(date: CalendarDate, duration: DateDuration): boolean;
16
23
  /**
17
24
  * Checks if a given date is today.
18
25
  * @param date - The date to check.
@@ -38,6 +38,15 @@ export class LocalDateF {
38
38
  }
39
39
  return years;
40
40
  }
41
+ /**
42
+ * Checks if a given date is within a specified duration before today (inclusive).
43
+ * @param date - The date to check.
44
+ * @param duration - The duration to check against.
45
+ * @returns True if the date is in the past and within the duration, false otherwise.
46
+ */
47
+ dateWithin(date, duration) {
48
+ return datesWithin(date, this.today(), duration);
49
+ }
41
50
  /**
42
51
  * Checks if a given date is today.
43
52
  * @param date - The date to check.
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public",
5
5
  "provenance": true
6
6
  },
7
- "version": "2.1.1",
7
+ "version": "2.2.0",
8
8
  "license": "MIT",
9
9
  "repository": {
10
10
  "type": "git",