Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic implementation for exclude filter #41

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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