@webdeveric/utils 0.74.2 → 0.75.0
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/dist/cjs/memo.js +18 -0
- package/dist/mjs/memo.js +15 -0
- package/dist/types/memo.d.ts +8 -0
- package/package.json +4 -4
package/dist/cjs/memo.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.memo = memo;
|
|
4
|
+
function memo(fn, getCacheKey) {
|
|
5
|
+
const defaultGetCacheKey = (args) => args[0] ?? JSON.stringify(args);
|
|
6
|
+
const resolvedGetCacheKey = getCacheKey ?? defaultGetCacheKey;
|
|
7
|
+
const memoized = (...args) => {
|
|
8
|
+
const key = resolvedGetCacheKey(args);
|
|
9
|
+
if (memoized.cache.has(key)) {
|
|
10
|
+
return memoized.cache.get(key);
|
|
11
|
+
}
|
|
12
|
+
const value = fn(...args);
|
|
13
|
+
memoized.cache.set(key, value);
|
|
14
|
+
return value;
|
|
15
|
+
};
|
|
16
|
+
memoized.cache = new Map();
|
|
17
|
+
return memoized;
|
|
18
|
+
}
|
package/dist/mjs/memo.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function memo(fn, getCacheKey) {
|
|
2
|
+
const defaultGetCacheKey = (args) => args[0] ?? JSON.stringify(args);
|
|
3
|
+
const resolvedGetCacheKey = getCacheKey ?? defaultGetCacheKey;
|
|
4
|
+
const memoized = (...args) => {
|
|
5
|
+
const key = resolvedGetCacheKey(args);
|
|
6
|
+
if (memoized.cache.has(key)) {
|
|
7
|
+
return memoized.cache.get(key);
|
|
8
|
+
}
|
|
9
|
+
const value = fn(...args);
|
|
10
|
+
memoized.cache.set(key, value);
|
|
11
|
+
return value;
|
|
12
|
+
};
|
|
13
|
+
memoized.cache = new Map();
|
|
14
|
+
return memoized;
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AnyFunction } from './types/common.js';
|
|
2
|
+
export type CacheKeyFn<Fn extends AnyFunction, ReturnValue = unknown> = (args: Parameters<Fn>) => ReturnValue;
|
|
3
|
+
export type DefaultCacheKeyFn<Fn extends AnyFunction> = CacheKeyFn<Fn, NonNullable<Parameters<Fn>[0]> | string>;
|
|
4
|
+
export type MemoizedFn<Fn extends AnyFunction, CkFn extends CacheKeyFn<Fn>> = Fn & {
|
|
5
|
+
cache: Map<ReturnType<CkFn>, ReturnType<Fn>>;
|
|
6
|
+
};
|
|
7
|
+
export declare function memo<Fn extends AnyFunction, CkFn extends CacheKeyFn<Fn>>(fn: Fn, getCacheKey: CkFn): MemoizedFn<Fn, CkFn>;
|
|
8
|
+
export declare function memo<Fn extends AnyFunction>(fn: Fn): MemoizedFn<Fn, DefaultCacheKeyFn<Fn>>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdeveric/utils",
|
|
3
3
|
"description": "Just another collection of utilities.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.75.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"url": "https://github.com/webdeveric/utils/issues"
|
|
67
67
|
},
|
|
68
68
|
"homepage": "https://github.com/webdeveric/utils/#readme",
|
|
69
|
-
"packageManager": "pnpm@10.
|
|
69
|
+
"packageManager": "pnpm@10.31.0+sha512.e3927388bfaa8078ceb79b748ffc1e8274e84d75163e67bc22e06c0d3aed43dd153151cbf11d7f8301ff4acb98c68bdc5cadf6989532801ffafe3b3e4a63c268",
|
|
70
70
|
"scripts": {
|
|
71
71
|
"clean": "rimraf ./dist/",
|
|
72
72
|
"prebuild": "pnpm clean",
|
|
@@ -87,7 +87,7 @@
|
|
|
87
87
|
"devDependencies": {
|
|
88
88
|
"@commitlint/config-conventional": "^20.4.3",
|
|
89
89
|
"@commitlint/types": "^20.4.3",
|
|
90
|
-
"@types/node": "^
|
|
90
|
+
"@types/node": "^24.12.0",
|
|
91
91
|
"@vitest/coverage-v8": "^4.0.18",
|
|
92
92
|
"@webdeveric/eslint-config-ts": "^0.12.0",
|
|
93
93
|
"@webdeveric/prettier-config": "^0.3.0",
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"lint-staged": "^16.3.2",
|
|
105
105
|
"prettier": "^3.8.1",
|
|
106
106
|
"rimraf": "^6.1.3",
|
|
107
|
-
"semantic-release": "^
|
|
107
|
+
"semantic-release": "^25.0.3",
|
|
108
108
|
"typescript": "6.0.0-dev.20260304",
|
|
109
109
|
"validate-package-exports": "^0.19.1",
|
|
110
110
|
"vitest": "^4.0.18"
|