Skip to content

Commit

Permalink
#14 simplify search algorithm, fix author search
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Sep 6, 2018
1 parent 46d2e13 commit 7882824
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default class App extends Vue {
};
@Prop() private menuVisibleProp!: boolean;
@Prop() private activeFiltersProp!: Keyword[];
@Prop() private activeFiltersProp!: Searchable[];
@Prop() private clippedProp!: boolean;
@Prop() private hasFocusProp!: boolean;
@Prop() private titleProp!: string;
Expand Down
27 changes: 12 additions & 15 deletions src/components/PackageList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ export default class Packages extends Vue {
if (!args.query) {
return true;
}
const pattern = new RegExp(args.query.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''), 'gi');
return item.matchesPattern(pattern);
return item.matchesPattern(args.query);
});
});
}
Expand All @@ -108,16 +107,15 @@ export default class Packages extends Vue {
</script>

<style lang="scss">
@import '../assets/variables';
@import "../assets/variables";
.v-list--three-line .v-list__tile {
height: auto;
}
.package-list {
.v-list__tile__content {
padding: .4em 0;
padding: 0.4em 0;
}
&--keywords {
Expand All @@ -132,23 +130,22 @@ export default class Packages extends Vue {
display: flex;
.CrafterAvatar {
margin-left: .5em;
margin-left: 0.5em;
}
}
& &--by {
padding-bottom: .3em;
padding-bottom: 0.3em;
color: $color-gray-light;
font-size: $package-list--author--font-size;
}
.v-list__tile__sub-title {
.v-chip {
&:first-child {
margin-left: 0;
}
}
}
.v-list__tile__sub-title {
.v-chip {
&:first-child {
margin-left: 0;
}
}
}
}
</style>
18 changes: 7 additions & 11 deletions types/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,18 @@ export default class Package extends Searchable implements PackageMetaDataDTO {
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[] {
let crafterTexts: string[] = [];
for (const crafter of this.crafters) {
crafterTexts = crafterTexts.concat(crafter.getSearchItemText());
}
return [
this.name || '',
this.description || '',
this.author ? this.author.toString() : '',
].concat(this.keywords || []);
]
.concat(this.keywords || [])
.concat(crafterTexts);
}

public get crafters(): Crafter[] {
Expand Down
6 changes: 2 additions & 4 deletions types/Searchable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export default abstract class Searchable {

public abstract getSearchItemText(): string[];

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

0 comments on commit 7882824

Please sign in to comment.