Skip to content

Commit

Permalink
Add abstract StorageProvider, JSONProvider, Util.assignNested
Browse files Browse the repository at this point in the history
Still so much to do T-T
  • Loading branch information
zajrik committed Mar 26, 2017
1 parent 1e9db3a commit 5eac4af
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
43 changes: 43 additions & 0 deletions src/lib/storage/JSONProvider.ts
Original file line number Diff line number Diff line change
@@ -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<void>
{
this._db = new DB(`storage/${this._name}`);
}

public async keys(): Promise<string[]>
{
return Object.keys(this._db.getData('/'));
}

public async get(key: string): Promise<string>
{
return this._db.getData(`/${key}`);
}

public async set(key: string, value: string): Promise<void>
{
this._db.push(`/${key}`, value);
}

public async remove(key: string): Promise<void>
{
this._db.delete(`/${key}`);
}

public async clear(): Promise<void>
{
for (const key of await this.keys()) this._db.delete(`/${key}`);
}
}
9 changes: 9 additions & 0 deletions src/lib/storage/StorageProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export abstract class StorageProvider
{
public abstract async init(): Promise<void>;
public abstract async keys(): Promise<string[]>;
public abstract async get(key: string): Promise<string>;
public abstract async set(key: string, value: string): Promise<void>;
public abstract async remove(key: string): Promise<void>;
public abstract async clear(): Promise<void>;
}

0 comments on commit 5eac4af

Please sign in to comment.