Skip to content

Commit

Permalink
Merge pull request #41 from mhelleboid/exclude_tag
Browse files Browse the repository at this point in the history
basic implementation for exclude filter
  • Loading branch information
benlau authored Aug 14, 2023
2 parents 0e4098d + 3eff8c7 commit 6a29f55
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default class Board {
);
if (matchesBaseFilters) {
const foundCol = this.nonBacklogColumns.find(({ rules }) =>
rules.some(({ filterNote }) => filterNote(note))
rules.every(({ filterNote }) => filterNote(note))
);
if (foundCol) return foundCol.name;
if (this.backlogColumn) return this.backlogColumn.name;
Expand Down
81 changes: 61 additions & 20 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,52 @@ type RuleFactory = (
*/
const rules: Record<string, RuleFactory> = {
async tag(arg: string | string[]) {
const tagName = Array.isArray(arg) ? arg[0] : arg;
const tagID = (await getTagId(tagName)) || (await createTag(tagName));
return {
name: "tag",
filterNote: (note: NoteData) => note.tags.includes(tagName),
set: (noteId: string) => [
{
type: "post",
path: ["tags", tagID, "notes"],
body: { id: noteId },
},
],
unset: (noteId: string) => [
{
type: "delete",
path: ["tags", tagID, "notes", noteId],
},
],
editorType: "text",
};
const tagArg = Array.isArray(arg) ? arg[0] : arg;
if (tagArg.startsWith('-')) {
const tagName = tagArg.substring(1)
console.info('replace ', tagArg, ' with ', tagName)
const tagID = (await getTagId(tagName)) || (await createTag(tagName));
return {
name: "tag",
filterNote: (note: NoteData) => !note.tags.includes(tagName),
set: (noteId: string) => [
{
type: "delete",
path: ["tags", tagID, "notes", noteId],
},
],
unset: (noteId: string) => [
{
type: "post",
path: ["tags", tagID, "notes"],
body: { id: noteId },
},
],
editorType: "text",
};
}
else {
const tagName = tagArg
const tagID = (await getTagId(tagName)) || (await createTag(tagName));
return {
name: "tag",
filterNote: (note: NoteData) => note.tags.includes(tagName),
set: (noteId: string) => [
{
type: "post",
path: ["tags", tagID, "notes"],
body: { id: noteId },
},
],
unset: (noteId: string) => [
{
type: "delete",
path: ["tags", tagID, "notes", noteId],
},
],
editorType: "text",
};
}
},

async tags(tagNames: string | string[], rootNbPath: string, config: Config) {
Expand All @@ -55,6 +81,21 @@ const rules: Record<string, RuleFactory> = {
};
},

async alltags(tagNames: string | string[], rootNbPath: string, config: Config) {
if (!Array.isArray(tagNames)) tagNames = [tagNames];
const tagRules = await Promise.all(
tagNames.map((t) => rules.tag(t, rootNbPath, config))
);
return {
name: "alltags",
filterNote: (note: NoteData) =>
tagRules.every(({ filterNote }) => filterNote(note)),
set: (noteId: string) => tagRules.flatMap(({ set }) => set(noteId)),
unset: (noteId: string) => tagRules.flatMap(({ unset }) => unset(noteId)),
editorType: "text",
};
},

async notebookPath(path: string | string[], rootNotebookPath: string) {
if (Array.isArray(path)) path = path[0];

Expand Down
5 changes: 3 additions & 2 deletions tests/board.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ describe("Board", () => {
parent_id: parentNb,
})) as Board;

const col1 = board.sortNoteIntoColumn(note({ tags: ["task", "done"] }));
const col1 = board.sortNoteIntoColumn(note({ tags: ["task", "done"], isTodo: true, isCompleted: true }));
expect(col1).toBe("Done");

const col2 = board.sortNoteIntoColumn(
note({ tags: ["task", "completed"] })
note({ tags: ["task", "completed"], isTodo: true, isCompleted: true })
);
expect(col2).toBe("Done");
});
Expand Down Expand Up @@ -276,6 +276,7 @@ describe("Board", () => {

const col1 = board.sortNoteIntoColumn(
note({
tags: ["task", "completed"],
isCompleted: true,
isTodo: true,
})
Expand Down

0 comments on commit 6a29f55

Please sign in to comment.