Files
notesnook/packages/core/api/lookup.js

70 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-12-21 11:24:45 +05:00
import { filter, parse } from "liqe";
2020-03-09 12:39:49 +05:00
export default class Lookup {
2020-03-19 12:38:33 +05:00
/**
*
* @param {import('./index').default} db
2020-03-19 12:38:33 +05:00
*/
constructor(db) {
this._db = db;
}
async notes(notes, query) {
2021-02-12 10:15:37 +05:00
const contents = await this._db.content.multi(
2021-02-20 02:11:32 +05:00
notes.map((note) => note.contentId || "")
);
2021-12-20 09:29:25 +05:00
2021-12-21 11:24:45 +05:00
return search(notes, query, (note) => {
let text = note.title;
2021-12-20 09:29:25 +05:00
if (
!note.locked &&
!!note.contentId &&
contents.hasOwnProperty(note.contentId)
)
2021-12-21 11:24:45 +05:00
text += contents[note.contentId]["data"];
return text;
});
2020-03-09 12:39:49 +05:00
}
notebooks(array, query) {
2021-12-21 11:24:45 +05:00
return search(array, query, (n) => `${n.title} ${n.description}`);
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);
}
2022-02-28 13:02:16 +05:00
attachments(array, query) {
return search(
array,
query,
(n) => `${n.metadata.filename} ${n.metadata.type} ${n.metadata.hash}`
);
}
2020-03-09 12:39:49 +05:00
_byTitle(array, query) {
2021-12-21 11:24:45 +05:00
return search(array, query, (n) => n.title);
}
}
function search(items, query, selector) {
try {
return filter(
parse(`text:"${query.toLowerCase()}"`),
items.map((item) => {
return { item, text: selector(item).toLowerCase() };
})
).map((v) => v.item);
} catch (e) {
return [];
2020-03-09 12:39:49 +05:00
}
}