Port from moment to Luxon

This commit is contained in:
Nils Streijffert
2021-02-04 16:36:39 +01:00
parent df6ade0ba0
commit e8532a0621
2 changed files with 22 additions and 12 deletions
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import moment from 'moment';
import { DateTime } from 'luxon';
import goodMorning from './locales/goodMorning.locales.json';
import goodAfternoon from './locales/goodAfternoon.locales.json';
import goodEvening from './locales/goodEvening.locales.json';
@@ -26,22 +26,22 @@ import goodEvening from './locales/goodEvening.locales.json';
* @returns {bool} Whether the date is valid or not.
*/
export function isValidDate(date) {
return moment(date).isValid();
return DateTime.fromISO(date).isValid;
}
/**
* Validates that a date is a valid ISO string and a specific format.
*
* @param date A date string
* @param format A format string or an array of format strings to validate against.
* @param format A format string to validate against. See https://moment.github.io/luxon/docs/manual/parsing.html#table-of-tokens for reference.
* @returns {bool} Whether the date is valid or not according to the format.
*/
export function isValidDateAndFormat(date, format) {
return moment(date, format, true).isValid();
return DateTime.fromFormat(date, format).isValid;
}
export function relativeTime(timestamp) {
return moment(timestamp).fromNow();
return DateTime.fromISO(timestamp).toRelative();
}
// Select a large random integer at startup, to prevent the greetings to change every time the user
@@ -14,19 +14,29 @@
* limitations under the License.
*/
import { isValidDateAndFormat, getTimeBasedGreeting } from './timeUtil';
import {
isValidDate,
isValidDateAndFormat,
getTimeBasedGreeting,
} from './timeUtil';
import { DateTime } from 'luxon';
it('validate isValidDate', () => {
expect(isValidDate(DateTime.local().toISO())).toBeTruthy();
expect(isValidDate('2021-02-04 15:00:10')).toBeFalsy();
});
it('validates time format', () => {
const valid = isValidDateAndFormat(
'1970-01-01T00:00:00',
'YYYY-MM-DD[T]HH:mm:ss',
'1970-01-01 00:00:00',
'yyyy-MM-dd hh:mm:ss',
);
const invalid = isValidDateAndFormat(
'1970/01/01T00:00:00',
'YYYY-MM-DD[T]HH:mm:ss',
'1970-01-01T00:00:00',
'yyyy-MM-ddThh:mm:ss',
);
expect(valid).toBe(true);
expect(invalid).toBe(false);
expect(valid).toBeTruthy();
expect(invalid).toBeFalsy();
});
it('has greeting and language', () => {