From 5eac4afb5377d1a1f55eb009cea83be8da21c13a Mon Sep 17 00:00:00 2001 From: Zack Campbell Date: Sun, 26 Mar 2017 04:46:42 -0500 Subject: [PATCH] Add abstract StorageProvider, JSONProvider, Util.assignNested Still so much to do T-T --- src/lib/Util.ts | 31 ++++++++++++++++++--- src/lib/storage/JSONProvider.ts | 43 ++++++++++++++++++++++++++++++ src/lib/storage/StorageProvider.ts | 9 +++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/lib/storage/JSONProvider.ts create mode 100644 src/lib/storage/StorageProvider.ts diff --git a/src/lib/Util.ts b/src/lib/Util.ts index b4de640f..72b55fce 100644 --- a/src/lib/Util.ts +++ b/src/lib/Util.ts @@ -8,8 +8,8 @@ export class Util /** * Pads the right side of a string with spaces to the given length * @static - * @param {string} text - Text to pad - * @param {number} length - Length to pad to + * @param {string} text Text to pad + * @param {number} length Length to pad to * @returns {string} */ public static padRight(text: string, length: number): string @@ -22,11 +22,36 @@ export class Util * Returns the given string lowercased with any non * alphanumeric chars removed * @static - * @param {string} text - Text to normalize + * @param {string} text Text to normalize * @returns {string} */ public static normalize(text: string): string { return text.toLowerCase().replace(/[^a-z0-9]+/g, ''); } + + /** + * Assigns the given value along the given nested path within + * the provided initial object + * @static + * @param {any} obj Object to assign to + * @param {string[]} path Nested path to follow within the object + * @param {any} value Value to assign within the object + */ + public static assignNested(obj: any, path: string[], value: any): void + { + if (typeof obj !== 'object' || obj instanceof Array) + throw new Error(`Initial input of type '${typeof obj}' is not valid for nested assignment`); + + if (path.length === 0) + throw new Error('Missing nested assignment path'); + + let first: string = path.shift(); + if (typeof obj[first] === 'undefined') obj[first] = {}; + if (path.length > 1 && (typeof obj[first] !== 'object' || obj[first] instanceof Array)) + throw new Error(`Target '${first}' is not valid for nested assignment.`); + + if (path.length === 0) obj[first] = value; + else Util.assignNested(obj[first], path, value); + } } diff --git a/src/lib/storage/JSONProvider.ts b/src/lib/storage/JSONProvider.ts new file mode 100644 index 00000000..1d9a0a28 --- /dev/null +++ b/src/lib/storage/JSONProvider.ts @@ -0,0 +1,43 @@ +import { StorageProvider } from './StorageProvider'; +import DB = require('node-json-db'); + +export class JSONProvider extends StorageProvider +{ + private _name: string; + private _db: DB; + public constructor(name: string) + { + super(); + this._name = name; + } + + public async init(): Promise + { + this._db = new DB(`storage/${this._name}`); + } + + public async keys(): Promise + { + return Object.keys(this._db.getData('/')); + } + + public async get(key: string): Promise + { + return this._db.getData(`/${key}`); + } + + public async set(key: string, value: string): Promise + { + this._db.push(`/${key}`, value); + } + + public async remove(key: string): Promise + { + this._db.delete(`/${key}`); + } + + public async clear(): Promise + { + for (const key of await this.keys()) this._db.delete(`/${key}`); + } +} diff --git a/src/lib/storage/StorageProvider.ts b/src/lib/storage/StorageProvider.ts new file mode 100644 index 00000000..e3cda74f --- /dev/null +++ b/src/lib/storage/StorageProvider.ts @@ -0,0 +1,9 @@ +export abstract class StorageProvider +{ + public abstract async init(): Promise; + public abstract async keys(): Promise; + public abstract async get(key: string): Promise; + public abstract async set(key: string, value: string): Promise; + public abstract async remove(key: string): Promise; + public abstract async clear(): Promise; +}