func-cache
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -5,4 +5,11 @@ 'use strict'; | ||
function cache(func) { | ||
function cache(func, options) { | ||
if (options === void 0) { | ||
options = { | ||
lifeTime: 0 | ||
}; | ||
} | ||
var cached = {}; | ||
var timeOfCreation = Date.now(); | ||
return function () { | ||
@@ -14,2 +21,3 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
var str = args.join(','); | ||
if (options.lifeTime !== 0) Date.now() - timeOfCreation > options.lifeTime && (cached = {}); | ||
if (str in cached) return cached[str]; | ||
@@ -16,0 +24,0 @@ return cached[str] = func.apply(void 0, args); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(e){var r={};return function(){for(var t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];var u=n.join(",");return u in r?r[u]:r[u]=e.apply(void 0,n)}}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(e,r){void 0===r&&(r={lifeTime:0});var i={},t=Date.now();return function(){for(var n=arguments.length,o=new Array(n),a=0;a<n;a++)o[a]=arguments[a];var f=o.join(",");return 0!==r.lifeTime&&Date.now()-t>r.lifeTime&&(i={}),f in i?i[f]:i[f]=e.apply(void 0,o)}}; | ||
//# sourceMappingURL=func-cache.cjs.production.min.js.map |
@@ -1,3 +0,10 @@ | ||
function cache(func) { | ||
function cache(func, options) { | ||
if (options === void 0) { | ||
options = { | ||
lifeTime: 0 | ||
}; | ||
} | ||
var cached = {}; | ||
var timeOfCreation = Date.now(); | ||
return function () { | ||
@@ -9,2 +16,3 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
var str = args.join(','); | ||
if (options.lifeTime !== 0) Date.now() - timeOfCreation > options.lifeTime && (cached = {}); | ||
if (str in cached) return cached[str]; | ||
@@ -11,0 +19,0 @@ return cached[str] = func.apply(void 0, args); |
@@ -1,1 +0,4 @@ | ||
export default function cache(func: Function): typeof func | any; | ||
export interface FCOptions { | ||
lifeTime: number; | ||
} | ||
export default function cache(func: Function, options?: FCOptions): typeof func | any; |
{ | ||
"name": "func-cache", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"private": false, | ||
@@ -42,2 +42,3 @@ "description": "TypeScript library for caching static functions", | ||
"@typescript-eslint/parser": "4.23.0", | ||
"aforwait": "^1.2.0", | ||
"eslint-plugin-import": "2.22.1", | ||
@@ -49,5 +50,4 @@ "eslint-plugin-jest": "24.3.6", | ||
"tsdx": "0.14.1", | ||
"tslib": "^2.2.0", | ||
"typescript": "4.0.7", | ||
"wait-sync": "^1.0.1" | ||
"tslib": "^2.3.1", | ||
"typescript": "4.0.7" | ||
}, | ||
@@ -114,2 +114,2 @@ "peerDependencies": {}, | ||
} | ||
} | ||
} |
@@ -1,16 +0,15 @@ | ||
//@ts-ignore | ||
import waitSync from "wait-sync"; | ||
import { aforSec } from "aforwait" | ||
import cache from "./"; | ||
const cachedWait: typeof wait = cache(wait); | ||
function wait() { | ||
waitSync(10) | ||
return "10 seconds" | ||
async function wait() { | ||
await aforSec(1); | ||
return "1 seconds" | ||
} | ||
describe("testing cache", () => { | ||
it("wait 10", () => { | ||
it("wait 1", async () => { | ||
const t0 = performance.now(); | ||
const val1 = cachedWait(); | ||
const val1 = await cachedWait(); | ||
@@ -22,3 +21,3 @@ const t1 = performance.now(); | ||
const val2 = cachedWait(); | ||
const val2 = await cachedWait(); | ||
@@ -25,0 +24,0 @@ const t2 = performance.now(); |
@@ -1,7 +0,21 @@ | ||
export default function cache(func: Function): typeof func | any { | ||
const cached: any = {}; | ||
export interface FCOptions { | ||
lifeTime: number; | ||
} | ||
export default function cache(func: Function, options: FCOptions = { lifeTime: 0 }): typeof func | any { | ||
let cached: any = {}; | ||
const timeOfCreation = Date.now(); | ||
return (...args: any) => { | ||
const str = args.join(','); | ||
if (options.lifeTime !== 0) | ||
Date.now() - timeOfCreation > options.lifeTime && (cached = {}); | ||
if (str in cached) return cached[str]; | ||
@@ -8,0 +22,0 @@ return cached[str] = func(...args); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
10986
91