unified-query
Version:
Composable search input with autocompletion and a rich query-language parser for the Unified Data System
47 lines (46 loc) • 1.32 kB
JavaScript
export function analyzeDraft(seg) {
let value = undefined;
const errors = [];
let seenBool = false;
for (const tok of seg.tokens) {
if (tok.kind === 'boolean') {
if (!seenBool) {
value = tok.value;
seenBool = true;
}
else {
errors.push(dupBoolErr(tok));
}
continue;
}
// any non-boolean token
errors.push(invalidTokErr(tok));
}
seg.errors.push(...errors);
return {
keyword: 'draft',
parsed: value, // true | false | undefined
from: seg.from,
to: seg.to,
raw: seg.raw,
};
}
/* -------------------------------------------------------------------------- */
/* helpers */
/* -------------------------------------------------------------------------- */
function dupBoolErr(tok) {
return {
message: `duplicate boolean flag "${tok.raw}" for @draft`,
token: tok.raw,
from: tok.from,
to: tok.to,
};
}
function invalidTokErr(tok) {
return {
message: `invalid token "${tok.raw}" for @draft (expects a single boolean)`,
token: tok.raw,
from: tok.from,
to: tok.to,
};
}