Public
Edited
May 16
Paused
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
// (modifier)?(tag delimiter){0,}(term | quote term quote)
tokenize = (query, {
modifiers: prefixChars = "-+",
quotes: quoteChars = "\"'`",
delimiter = ":",
} = {}) => {
const modifiers = Object.fromEntries([...prefixChars].map(k => [k, true]));
const quotes = Object.fromEntries([...quoteChars].map(k => [k, true]));
let term, tags, modifier, quote, quoted;
const terms = [];
const reset = () => { term = ""; tags = []; modifier = null; quote = null; quoted = null };
const commit = () => {
if(term.length) terms.push({term, tags, modifier, quoted});
reset();
};
reset();
for(const c of query) {
if(quote === c) quote = null;
else if(quote === null && c === " ") commit();
else if(quote === null && !term.length && !tags.length && modifier === null && modifiers[c]) modifier = c;
else if(quote === null && term.length && c === delimiter) tags.push(term), term = "", quoted = null;
else if(quote === null && quotes[c]) quoted = quote = c;
else term += c;
}
commit();
return terms;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more