• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IgniteUI / igniteui-angular / 13331632524

14 Feb 2025 02:51PM CUT coverage: 22.015% (-69.6%) from 91.622%
13331632524

Pull #15372

github

web-flow
Merge d52d57714 into bcb78ae0a
Pull Request #15372: chore(*): test ci passing

1990 of 15592 branches covered (12.76%)

431 of 964 new or added lines in 18 files covered. (44.71%)

19956 existing lines in 307 files now uncovered.

6452 of 29307 relevant lines covered (22.02%)

249.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/projects/igniteui-angular/src/lib/calendar/common/helpers.ts
1
import {
2
    CalendarDay,
3
    CalendarRangeParams,
4
    DayParameter,
5
    daysInWeek,
6
    toCalendarDay,
7
} from "./model";
8
import { DateRangeDescriptor, DateRangeType } from "./types";
9
import { first, last, modulo } from "../../core/utils";
10

11
interface IFormattedParts {
12
    value: string;
13
    literal: string;
14
    combined: string;
15
}
16

17
export function areSameMonth(
18
    firstMonth: DayParameter,
19
    secondMonth: DayParameter,
20
) {
UNCOV
21
    const [a, b] = [toCalendarDay(firstMonth), toCalendarDay(secondMonth)];
×
UNCOV
22
    return a.year === b.year && a.month === b.month;
×
23
}
24

25
export function isNextMonth(target: DayParameter, origin: DayParameter) {
UNCOV
26
    const [a, b] = [toCalendarDay(target), toCalendarDay(origin)];
×
UNCOV
27
    return a.year === b.year ? a.month > b.month : a.year > b.year;
×
28
}
29

30
export function isPreviousMonth(target: DayParameter, origin: DayParameter) {
UNCOV
31
    const [a, b] = [toCalendarDay(target), toCalendarDay(origin)];
×
UNCOV
32
    return a.year === b.year ? a.month < b.month : a.year < b.year;
×
33
}
34

35
/** Returns the next date starting from `start` that does not match the `disabled` descriptors */
36
export function getNextActiveDate(
37
    start: CalendarDay,
38
    disabled: DateRangeDescriptor[] = [],
×
39
) {
UNCOV
40
    while (isDateInRanges(start, disabled)) {
×
UNCOV
41
        start = start.add("day", 1);
×
42
    }
43

UNCOV
44
    return start;
×
45
}
46

47
/** Returns the previous date starting from `start` that does not match the `disabled` descriptors */
48
export function getPreviousActiveDate(
49
    start: CalendarDay,
50
    disabled: DateRangeDescriptor[] = [],
×
51
) {
UNCOV
52
    while (isDateInRanges(start, disabled)) {
×
UNCOV
53
        start = start.add("day", -1);
×
54
    }
55

UNCOV
56
    return start;
×
57
}
58

59
export function getClosestActiveDate(
60
    start: CalendarDay,
61
    delta: number,
62
    disabled: DateRangeDescriptor[] = [],
×
63
): CalendarDay {
64
    // TODO: implement a more robust logic for max attempts,
65
    // i.e. the amount of days to jump between before giving up
66
    // currently it will try to find the closest date for a year
UNCOV
67
    const maxAttempts = 366;
×
UNCOV
68
    let date = start;
×
UNCOV
69
    let attempts = 0;
×
70

UNCOV
71
    while (attempts < maxAttempts) {
×
UNCOV
72
        date = start.add("day", delta * (attempts + 1));
×
73

UNCOV
74
        if (!isDateInRanges(date, disabled)) {
×
UNCOV
75
            return date;
×
76
        }
77

UNCOV
78
        attempts++;
×
79
    }
80

81
    return date;
×
82
}
83

84
/**
85
 * Returns a generator yielding day values between `start` and `end` (non-inclusive)
86
 * by a given `unit` as a step.
87
 *
88
 * @remarks
89
 * By default, `unit` is set to 'day'.
90
 */
91
export function* calendarRange(options: CalendarRangeParams) {
UNCOV
92
    let low = toCalendarDay(options.start);
×
UNCOV
93
    const unit = options.unit ?? "day";
×
94
    const high =
UNCOV
95
        typeof options.end === "number"
×
96
            ? low.add(unit, options.end)
97
            : toCalendarDay(options.end);
98

UNCOV
99
    const reverse = high.lessThan(low);
×
UNCOV
100
    const step = reverse ? -1 : 1;
×
101

UNCOV
102
    while (!reverse ? low.lessThan(high) : low.greaterThan(high)) {
×
UNCOV
103
        yield low;
×
UNCOV
104
        low = low.add(unit, step);
×
105
    }
106
}
107

108
export function* generateMonth(value: DayParameter, firstWeekDay: number) {
UNCOV
109
    const { year, month } = toCalendarDay(value);
×
110

UNCOV
111
    const start = new CalendarDay({ year, month });
×
UNCOV
112
    const offset = modulo(start.day - firstWeekDay, daysInWeek);
×
UNCOV
113
    yield* calendarRange({
×
114
        start: start.add("day", -offset),
115
        end: 42,
116
    });
117
}
118

119
export function getYearRange(current: DayParameter, range: number) {
UNCOV
120
    const year = toCalendarDay(current).year;
×
UNCOV
121
    const start = Math.floor(year / range) * range;
×
UNCOV
122
    return { start, end: start + range - 1 };
×
123
}
124

125
export function isDateInRanges(
126
    date: DayParameter,
127
    ranges: DateRangeDescriptor[],
128
) {
UNCOV
129
    const value = toCalendarDay(date);
×
130

UNCOV
131
    return ranges.some((range) => {
×
UNCOV
132
        const days = (range.dateRange ?? []).map((day) => toCalendarDay(day));
×
133

UNCOV
134
        switch (range.type) {
×
135
            case DateRangeType.After:
UNCOV
136
                return value.greaterThan(first(days));
×
137

138
            case DateRangeType.Before:
UNCOV
139
                return value.lessThan(first(days));
×
140

141
            case DateRangeType.Between: {
UNCOV
142
                const min = Math.min(
×
143
                    first(days).timestamp,
144
                    last(days).timestamp,
145
                );
UNCOV
146
                const max = Math.max(
×
147
                    first(days).timestamp,
148
                    last(days).timestamp,
149
                );
UNCOV
150
                return value.timestamp >= min && value.timestamp <= max;
×
151
            }
152

153
            case DateRangeType.Specific:
UNCOV
154
                return days.some((day) => day.equalTo(value));
×
155

156
            case DateRangeType.Weekdays:
UNCOV
157
                return !value.weekend;
×
158

159
            case DateRangeType.Weekends:
UNCOV
160
                return value.weekend;
×
161

162
            default:
163
                return false;
×
164
        }
165
    });
166
}
167

168
export function formatToParts(
169
    date: Date,
170
    locale: string,
171
    options: Intl.DateTimeFormatOptions,
172
    parts: string[],
173
): Record<string, any> {
UNCOV
174
    const formatter = new Intl.DateTimeFormat(locale, options);
×
UNCOV
175
    const result: Record<string, any> = {
×
176
        date,
177
        full: formatter.format(date),
178
    };
179

UNCOV
180
    const getFormattedPart = (
×
181
        formattedParts: Intl.DateTimeFormatPart[],
182
        partType: string,
183
    ): IFormattedParts => {
UNCOV
184
        const part = formattedParts.find(({ type }) => type === partType);
×
UNCOV
185
        const nextPart = formattedParts[formattedParts.indexOf(part) + 1];
×
UNCOV
186
        const value = part?.value || "";
×
UNCOV
187
        const literal = nextPart?.type === "literal" ? nextPart.value : "";
×
UNCOV
188
        return {
×
189
            value,
190
            literal,
191
            combined: value + literal,
192
        };
193
    };
194

UNCOV
195
    if ("formatToParts" in formatter) {
×
UNCOV
196
        const formattedParts = formatter.formatToParts(date);
×
UNCOV
197
        parts.forEach(
×
UNCOV
198
            (part) => (result[part] = getFormattedPart(formattedParts, part)),
×
199
        );
200
    } else {
201
        parts.forEach(
×
202
            (part) => (result[part] = { value: "", literal: "", combined: "" }),
×
203
        );
204
    }
205

UNCOV
206
    return result;
×
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc