diff options
Diffstat (limited to 'node_modules/mem')
-rw-r--r-- | node_modules/mem/index.d.ts | 96 | ||||
-rw-r--r-- | node_modules/mem/index.js | 81 | ||||
-rw-r--r-- | node_modules/mem/license | 20 | ||||
-rw-r--r-- | node_modules/mem/node_modules/mimic-fn/index.d.ts | 54 | ||||
-rw-r--r-- | node_modules/mem/node_modules/mimic-fn/index.js | 13 | ||||
-rw-r--r-- | node_modules/mem/node_modules/mimic-fn/license | 9 | ||||
-rw-r--r-- | node_modules/mem/node_modules/mimic-fn/package.json | 74 | ||||
-rw-r--r-- | node_modules/mem/node_modules/mimic-fn/readme.md | 69 | ||||
-rw-r--r-- | node_modules/mem/package.json | 43 | ||||
-rw-r--r-- | node_modules/mem/readme.md | 74 |
10 files changed, 88 insertions, 445 deletions
diff --git a/node_modules/mem/index.d.ts b/node_modules/mem/index.d.ts deleted file mode 100644 index 786625520..000000000 --- a/node_modules/mem/index.d.ts +++ /dev/null @@ -1,96 +0,0 @@ -declare namespace mem { - interface CacheStorage<KeyType extends unknown, ValueType extends unknown> { - has(key: KeyType): boolean; - get(key: KeyType): ValueType | undefined; - set(key: KeyType, value: ValueType): void; - delete(key: KeyType): void; - clear?: () => void; - } - - interface Options< - ArgumentsType extends unknown[], - CacheKeyType extends unknown, - ReturnType extends unknown - > { - /** - Milliseconds until the cache expires. - - @default Infinity - */ - readonly maxAge?: number; - - /** - Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array. - - You could for example change it to only cache on the first argument `x => JSON.stringify(x)`. - */ - readonly cacheKey?: (...arguments: ArgumentsType) => CacheKeyType; - - /** - Use a different cache storage. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache. - - @default new Map() - */ - readonly cache?: CacheStorage<CacheKeyType, {data: ReturnType; maxAge: number}>; - - /** - Cache rejected promises. - - @default false - */ - readonly cachePromiseRejection?: boolean; - } -} - -declare const mem: { - /** - [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input. - - @param fn - Function to be memoized. - - @example - ``` - import mem = require('mem'); - - let i = 0; - const counter = () => ++i; - const memoized = mem(counter); - - memoized('foo'); - //=> 1 - - // Cached as it's the same arguments - memoized('foo'); - //=> 1 - - // Not cached anymore as the arguments changed - memoized('bar'); - //=> 2 - - memoized('bar'); - //=> 2 - ``` - */ - < - ArgumentsType extends unknown[], - ReturnType extends unknown, - CacheKeyType extends unknown - >( - fn: (...arguments: ArgumentsType) => ReturnType, - options?: mem.Options<ArgumentsType, CacheKeyType, ReturnType> - ): (...arguments: ArgumentsType) => ReturnType; - - /** - Clear all cached data of a memoized function. - - @param fn - Memoized function. - */ - clear<ArgumentsType extends unknown[], ReturnType extends unknown>( - fn: (...arguments: ArgumentsType) => ReturnType - ): void; - - // TODO: Remove this for the next major release - default: typeof mem; -}; - -export = mem; diff --git a/node_modules/mem/index.js b/node_modules/mem/index.js index 51faf012c..aa5a07398 100644 --- a/node_modules/mem/index.js +++ b/node_modules/mem/index.js @@ -1,84 +1,51 @@ 'use strict'; const mimicFn = require('mimic-fn'); -const isPromise = require('p-is-promise'); -const mapAgeCleaner = require('map-age-cleaner'); const cacheStore = new WeakMap(); -const defaultCacheKey = (...arguments_) => { - if (arguments_.length === 0) { - return '__defaultKey'; +const defaultCacheKey = function (x) { + if (arguments.length === 1 && (x === null || x === undefined || (typeof x !== 'function' && typeof x !== 'object'))) { + return x; } - if (arguments_.length === 1) { - const [firstArgument] = arguments_; - if ( - firstArgument === null || - firstArgument === undefined || - (typeof firstArgument !== 'function' && typeof firstArgument !== 'object') - ) { - return firstArgument; - } - } - - return JSON.stringify(arguments_); + return JSON.stringify(arguments); }; -const mem = (fn, options) => { - options = Object.assign({ +module.exports = (fn, opts) => { + opts = Object.assign({ cacheKey: defaultCacheKey, - cache: new Map(), - cachePromiseRejection: false - }, options); - - if (typeof options.maxAge === 'number') { - mapAgeCleaner(options.cache); - } - - const {cache} = options; - options.maxAge = options.maxAge || 0; - - const setData = (key, data) => { - cache.set(key, { - data, - maxAge: Date.now() + options.maxAge - }); - }; + cache: new Map() + }, opts); - const memoized = function (...arguments_) { - const key = options.cacheKey(...arguments_); + const memoized = function () { + const cache = cacheStore.get(memoized); + const key = opts.cacheKey.apply(null, arguments); if (cache.has(key)) { - return cache.get(key).data; - } + const c = cache.get(key); - const cacheItem = fn.call(this, ...arguments_); + if (typeof opts.maxAge !== 'number' || Date.now() < c.maxAge) { + return c.data; + } + } - setData(key, cacheItem); + const ret = fn.apply(null, arguments); - if (isPromise(cacheItem) && options.cachePromiseRejection === false) { - // Remove rejected promises from cache unless `cachePromiseRejection` is set to `true` - cacheItem.catch(() => cache.delete(key)); - } + cache.set(key, { + data: ret, + maxAge: Date.now() + (opts.maxAge || 0) + }); - return cacheItem; + return ret; }; - try { - // The below call will throw in some host environments - // See https://github.com/sindresorhus/mimic-fn/issues/10 - mimicFn(memoized, fn); - } catch (_) {} + mimicFn(memoized, fn); - cacheStore.set(memoized, options.cache); + cacheStore.set(memoized, opts.cache); return memoized; }; -module.exports = mem; -// TODO: Remove this for the next major release -module.exports.default = mem; - module.exports.clear = fn => { const cache = cacheStore.get(fn); diff --git a/node_modules/mem/license b/node_modules/mem/license index e7af2f771..654d0bfe9 100644 --- a/node_modules/mem/license +++ b/node_modules/mem/license @@ -1,9 +1,21 @@ -MIT License +The MIT License (MIT) Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/node_modules/mem/node_modules/mimic-fn/index.d.ts b/node_modules/mem/node_modules/mimic-fn/index.d.ts deleted file mode 100644 index b4047d589..000000000 --- a/node_modules/mem/node_modules/mimic-fn/index.d.ts +++ /dev/null @@ -1,54 +0,0 @@ -declare const mimicFn: { - /** - Make a function mimic another one. It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set. - - @param to - Mimicking function. - @param from - Function to mimic. - @returns The modified `to` function. - - @example - ``` - import mimicFn = require('mimic-fn'); - - function foo() {} - foo.unicorn = '🦄'; - - function wrapper() { - return foo(); - } - - console.log(wrapper.name); - //=> 'wrapper' - - mimicFn(wrapper, foo); - - console.log(wrapper.name); - //=> 'foo' - - console.log(wrapper.unicorn); - //=> '🦄' - ``` - */ - < - ArgumentsType extends unknown[], - ReturnType, - FunctionType extends (...arguments: ArgumentsType) => ReturnType - >( - to: (...arguments: ArgumentsType) => ReturnType, - from: FunctionType - ): FunctionType; - - // TODO: Remove this for the next major release, refactor the whole definition to: - // declare function mimicFn< - // ArgumentsType extends unknown[], - // ReturnType, - // FunctionType extends (...arguments: ArgumentsType) => ReturnType - // >( - // to: (...arguments: ArgumentsType) => ReturnType, - // from: FunctionType - // ): FunctionType; - // export = mimicFn; - default: typeof mimicFn; -}; - -export = mimicFn; diff --git a/node_modules/mem/node_modules/mimic-fn/index.js b/node_modules/mem/node_modules/mimic-fn/index.js deleted file mode 100644 index 1a5970517..000000000 --- a/node_modules/mem/node_modules/mimic-fn/index.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -const mimicFn = (to, from) => { - for (const prop of Reflect.ownKeys(from)) { - Object.defineProperty(to, prop, Object.getOwnPropertyDescriptor(from, prop)); - } - - return to; -}; - -module.exports = mimicFn; -// TODO: Remove this for the next major release -module.exports.default = mimicFn; diff --git a/node_modules/mem/node_modules/mimic-fn/license b/node_modules/mem/node_modules/mimic-fn/license deleted file mode 100644 index e7af2f771..000000000 --- a/node_modules/mem/node_modules/mimic-fn/license +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/mem/node_modules/mimic-fn/package.json b/node_modules/mem/node_modules/mimic-fn/package.json deleted file mode 100644 index b4970a28d..000000000 --- a/node_modules/mem/node_modules/mimic-fn/package.json +++ /dev/null @@ -1,74 +0,0 @@ -{ - "_from": "mimic-fn@^2.0.0", - "_id": "mimic-fn@2.1.0", - "_inBundle": false, - "_integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==", - "_location": "/mem/mimic-fn", - "_phantomChildren": {}, - "_requested": { - "type": "range", - "registry": true, - "raw": "mimic-fn@^2.0.0", - "name": "mimic-fn", - "escapedName": "mimic-fn", - "rawSpec": "^2.0.0", - "saveSpec": null, - "fetchSpec": "^2.0.0" - }, - "_requiredBy": [ - "/mem" - ], - "_resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", - "_shasum": "7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b", - "_spec": "mimic-fn@^2.0.0", - "_where": "/Users/mperrotte/npminc/cli/node_modules/mem", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - "bugs": { - "url": "https://github.com/sindresorhus/mimic-fn/issues" - }, - "bundleDependencies": false, - "deprecated": false, - "description": "Make a function mimic another one", - "devDependencies": { - "ava": "^1.4.1", - "tsd": "^0.7.1", - "xo": "^0.24.0" - }, - "engines": { - "node": ">=6" - }, - "files": [ - "index.js", - "index.d.ts" - ], - "homepage": "https://github.com/sindresorhus/mimic-fn#readme", - "keywords": [ - "function", - "mimic", - "imitate", - "rename", - "copy", - "inherit", - "properties", - "name", - "func", - "fn", - "set", - "infer", - "change" - ], - "license": "MIT", - "name": "mimic-fn", - "repository": { - "type": "git", - "url": "git+https://github.com/sindresorhus/mimic-fn.git" - }, - "scripts": { - "test": "xo && ava && tsd" - }, - "version": "2.1.0" -} diff --git a/node_modules/mem/node_modules/mimic-fn/readme.md b/node_modules/mem/node_modules/mimic-fn/readme.md deleted file mode 100644 index 0ef8a13d7..000000000 --- a/node_modules/mem/node_modules/mimic-fn/readme.md +++ /dev/null @@ -1,69 +0,0 @@ -# mimic-fn [![Build Status](https://travis-ci.org/sindresorhus/mimic-fn.svg?branch=master)](https://travis-ci.org/sindresorhus/mimic-fn) - -> Make a function mimic another one - -Useful when you wrap a function in another function and like to preserve the original name and other properties. - - -## Install - -``` -$ npm install mimic-fn -``` - - -## Usage - -```js -const mimicFn = require('mimic-fn'); - -function foo() {} -foo.unicorn = '🦄'; - -function wrapper() { - return foo(); -} - -console.log(wrapper.name); -//=> 'wrapper' - -mimicFn(wrapper, foo); - -console.log(wrapper.name); -//=> 'foo' - -console.log(wrapper.unicorn); -//=> '🦄' -``` - - -## API - -It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set. - -### mimicFn(to, from) - -Modifies the `to` function and returns it. - -#### to - -Type: `Function` - -Mimicking function. - -#### from - -Type: `Function` - -Function to mimic. - - -## Related - -- [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function -- [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name, length and other properties - - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/node_modules/mem/package.json b/node_modules/mem/package.json index eedf07469..1d1a2f732 100644 --- a/node_modules/mem/package.json +++ b/node_modules/mem/package.json @@ -1,27 +1,27 @@ { - "_from": "mem@^4.0.0", - "_id": "mem@4.3.0", + "_from": "mem@^1.1.0", + "_id": "mem@1.1.0", "_inBundle": false, - "_integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", + "_integrity": "sha1-Xt1StIXKHZAP5kiVUFOZoN+kX3Y=", "_location": "/mem", "_phantomChildren": {}, "_requested": { "type": "range", "registry": true, - "raw": "mem@^4.0.0", + "raw": "mem@^1.1.0", "name": "mem", "escapedName": "mem", - "rawSpec": "^4.0.0", + "rawSpec": "^1.1.0", "saveSpec": null, - "fetchSpec": "^4.0.0" + "fetchSpec": "^1.1.0" }, "_requiredBy": [ "/os-locale" ], - "_resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", - "_shasum": "461af497bc4ae09608cdb2e60eefb69bff744178", - "_spec": "mem@^4.0.0", - "_where": "/Users/mperrotte/npminc/cli/node_modules/os-locale", + "_resolved": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz", + "_shasum": "5edd52b485ca1d900fe64895505399a0dfa45f76", + "_spec": "mem@^1.1.0", + "_where": "/Users/isaacs/dev/npm/cli/node_modules/os-locale", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", @@ -32,24 +32,20 @@ }, "bundleDependencies": false, "dependencies": { - "map-age-cleaner": "^0.1.1", - "mimic-fn": "^2.0.0", - "p-is-promise": "^2.0.0" + "mimic-fn": "^1.0.0" }, "deprecated": false, "description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input", "devDependencies": { - "ava": "^1.4.1", - "delay": "^4.1.0", - "tsd": "^0.7.1", - "xo": "^0.24.0" + "ava": "*", + "delay": "^1.1.0", + "xo": "*" }, "engines": { - "node": ">=6" + "node": ">=4" }, "files": [ - "index.js", - "index.d.ts" + "index.js" ], "homepage": "https://github.com/sindresorhus/mem#readme", "keywords": [ @@ -72,7 +68,10 @@ "url": "git+https://github.com/sindresorhus/mem.git" }, "scripts": { - "test": "xo && ava && tsd" + "test": "xo && ava" }, - "version": "4.3.0" + "version": "1.1.0", + "xo": { + "esnext": true + } } diff --git a/node_modules/mem/readme.md b/node_modules/mem/readme.md index add4222b6..7ebab84f0 100644 --- a/node_modules/mem/readme.md +++ b/node_modules/mem/readme.md @@ -2,13 +2,11 @@ > [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input -Memory is automatically released when an item expires. - ## Install ``` -$ npm install mem +$ npm install --save mem ``` @@ -24,11 +22,11 @@ const memoized = mem(counter); memoized('foo'); //=> 1 -// Cached as it's the same arguments +// cached as it's the same arguments memoized('foo'); //=> 1 -// Not cached anymore as the arguments changed +// not cached anymore as the arguments changed memoized('bar'); //=> 2 @@ -42,37 +40,35 @@ memoized('bar'); const mem = require('mem'); let i = 0; -const counter = async () => ++i; +const counter = () => Promise.resolve(++i); const memoized = mem(counter); -(async () => { - console.log(await memoized()); +memoized().then(a => { + console.log(a); //=> 1 - // The return value didn't increase as it's cached - console.log(await memoized()); - //=> 1 -})(); + memoized().then(b => { + // the return value didn't increase as it's cached + console.log(b); + //=> 1 + }); +}); ``` ```js const mem = require('mem'); const got = require('got'); -const delay = require('delay'); - const memGot = mem(got, {maxAge: 1000}); -(async () => { - await memGot('sindresorhus.com'); - - // This call is cached - await memGot('sindresorhus.com'); - - await delay(2000); - - // This call is not cached as the cache has expired - await memGot('sindresorhus.com'); -})(); +memGot('sindresorhus.com').then(() => { + // this call is cached + memGot('sindresorhus.com').then(() => { + setTimeout(() => { + // this call is not cached as the cache has expired + memGot('sindresorhus.com').then(() => {}); + }, 2000); + }); +}); ``` @@ -88,8 +84,6 @@ Function to be memoized. #### options -Type: `Object` - ##### maxAge Type: `number`<br> @@ -110,14 +104,7 @@ You could for example change it to only cache on the first argument `x => JSON.s Type: `Object`<br> Default: `new Map()` -Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache. - -##### cachePromiseRejection - -Type: `boolean`<br> -Default: `false` - -Cache rejected promises. +Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, and optionally `.clear()`. You could for example use a `WeakMap` instead. ### mem.clear(fn) @@ -146,22 +133,15 @@ const got = require('got'); const cache = new StatsMap(); const memGot = mem(got, {cache}); -(async () => { - await memGot('sindresorhus.com'); - await memGot('sindresorhus.com'); - await memGot('sindresorhus.com'); +memGot('sindresorhus.com') + .then(() => memGot('sindresorhus.com')) + .then(() => memGot('sindresorhus.com')); - console.log(cache.stats); - //=> {hits: 2, misses: 1} -})(); +console.log(cache.stats); +//=> {hits: 2, misses: 1} ``` -## Related - -- [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions - - ## License MIT © [Sindre Sorhus](https://sindresorhus.com) |