-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made ModelStore interface and ArrayModelStore class
- Loading branch information
Showing
2 changed files
with
32 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |