From fb024be770ec790ba33716d0f031da7326a85c79 Mon Sep 17 00:00:00 2001 From: Nikhil Satish Date: Sun, 10 Feb 2019 19:00:34 +0530 Subject: [PATCH] made ModelStore interface and ArrayModelStore class --- src/ArrayModelStore.ts | 26 ++++++++++++++++++++++++++ src/ModelStore.ts | 24 ++++++------------------ 2 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 src/ArrayModelStore.ts diff --git a/src/ArrayModelStore.ts b/src/ArrayModelStore.ts new file mode 100644 index 0000000..af0844d --- /dev/null +++ b/src/ArrayModelStore.ts @@ -0,0 +1,26 @@ +import { ModelStore } from './ModelStore'; + +export class ArrayModelStore implements ModelStore { + 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); + } + +} diff --git a/src/ModelStore.ts b/src/ModelStore.ts index b821660..74cda8d 100644 --- a/src/ModelStore.ts +++ b/src/ModelStore.ts @@ -1,24 +1,12 @@ -export class ModelStore { - private data: M[]; +export interface ModelStore { + 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; }