feat: implement get tag and get tags

This commit is contained in:
thecodrr
2020-02-01 14:45:20 +05:00
parent a0c7b6e652
commit 249a9e70f0
2 changed files with 41 additions and 14 deletions

View File

@@ -74,6 +74,10 @@ class Database {
return tfun.filter(".pinned == true")(this.getNotes());
}
getTag(tag) {
return tfun.filter(`.tags.includes('${tag}')`)(this.getNotes());
}
/**
* @param {string} by One from 'abc', 'month', 'year' or 'week'. Leave it empty for default grouping.
* @param {boolean} special Should only be used in the React app.
@@ -157,10 +161,13 @@ class Database {
if (oldNote) {
note.colors = setManipulator.union(oldNote.colors, note.colors);
await this.updateTags(setManipulator.complement(note.tags, oldNote.tags));
await updateTags.call(
this,
setManipulator.complement(note.tags, oldNote.tags)
);
note.tags = setManipulator.union(oldNote.tags, note.tags);
} else {
await this.updateTags(note.tags);
await updateTags.call(this, note.tags);
}
this.notes[timestamp] = note;
@@ -168,18 +175,6 @@ class Database {
return timestamp;
}
async updateTags(tags) {
for (let tag of tags) {
if (!tag || tag.trim().length <= 0) continue;
let oldCount = this.tags[tag] ? this.tags[tag].count : 0;
this.tags[tag] = {
title: tag,
count: oldCount + 1
};
}
await this.storage.write(KEYS.tags, this[KEYS.tags]);
}
pinItem(type, id) {
return editItem.call(this, type, id, "pinned");
}
@@ -421,6 +416,10 @@ class Database {
getUser() {
return this.user;
}
getTags() {
return extractValues(this.tags);
}
}
export default Database;
@@ -589,3 +588,15 @@ function getNoteContent(note) {
delta: note.content.delta
};
}
async function updateTags(tags) {
for (let tag of tags) {
if (!tag || tag.trim().length <= 0) continue;
let oldCount = this.tags[tag] ? this.tags[tag].count : 0;
this.tags[tag] = {
title: tag,
count: oldCount + 1
};
}
await this.storage.write(KEYS.tags, this[KEYS.tags]);
}