forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.ts
139 lines (119 loc) · 4.08 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {InjectionToken} from '@angular/core';
import {DateAdapter, MatDateFormats} from '../core';
/** Pattern that interval strings have to match. */
const INTERVAL_PATTERN = /^(\d*\.?\d+)\s*(h|hour|hours|m|min|minute|minutes|s|second|seconds)?$/i;
/**
* Object that can be used to configure the default options for the timepicker component.
*/
export interface MatTimepickerConfig {
/** Default interval for all time pickers. */
interval?: string | number;
/** Whether ripples inside the timepicker should be disabled by default. */
disableRipple?: boolean;
}
/**
* Injection token that can be used to configure the default options for the timepicker component.
*/
export const MAT_TIMEPICKER_CONFIG = new InjectionToken<MatTimepickerConfig>(
'MAT_TIMEPICKER_CONFIG',
);
/**
* Time selection option that can be displayed within a `mat-timepicker`.
*/
export interface MatTimepickerOption<D = unknown> {
/** Date value of the option. */
value: D;
/** Label to show to the user. */
label: string;
}
/** Parses an interval value into seconds. */
export function parseInterval(value: number | string | null): number | null {
let result: number;
if (value === null) {
return null;
} else if (typeof value === 'number') {
result = value;
} else {
if (value.trim().length === 0) {
return null;
}
const parsed = value.match(INTERVAL_PATTERN);
const amount = parsed ? parseFloat(parsed[1]) : null;
const unit = parsed?.[2]?.toLowerCase() || null;
if (!parsed || amount === null || isNaN(amount)) {
return null;
}
if (unit === 'h' || unit === 'hour' || unit === 'hours') {
result = amount * 3600;
} else if (unit === 'm' || unit === 'min' || unit === 'minute' || unit === 'minutes') {
result = amount * 60;
} else {
result = amount;
}
}
return result;
}
/**
* Generates the options to show in a timepicker.
* @param adapter Date adapter to be used to generate the options.
* @param formats Formatting config to use when displaying the options.
* @param min Time from which to start generating the options.
* @param max Time at which to stop generating the options.
* @param interval Amount of seconds between each option.
*/
export function generateOptions<D>(
adapter: DateAdapter<D>,
formats: MatDateFormats,
min: D,
max: D,
interval: number,
): MatTimepickerOption<D>[] {
const options: MatTimepickerOption<D>[] = [];
let current = adapter.compareTime(min, max) < 1 ? min : max;
while (
adapter.sameDate(current, min) &&
adapter.compareTime(current, max) < 1 &&
adapter.isValid(current)
) {
options.push({value: current, label: adapter.format(current, formats.display.timeOptionLabel)});
current = adapter.addSeconds(current, interval);
}
return options;
}
/** Checks whether a date adapter is set up correctly for use with the timepicker. */
export function validateAdapter(
adapter: DateAdapter<unknown> | null,
formats: MatDateFormats | null,
) {
function missingAdapterError(provider: string) {
return Error(
`MatTimepicker: No provider found for ${provider}. You must add one of the following ` +
`to your app config: provideNativeDateAdapter, provideDateFnsAdapter, ` +
`provideLuxonDateAdapter, provideMomentDateAdapter, or provide a custom implementation.`,
);
}
if (!adapter) {
throw missingAdapterError('DateAdapter');
}
if (!formats) {
throw missingAdapterError('MAT_DATE_FORMATS');
}
if (
formats.display.timeInput === undefined ||
formats.display.timeOptionLabel === undefined ||
formats.parse.timeInput === undefined
) {
throw new Error(
'MatTimepicker: Incomplete `MAT_DATE_FORMATS` has been provided. ' +
'`MAT_DATE_FORMATS` must provide `display.timeInput`, `display.timeOptionLabel` ' +
'and `parse.timeInput` formats in order to be compatible with MatTimepicker.',
);
}
}