cumulocity-cypress
Version:
Cypress commands for Cumulocity IoT
34 lines (33 loc) • 1.28 kB
JavaScript
import _ from "lodash";
import { parse } from "date-fns";
function normalizeDateParsingWhitespace(value) {
// Angular locale patterns may contain non-breaking or narrow non-breaking spaces.
// Normalizing allows parsing inputs that use regular spaces.
return value.replace(/[\u00A0\u202F]/g, " ");
}
export function parseDate(date, format) {
let parsedDate = undefined;
// try to parse as number first, if string is passed it might be converted without format being used
if (_.isNumber(date)) {
parsedDate = new Date(date);
}
// parse with format
if (!isValidDate(parsedDate) && _.isString(date)) {
const normalizedDate = normalizeDateParsingWhitespace(date);
const normalizedFormat = normalizeDateParsingWhitespace(format);
parsedDate = parse(normalizedDate, normalizedFormat, new Date());
// if (!isValidDate(parsedDate) && _.isString(date)) {
// parsedDate = new Date(date);
// }
}
if (!isValidDate(parsedDate)) {
parsedDate = undefined;
}
return parsedDate;
}
export function isValidDate(date) {
return date != null && !isNaN(date) && _.isDate(date);
}
export function normalizeLocaleId(localeId) {
return localeId.toLowerCase().replace(/_/g, "-");
}