From 42683dfcef3ec68497e190a56f97a792299226b2 Mon Sep 17 00:00:00 2001 From: ikokostya Date: Sat, 16 Feb 2019 22:13:22 +0300 Subject: [PATCH] [lru-cache] Introduce 5.0.0 version --- types/lru-cache/index.d.ts | 240 ++++++++++++++--------------- types/lru-cache/lru-cache-tests.ts | 30 ++-- types/lru-cache/tsconfig.json | 2 +- 3 files changed, 133 insertions(+), 139 deletions(-) diff --git a/types/lru-cache/index.d.ts b/types/lru-cache/index.d.ts index ed40619b11..75550e7249 100644 --- a/types/lru-cache/index.d.ts +++ b/types/lru-cache/index.d.ts @@ -1,23 +1,127 @@ -// Type definitions for lru-cache 4.1 +// Type definitions for lru-cache 5.0 // Project: https://github.com/isaacs/node-lru-cache // Definitions by: Bart van der Schoor // BendingBender // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -export = LRU; +declare class LRUCache { + constructor(options?: LRUCache.Options); + constructor(max: number); -declare const LRU: LRU; + /** + * Return total length of objects in cache taking into account `length` options function. + */ + readonly length: number; -interface LRU { - (opts?: LRU.Options): LRU.Cache; - (max: number): LRU.Cache; - new (opts?: LRU.Options): LRU.Cache; - new (max: number): LRU.Cache; + /** + * Return total quantity of objects currently in cache. Note, + * that `stale` (see options) items are returned as part of this item count. + */ + readonly itemCount: number; + + /** + * Same as Options.allowStale. + */ + allowStale: boolean; + + /** + * Same as Options.length. + */ + lengthCalculator(value: V): number; + + /** + * Same as Options.max. Resizes the cache when the `max` changes. + */ + max: number; + + /** + * Same as Options.maxAge. Resizes the cache when the `maxAge` changes. + */ + maxAge: number; + + /** + * Will update the "recently used"-ness of the key. They do what you think. + * `maxAge` is optional and overrides the cache `maxAge` option if provided. + */ + set(key: K, value: V, maxAge?: number): boolean; + + /** + * Will update the "recently used"-ness of the key. They do what you think. + * `maxAge` is optional and overrides the cache `maxAge` option if provided. + * + * If the key is not found, will return `undefined`. + */ + get(key: K): V | undefined; + + /** + * Returns the key value (or `undefined` if not found) without updating + * the "recently used"-ness of the key. + * + * (If you find yourself using this a lot, you might be using the wrong + * sort of data structure, but there are some use cases where it's handy.) + */ + peek(key: K): V | undefined; + + /** + * Check if a key is in the cache, without updating the recent-ness + * or deleting it for being stale. + */ + has(key: K): boolean; + + /** + * Deletes a key out of the cache. + */ + del(key: K): void; + + /** + * Clear the cache entirely, throwing away all values. + */ + reset(): void; + + /** + * Manually iterates over the entire cache proactively pruning old entries. + */ + prune(): void; + + /** + * Just like `Array.prototype.forEach`. Iterates over all the keys in the cache, + * in order of recent-ness. (Ie, more recently used items are iterated over first.) + */ + forEach(callbackFn: (this: T, value: V, key: K, cache: this) => void, thisArg?: T): void; + + /** + * The same as `cache.forEach(...)` but items are iterated over in reverse order. + * (ie, less recently used items are iterated over first.) + */ + rforEach(callbackFn: (this: T, value: V, key: K, cache: this) => void, thisArg?: T): void; + + /** + * Return an array of the keys in the cache. + */ + keys(): K[]; + + /** + * Return an array of the values in the cache. + */ + values(): V[]; + + /** + * Return an array of the cache entries ready for serialization and usage with `destinationCache.load(arr)`. + */ + dump(): Array>; + + /** + * Loads another cache entries array, obtained with `sourceCache.dump()`, + * into the cache. The destination cache is reset before loading new entries + * + * @param cacheEntries Obtained from `sourceCache.dump()` + */ + load(cacheEntries: ReadonlyArray>): void; } -declare namespace LRU { - interface Options { +declare namespace LRUCache { + interface Options { /** * The maximum size of the cache, checked by applying the length * function to all values in the cache. Not setting this is kind of silly, @@ -71,121 +175,11 @@ declare namespace LRU { noDisposeOnSet?: boolean; } - interface Cache { - /** - * Return total length of objects in cache taking into account `length` options function. - */ - readonly length: number; - - /** - * Return total quantity of objects currently in cache. Note, - * that `stale` (see options) items are returned as part of this item count. - */ - readonly itemCount: number; - - /** - * Same as Options.allowStale. - */ - allowStale: boolean; - - /** - * Same as Options.length. - */ - lengthCalculator(value: V): number; - - /** - * Same as Options.max. Resizes the cache when the `max` changes. - */ - max: number; - - /** - * Same as Options.maxAge. Resizes the cache when the `maxAge` changes. - */ - maxAge: number; - - /** - * Will update the "recently used"-ness of the key. They do what you think. - * `maxAge` is optional and overrides the cache `maxAge` option if provided. - */ - set(key: K, value: V, maxAge?: number): boolean; - - /** - * Will update the "recently used"-ness of the key. They do what you think. - * `maxAge` is optional and overrides the cache `maxAge` option if provided. - * - * If the key is not found, will return `undefined`. - */ - get(key: K): V | undefined; - - /** - * Returns the key value (or `undefined` if not found) without updating - * the "recently used"-ness of the key. - * - * (If you find yourself using this a lot, you might be using the wrong - * sort of data structure, but there are some use cases where it's handy.) - */ - peek(key: K): V | undefined; - - /** - * Check if a key is in the cache, without updating the recent-ness - * or deleting it for being stale. - */ - has(key: K): boolean; - - /** - * Deletes a key out of the cache. - */ - del(key: K): void; - - /** - * Clear the cache entirely, throwing away all values. - */ - reset(): void; - - /** - * Manually iterates over the entire cache proactively pruning old entries. - */ - prune(): void; - - /** - * Just like `Array.prototype.forEach`. Iterates over all the keys in the cache, - * in order of recent-ness. (Ie, more recently used items are iterated over first.) - */ - forEach(callbackFn: (this: T, value: V, key: K, cache: this) => void, thisArg?: T): void; - - /** - * The same as `cache.forEach(...)` but items are iterated over in reverse order. - * (ie, less recently used items are iterated over first.) - */ - rforEach(callbackFn: (this: T, value: V, key: K, cache: this) => void, thisArg?: T): void; - - /** - * Return an array of the keys in the cache. - */ - keys(): K[]; - - /** - * Return an array of the values in the cache. - */ - values(): V[]; - - /** - * Return an array of the cache entries ready for serialization and usage with `destinationCache.load(arr)`. - */ - dump(): Array>; - - /** - * Loads another cache entries array, obtained with `sourceCache.dump()`, - * into the cache. The destination cache is reset before loading new entries - * - * @param cacheEntries Obtained from `sourceCache.dump()` - */ - load(cacheEntries: ReadonlyArray>): void; - } - - interface LRUEntry { + interface Entry { k: K; v: V; e: number; } } + +export = LRUCache; diff --git a/types/lru-cache/lru-cache-tests.ts b/types/lru-cache/lru-cache-tests.ts index c7d3977f7f..ec8fa21bf2 100644 --- a/types/lru-cache/lru-cache-tests.ts +++ b/types/lru-cache/lru-cache-tests.ts @@ -1,4 +1,4 @@ -import LRU = require('lru-cache'); +import * as LRUCache from 'lru-cache'; const num = 1; @@ -10,9 +10,9 @@ const foo = { foo() {} }; -const cache = LRU(); -cache; // $ExpectType Cache -LRU({ // $ExpectType Cache +const cache = new LRUCache(); +cache; // $ExpectType LRUCache +new LRUCache({ // $ExpectType LRUCache max: num, maxAge: num, length(value) { @@ -26,9 +26,9 @@ LRU({ // $ExpectType Cache stale: false, noDisposeOnSet: false, }); -LRU(num); // $ExpectType Cache -new LRU(); // $ExpectType Cache -new LRU({ // $ExpectType Cache +new LRUCache(num); // $ExpectType LRUCache +new LRUCache(); // $ExpectType LRUCache +new LRUCache({ // $ExpectType LRUCache max: num, maxAge: num, length: (value) => { @@ -38,7 +38,7 @@ new LRU({ // $ExpectType Cache stale: false, noDisposeOnSet: false, }); -new LRU(num); // $ExpectType Cache +new LRUCache(num); // $ExpectType LRUCache cache.length; // $ExpectType number cache.length = 1; // $ExpectError @@ -80,26 +80,26 @@ cache.prune(); cache.forEach(function(value, key, cache) { value; // $ExpectType Foo key; // $ExpectType string - cache; // $ExpectType Cache - this; // $ExpectType Cache + cache; // $ExpectType LRUCache + this; // $ExpectType LRUCache }); cache.forEach(function(value, key, cache) { value; // $ExpectType Foo key; // $ExpectType string - cache; // $ExpectType Cache + cache; // $ExpectType LRUCache this; // $ExpectType { foo(): void; } }, foo); cache.rforEach(function(value, key, cache) { value; // $ExpectType Foo key; // $ExpectType string - cache; // $ExpectType Cache - this; // $ExpectType Cache + cache; // $ExpectType LRUCache + this; // $ExpectType LRUCache }); cache.rforEach(function(value, key, cache) { value; // $ExpectType Foo key; // $ExpectType string - cache; // $ExpectType Cache + cache; // $ExpectType LRUCache this; // $ExpectType { foo(): void; } }, foo); @@ -107,5 +107,5 @@ cache.keys(); // $ExpectType string[] cache.values(); // $ExpectType Foo[] const dump = cache.dump(); -dump; // $ExpectType LRUEntry[] +dump; // $ExpectType Entry[] cache.load(dump); diff --git a/types/lru-cache/tsconfig.json b/types/lru-cache/tsconfig.json index de5c387339..c7d3687334 100644 --- a/types/lru-cache/tsconfig.json +++ b/types/lru-cache/tsconfig.json @@ -20,4 +20,4 @@ "index.d.ts", "lru-cache-tests.ts" ] -} \ No newline at end of file +}