@thi.ng/memoize
Advanced tools
Comparing version 3.1.45 to 3.1.46
# Change Log | ||
- **Last updated**: 2023-12-09T19:12:03Z | ||
- **Last updated**: 2023-12-11T10:07:09Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -5,0 +5,0 @@ |
const cache = {}; | ||
/** | ||
* Lightweight named singleton factory, intended for hot-module | ||
* replacement situations. Takes a (preferably globally unique) `id` and | ||
* `factory` function. If there's no value defined for `id` yet, calls | ||
* `factory` to produce the singleton value and caches it. Returns | ||
* singleton value. | ||
* | ||
* Note: All created values will remain in the private cache until the | ||
* JS process terminates or this module itself has been reloaded (though | ||
* the latter shouldn't happen in an HMR workflow). | ||
* | ||
* @param id - | ||
* @param factory - | ||
*/ | ||
export const defonce = (id, factory) => cache.hasOwnProperty(id) ? cache[id] : (cache[id] = factory()); | ||
const defonce = (id, factory) => cache.hasOwnProperty(id) ? cache[id] : cache[id] = factory(); | ||
export { | ||
defonce | ||
}; |
@@ -1,16 +0,12 @@ | ||
/** | ||
* Similar to {@link memoize1}, however optimized for side effects only, i.e. | ||
* functions which DO NOT return any result. | ||
* | ||
* @param fn - | ||
* @param cache - | ||
*/ | ||
export const doOnce = (fn, cache) => { | ||
!cache && (cache = new Map()); | ||
return (x) => { | ||
if (!cache.has(x)) { | ||
cache.set(x, true); | ||
fn(x); | ||
} | ||
}; | ||
const doOnce = (fn, cache) => { | ||
!cache && (cache = /* @__PURE__ */ new Map()); | ||
return (x) => { | ||
if (!cache.has(x)) { | ||
cache.set(x, true); | ||
fn(x); | ||
} | ||
}; | ||
}; | ||
export { | ||
doOnce | ||
}; |
@@ -1,8 +0,9 @@ | ||
export function memoize(fn, cache) { | ||
return (...args) => { | ||
let res; | ||
return cache.has(args) | ||
? cache.get(args) | ||
: (cache.set(args, (res = fn.apply(null, args))), res); | ||
}; | ||
function memoize(fn, cache) { | ||
return (...args) => { | ||
let res; | ||
return cache.has(args) ? cache.get(args) : (cache.set(args, res = fn.apply(null, args)), res); | ||
}; | ||
} | ||
export { | ||
memoize | ||
}; |
@@ -1,19 +0,10 @@ | ||
/** | ||
* Optimized memoization for single arg functions. If the function expects args | ||
* other than strings or numbers, you MUST provide a `Map` implementation which | ||
* supports value (rather than object) equality, e.g. one of those provided by | ||
* [`thi.ng/associative`](https://thi.ng/associative). Using a native `Map` type | ||
* here will lead to memory leaks! Alternatively, use {@link memoizeJ}. | ||
* | ||
* @param fn - | ||
* @param cache - | ||
*/ | ||
export const memoize1 = (fn, cache) => { | ||
!cache && (cache = new Map()); | ||
return (x) => { | ||
let res; | ||
return cache.has(x) | ||
? cache.get(x) | ||
: (cache.set(x, (res = fn(x))), res); | ||
}; | ||
const memoize1 = (fn, cache) => { | ||
!cache && (cache = /* @__PURE__ */ new Map()); | ||
return (x) => { | ||
let res; | ||
return cache.has(x) ? cache.get(x) : (cache.set(x, res = fn(x)), res); | ||
}; | ||
}; | ||
export { | ||
memoize1 | ||
}; |
@@ -1,12 +0,13 @@ | ||
export function memoizeJ(fn, cache) { | ||
!cache && (cache = {}); | ||
return (...args) => { | ||
const key = JSON.stringify(args); | ||
if (key !== undefined) { | ||
return key in cache | ||
? cache[key] | ||
: (cache[key] = fn.apply(null, args)); | ||
} | ||
return fn.apply(null, args); | ||
}; | ||
function memoizeJ(fn, cache) { | ||
!cache && (cache = {}); | ||
return (...args) => { | ||
const key = JSON.stringify(args); | ||
if (key !== void 0) { | ||
return key in cache ? cache[key] : cache[key] = fn.apply(null, args); | ||
} | ||
return fn.apply(null, args); | ||
}; | ||
} | ||
export { | ||
memoizeJ | ||
}; |
{ | ||
"name": "@thi.ng/memoize", | ||
"version": "3.1.45", | ||
"version": "3.1.46", | ||
"description": "Function memoization with configurable caching", | ||
@@ -27,3 +27,5 @@ "type": "module", | ||
"scripts": { | ||
"build": "yarn clean && tsc --declaration", | ||
"build": "yarn build:esbuild && yarn build:decl", | ||
"build:decl": "tsc --declaration --emitDeclarationOnly", | ||
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts", | ||
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc", | ||
@@ -37,6 +39,7 @@ "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts", | ||
"dependencies": { | ||
"@thi.ng/api": "^8.9.11" | ||
"@thi.ng/api": "^8.9.12" | ||
}, | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.38.3", | ||
"esbuild": "^0.19.8", | ||
"rimraf": "^5.0.5", | ||
@@ -92,3 +95,3 @@ "tools": "^0.0.1", | ||
}, | ||
"gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n" | ||
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n" | ||
} |
@@ -71,3 +71,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (brotli'd, pre-treeshake): ESM: 248 bytes | ||
Package sizes (brotli'd, pre-treeshake): ESM: 247 bytes | ||
@@ -74,0 +74,0 @@ ## Dependencies |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30378
6
142
Updated@thi.ng/api@^8.9.12