From 10d9fda78780b2423fe785fc237d4b90b4db77fb Mon Sep 17 00:00:00 2001 From: Travis Thieman Date: Mon, 16 Oct 2017 17:39:27 -0400 Subject: [PATCH] memory-cache: Add type parameters and expose constructor to instantiate new caches (#20501) * memory-cache: Add type parameters and expose constructor to instantiate new caches * Return arrays from keys() function * Extend dtslint and fix linting errors * Add version 0.2 to type header --- types/memory-cache/index.d.ts | 41 +++++++++++++++++------- types/memory-cache/memory-cache-tests.ts | 32 +++++++++++------- types/memory-cache/tslint.json | 1 + 3 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 types/memory-cache/tslint.json diff --git a/types/memory-cache/index.d.ts b/types/memory-cache/index.d.ts index 4825fbe7a5..3c40a4b6dd 100644 --- a/types/memory-cache/index.d.ts +++ b/types/memory-cache/index.d.ts @@ -1,20 +1,37 @@ -// Type definitions for memory-cache +// Type definitions for memory-cache 0.2 // Project: https://github.com/ptarjan/node-cache // Definitions by: Jeff Goddard +// Travis Thieman // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Imported from: https://github.com/soywiz/typescript-node-definitions/memory-cache.d.ts +// Originally imported from: https://github.com/soywiz/typescript-node-definitions/memory-cache.d.ts +export class CacheClass { + put(key: K, value: V, time?: number, timeoutCallback?: (key: K, value: V) => void): V; + get(key: K): V; + del(key: K): void; + clear(): void; -export declare function put(key: any, value: any, time?: number, timeoutCallback?: (key: any, value: any) => void): void; -export declare function get(key: any): any; -export declare function del(key: any): void; -export declare function clear(): void; + size(): number; + memsize(): number; -export declare function size(): number; -export declare function memsize(): number; + debug(bool: boolean): void; + hits(): number; + misses(): number; + keys(): K[]; +} -export declare function debug(bool: boolean): void; -export declare function hits(): number; -export declare function misses(): number; -export declare function keys(): any; +export const Cache: typeof CacheClass; + +export function put(key: any, value: V, time?: number, timeoutCallback?: (key: any, value: any) => void): V; +export function get(key: any): any; +export function del(key: any): void; +export function clear(): void; + +export function size(): number; +export function memsize(): number; + +export function debug(bool: boolean): void; +export function hits(): number; +export function misses(): number; +export function keys(): any[]; diff --git a/types/memory-cache/memory-cache-tests.ts b/types/memory-cache/memory-cache-tests.ts index 15fad71f10..4cf5bc2a70 100644 --- a/types/memory-cache/memory-cache-tests.ts +++ b/types/memory-cache/memory-cache-tests.ts @@ -1,19 +1,16 @@ - import memoryCache = require('memory-cache'); -var key: any; -var value: any; -var bool: boolean; -var num: number; +const key: any = 'sampleKey'; +let value: string; +const bool = false; +let num: number; +let returnedValue: string; -memoryCache.put(key, value); -memoryCache.put(key, value, num); -memoryCache.put(key, value, num, (key) => { +returnedValue = memoryCache.put(key, value); +returnedValue = memoryCache.put(key, value, num); +returnedValue = memoryCache.put(key, value, num, (key) => { }); +returnedValue = memoryCache.put(key, value, num, (key, value) => { }); -}); -memoryCache.put(key, value, num, (key, value) => { - -}); value = memoryCache.get(key); memoryCache.del(key); memoryCache.clear(); @@ -24,3 +21,14 @@ num = memoryCache.memsize(); memoryCache.debug(bool); num = memoryCache.hits(); num = memoryCache.misses(); + +const customCache = new memoryCache.Cache(); + +const customKey = 'customKey'; +let customValue: boolean; +let customKeys: string[]; + +customValue = customCache.put(customKey, customValue); +customCache.get(customKey); +customCache.del(customKey); +customKeys = customCache.keys(); diff --git a/types/memory-cache/tslint.json b/types/memory-cache/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/memory-cache/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }