Skip to content

Commit 490bcfe

Browse files
committed
fix(material/timepicker): more flexible interval parsing
* Allows spaces between the interval value and unit. * Adds support for `hour`, `hours`, `minute`, `min`, `minutes`, `second` and `seconds` units in the interval.
1 parent fb6e202 commit 490bcfe

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/material/timepicker/util.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ describe('timepicker utilities', () => {
5656
expect(parseInterval('3M')).toBe(180);
5757
expect(parseInterval('3S')).toBe(3);
5858
});
59+
60+
it('should parse interval with space', () => {
61+
expect(parseInterval('3 h')).toBe(10_800);
62+
expect(parseInterval('6 h')).toBe(21_600);
63+
});
64+
65+
it('should handle long versions of units', () => {
66+
expect(parseInterval('1 hour')).toBe(3600);
67+
expect(parseInterval('3 hours')).toBe(10_800);
68+
expect(parseInterval('1 minute')).toBe(60);
69+
expect(parseInterval('3 min')).toBe(180);
70+
expect(parseInterval('3 minutes')).toBe(180);
71+
expect(parseInterval('1 second')).toBe(1);
72+
expect(parseInterval('10 seconds')).toBe(10);
73+
});
5974
});
6075

6176
describe('generateOptions', () => {

src/material/timepicker/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {InjectionToken} from '@angular/core';
1010
import {DateAdapter, MatDateFormats} from '@angular/material/core';
1111

1212
/** Pattern that interval strings have to match. */
13-
const INTERVAL_PATTERN = /^(\d*\.?\d+)(h|m|s)?$/i;
13+
const INTERVAL_PATTERN = /^(\d*\.?\d+)\s*(h|hour|hours|m|min|minute|minutes|s|second|seconds)?$/i;
1414

1515
/**
1616
* Object that can be used to configure the default options for the timepicker component.
@@ -62,9 +62,9 @@ export function parseInterval(value: number | string | null): number | null {
6262
return null;
6363
}
6464

65-
if (unit === 'h') {
65+
if (unit === 'h' || unit === 'hour' || unit === 'hours') {
6666
result = amount * 3600;
67-
} else if (unit === 'm') {
67+
} else if (unit === 'm' || unit === 'min' || unit === 'minute' || unit === 'minutes') {
6868
result = amount * 60;
6969
} else {
7070
result = amount;

0 commit comments

Comments
 (0)