Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More โ†’
Socket
Sign inDemoInstall
Socket

kashe

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kashe - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

dist/es2015/cache-models/async-node-cache.d.ts

3

dist/es2015/index.d.ts
import { kashe, weakKashe, boxed, inboxed, fork } from "./weak";
export { kashe, weakKashe, boxed, inboxed, fork, };
import { configureCacheModel } from './cache';
export { kashe, weakKashe, boxed, inboxed, fork, configureCacheModel, };
import { kashe, weakKashe, boxed, inboxed, fork } from "./weak";
export { kashe, weakKashe, boxed, inboxed, fork, };
import { configureCacheModel } from './cache';
export { kashe, weakKashe, boxed, inboxed, fork, configureCacheModel, };

@@ -6,14 +6,10 @@ import { WeakStorage } from "./types";

* weak memoization helper.
* Uses the __first__ given argument to store result.
* Uses non-primitive arguments to store result. Thus NOT suitable for functions with "simple" argument. See {@link boxed} for such cases.
*
* `kache`'s API is equal to any other single-line memoization library except the requirement for
* the first argument to be an object.
*
* ๐Ÿ’ก hint: sometimes it worth to move some arguments, put pick the "best one" to be the first
*
* @param {Object} argument0 - first argument has to be {object}, {array} or {function}
* @param argument1 - any other, as well we any number or arguments
*
* `kashe`'s API is equal to any other single-line memoization library except the requirement for
* some arguments to be an object (or a function).
**
* @see https://github.com/theKashey/kashe#kashe
* @example
* ```ts
* // create a selector, which returns a new array using `array.filter` every time

@@ -23,2 +19,3 @@ * const badSelector = (array) => array.filter(somehow)

* const goodSelector = kashe(badSelector);
* ```
*/

@@ -28,9 +25,12 @@ export declare const kashe: <Arg extends object, T extends any[], Return>(func: (x: Arg, ...rest: T) => Return, cache?: WeakStorage) => (x: Arg, ...rest: T) => Return;

* a special version of {@link kashe} which does not strictly checks arg1+.
* Could be used to bypass equality check, however use with caution
* Requires first argument to be a non-primitive value.
* Could be used to bypass equality check, however use with โš ๏ธcaution and for a good reason๐Ÿง‘โ€๐Ÿญ
*
* @see https://github.com/theKashey/kashe#weakkashe
* @example
* ```ts
* const weakMap = weakKashe((data, iterator, ...deps) => data.map(iterator));
* const derived = weakMap(data, line => ({...line, somethingElse}), localVariable1);
* // ๐Ÿ‘† second argument is changing every time, but as long as it's __String representation__ is the same - result is unchanged.
* ```
*/

@@ -37,0 +37,0 @@ export declare const weakKashe: <Arg extends object, T extends any[], Return>(func: (x: Arg, ...rest: T) => Return, cache?: WeakStorage) => (x: Arg, ...rest: T) => Return;

@@ -0,32 +1,6 @@

import { __spreadArrays } from "tslib";
import functionDouble from "function-double";
import { createWeakStorage } from "./weakStorage";
var cacheStack = [];
var cacheOverride;
var pushCache = function (cache) {
cacheStack.push(cache);
cacheOverride = cache;
};
var popCache = function (cache) {
var popped = cacheStack.pop();
if (cache !== popped) {
console.error({
expected: cache,
given: popped,
stack: cacheStack,
});
throw new Error('kashe synchronization failed');
}
cacheOverride = cacheStack[cacheStack.length - 1];
};
var addKashePrefix = function (name) { return "kashe-" + name; };
var getCacheFor = function (fn, cacheCreator) {
if (!cacheOverride) {
return;
}
var cache = cacheOverride.get([fn]);
if (cache) {
return cache.value;
}
return cacheOverride.set([fn], cacheCreator());
};
import { getCacheFor, getCacheOverride, withCacheScope } from "./cache";
import { addKashePrefix } from "./helpers";
export function weakMemoizeCreator(cacheCreator, mapper) {

@@ -47,9 +21,10 @@ if (cacheCreator === void 0) { cacheCreator = createWeakStorage; }

var usedArgs = mapper ? args.map(mapper) : args;
var test = localCache.get(usedArgs);
var thisArgs = __spreadArrays([this], usedArgs);
var test = localCache.get(thisArgs);
if (test) {
return test.value;
}
return localCache.set(usedArgs,
return localCache.set(thisArgs,
// @ts-ignore
func.apply(void 0, args));
func.apply(this, args));
}, func, { name: addKashePrefix });

@@ -60,14 +35,10 @@ };

* weak memoization helper.
* Uses the __first__ given argument to store result.
* Uses non-primitive arguments to store result. Thus NOT suitable for functions with "simple" argument. See {@link boxed} for such cases.
*
* `kache`'s API is equal to any other single-line memoization library except the requirement for
* the first argument to be an object.
*
* ๐Ÿ’ก hint: sometimes it worth to move some arguments, put pick the "best one" to be the first
*
* @param {Object} argument0 - first argument has to be {object}, {array} or {function}
* @param argument1 - any other, as well we any number or arguments
*
* `kashe`'s API is equal to any other single-line memoization library except the requirement for
* some arguments to be an object (or a function).
**
* @see https://github.com/theKashey/kashe#kashe
* @example
* ```ts
* // create a selector, which returns a new array using `array.filter` every time

@@ -77,2 +48,3 @@ * const badSelector = (array) => array.filter(somehow)

* const goodSelector = kashe(badSelector);
* ```
*/

@@ -82,9 +54,12 @@ export var kashe = weakMemoizeCreator(createWeakStorage);

* a special version of {@link kashe} which does not strictly checks arg1+.
* Could be used to bypass equality check, however use with caution
* Requires first argument to be a non-primitive value.
* Could be used to bypass equality check, however use with โš ๏ธcaution and for a good reason๐Ÿง‘โ€๐Ÿญ
*
* @see https://github.com/theKashey/kashe#weakkashe
* @example
* ```ts
* const weakMap = weakKashe((data, iterator, ...deps) => data.map(iterator));
* const derived = weakMap(data, line => ({...line, somethingElse}), localVariable1);
* // ๐Ÿ‘† second argument is changing every time, but as long as it's __String representation__ is the same - result is unchanged.
* ```
*/

@@ -100,3 +75,3 @@ export var weakKashe = weakMemoizeCreator(createWeakStorage, function (arg, i) { return i > 0 ? String(arg) : arg; });

}
var localCache = cacheOverride || cache;
var localCache = getCacheOverride() || cache;
var cacheArg = [args[indexId]];

@@ -170,9 +145,3 @@ var test = localCache.get(cacheArg);

}
try {
pushCache(cache);
return fn.apply(void 0, rest);
}
finally {
popCache(cache);
}
return withCacheScope(cache, function () { return fn.apply(void 0, rest); });
};

@@ -207,10 +176,4 @@ });

var cacheOverride = ((!options || !options.singleton) ? getCacheFor(cache, genLocalCache) : null) || cache;
try {
pushCache(cacheOverride);
return fn.apply(void 0, rest);
}
finally {
popCache(cacheOverride);
}
return withCacheScope(cacheOverride, function () { return fn.apply(void 0, rest); });
};
}
import { Mappable, WeakStorage } from "./types";
export declare const breakdownArgs: (args: any[]) => any[];
/**
* Splits argiments into weak-mappable and _plain_ ones
*/
export declare const breakdownArgs: (args: any[]) => any[][];
declare type Test = {

@@ -4,0 +7,0 @@ storedValue?: any;

@@ -1,3 +0,5 @@

import { __spreadArrays } from "tslib";
import { isWeakable } from "./utils";
/**
* Splits argiments into weak-mappable and _plain_ ones
*/
export var breakdownArgs = function (args) {

@@ -14,3 +16,3 @@ var weaks = [];

}
return __spreadArrays(weaks, strongs);
return [weaks, strongs];
};

@@ -21,4 +23,4 @@ export var createWeakStorage = function (storage) {

get: function (args) {
var slices = breakdownArgs(args);
if (!slices.length) {
var _a = breakdownArgs(args), weaks = _a[0], strongs = _a[1];
if (!weaks.length) {
throw new Error("No weak-mappable (object, function, symbol) argument found.");

@@ -28,11 +30,16 @@ }

var test = { weak: storage };
for (var i = 0; i < slices.length; ++i) {
var storageKey = isWeakable(slices[i]) ? 'weak' : 'strong';
readFrom = test[storageKey];
test = readFrom.get(slices[i]);
for (var i = 0; i < weaks.length; ++i) {
readFrom = test.weak;
test = readFrom === null || readFrom === void 0 ? void 0 : readFrom.get(weaks[i]);
if (!test) {
// get: no forward
return undefined;
}
}
for (var i = 0; i < strongs.length; ++i) {
readFrom = test.strong;
test = readFrom === null || readFrom === void 0 ? void 0 : readFrom.get(strongs[i]);
if (!test) {
return undefined;
}
}
if (!readFrom) {

@@ -46,20 +53,31 @@ return undefined;

set: function (args, value) {
var _a;
var slices = breakdownArgs(args);
var _a = breakdownArgs(args), weaks = _a[0], strongs = _a[1];
var writeTo = storage;
var next = { weak: storage };
for (var i = 0; i < slices.length; ++i) {
var _b = isWeakable(slices[i]) ? ['weak', function () { return new WeakMap(); }] : ['strong', function () { return new Map(); }], storageKey = _b[0], factory = _b[1];
writeTo = next[storageKey];
for (var i = 0; i < weaks.length; ++i) {
writeTo = next.weak;
if (!writeTo) {
next[storageKey] = writeTo = factory();
next.weak = writeTo = new WeakMap();
}
next = writeTo.get(slices[i]);
if (!next || !next[storageKey]) {
next = (_a = {},
_a[storageKey] = factory(),
_a);
writeTo.set(slices[i], next);
next = writeTo.get(weaks[i]);
if (!next || !next.weak) {
next = {
weak: new WeakMap()
};
writeTo.set(weaks[i], next);
}
}
for (var i = 0; i < strongs.length; ++i) {
writeTo = next.strong;
if (!writeTo) {
next.strong = writeTo = new Map();
}
next = writeTo.get(strongs[i]);
if (!next || !next.strong) {
next = {
strong: new Map(),
};
writeTo.set(strongs[i], next);
}
}
next.storedValue = value;

@@ -66,0 +84,0 @@ return value;

import { kashe, weakKashe, boxed, inboxed, fork } from "./weak";
export { kashe, weakKashe, boxed, inboxed, fork, };
import { configureCacheModel } from './cache';
export { kashe, weakKashe, boxed, inboxed, fork, configureCacheModel, };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fork = exports.inboxed = exports.boxed = exports.weakKashe = exports.kashe = void 0;
exports.configureCacheModel = exports.fork = exports.inboxed = exports.boxed = exports.weakKashe = exports.kashe = void 0;
var weak_1 = require("./weak");

@@ -10,1 +10,3 @@ Object.defineProperty(exports, "kashe", { enumerable: true, get: function () { return weak_1.kashe; } });

Object.defineProperty(exports, "fork", { enumerable: true, get: function () { return weak_1.fork; } });
var cache_1 = require("./cache");
Object.defineProperty(exports, "configureCacheModel", { enumerable: true, get: function () { return cache_1.configureCacheModel; } });

@@ -6,14 +6,10 @@ import { WeakStorage } from "./types";

* weak memoization helper.
* Uses the __first__ given argument to store result.
* Uses non-primitive arguments to store result. Thus NOT suitable for functions with "simple" argument. See {@link boxed} for such cases.
*
* `kache`'s API is equal to any other single-line memoization library except the requirement for
* the first argument to be an object.
*
* ๐Ÿ’ก hint: sometimes it worth to move some arguments, put pick the "best one" to be the first
*
* @param {Object} argument0 - first argument has to be {object}, {array} or {function}
* @param argument1 - any other, as well we any number or arguments
*
* `kashe`'s API is equal to any other single-line memoization library except the requirement for
* some arguments to be an object (or a function).
**
* @see https://github.com/theKashey/kashe#kashe
* @example
* ```ts
* // create a selector, which returns a new array using `array.filter` every time

@@ -23,2 +19,3 @@ * const badSelector = (array) => array.filter(somehow)

* const goodSelector = kashe(badSelector);
* ```
*/

@@ -28,9 +25,12 @@ export declare const kashe: <Arg extends object, T extends any[], Return>(func: (x: Arg, ...rest: T) => Return, cache?: WeakStorage) => (x: Arg, ...rest: T) => Return;

* a special version of {@link kashe} which does not strictly checks arg1+.
* Could be used to bypass equality check, however use with caution
* Requires first argument to be a non-primitive value.
* Could be used to bypass equality check, however use with โš ๏ธcaution and for a good reason๐Ÿง‘โ€๐Ÿญ
*
* @see https://github.com/theKashey/kashe#weakkashe
* @example
* ```ts
* const weakMap = weakKashe((data, iterator, ...deps) => data.map(iterator));
* const derived = weakMap(data, line => ({...line, somethingElse}), localVariable1);
* // ๐Ÿ‘† second argument is changing every time, but as long as it's __String representation__ is the same - result is unchanged.
* ```
*/

@@ -37,0 +37,0 @@ export declare const weakKashe: <Arg extends object, T extends any[], Return>(func: (x: Arg, ...rest: T) => Return, cache?: WeakStorage) => (x: Arg, ...rest: T) => Return;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fork = exports.inboxed = exports.boxed = exports.swap = exports.weakKashe = exports.kashe = exports.weakMemoizeCreator = void 0;
var tslib_1 = require("tslib");
var function_double_1 = require("function-double");
var weakStorage_1 = require("./weakStorage");
var cacheStack = [];
var cacheOverride;
var pushCache = function (cache) {
cacheStack.push(cache);
cacheOverride = cache;
};
var popCache = function (cache) {
var popped = cacheStack.pop();
if (cache !== popped) {
console.error({
expected: cache,
given: popped,
stack: cacheStack,
});
throw new Error('kashe synchronization failed');
}
cacheOverride = cacheStack[cacheStack.length - 1];
};
var addKashePrefix = function (name) { return "kashe-" + name; };
var getCacheFor = function (fn, cacheCreator) {
if (!cacheOverride) {
return;
}
var cache = cacheOverride.get([fn]);
if (cache) {
return cache.value;
}
return cacheOverride.set([fn], cacheCreator());
};
var cache_1 = require("./cache");
var helpers_1 = require("./helpers");
function weakMemoizeCreator(cacheCreator, mapper) {

@@ -48,12 +22,13 @@ if (cacheCreator === void 0) { cacheCreator = weakStorage_1.createWeakStorage; }

}
var localCache = getCacheFor(_this_, cacheCreator) || cache;
var localCache = cache_1.getCacheFor(_this_, cacheCreator) || cache;
var usedArgs = mapper ? args.map(mapper) : args;
var test = localCache.get(usedArgs);
var thisArgs = tslib_1.__spreadArrays([this], usedArgs);
var test = localCache.get(thisArgs);
if (test) {
return test.value;
}
return localCache.set(usedArgs,
return localCache.set(thisArgs,
// @ts-ignore
func.apply(void 0, args));
}, func, { name: addKashePrefix });
func.apply(this, args));
}, func, { name: helpers_1.addKashePrefix });
};

@@ -64,14 +39,10 @@ }

* weak memoization helper.
* Uses the __first__ given argument to store result.
* Uses non-primitive arguments to store result. Thus NOT suitable for functions with "simple" argument. See {@link boxed} for such cases.
*
* `kache`'s API is equal to any other single-line memoization library except the requirement for
* the first argument to be an object.
*
* ๐Ÿ’ก hint: sometimes it worth to move some arguments, put pick the "best one" to be the first
*
* @param {Object} argument0 - first argument has to be {object}, {array} or {function}
* @param argument1 - any other, as well we any number or arguments
*
* `kashe`'s API is equal to any other single-line memoization library except the requirement for
* some arguments to be an object (or a function).
**
* @see https://github.com/theKashey/kashe#kashe
* @example
* ```ts
* // create a selector, which returns a new array using `array.filter` every time

@@ -81,2 +52,3 @@ * const badSelector = (array) => array.filter(somehow)

* const goodSelector = kashe(badSelector);
* ```
*/

@@ -86,9 +58,12 @@ exports.kashe = weakMemoizeCreator(weakStorage_1.createWeakStorage);

* a special version of {@link kashe} which does not strictly checks arg1+.
* Could be used to bypass equality check, however use with caution
* Requires first argument to be a non-primitive value.
* Could be used to bypass equality check, however use with โš ๏ธcaution and for a good reason๐Ÿง‘โ€๐Ÿญ
*
* @see https://github.com/theKashey/kashe#weakkashe
* @example
* ```ts
* const weakMap = weakKashe((data, iterator, ...deps) => data.map(iterator));
* const derived = weakMap(data, line => ({...line, somethingElse}), localVariable1);
* // ๐Ÿ‘† second argument is changing every time, but as long as it's __String representation__ is the same - result is unchanged.
* ```
*/

@@ -104,3 +79,3 @@ exports.weakKashe = weakMemoizeCreator(weakStorage_1.createWeakStorage, function (arg, i) { return i > 0 ? String(arg) : arg; });

}
var localCache = cacheOverride || cache;
var localCache = cache_1.getCacheOverride() || cache;
var cacheArg = [args[indexId]];

@@ -176,9 +151,3 @@ var test = localCache.get(cacheArg);

}
try {
pushCache(cache);
return fn.apply(void 0, rest);
}
finally {
popCache(cache);
}
return cache_1.withCacheScope(cache, function () { return fn.apply(void 0, rest); });
};

@@ -213,12 +182,6 @@ });

}
var cacheOverride = ((!options || !options.singleton) ? getCacheFor(cache, genLocalCache) : null) || cache;
try {
pushCache(cacheOverride);
return fn.apply(void 0, rest);
}
finally {
popCache(cacheOverride);
}
var cacheOverride = ((!options || !options.singleton) ? cache_1.getCacheFor(cache, genLocalCache) : null) || cache;
return cache_1.withCacheScope(cacheOverride, function () { return fn.apply(void 0, rest); });
};
}
exports.fork = fork;
import { Mappable, WeakStorage } from "./types";
export declare const breakdownArgs: (args: any[]) => any[];
/**
* Splits argiments into weak-mappable and _plain_ ones
*/
export declare const breakdownArgs: (args: any[]) => any[][];
declare type Test = {

@@ -4,0 +7,0 @@ storedValue?: any;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWeakStorage = exports.breakdownArgs = void 0;
var tslib_1 = require("tslib");
var utils_1 = require("./utils");
/**
* Splits argiments into weak-mappable and _plain_ ones
*/
exports.breakdownArgs = function (args) {

@@ -17,3 +19,3 @@ var weaks = [];

}
return tslib_1.__spreadArrays(weaks, strongs);
return [weaks, strongs];
};

@@ -24,4 +26,4 @@ exports.createWeakStorage = function (storage) {

get: function (args) {
var slices = exports.breakdownArgs(args);
if (!slices.length) {
var _a = exports.breakdownArgs(args), weaks = _a[0], strongs = _a[1];
if (!weaks.length) {
throw new Error("No weak-mappable (object, function, symbol) argument found.");

@@ -31,11 +33,16 @@ }

var test = { weak: storage };
for (var i = 0; i < slices.length; ++i) {
var storageKey = utils_1.isWeakable(slices[i]) ? 'weak' : 'strong';
readFrom = test[storageKey];
test = readFrom.get(slices[i]);
for (var i = 0; i < weaks.length; ++i) {
readFrom = test.weak;
test = readFrom === null || readFrom === void 0 ? void 0 : readFrom.get(weaks[i]);
if (!test) {
// get: no forward
return undefined;
}
}
for (var i = 0; i < strongs.length; ++i) {
readFrom = test.strong;
test = readFrom === null || readFrom === void 0 ? void 0 : readFrom.get(strongs[i]);
if (!test) {
return undefined;
}
}
if (!readFrom) {

@@ -49,20 +56,31 @@ return undefined;

set: function (args, value) {
var _a;
var slices = exports.breakdownArgs(args);
var _a = exports.breakdownArgs(args), weaks = _a[0], strongs = _a[1];
var writeTo = storage;
var next = { weak: storage };
for (var i = 0; i < slices.length; ++i) {
var _b = utils_1.isWeakable(slices[i]) ? ['weak', function () { return new WeakMap(); }] : ['strong', function () { return new Map(); }], storageKey = _b[0], factory = _b[1];
writeTo = next[storageKey];
for (var i = 0; i < weaks.length; ++i) {
writeTo = next.weak;
if (!writeTo) {
next[storageKey] = writeTo = factory();
next.weak = writeTo = new WeakMap();
}
next = writeTo.get(slices[i]);
if (!next || !next[storageKey]) {
next = (_a = {},
_a[storageKey] = factory(),
_a);
writeTo.set(slices[i], next);
next = writeTo.get(weaks[i]);
if (!next || !next.weak) {
next = {
weak: new WeakMap()
};
writeTo.set(weaks[i], next);
}
}
for (var i = 0; i < strongs.length; ++i) {
writeTo = next.strong;
if (!writeTo) {
next.strong = writeTo = new Map();
}
next = writeTo.get(strongs[i]);
if (!next || !next.strong) {
next = {
strong: new Map(),
};
writeTo.set(strongs[i], next);
}
}
next.storedValue = value;

@@ -69,0 +87,0 @@ return value;

{
"name": "kashe",
"version": "1.2.0",
"version": "2.0.0",
"description": "Stateless weak memoization replacement for reselect and memoize-one",
"main": "dist/es5/index.js",
"sideEffects": false,
"sideEffects": [
"**/cache.js"
],
"scripts": {

@@ -8,0 +10,0 @@ "test": "ts-react-toolbox test",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc