Published
Edited
Feb 11, 2019
Fork of Dataflock
Importers
1 star
Insert cell
Insert cell
Insert cell
selectorFn = {
const types = sels =>
sels.map(x => (checks.isRegExp(x) ? "regexp" : typeof x));

const typesStr = sels => types(sels).toString();

const selectorFn = defmulti(sel => typesStr(sel));
selectorFn.isa("function", "fn");
selectorFn.isa("function,string", "fn+to");
selectorFn.isa("string,function", "key+fn");
selectorFn.isa("string,function,string", "from+fn+to");
selectorFn.isa("string,string,function", "from+key+fn");
selectorFn.isa("string,string,function,string", "from+key+fn+to");
selectorFn.isa("string,string", "key+equal");
selectorFn.isa("string,number", "key+equal");
selectorFn.isa("string,string,string", "key+equal+to");
selectorFn.isa("string,number,string", "key+equal+to");
selectorFn.isa("string,string,string,string", "from+key+equal+to");
selectorFn.isa("string,string,number,string", "from+key+equal+to");
selectorFn.isa("string,regexp", "key+match");
selectorFn.isa("string,regexp,string", "key+match+to");
selectorFn.isa("string,string,regexp,string", "from+key+match+to");

const getK = k => obj => obj[k];

const compileSel = (from, keyFn, filterFn, to) => acc => {
const fr = [...acc[from]];
acc[to] = new Set(fr.filter(x => filterFn(keyFn(x))));
return acc;
};

// fn(obj)
selectorFn.add("fn", ([fn]) => compileSel(ALL, tx.identity, fn, ALL));
selectorFn.add("fn+to", ([fn, to]) => compileSel(ALL, tx.identity, fn, to));
selectorFn.add("from+fn+to", ([from, fn, to]) =>
compileSel(from, tx.identity, fn, to)
);
// fn(obj[k])
selectorFn.add("key+fn", ([key, fn]) => compileSel(ALL, getK(key), fn, ALL));
// selectorFn.add("key+fn+to", ([key, fn, to]) =>
// compileSel(ALL, getK(key), fn, to)
// );
selectorFn.add("from+key+fn", ([from, key, fn]) =>
compileSel(from, getK(key), fn, ALL)
);
selectorFn.add("from+key+fn+to", ([from, key, fn, to]) =>
compileSel(from, getK(key), fn, to)
);
// equality
selectorFn.add("key+equal", ([key, equal, to]) =>
compileSel(ALL, getK(key), x => x == equal, ALL)
);
selectorFn.add("key+equal+to", ([key, equal, to]) =>
compileSel(ALL, getK(key), x => x == equal, to)
);
selectorFn.add("from+key+equal+to", ([from, key, equal, to]) =>
compileSel(from, getK(key), x => x == equal, to)
);
// regexp -> match
selectorFn.add("key+match", ([key, regexp]) =>
compileSel(ALL, getK(key), x => regexp.test(x), ALL)
);
selectorFn.add("key+match+to", ([key, regexp, to]) =>
compileSel(ALL, getK(key), x => regexp.test(x), to)
);
selectorFn.add("from+key+match+to", ([from, key, regexp, to]) =>
compileSel(from, getK(key), x => regexp.test(x), to)
);

return selectorFn;
}
Insert cell
query = {
const compileSels = sels => sels.map(selectorFn);
// Returns ALL if no other groupings where defined
const wrapOut = acc => (Object.keys(acc).length == 1 ? acc[ALL] : acc);

return (qSels, data) => {
const compiledSels = compileSels(qSels);
const qfn = data => {
const acc = data[ALL] ? data : { [ALL]: data };
return wrapOut(compiledSels.reduce((acc, s) => (s(acc), acc), acc));
};
return data ? qfn(data) : qfn;
};
}
Insert cell
Insert cell
Insert cell
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