diff --git a/src/services/DataStore.ts b/src/services/DataStore.ts index 02c6ed6..f4f4e74 100644 --- a/src/services/DataStore.ts +++ b/src/services/DataStore.ts @@ -15,7 +15,7 @@ export default class DataStore { } public get searchItems(): Searchable[] { - return this.searchItemList; + return this.tagList; } public get crafters(): Crafter[] { @@ -26,7 +26,7 @@ export default class DataStore { private request!: Promise; private packages!: Package[]; - private searchItemList!: Tag[]; + private tagList!: Tag[]; private crafterList!: Crafter[]; private packageDetails!: { [packageName: string]: { @@ -39,7 +39,7 @@ export default class DataStore { private constructor() { this.packages = []; - this.searchItemList = []; + this.tagList = []; this.crafterList = []; this.packageDetails = {}; } @@ -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); } } } diff --git a/types/Package.ts b/types/Package.ts index ba272de..9f8af37 100644 --- a/types/Package.ts +++ b/types/Package.ts @@ -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(); @@ -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)); @@ -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())); } @@ -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); diff --git a/types/Tag.ts b/types/Tag.ts index 31530bf..1244f5c 100644 --- a/types/Tag.ts +++ b/types/Tag.ts @@ -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 { @@ -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; }