Created
July 14, 2019 19:14
-
-
Save bennycode/f81e4d630384c6238c7bd0ed59b694a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| interface MandatoryProps { | |
| id: number; | |
| } | |
| type StoreRecord = | |
| MandatoryProps & | |
| Object; | |
| type Store = {[index: number]: StoreRecord}; | |
| export class StoreHelper { | |
| static addRecord(store: Store, record: StoreRecord): Store { | |
| store[record.id] = record; | |
| return store; | |
| } | |
| static deleteRecord(store: Store, id: number): Store { | |
| delete store[id]; | |
| return store; | |
| } | |
| static setRecords(store: Store, records: StoreRecord[]): Store { | |
| const findById = (id: number) => { | |
| return (record: StoreRecord) => (record.id === id); | |
| }; | |
| const ids = records.map(item => item.id); | |
| ids.forEach((id) => { | |
| const value = records.find(findById(id)); | |
| if (value) { | |
| store[id] = value; | |
| } | |
| }); | |
| return store; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment