diff --git a/types/node-localstorage/index.d.ts b/types/node-localstorage/index.d.ts new file mode 100644 index 0000000000..ffc9bb5dc7 --- /dev/null +++ b/types/node-localstorage/index.d.ts @@ -0,0 +1,63 @@ +// Type definitions for node-localstorage 1.3 +// Project: https://github.com/lmaccherone/node-localstorage +// Definitions by: Allen Gammel +// 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); + /** + * The number of keys in the local storage. + */ + length: number; + /** + * Clears the local storage + */ + clear(): void; + /** + * Gets the value of the given [key] + * @param key The key whose value you want to retrieve. + */ + getItem(key: string): string | null; + /** + * Retrieves the value of the key at the [index]. + * @param index The index of the key in which you want to return the value of. + */ + key(index: number): string; + /** + * Removes the specified [key] from local storage. + * @param key The key you want to remove. + */ + removeItem(key: string): void; + /** + * + * @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 { + /** + * 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); + /** + * Gets the value of the given [key] + * @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. + */ + getItem(key: string): any | null; + /** + * + * @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. + */ + setItem(key: string, value: string | [] | object): void; +} diff --git a/types/node-localstorage/node-localstorage-tests.ts b/types/node-localstorage/node-localstorage-tests.ts new file mode 100644 index 0000000000..5874e08135 --- /dev/null +++ b/types/node-localstorage/node-localstorage-tests.ts @@ -0,0 +1,36 @@ +import { LocalStorage, JSONStorage } from 'node-localstorage'; + +const jsonStore = new JSONStorage('./jsonStore'); +const textStore = new LocalStorage('./textStore'); + +const mjkModel = { + firstName: 'Maynard', + middleName: 'James', + familyName: 'Keenan', +}; + +textStore.clear(); +jsonStore.clear(); + +textStore.setItem('tool', '46&2'); + +if (textStore.getItem('tool') !== '46&2') { + throw new Error('.setItem method is not working!'); +} + +jsonStore.setItem('mjk', mjkModel); + +const mjkTest = jsonStore.getItem('mjk'); + +if (mjkTest === null || mjkTest.firstName !== mjkModel.firstName || mjkTest.middleName !== mjkModel.middleName || mjkTest.familyName !== mjkModel.familyName) { + throw new Error('.setJson method is not working!'); +} + +const keyCheck = textStore.key(0); + +if (keyCheck && keyCheck !== 'tool') { + throw new Error('.key method has failed.'); +} + +textStore.clear(); +jsonStore.clear(); diff --git a/types/node-localstorage/tsconfig.json b/types/node-localstorage/tsconfig.json new file mode 100644 index 0000000000..9411b285e2 --- /dev/null +++ b/types/node-localstorage/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-localstorage-tests.ts" + ] +} \ No newline at end of file diff --git a/types/node-localstorage/tslint.json b/types/node-localstorage/tslint.json new file mode 100644 index 0000000000..e60c15844f --- /dev/null +++ b/types/node-localstorage/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file