added required files.

This commit is contained in:
Allen Gammel
2018-08-21 16:53:48 -05:00
parent b3c691dcd2
commit fe2e689e50
4 changed files with 206 additions and 0 deletions
@@ -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();
+108
View File
@@ -0,0 +1,108 @@
// Type definitions for extended-storage v1.0.2
// Project: https://github.com/RonenNess/ExpiredStorage/
// Definitions by: Allen Gammel <https://github.com/intolerance>
// 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<string> | 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<string> | 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,
}
}
}
+23
View File
@@ -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"
]
}
+8
View File
@@ -0,0 +1,8 @@
{
"extends": "dtslint/dt.json",
"rules": {
"no-void-expression": false,
"no-duplicate-imports": false,
"no-unnecessary-generics": false
}
}