Skip to content

Commit

Permalink
#14 Use Package.Tags Instead of keywords for search
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Sep 6, 2018
1 parent 2f52d51 commit 5e9d002
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/services/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class DataStore {
}

public get searchItems(): Searchable[] {
return this.searchItemList;
return this.tagList;
}

public get crafters(): Crafter[] {
Expand All @@ -26,7 +26,7 @@ export default class DataStore {

private request!: Promise<Package[]>;
private packages!: Package[];
private searchItemList!: Tag[];
private tagList!: Tag[];
private crafterList!: Crafter[];
private packageDetails!: {
[packageName: string]: {
Expand All @@ -39,7 +39,7 @@ export default class DataStore {

private constructor() {
this.packages = [];
this.searchItemList = [];
this.tagList = [];
this.crafterList = [];
this.packageDetails = {};
}
Expand Down Expand Up @@ -102,10 +102,10 @@ export default class DataStore {
const modifiedPackage: Package = new Package(packagesResponse[packageName]);
this.packages.push(modifiedPackage);

if (modifiedPackage.keywords) {
for (const keyword of modifiedPackage.keywords!) {
if (! this.searchItemList.some((currentSearchItem) => keyword === currentSearchItem.value)) {
this.searchItemList.push(new Tag(keyword));
if (modifiedPackage.tags) {
for (const tag of modifiedPackage.tags) {
if (! this.tagList.some((currentTag) => tag.value === currentTag.value)) {
this.tagList.push(tag);
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions types/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class Package extends Searchable implements PackageMetaDataDTO {
public readonly scope: string | undefined;
public readonly mainCode: string | undefined;
private craftersList: Crafter[];
private tagList: Tag[];

constructor(packageMetaData: PackageMetaDataDTO) {
super();
Expand Down Expand Up @@ -101,11 +102,12 @@ export default class Package extends Searchable implements PackageMetaDataDTO {
}

this.craftersList = [];
this.tagList = [];
}

public matches(other: Searchable): boolean {
if (other instanceof Tag) {
return this.keywords !== undefined && this.keywords.indexOf(other.value) > -1;
return this.tags.some((tag) => tag.value === other.value);
}
if (other instanceof Crafter) {
return this.crafters.some((crafter) => crafter.equals(other));
Expand All @@ -122,7 +124,7 @@ export default class Package extends Searchable implements PackageMetaDataDTO {
this.description || '',
this.author ? this.author.toString() : '',
]
.concat(this.keywords || []) // TODO: use Tag object instead.
.concat(...this.tags.map((tag) => tag.getSearchItemText()))
.concat(...this.crafters.map((crafter) => crafter.getSearchItemText()));
}

Expand All @@ -141,6 +143,16 @@ export default class Package extends Searchable implements PackageMetaDataDTO {
return this.craftersList;
}

public get tags(): Tag[] {
if (this.tagList.length) {
return this.tagList;
}
if (this.keywords && this.keywords.length) {
this.tagList.push(...this.keywords.map((keyword) => new Tag(keyword)));
}
return this.tagList;
}

public get repositoryName(): string | undefined {
if (this.repositoryUrl) {
return this.url2Name(this.repositoryUrl);
Expand Down
12 changes: 10 additions & 2 deletions types/Tag.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import Searchable from './Searchable';
import Package from './Package';
export class Tag extends Searchable {

public static allTags: Tag[] = [];
public value: string;

constructor(value: string) {
super();
this.value = value;
const alreadyCreatedTag = Tag.allTags.find((tag) => tag.value === this.value);
if (alreadyCreatedTag) {
return alreadyCreatedTag;
} else {
Tag.allTags.push(this);
}
}

public matches(other: Searchable, packages: Package[]): boolean {
Expand All @@ -14,8 +22,8 @@ export class Tag extends Searchable {
}
if (
other instanceof Package
&& other.keywords !== undefined
&& other.keywords.some((keyword) => keyword === this.value)
&& other.tags !== undefined
&& other.tags.some((tag) => tag.value === this.value)
) {
return true;
}
Expand Down

0 comments on commit 5e9d002

Please sign in to comment.