Skip to content

Commit

Permalink
simplify searchItemText code #14
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Nov 26, 2018
1 parent 9b0d7fd commit 2bc8766
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
27 changes: 1 addition & 26 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -242,32 +242,7 @@ export default class App extends Vue {
}
private getSearchItemText(item: Searchable) {
if (item instanceof Package) {
return [
item.name,
item.description,
item.author,
].concat(item.keywords);
}
if (item instanceof Crafter) {
return [
`author:${item.name}`,
`crafter:${item.name}`,
`contributor:${item.name}`,
`collaborator:${item.name}`,
item.name,
item.email,
item.url,
item.initials,
];
}
if (item instanceof SearchItem) {
return [
`#${item.value}`,
`keyword:${item.value}`,
`tag:${item.value}`,
];
}
return item.getSearchItemText();
}
private isPackage(item: SearchItem | Package | Crafter): boolean {
Expand Down
20 changes: 20 additions & 0 deletions types/Crafter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ export default class Crafter implements Searchable {
return false;
}

public getSearchItemText(): string[] {
const text: string[] = [];
if (this.name) {
text.push(
this.name,
`author:${this.name}`,
`crafter:${this.name}`,
`contributor:${this.name}`,
`collaborator:${this.name}`,
);
}
text.push(
this.name || '',
this.email || '',
this.url || '',
this.initials || '',
);
return text;
}

public equals(other: Crafter): boolean {
if (this.email && other.email) {
return this.email === other.email;
Expand Down
8 changes: 8 additions & 0 deletions types/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ export default class Package implements PackageMetaDataDTO, Searchable {
return false;
}

public getSearchItemText(): string[] {
return [
this.name || '',
this.description || '',
this.author ? this.author.toString() : '',
].concat(this.keywords || []);
}

public get crafters(): Crafter[] {
if (this.craftersList.length) {
return this.craftersList;
Expand Down
8 changes: 8 additions & 0 deletions types/SearchItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ export class SearchItem implements Searchable {
}
return false;
}

public getSearchItemText(): string[] {
return [
`#${this.value}`,
`keyword:${this.value}`,
`tag:${this.value}`,
];
}
}
1 change: 1 addition & 0 deletions types/Searchable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import Package from './Package';

export default interface Searchable {
matches(other: Searchable, packages: Package[]): boolean;
getSearchItemText(): string[];
}

0 comments on commit 2bc8766

Please sign in to comment.