@common.js/mem 9.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @common.js/mem
2
+
3
+ The [mem](https://www.npmjs.com/package/mem) package exported as CommonJS modules.
4
+
5
+ Exported from [mem@9.0.2](https://www.npmjs.com/package/mem/v/9.0.2) using https://github.com/etienne-martin/common.js.
@@ -0,0 +1,116 @@
1
+ declare type AnyFunction = (...arguments_: any) => any;
2
+ interface CacheStorageContent<ValueType> {
3
+ data: ValueType;
4
+ maxAge: number;
5
+ }
6
+ interface CacheStorage<KeyType, ValueType> {
7
+ has: (key: KeyType) => boolean;
8
+ get: (key: KeyType) => CacheStorageContent<ValueType> | undefined;
9
+ set: (key: KeyType, value: CacheStorageContent<ValueType>) => void;
10
+ delete: (key: KeyType) => void;
11
+ clear?: () => void;
12
+ }
13
+ export interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
14
+ /**
15
+ Milliseconds until the cache expires.
16
+
17
+ @default Infinity
18
+ */
19
+ readonly maxAge?: number;
20
+ /**
21
+ Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
22
+
23
+ A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option).
24
+
25
+ You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
26
+
27
+ ```
28
+ import mem from 'mem';
29
+
30
+ mem(function_, {cacheKey: JSON.stringify});
31
+ ```
32
+
33
+ Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on.
34
+
35
+ ```
36
+ import mem from 'mem';
37
+ import serializeJavascript from 'serialize-javascript';
38
+
39
+ mem(function_, {cacheKey: serializeJavascript});
40
+ ```
41
+
42
+ @default arguments_ => arguments_[0]
43
+ @example arguments_ => JSON.stringify(arguments_)
44
+ */
45
+ readonly cacheKey?: (arguments_: Parameters<FunctionToMemoize>) => CacheKeyType;
46
+ /**
47
+ 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.
48
+
49
+ @default new Map()
50
+ @example new WeakMap()
51
+ */
52
+ readonly cache?: CacheStorage<CacheKeyType, ReturnType<FunctionToMemoize>>;
53
+ }
54
+ /**
55
+ [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.
56
+
57
+ @param fn - Function to be memoized.
58
+
59
+ @example
60
+ ```
61
+ import mem from 'mem';
62
+
63
+ let index = 0;
64
+ const counter = () => ++index;
65
+ const memoized = mem(counter);
66
+
67
+ memoized('foo');
68
+ //=> 1
69
+
70
+ // Cached as it's the same argument
71
+ memoized('foo');
72
+ //=> 1
73
+
74
+ // Not cached anymore as the arguments changed
75
+ memoized('bar');
76
+ //=> 2
77
+
78
+ memoized('bar');
79
+ //=> 2
80
+ ```
81
+ */
82
+ export default function mem<FunctionToMemoize extends AnyFunction, CacheKeyType>(fn: FunctionToMemoize, { cacheKey, cache, maxAge, }?: Options<FunctionToMemoize, CacheKeyType>): FunctionToMemoize;
83
+ /**
84
+ @returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
85
+
86
+ @example
87
+ ```
88
+ import {memDecorator} from 'mem';
89
+
90
+ class Example {
91
+ index = 0
92
+
93
+ @memDecorator()
94
+ counter() {
95
+ return ++this.index;
96
+ }
97
+ }
98
+
99
+ class ExampleWithOptions {
100
+ index = 0
101
+
102
+ @memDecorator({maxAge: 1000})
103
+ counter() {
104
+ return ++this.index;
105
+ }
106
+ }
107
+ ```
108
+ */
109
+ export declare function memDecorator<FunctionToMemoize extends AnyFunction, CacheKeyType>(options?: Options<FunctionToMemoize, CacheKeyType>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
110
+ /**
111
+ Clear all cached data of a memoized function.
112
+
113
+ @param fn - Memoized function.
114
+ */
115
+ export declare function memClear(fn: AnyFunction): void;
116
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ /**
13
+ [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.
14
+
15
+ @param fn - Function to be memoized.
16
+
17
+ @example
18
+ ```
19
+ import mem from 'mem';
20
+
21
+ let index = 0;
22
+ const counter = () => ++index;
23
+ const memoized = mem(counter);
24
+
25
+ memoized('foo');
26
+ //=> 1
27
+
28
+ // Cached as it's the same argument
29
+ memoized('foo');
30
+ //=> 1
31
+
32
+ // Not cached anymore as the arguments changed
33
+ memoized('bar');
34
+ //=> 2
35
+
36
+ memoized('bar');
37
+ //=> 2
38
+ ```
39
+ */ default: function() {
40
+ return mem;
41
+ },
42
+ memDecorator: function() {
43
+ return memDecorator;
44
+ },
45
+ memClear: function() {
46
+ return memClear;
47
+ }
48
+ });
49
+ var _mimicFn = /*#__PURE__*/ _interopRequireDefault(require("mimic-fn"));
50
+ var _mapAgeCleaner = /*#__PURE__*/ _interopRequireDefault(require("map-age-cleaner"));
51
+ function _interopRequireDefault(obj) {
52
+ return obj && obj.__esModule ? obj : {
53
+ default: obj
54
+ };
55
+ }
56
+ var cacheStore = new WeakMap();
57
+ function mem(fn) {
58
+ var ref = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, cacheKey = ref.cacheKey, _cache = ref.cache, cache = _cache === void 0 ? new Map() : _cache, maxAge = ref.maxAge;
59
+ if (typeof maxAge === "number") {
60
+ (0, _mapAgeCleaner.default)(cache);
61
+ }
62
+ var memoized = function memoized() {
63
+ for(var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++){
64
+ arguments_[_key] = arguments[_key];
65
+ }
66
+ var key = cacheKey ? cacheKey(arguments_) : arguments_[0];
67
+ var cacheItem = cache.get(key);
68
+ if (cacheItem) {
69
+ return cacheItem.data; // eslint-disable-line @typescript-eslint/no-unsafe-return
70
+ }
71
+ var result = fn.apply(this, arguments_);
72
+ cache.set(key, {
73
+ data: result,
74
+ maxAge: maxAge ? Date.now() + maxAge : Number.POSITIVE_INFINITY
75
+ });
76
+ return result; // eslint-disable-line @typescript-eslint/no-unsafe-return
77
+ };
78
+ (0, _mimicFn.default)(memoized, fn, {
79
+ ignoreNonConfigurable: true
80
+ });
81
+ cacheStore.set(memoized, cache);
82
+ return memoized;
83
+ }
84
+ function memDecorator() {
85
+ var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
86
+ var instanceMap = new WeakMap();
87
+ return function(target, propertyKey, descriptor) {
88
+ var input = target[propertyKey]; // eslint-disable-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
89
+ if (typeof input !== "function") {
90
+ throw new TypeError("The decorated value must be a function");
91
+ }
92
+ delete descriptor.value;
93
+ delete descriptor.writable;
94
+ descriptor.get = function() {
95
+ if (!instanceMap.has(this)) {
96
+ var value = mem(input, options);
97
+ instanceMap.set(this, value);
98
+ return value;
99
+ }
100
+ return instanceMap.get(this);
101
+ };
102
+ };
103
+ }
104
+ function memClear(fn) {
105
+ var cache = cacheStore.get(fn);
106
+ if (!cache) {
107
+ throw new TypeError("Can't clear a function that was not memoized!");
108
+ }
109
+ if (typeof cache.clear !== "function") {
110
+ throw new TypeError("The cache Map can't be cleared!");
111
+ }
112
+ cache.clear();
113
+ }
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@common.js/mem",
3
+ "version": "9.0.2",
4
+ "description": "mem package exported as CommonJS modules",
5
+ "license": "MIT",
6
+ "repository": "etienne-martin/common.js",
7
+ "funding": "https://github.com/sindresorhus/mem?sponsor=1",
8
+ "type": "commonjs",
9
+ "engines": {
10
+ "node": ">=12.20"
11
+ },
12
+ "scripts": {
13
+ "test": "xo && ava && npm run build && tsd",
14
+ "build": "del-cli dist && tsc"
15
+ },
16
+ "types": "dist/index.d.ts",
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "dependencies": {
21
+ "map-age-cleaner": "^0.1.3",
22
+ "@common.js/mimic-fn": "^4.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "@ava/typescript": "^1.1.1",
26
+ "@sindresorhus/tsconfig": "^1.0.2",
27
+ "@types/serialize-javascript": "^4.0.0",
28
+ "ava": "^3.15.0",
29
+ "del-cli": "^3.0.1",
30
+ "delay": "^4.4.0",
31
+ "serialize-javascript": "^5.0.1",
32
+ "ts-node": "^10.1.0",
33
+ "tsd": "^0.13.1",
34
+ "typescript": "^4.3.5",
35
+ "xo": "^0.41.0"
36
+ },
37
+ "ava": {
38
+ "timeout": "1m",
39
+ "extensions": {
40
+ "ts": "module"
41
+ },
42
+ "nonSemVerExperiments": {
43
+ "configurableModuleFormat": true
44
+ },
45
+ "nodeArguments": [
46
+ "--loader=ts-node/esm"
47
+ ]
48
+ },
49
+ "xo": {
50
+ "rules": {
51
+ "@typescript-eslint/member-ordering": "off",
52
+ "@typescript-eslint/no-var-requires": "off",
53
+ "@typescript-eslint/no-empty-function": "off"
54
+ }
55
+ },
56
+ "homepage": "https://github.com/etienne-martin/common.js#readme",
57
+ "main": "./dist/index.js"
58
+ }