From fe2e689e508fc4e120cf409849606c20e3ce0bc3 Mon Sep 17 00:00:00 2001 From: Allen Gammel Date: Tue, 21 Aug 2018 16:53:48 -0500 Subject: [PATCH] added required files. --- .../expired-storage/expired-storage-tests.ts | 67 +++++++++++ types/expired-storage/index.d.ts | 108 ++++++++++++++++++ types/expired-storage/tsconfig.json | 23 ++++ types/expired-storage/tslint.json | 8 ++ 4 files changed, 206 insertions(+) create mode 100644 types/expired-storage/expired-storage-tests.ts create mode 100644 types/expired-storage/index.d.ts create mode 100644 types/expired-storage/tsconfig.json create mode 100644 types/expired-storage/tslint.json diff --git a/types/expired-storage/expired-storage-tests.ts b/types/expired-storage/expired-storage-tests.ts new file mode 100644 index 0000000000..946051e218 --- /dev/null +++ b/types/expired-storage/expired-storage-tests.ts @@ -0,0 +1,67 @@ +import { LocalStorage } from 'node-localstorage'; +import ExpiredStorage = require('expired-storage'); + +const localStorage = new LocalStorage('./scratch', Infinity); +const expiredStorage = new ExpiredStorage(localStorage); + +const mjkModel = { + firstName: 'Maynard', + middleName: 'James', + familyName: 'Keenan', +}; + +expiredStorage.clear(); + +expiredStorage.setItem('tool', '46&2', 10); + +if (expiredStorage.getItem('tool') !== '46&2') { + throw new Error('.setItem method is not working!'); +} + +expiredStorage.setJson('mjk', mjkModel, 1); + +const mjkTest = expiredStorage.getJson('mjk'); + +if (typeof mjkTest === 'undefined' || mjkTest.firstName !== mjkModel.firstName || mjkTest.middleName !== mjkModel.middleName || mjkTest.familyName !== mjkModel.familyName) { + throw new Error('.setJson method is not working!'); +} + +if (expiredStorage.keys(true).length === 2) { + throw new Error('.keys method has failed.'); +} + +expiredStorage.setItem('apc', 'brena', 2); + +const goBackToSleep = expiredStorage.peek('apc'); + +if (!goBackToSleep.isExpired && goBackToSleep.value !== 'brena') { + throw new Error('.peek -- we have expired early... or our value was incorrect...'); +} + +do { + // nothing here to see +} while (!expiredStorage.isExpired('apc')); + +const expiredKeys = expiredStorage.clearExpired(); + +if (typeof expiredKeys['apc'] !== 'undefined') { + throw new Error('.clearExpired did not work..'); +} + +if (expiredStorage.getItem('apc') !== null) { + throw new Error('Expiration as a whole does not work.'); +} + +expiredStorage.setItem('puscifer', 'the humbling river', 10000); + +expiredStorage.updateExpiration('puscifer', 5000); + +if (expiredStorage.getTimeLeft('puscifer') >= 10000) { + throw new Error('.updateExpiration method is broken -- or .getTimeLeft method is broken.'); +} + +if (!expiredStorage.isExpired('mjk')) { + throw new Error('.isExpired or Expiration as a whole is not working...'); +} + +expiredStorage.clear(); diff --git a/types/expired-storage/index.d.ts b/types/expired-storage/index.d.ts new file mode 100644 index 0000000000..c7c113726e --- /dev/null +++ b/types/expired-storage/index.d.ts @@ -0,0 +1,108 @@ +// Type definitions for extended-storage v1.0.2 +// Project: https://github.com/RonenNess/ExpiredStorage/ +// Definitions by: Allen Gammel +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module "expired-storage" { + + export = ExpiredStorage; + + class ExpiredStorage { + constructor(); + constructor(localStorage?: any); + /** + * Clear the entire storage and all keys in it. + */ + clear(): void; + /** + * Get a json serializable value. This basically calls JSON.parse on the returned value. + * @param key Item key to get (string). + * @return Stored value (JSON.parsed), or undefined if not set / expired. + **/ + getJson(key: string): any; + /** + * Get item. + * @param key: Item key to get (string). + * @return: Stored value, or undefined if not set / expired. + */ + getItem(key: string): string | null; + /** + * Remove an item. + * @param key Item key to remove (string). + * @return Storage.removeItem() return code. + */ + removeItem(key: string): any; + /** + * Set a json serializable value. This basically calls JSON.stringify on 'val' before setting it. + * @param key Item key to set (string). + * @param value Value to store (object, will be JSON.stringified). + * @param expiration Expiration time, in seconds. If not provided, will not set expiration time. + * @param return Storage.setItem() return code. + **/ + setJson(key: string, value: object, expirationTime?: number): any; + /** + * Set item. + * @param key Item key to set (string). + * @param value Value to store (string). + * @param expiration Expiration time, in seconds. If not provided, will not set expiration time. + * @return Storage.setItem() return code. + **/ + setItem(key: string, value: string, expiration?: number): any; + /** + * Get all keys in storage, not including internal keys used to store expiration. + * @param: includeExpired: if true, will also include expired keys. + * @return: Array with keys. + */ + keys(includeExpired: boolean): Array | null; + /** + * Get item time left to live. + * @param key Item key to get (string). + * @return Time left to expire (in seconds), or null if don't have expiration date. + */ + getTimeLeft(key: string): number | null; + /** + * Clear expired keys. + * If you never call this function, expired keys will remain until you try to get them / reset a new value. + * + * @param return: List of removed keys due to expiration. + */ + clearExpired(): Array | null; + /** + * Return if an item is expired (don't remove it, even if expired). + * @param key Item key to check (string). + * @return True if expired, False otherwise. + */ + isExpired(key: string): boolean; + /** + * Get item + metadata such as time left and if expired. + * Even if item expired, will not remove it. + * @param key Item key to get (string). + * @return {ExpiredStorage.PeekInterface} PeekInterface with: {value, timeLeft, isExpired} + */ + peek(key: string): ExpiredStorage.PeekInterface; + /** + * Update expiration time for an item (note: doesn't validate that the item is set). + * @param key: Item key to update expiration for (string). + * @param expiration: New expiration time in seconds to set. + * @return: Storage.setItem() return code for setting new expiration. + **/ + updateExpiration(key: string, expiration: number): any; + } + + namespace ExpiredStorage { + export interface PeekInterface { + /** + * The value of a [key] + */ + value: string | null, + /** + * Time remaining until expiration + */ + timeLeft: number | null, + /** + * Has the [key] expired or not + */ + isExpired: boolean, + } + } +} diff --git a/types/expired-storage/tsconfig.json b/types/expired-storage/tsconfig.json new file mode 100644 index 0000000000..140265cea8 --- /dev/null +++ b/types/expired-storage/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": false, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "expired-storage-tests.ts" + ] +} \ No newline at end of file diff --git a/types/expired-storage/tslint.json b/types/expired-storage/tslint.json new file mode 100644 index 0000000000..705507d3a2 --- /dev/null +++ b/types/expired-storage/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "no-void-expression": false, + "no-duplicate-imports": false, + "no-unnecessary-generics": false + } +} \ No newline at end of file