Skip to content

Commit

Permalink
made ModelStore interface and ArrayModelStore class
Browse files Browse the repository at this point in the history
  • Loading branch information
niksoc-hi committed Feb 10, 2019
1 parent 96a8588 commit fb024be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
26 changes: 26 additions & 0 deletions src/ArrayModelStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ModelStore } from './ModelStore';

export class ArrayModelStore<M extends Model> implements ModelStore<M> {
private data: M[];

getById(id: number): M {
return this.filter(m => m.id === id)[0];
}

filter(predicate: (obj: M) => boolean): M[] {
return this.data.filter(predicate);
}

add(obj: M): void {
this.data.push(obj);
}

remove(id: number): void {
this.data = this.data.filter(m => m.id !== id);
}

update(obj: M): void {
this.data = this.data.map(m => obj.id === m.id ? obj : m);
}

}
24 changes: 6 additions & 18 deletions src/ModelStore.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
export class ModelStore<M extends Model> {
private data: M[];
export interface ModelStore<M extends Model> {
getById(id: number): M;

getById(id: number): M {
return this.filter(m => m.id === id)[0];
}
filter(predicate: (obj: M) => boolean): M[];

filter(predicate: (obj: M) => boolean): M[] {
return this.data.filter(predicate);
}
add(obj: M): void;

add(obj: M): void {
this.data.push(obj);
}
remove(id: number): void;

remove(id: number): void {
this.data = this.data.filter(m => m.id !== id);
}

update(obj: M): void {
this.data = this.data.map(m => obj.id === m.id ? obj : m);
}
update(obj: M): void;

}

0 comments on commit fb024be

Please sign in to comment.