Skip to content

Commit

Permalink
Add entry tags
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Jan 25, 2024
1 parent 52403eb commit 2041285
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
52 changes: 50 additions & 2 deletions source/core/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { generateUUID } from "../tools/uuid.js";
import { getEntryURLs, EntryURLType } from "../tools/entry.js";
import { Group } from "./Group.js";
import { Vault } from "./Vault.js";
import { isValidTag } from "../tools/tag.js";
import {
EntryChange,
EntryPropertyValueType,
Expand All @@ -20,7 +21,8 @@ export class Entry extends VaultItem {
static Attributes = Object.freeze({
AttachmentPrefix: "BC_ENTRY_ATTACHMENT:",
FacadeType: "BC_ENTRY_FACADE_TYPE",
FieldTypePrefix: "BC_ENTRY_FIELD_TYPE:"
FieldTypePrefix: "BC_ENTRY_FIELD_TYPE:",
Tags: "BC_ENTRY_TAGS"
});

/**
Expand All @@ -47,6 +49,23 @@ export class Entry extends VaultItem {
return vault.findEntryByID(id);
}

/**
* Add one or more tags to the entry
* @param tags A collection of tag strings
* @returns Self
*/
addTags(...tags: Array<string>): this {
const current = new Set(this.getTags());
for (const tag of tags) {
if (!isValidTag(tag)) {
throw new Error(`Cannot add entry tag: Invalid format: ${tag}`);
}
current.add(tag);
}
this.setAttribute(Entry.Attributes.Tags, [...current].join(","));
return this;
}

/**
* Delete the entry - either trashes the entry, or removes it completely.
* If the entry is in the trash already, it is removed (including if there is no
Expand Down Expand Up @@ -108,6 +127,8 @@ export class Entry extends VaultItem {
* containing all attribute keys and their values if no attribute name
* is provided
*/
getAttribute(): PropertyKeyValueObject;
getAttribute(attribute: string): string | undefined;
getAttribute(attribute?: string): PropertyKeyValueObject | string | undefined {
const attributes = this.vault.format.getEntryAttributes(this._source) || {};
if (typeof attribute === "undefined") {
Expand Down Expand Up @@ -189,9 +210,22 @@ export class Entry extends VaultItem {
}, {});
}

/**
* Get entry tags
* @returns An array of tag strings
*/
getTags(): Array<string> {
const tags = this.getAttribute(Entry.Attributes.Tags);
return typeof tags === "string"
? tags.split(",").reduce((output, tag) => {
return isValidTag(tag) ? [...output, tag] : output;
}, [])
: [];
}

/**
* Get the entry type
* @returns
* @returns The entry type
*/
getType(): EntryType {
return (
Expand Down Expand Up @@ -233,6 +267,20 @@ export class Entry extends VaultItem {
return this;
}

/**
* Remove one or more tags
* @param tags Collection of tags to remove
* @returns Self
*/
removeTags(...tags: Array<string>): this {
const current = new Set(this.getTags());
for (const tag of tags) {
current.delete(tag);
}
this.setAttribute(Entry.Attributes.Tags, [...current].join(","));
return this;
}

/**
* Set an attribute on the entry
* @param attribute The name of the attribute
Expand Down
3 changes: 3 additions & 0 deletions source/tools/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isValidTag(tag: string): boolean {
return /^[a-zA-Z0-9_]+$/.test(tag);
}

0 comments on commit 2041285

Please sign in to comment.