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

adobe / spectrum-web-components / 12693741474

09 Jan 2025 04:03PM UTC coverage: 98.259% (+0.05%) from 98.206%
12693741474

Pull #5002

github

web-flow
Merge 3502508d0 into 5bf31e817
Pull Request #5002: feat: Calendar and DateTimePicker components

5677 of 5977 branches covered (94.98%)

Branch coverage included in aggregate %.

2878 of 2891 new or added lines in 26 files covered. (99.55%)

3 existing lines in 1 file now uncovered.

35918 of 36355 relevant lines covered (98.8%)

1123.79 hits per line

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

98.84
/packages/date-time-picker/src/segments/SegmentsFactory.ts
1
/*
2✔
2
Copyright 2024 Adobe. All rights reserved.
2✔
3
This file is licensed to you under the Apache License, Version 2.0 (the "License");
2✔
4
you may not use this file except in compliance with the License. You may obtain a copy
2✔
5
of the License at http://www.apache.org/licenses/LICENSE-2.0
2✔
6

2✔
7
Unless required by applicable law or agreed to in writing, software distributed under
2✔
8
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
2✔
9
OF ANY KIND, either express or implied. See the License for the specific language
2✔
10
governing permissions and limitations under the License.
2✔
11
*/
2✔
12

2✔
13
import { DateFormatter, ZonedDateTime } from '@internationalized/date';
2✔
14
import { NumberFormatter } from '@internationalized/number';
2✔
15
import { dateValueToDate } from '../helpers';
2✔
16
import {
2✔
17
    EditableSegmentType,
2✔
18
    LiteralSegmentType,
2✔
19
    SegmentType,
2✔
20
    SegmentTypes,
2✔
21
} from '../types';
2✔
22
import { DateTimeSegments, Segment } from './DateTimeSegments';
2✔
23
import { LiteralSegment } from './LiteralSegment';
2✔
24
import { DaySegment } from './date/DaySegment';
2✔
25
import { MonthSegment } from './date/MonthSegment';
2✔
26
import { YearSegment } from './date/YearSegment';
2✔
27
import { DayPeriodSegment } from './time/DayPeriodSegment';
2✔
28
import { HourSegment } from './time/HourSegment';
2✔
29
import { MinuteSegment } from './time/MinuteSegment';
2✔
30
import { SecondSegment } from './time/SecondSegment';
2✔
31

2✔
32
export class SegmentsFactory {
2✔
33
    private dateFormatter: DateFormatter;
2✔
34
    private numberFormatter: NumberFormatter;
2✔
35
    private dateTimeFieldDisplayNames: Intl.DisplayNames;
2✔
36

2✔
37
    constructor(dateFormatter: DateFormatter) {
2✔
38
        this.dateFormatter = dateFormatter;
2✔
39

2✔
40
        const locale = this.dateFormatter.resolvedOptions().locale;
2✔
41
        this.numberFormatter = new NumberFormatter(locale, {
531✔
42
            useGrouping: false,
531✔
43
        });
531✔
44
        this.dateTimeFieldDisplayNames = new Intl.DisplayNames([locale], {
531✔
45
            type: 'dateTimeField',
531✔
46
        });
531✔
47
    }
531✔
48

531✔
49
    /**
531✔
50
     * Creates the `DateTimeSegments` needed for the DateTimePicker
531✔
51
     * @param currentDate - The date to create the segments from. This is used to set the limits and values of the segments.
531✔
52
     * @param setValues - If true, the segments will have their values set from the currentDate. If false, the segments will have no values.
531✔
53
     */
531✔
54
    createSegments(
531✔
55
        currentDate: ZonedDateTime,
531✔
56
        setValues: boolean = false
531✔
57
    ): DateTimeSegments {
531✔
58
        const date = dateValueToDate(currentDate);
531✔
59

531✔
60
        const createdSegments = this.dateFormatter
531✔
61
            .formatToParts(date)
531✔
62
            .map((part) => {
2✔
63
                const type = part.type as SegmentType;
531✔
64
                let formatted = part.value;
531✔
65
                // Avoid unexpected display (e.g., "2" becoming "1902").
531✔
66
                if (type === 'year')
531✔
67
                    formatted = this.numberFormatter.format(currentDate.year);
531✔
68
                return this.createSegment(type, formatted);
5,907✔
69
            });
5,907✔
70

5,907✔
71
        const segments = new DateTimeSegments(createdSegments);
5,907✔
72

5,907✔
73
        const year = segments.year!;
5,907✔
74
        const month = segments.month!;
531✔
75
        const day = segments.day!;
531✔
76

531✔
77
        year.setLimits(currentDate);
531✔
78
        month.setLimits(currentDate);
5,907✔
79
        if (setValues) {
5,907✔
80
            year.setValueFromDate(currentDate);
531✔
81
            month.setValueFromDate(currentDate);
531✔
82
        }
531✔
83

531✔
84
        day.setLimits(currentDate, month.value, year.value);
531✔
85
        if (setValues) day.setValueFromDate(currentDate);
531✔
86

124✔
87
        const hour = segments.hour;
124✔
88
        const minute = segments.minute;
124✔
89
        const second = segments.second;
531✔
90
        const dayPeriod = segments.dayPeriod;
531✔
91

531✔
92
        if (dayPeriod) dayPeriod.setLocalizedLimits(this.dateFormatter);
531✔
93

531✔
94
        if (!hour) return segments;
531✔
95

531✔
96
        const is12HourFormat = Boolean(dayPeriod);
531✔
97
        hour.setLimits(is12HourFormat);
531✔
98
        if (setValues) {
484✔
99
            hour.setValueFromDate(currentDate, is12HourFormat);
484✔
100

484✔
101
            if (is12HourFormat) {
484✔
102
                const dayPeriod = segments.dayPeriod!;
484✔
103
                dayPeriod.setValueFromDate(currentDate);
531✔
104
            }
503✔
105

503✔
106
            if (!minute) return segments;
503✔
107
            minute.setValueFromDate(currentDate);
531✔
108

117✔
109
            if (!second) return segments;
117✔
110
            second.setValueFromDate(currentDate);
117✔
111
        }
108✔
112

108✔
113
        return segments;
108✔
114
    }
108✔
115

108✔
116
    private createSegment(
108✔
117
        type: EditableSegmentType | LiteralSegmentType,
111✔
118
        formatted: string
111✔
119
    ): Segment {
111✔
120
        if (type === SegmentTypes.Literal) return new LiteralSegment(formatted);
117✔
121

44✔
122
        const label = this.displayNameOfType(type);
2✔
123

5,907✔
124
        switch (type) {
5,907✔
125
            case SegmentTypes.Year:
5,907✔
126
                return new YearSegment(formatted, label);
5,907✔
127
            case SegmentTypes.Month:
5,907✔
128
                return new MonthSegment(formatted, label);
3,219✔
129
            case SegmentTypes.Day:
3,219✔
130
                return new DaySegment(formatted, label);
5,907✔
131
            case SegmentTypes.Hour:
531✔
132
                return new HourSegment(formatted, label);
531✔
133
            case SegmentTypes.Minute:
531✔
134
                return new MinuteSegment(formatted, label);
531✔
135
            case SegmentTypes.Second:
531✔
136
                return new SecondSegment(formatted, label);
5,907✔
137
            case SegmentTypes.DayPeriod:
503✔
138
                return new DayPeriodSegment(formatted, label);
489✔
139
        }
489✔
140
    }
489✔
141

489✔
142
    private displayNameOfType(type: EditableSegmentType): string {
5,907✔
143
        const label = this.dateTimeFieldDisplayNames.of(type);
5,907✔
144
        if (!label) return '';
484✔
145

484✔
146
        return label.charAt(0).toUpperCase() + label.slice(1);
2✔
147
    }
3,219✔
NEW
148
}
×
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