Skip to content

Commit

Permalink
fix #14 reduce redundancy in search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Sep 5, 2018
1 parent f188722 commit ccdf04d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 40 deletions.
7 changes: 5 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,11 @@ export default class App extends Vue {
});
}
private getSearchItemText(item: Searchable) {
return item.getSearchItemText();
private getSearchItemText(item: Searchable): string[] {
if ('getSearchItemText' in item) {
return item.getSearchItemText();
}
return [];
}
private isPackage(item: Searchable): boolean {
Expand Down
23 changes: 1 addition & 22 deletions src/components/PackageList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export default class Packages extends Vue {
this.loadConfig();
// TODO: refactor this (#14)
EventBus.$on(Events.FILTER_SEARCH, async ( args: { filters: Searchable[], query: string} ) => {
this.packages.all = await DataStore.Instance.getPackages();
this.packages.data = this.packages.all
Expand All @@ -91,27 +90,7 @@ export default class Packages extends Vue {
return true;
}
const pattern = new RegExp(args.query.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''), 'gi');
const someCrafterMatches = item.crafters.some((crafter) => {
if (
crafter.name &&
(crafter.name.match(pattern) ||
`crafter:${crafter.name}`.match(pattern) ||
`author:${crafter.name}`.match(pattern))
) {
return true;
}
return false;
});
return `/${item.name}`.match(pattern) ||
someCrafterMatches ||
item.description && `/${item.description}`.match(pattern) ||
item.keywords && item.keywords.some((keyword) => {
if (`#${keyword}`.match(pattern)) {
return true;
} else {
return false;
}
});
return item.matchesPattern(pattern);
});
});
}
Expand Down
6 changes: 6 additions & 0 deletions types/Crafter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ export default class Crafter implements Searchable {
return false;
}

public matchesPattern(pattern: RegExp): boolean {
return this.getSearchItemText().some((text) => {
return text.match(pattern) != null;
});
}

public getSearchItemText(): string[] {
const text: string[] = [];
if (this.name) {
Expand Down
6 changes: 6 additions & 0 deletions types/Keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export class Keyword implements Searchable {
return false;
}

public matchesPattern(pattern: RegExp): boolean {
return this.getSearchItemText().some((text) => {
return text.match(pattern) != null;
});
}

public getSearchItemText(): string[] {
return [
`#${this.value}`,
Expand Down
26 changes: 10 additions & 16 deletions types/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ export default class Package implements PackageMetaDataDTO, Searchable {
return false;
}

public matchesPattern(pattern: RegExp): boolean {
const someCrafterMatch: boolean = this.crafters.some((crafter) => crafter.matchesPattern(pattern));
const nameMatch: boolean = `/${this.name}`.match(pattern) !== null;
const descriptionMatch: boolean = this.description !== undefined
&& `/${this.description}`.match(pattern) !== null;
const someKeywordsMatch: boolean = this.keywords !== undefined
&& this.keywords.some((keyword) => keyword.match(pattern) != null);
return someCrafterMatch || nameMatch || descriptionMatch || someKeywordsMatch;
}

public getSearchItemText(): string[] {
return [
this.name || '',
Expand All @@ -138,22 +148,6 @@ export default class Package implements PackageMetaDataDTO, Searchable {
return this.craftersList;
}

public matchesCrafter(pattern: RegExp): boolean {
for (const crafter of this.crafters) {
if (crafter.name) {
if (
crafter.name.match(pattern) ||
`crafter:${crafter.name}`.match(pattern) ||
`author:${crafter.name}`.match(pattern)
) {
return true;
}
}
}
return false;
}


public get repositoryName(): string | undefined {
if (this.repositoryUrl) {
return this.url2Name(this.repositoryUrl);
Expand Down
1 change: 1 addition & 0 deletions types/Searchable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import Package from './Package';
export default interface Searchable {
matches(other: Searchable, packages: Package[]): boolean;
getSearchItemText(): string[];
matchesPattern(pattern: RegExp): boolean;
}

0 comments on commit ccdf04d

Please sign in to comment.