Skip to content

Commit

Permalink
Add tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Jan 27, 2024
1 parent 2041285 commit 44e57a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
10 changes: 6 additions & 4 deletions source/core/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ export class Entry extends VaultItem {
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}`);
const tagLower = tag.toLowerCase();
if (!isValidTag(tagLower)) {
throw new Error(`Cannot add entry tag: Invalid format: ${tagLower}`);
}
current.add(tag);
current.add(tagLower);
}
this.setAttribute(Entry.Attributes.Tags, [...current].join(","));
return this;
Expand Down Expand Up @@ -275,7 +276,8 @@ export class Entry extends VaultItem {
removeTags(...tags: Array<string>): this {
const current = new Set(this.getTags());
for (const tag of tags) {
current.delete(tag);
const tagLower = tag.toLowerCase();
current.delete(tagLower);
}
this.setAttribute(Entry.Attributes.Tags, [...current].join(","));
return this;
Expand Down
33 changes: 33 additions & 0 deletions source/core/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class Vault extends EventEmitter {

_onCommandExec: () => void;

_tagMap: Map<string, Array<EntryID>> = new Map();

/**
* The vault format
* @readonly
Expand Down Expand Up @@ -171,6 +173,12 @@ export class Vault extends EventEmitter {
return findEntriesByProperty(this._entries, property, value);
}

findEntriesByTag(tag: string): Array<Entry> {
const tagLower = tag.toLowerCase();
const entryIDs = this._tagMap.has(tagLower) ? this._tagMap.get(tagLower) : [];
return entryIDs.map((id) => this.findEntryByID(id));
}

/**
* Find a group by its ID
* @param id The group ID to search for
Expand Down Expand Up @@ -209,6 +217,14 @@ export class Vault extends EventEmitter {
return [...this._groups];
}

/**
* Get all registered entry tags
* @returns An array of tag strings
*/
getAllTags(): Array<string> {
return [...this._tagMap.keys()];
}

/**
* Get the value of an attribute
* @param attributeName The attribute to get
Expand Down Expand Up @@ -277,6 +293,23 @@ export class Vault extends EventEmitter {
});
}

_rebuildTags() {
this._tagMap = new Map();
this.getAllEntries().forEach((entry) => {
const tags = entry.getTags();
for (const tag of tags) {
const tagLower = tag.toLowerCase();
const existingIDs = this._tagMap.has(tagLower)
? [...this._tagMap.get(tagLower)]
: [];
if (!existingIDs.includes(entry.id)) {
existingIDs.push(entry.id);
}
this._tagMap.set(tagLower, existingIDs);
}
});
}

/**
* Update the format reference
* @param format The new format instance
Expand Down

0 comments on commit 44e57a3

Please sign in to comment.