2020-10-05 21:34:52 +00:00
|
|
|
import { helper } from '@ember/component/helper';
|
|
|
|
import { isValid } from 'date-fns';
|
|
|
|
|
|
|
|
export function parseDateString(date, separator = '-') {
|
2021-02-08 20:13:00 +00:00
|
|
|
// Expects format MM-yyyy by default: no dates
|
2020-10-05 21:34:52 +00:00
|
|
|
let datePieces = date.split(separator);
|
|
|
|
if (datePieces.length === 2) {
|
|
|
|
if (datePieces[0] < 1 || datePieces[0] > 12) {
|
|
|
|
throw new Error('Not a valid month value');
|
|
|
|
}
|
|
|
|
let firstOfMonth = new Date(datePieces[1], datePieces[0] - 1, 1);
|
|
|
|
if (isValid(firstOfMonth)) {
|
|
|
|
return firstOfMonth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// what to return if not valid?
|
2021-02-08 20:13:00 +00:00
|
|
|
throw new Error(`Please use format MM${separator}yyyy`);
|
2020-10-05 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default helper(parseDateString);
|