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

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-02-20 02:11:32 +05:00
import { qclone } from "qclone";
2020-11-16 15:54:16 +05:00
import { getContentFromData } from "../content-types";
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-20 02:11:32 +05:00
notes = qclone(notes);
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-04-19 09:50:44 +05:00
return notes.filter((note) => {
let content = "";
if (!note.locked) {
2021-04-19 12:06:05 +05:00
content = contents.find(
(content) => !!content && content.id === note.contentId
);
2021-04-19 09:50:44 +05:00
if (!content) return false;
content = getContentFromData(content.type, content.data);
2021-04-19 09:50:44 +05:00
content = content.toHTML();
}
2021-04-19 09:50:44 +05:00
return search(query, note.title) || search(query, content);
2021-02-20 02:11:32 +05:00
});
2020-03-09 12:39:49 +05:00
}
notebooks(array, query) {
2021-04-19 09:50:44 +05:00
return array.filter(
(item) => search(query, item.title) || search(query, item.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);
}
_byTitle(array, query) {
2021-04-19 09:50:44 +05:00
return array.filter((item) => search(query, item.title));
2020-03-09 12:39:49 +05:00
}
}
2021-02-22 10:48:03 +05:00
2021-04-19 09:50:44 +05:00
function search(query, string) {
const words = query.toLowerCase().split(" ");
return words.some((word) => string.toLowerCase().indexOf(word) > -1);
2021-02-22 10:48:03 +05:00
}