Skip to content

Commit

Permalink
Add vault source entry search
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Feb 6, 2024
1 parent 1045cfc commit 57bb834
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions source/index.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export { SearchResult } from "./search/BaseSearch.js";
export { VaultEntrySearch as Search } from "./search/VaultEntrySearch.js"; // compat @todo remove
export { VaultEntrySearch } from "./search/VaultEntrySearch.js";
export { VaultFacadeEntrySearch } from "./search/VaultFacadeEntrySearch.js";
export { VaultSourceEntrySearch } from "./search/VaultSourceEntrySearch.js";
export { SearchKey, buildSearcher } from "./search/searcher.js";

export { AppEnv, AppEnvGetPropertyOptions } from "./env/core/appEnv.js";
Expand Down
14 changes: 12 additions & 2 deletions source/search/BaseSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import levenshtein from "fast-levenshtein";
import { StorageInterface } from "../storage/StorageInterface.js";
import { buildSearcher } from "./searcher.js";
import { Vault } from "../core/Vault.js";
import { EntryID, EntryType, GroupID, VaultFacade, VaultID } from "../types.js";
import { extractTagsFromSearchTerm, tagsMatchSearch } from "./tags.js";
import { EntryID, EntryType, GroupID, VaultFacade, VaultID, VaultSourceID } from "../types.js";

interface DomainScores {
[domain: string]: number;
Expand All @@ -28,6 +28,7 @@ export interface SearchResult {
id: EntryID;
properties: { [property: string]: string };
tags: Array<string>;
sourceID?: VaultSourceID;
urls: Array<string>;
vaultID: VaultID;
}
Expand Down Expand Up @@ -78,11 +79,20 @@ export class BaseSearch {

/**
* Last search results
* @deprecated Use `getResults` instead
*/
get results(): Array<SearchResult> {
return this._results;
}

/**
* Get last search results
* @returns An array of results
*/
getResults(): Array<SearchResult> {
return this._results;
}

/**
* Increment the score of a URL in an entry
* @param vaultID The vault ID
Expand Down Expand Up @@ -110,7 +120,7 @@ export class BaseSearch {
* Prepare the search instance by processing
* entries
*/
async prepare() {
async prepare(): Promise<void> {
this._entries = [];
this._scores = {};
for (const target of this._targets) {
Expand Down
39 changes: 39 additions & 0 deletions source/search/VaultSourceEntrySearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SearchResult, SearcherFactory } from "./BaseSearch.js";
import { VaultSource } from "../core/VaultSource.js";
import { Vault } from "../core/Vault.js";
import { StorageInterface } from "../storage/StorageInterface.js";
import { VaultEntrySearch } from "./VaultEntrySearch.js";
import { VaultSourceStatus } from "../types.js";

export class VaultSourceEntrySearch extends VaultEntrySearch {
_sources: Array<VaultSource>;

constructor(
sources: Array<VaultSource>,
memory?: StorageInterface,
searcherFactory?: SearcherFactory
) {
const vaults: Array<Vault> = sources.reduce((output, source) => {
if (source.status === VaultSourceStatus.Unlocked) {
return [...output, source.vault];
}
return output;
}, []);
super(vaults, memory, searcherFactory);
this._sources = [...sources];
}

/**
* Last search results
*/
get results(): Array<SearchResult> {
return this._results.map((res) => {
const output = res;
const source = this._sources.find((src) => src?.vault?.id === output.vaultID);
if (source) {
output.sourceID = source.id;
}
return output;
});
}
}

0 comments on commit 57bb834

Please sign in to comment.