2020-03-09 12:39:49 +05:00
|
|
|
import fuzzysearch from "fuzzysearch";
|
2020-11-06 22:44:16 +05:00
|
|
|
import sm from "../utils/set";
|
2020-03-09 12:39:49 +05:00
|
|
|
var tfun = require("transfun/transfun.js").tfun;
|
|
|
|
|
if (!tfun) {
|
|
|
|
|
tfun = global.tfun;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class Lookup {
|
2020-03-19 12:38:33 +05:00
|
|
|
/**
|
|
|
|
|
*
|
2020-04-12 11:04:30 +05:00
|
|
|
* @param {import('./index').default} db
|
2020-03-19 12:38:33 +05:00
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 10:24:43 +05:00
|
|
|
notes(notes, query) {
|
2020-11-06 22:44:16 +05:00
|
|
|
return sm.union(
|
|
|
|
|
this._db.content._collection.search.search(query, {
|
|
|
|
|
map: (elem) => {
|
|
|
|
|
return notes.find((note) => note.contentId === elem);
|
|
|
|
|
},
|
|
|
|
|
}),
|
2020-11-16 14:59:15 +05:00
|
|
|
this._db.notes._collection.search.search(query)
|
2020-11-06 22:44:16 +05:00
|
|
|
);
|
2020-03-09 12:39:49 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notebooks(array, query) {
|
2020-11-06 22:44:16 +05:00
|
|
|
return this._db.notebooks._collection.search.search(query, {
|
|
|
|
|
map: (elem) => {
|
|
|
|
|
return array.find((nb) => nb.id === elem);
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-03-09 12:39:49 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topics(array, query) {
|
|
|
|
|
return this._byTitle(array, query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tags(array, query) {
|
|
|
|
|
return this._byTitle(array, query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trash(array, query) {
|
|
|
|
|
return this._byTitle(array, query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_byTitle(array, query) {
|
2020-05-04 12:24:33 +05:00
|
|
|
return tfun.filter((item) => fzs(query, item.title))(array);
|
2020-03-09 12:39:49 +05:00
|
|
|
}
|
|
|
|
|
}
|
2020-05-04 12:24:33 +05:00
|
|
|
|
|
|
|
|
function fzs(query, text) {
|
|
|
|
|
return fuzzysearch(query.toLowerCase(), text.toLowerCase());
|
|
|
|
|
}
|