import { asJson, toFile } from './io'; function all(_item: T) { return true; } function none(_item: T) { return false; } export class JsonStore { constructor(public file: string) {} insert(item: T) { const file = this.file; const items = asJson>(file, []); items.push(item); toFile(file, items); } delete(predicate?: (item: T) => boolean) { const file = this.file; const items = asJson>(file, undefined); if (items && items.length) { for (let i = items.length; i--; ) { if ((predicate || none)(items[i])) { items.splice(i, 1); } } toFile(file, items); } } select(predicate?: (item: T) => boolean) { const file = this.file; const items = asJson>(file, []); return items.filter(predicate || all); } switchTo(file: string) { this.file = file; } } export function open(file: string) { return new JsonStore(file); }