From 2575007d084f3c23955120d9764f6238dfb3b192 Mon Sep 17 00:00:00 2001 From: Allen Gammel Date: Wed, 29 Aug 2018 13:35:04 -0500 Subject: [PATCH] Added support fot the custom EventEmitter. Added jsdoc comments. --- types/node-localstorage/index.d.ts | 95 +++++++++++++++---- .../node-localstorage-tests.ts | 32 ++++++- 2 files changed, 106 insertions(+), 21 deletions(-) diff --git a/types/node-localstorage/index.d.ts b/types/node-localstorage/index.d.ts index ffc9bb5dc7..960d40f1da 100644 --- a/types/node-localstorage/index.d.ts +++ b/types/node-localstorage/index.d.ts @@ -4,13 +4,14 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 -export class LocalStorage { - /** - * Creates a new LocalStorage instance - * @param location The location in which the local storage resides - * @param quota The partitioned size of the local storage - */ - constructor(location: string, quota?: number); +import { EventEmitter } from 'events'; + +/** + * A drop-in substitute for the browser native localStorage API that runs on node.js. + * Copyright (c) 2012, Lawrence S. Maccherone, Jr. -- https://github.com/lmaccherone/node-localstorage + * (MIT License) + */ +export class LocalStorage extends EventEmitter { /** * The number of keys in the local storage. */ @@ -35,29 +36,91 @@ export class LocalStorage { */ removeItem(key: string): void; /** - * + * Stores a `key` and it's `value` in to the local store. * @param key Unique identifier for the new local storage item * @param value The value of the item */ setItem(key: string, value: string): void; -} -export class JSONStorage extends LocalStorage { + /** + * Internal event `storage` is fired on the `removeItem`, `setItem` and `clear` methods, if subscribed. + * @param eventName The only `eventName` that is supported is `storage`. + * @param callback The call back type is of `StorageEvent`. + */ + on(eventName: 'storage', callback: (event: StorageEvent) => void): this; /** * Creates a new LocalStorage instance * @param location The location in which the local storage resides * @param quota The partitioned size of the local storage */ constructor(location: string, quota?: number); +} +export class JSONStorage extends LocalStorage { /** - * Gets the value of the given [key] + * Gets the value of the given `key`. This method performs a `JSON.parse` on the `value` before returning it. * @param key The key whose value you want to retrieve. - * @returns `Array of Objects` or an `Object`. This `getItem` method performs a `JSON.parse` on the value before returning it. + * @returns `Array of Objects` or an `Object`. This `getItem` method performs a `JSON.parse` on the `value` before returning it. */ - getItem(key: string): any | null; + getItem(key: string): any; /** - * + * Stores a `key` and it's `value` in to the local store. The given `value` will have `JSON.stringify` performed on it prior to storing. * @param key Unique identifier for the new local storage item - * @param value The value associated with the `key`. This `setItem` method performs a `JSON.stringify` on the value before storing it. The `value` *must* be a valid `JSON` object. + * @param value The value associated with the `key`. This `setItem` method performs a `JSON.stringify` on the value before storage. The `value` *must* be a valid `JSON` object. */ - setItem(key: string, value: string | [] | object): void; + setItem(key: string, value: any): void; + /** + * Creates a new LocalStorage instance + * @param location The location in which the local storage resides + * @param quota The partitioned size of the local storage + */ + constructor(location: string, quota?: number); +} + +/** + * Contains information regarding a `storage` event. + */ +export class StorageEvent { + /** + * Affected `key`. + */ + key: string; + /** + * Previous `value` associated with the `key`. + */ + oldValue: string; + /** + * Current `value` associated with the `key`. + */ + newValue: string; + /** + * The current `process.pid` for the calling process. + */ + url: string; + /** + * The location of the storage area. Defaults to `localStorage`. + */ + storageArea: string; + /** + * Creates an object containing information regarding a `storage` event. + * @param key Affected `key`. + * @param oldValue Previous `value` associated with the `key`. + * @param newValue Current `value` associated with the `key`. + * @param url The current `process.pid` for the calling process. + * @param storageArea The location of the storage area. Defaults to `localStorage`. + */ + constructor(key: string, oldValue: string, newValue: string, url: string, storageArea: string); +} + +/** + * Provides a specific `Error` object for **Quota Exceeded Errors**. + */ +export class QUOTA_EXCEEDED_ERR extends Error { + /** + * Message explaining the error. + */ + message: string; + /** + * Creates a specific `Error` object for **Quota Exceeded Errors**. + * @param message Message explaining the error. + */ + constructor(message: string); } diff --git a/types/node-localstorage/node-localstorage-tests.ts b/types/node-localstorage/node-localstorage-tests.ts index 5874e08135..25c27e622c 100644 --- a/types/node-localstorage/node-localstorage-tests.ts +++ b/types/node-localstorage/node-localstorage-tests.ts @@ -3,8 +3,14 @@ import { LocalStorage, JSONStorage } from 'node-localstorage'; const jsonStore = new JSONStorage('./jsonStore'); const textStore = new LocalStorage('./textStore'); -const mjkModel = { - firstName: 'Maynard', +interface Person { + givenName: string; + middleName: string; + familyName: string; +} + +const mjkModel: Person = { + givenName: 'Maynard', middleName: 'James', familyName: 'Keenan', }; @@ -12,6 +18,18 @@ const mjkModel = { textStore.clear(); jsonStore.clear(); +textStore.on('storage', (event) => { + if (textStore.getItem(event.key) !== event.newValue) { + throw new Error('textStore: An error occurred with the event handler.'); + } +}); + +jsonStore.on('storage', (event) => { + if (jsonStore.getItem(event.key) !== event.newValue) { + throw new Error('jsonStore: An error occurred with the event handler.'); + } +}); + textStore.setItem('tool', '46&2'); if (textStore.getItem('tool') !== '46&2') { @@ -20,10 +38,14 @@ if (textStore.getItem('tool') !== '46&2') { jsonStore.setItem('mjk', mjkModel); -const mjkTest = jsonStore.getItem('mjk'); +if (jsonStore.getItem('mjk') === null) { + throw new Error('.getItem failed.'); +} -if (mjkTest === null || mjkTest.firstName !== mjkModel.firstName || mjkTest.middleName !== mjkModel.middleName || mjkTest.familyName !== mjkModel.familyName) { - throw new Error('.setJson method is not working!'); +const mjkTest: Person = jsonStore.getItem('mjk'); + +if (mjkTest === null || mjkTest.givenName !== mjkModel.givenName || mjkTest.middleName !== mjkModel.middleName || mjkTest.familyName !== mjkModel.familyName) { + throw new Error('.setItem method is not working!'); } const keyCheck = textStore.key(0);