in packages/eui/src/components/date_picker/super_date_picker/relative_utils.ts [28:66]
export function parseRelativeParts(value: string): RelativeParts {
const matches =
isString(value) &&
value.match(/now(([\-\+])([0-9]+)([smhdwMy])(\/[smhdwMy])?)?/);
const operator = matches && matches[2];
const count = matches && matches[3];
const unit = matches && matches[4];
const roundBy = matches && matches[5];
if (count && unit) {
const isRounded = roundBy ? true : false;
const roundUnit =
isRounded && roundBy
? (roundBy.replace(ROUND_DELIMETER, '') as TimeUnitId)
: undefined;
return {
count: parseInt(count, 10),
unit: operator === '+' ? `${unit}+` : unit,
round: isRounded,
...(roundUnit ? { roundUnit } : {}),
};
}
const results = { count: 0, unit: 's', round: false };
const duration = moment.duration(moment().diff(dateMath.parse(value)));
let unitOp = '';
for (let i = 0; i < relativeUnitsFromLargestToSmallest.length; i++) {
const asRelative = duration.as(relativeUnitsFromLargestToSmallest[i]);
if (asRelative < 0) unitOp = '+';
if (Math.abs(asRelative) > 1) {
results.count = Math.round(Math.abs(asRelative));
results.unit = relativeUnitsFromLargestToSmallest[i] + unitOp;
results.round = false;
break;
}
}
return results;
}