Skip to content

Commit

Permalink
Searchable transform interface to abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Sep 5, 2018
1 parent 9b50d91 commit c35c189
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
9 changes: 2 additions & 7 deletions types/Crafter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import vuetifyColors from 'vuetify/es5/util/colors';

const forbiddenColors = ['shades', 'grey', 'blueGrey'];

export default class Crafter implements Searchable {
export default class Crafter extends Searchable {

public get initials(): string | undefined {
if (this.name) {
Expand Down Expand Up @@ -51,6 +51,7 @@ export default class Crafter implements Searchable {
}

constructor(author?: IAuthor | string) {
super();
if (author) {
if (typeof author === 'string') {
const authorParts = author.split('<');
Expand Down Expand Up @@ -87,12 +88,6 @@ 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
9 changes: 2 additions & 7 deletions types/Keyword.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Searchable from './Searchable';
import Package from './Package';
export class Keyword implements Searchable {
export class Keyword extends Searchable {
public value: string;

constructor(value: string) {
super();
this.value = value;
}

Expand All @@ -26,12 +27,6 @@ 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
3 changes: 2 additions & 1 deletion types/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Crafter from './Crafter';
import Searchable from './Searchable';
import { Keyword } from './Keyword';

export default class Package implements PackageMetaDataDTO, Searchable {
export default class Package extends Searchable implements PackageMetaDataDTO {
public readonly distTags!: IDistTags;
public readonly time!: ITimes;
public readonly users!: {};
Expand Down Expand Up @@ -62,6 +62,7 @@ export default class Package implements PackageMetaDataDTO, Searchable {
private craftersList: Crafter[];

constructor(packageMetaData: PackageMetaDataDTO) {
super();
Object.assign(this, packageMetaData);

if (!this.distTags) {
Expand Down
15 changes: 11 additions & 4 deletions types/Searchable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import Package from './Package';

export default interface Searchable {
matches(other: Searchable, packages: Package[]): boolean;
getSearchItemText(): string[];
matchesPattern(pattern: RegExp): boolean;
export default abstract class Searchable {
public abstract matches(other: Searchable, packages: Package[]): boolean;

public abstract getSearchItemText(): string[];

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

0 comments on commit c35c189

Please sign in to comment.