unified-query
Version:
Composable search input with autocompletion and a rich query-language parser for the Unified Data System
26 lines (25 loc) • 746 B
JavaScript
const TODAY_RE = /^(today|yesterday)$/i;
const LAST_RE = /^last(\d+)(days|weeks|months|years)$/i;
export const utilityDateParser = (word, pos) => {
let op;
if (word[0] === '<' || word[0] === '>') {
op = word[0];
word = word.slice(1);
pos += 1;
}
let m = TODAY_RE.exec(word);
if (m) {
const value = { util: m[1].toLowerCase() };
return { kind: 'dateutil', op, value, raw: word, from: pos, to: pos + word.length };
}
m = LAST_RE.exec(word);
if (m) {
const value = {
util: 'last',
n: +m[1],
unit: m[2],
};
return { kind: 'dateutil', op, value, raw: word, from: pos, to: pos + word.length };
}
return null;
};