Skip to content

Instantly share code, notes, and snippets.

@bennycode
Created July 14, 2019 19:14
Show Gist options
  • Select an option

  • Save bennycode/f81e4d630384c6238c7bd0ed59b694a9 to your computer and use it in GitHub Desktop.

Select an option

Save bennycode/f81e4d630384c6238c7bd0ed59b694a9 to your computer and use it in GitHub Desktop.
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