2020-03-09 12:39:49 +05:00
|
|
|
import fuzzysearch from "fuzzysearch";
|
|
|
|
|
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-04-12 11:04:30 +05:00
|
|
|
return new Promise((resolve) => {
|
2020-03-26 10:24:43 +05:00
|
|
|
const results = [];
|
|
|
|
|
let index = 0,
|
|
|
|
|
max = notes.length;
|
2020-04-12 11:04:30 +05:00
|
|
|
notes.forEach(async (note) => {
|
2020-03-26 10:24:43 +05:00
|
|
|
const text = await this._db.text.get(note.content.text);
|
|
|
|
|
const title = note.title;
|
|
|
|
|
if (fuzzysearch(query, text + title)) results.push(note);
|
|
|
|
|
if (++index >= max) return resolve(results);
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-03-09 12:39:49 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notebooks(array, query) {
|
|
|
|
|
return tfun.filter(
|
2020-04-12 11:04:30 +05:00
|
|
|
(nb) =>
|
2020-03-09 12:39:49 +05:00
|
|
|
fuzzysearch(query, nb.title + " " + nb.description) ||
|
2020-04-12 11:04:30 +05:00
|
|
|
nb.topics.some((topic) => fuzzysearch(query, topic.title))
|
2020-03-09 12:39:49 +05:00
|
|
|
)(array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-04-12 11:04:30 +05:00
|
|
|
return tfun.filter((item) => fuzzysearch(query, item.title))(array);
|
2020-03-09 12:39:49 +05:00
|
|
|
}
|
|
|
|
|
}
|