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

micro-memoize

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micro-memoize - npm Package Compare versions

Comparing version 4.0.2 to 4.0.3

10

__tests__/Cache.ts
import { Cache } from '../src/Cache';
import { Memoized } from '../src/types';
import { MicroMemoize } from '../src/types';

@@ -387,3 +387,3 @@ import { isSameValueZero } from '../src/utils';

const key = ['foo'];
const memoized = ((() => {}) as unknown) as Memoized<Function>;
const memoized = ((() => {}) as unknown) as MicroMemoize.Memoized<Function>;

@@ -435,3 +435,3 @@ const value = fn();

const key = ['foo'];
const memoized = ((() => {}) as unknown) as Memoized<Function>;
const memoized = ((() => {}) as unknown) as MicroMemoize.Memoized<Function>;

@@ -507,3 +507,3 @@ const value = fn();

const memoized = ((() => {}) as unknown) as Memoized<Function>;
const memoized = ((() => {}) as unknown) as MicroMemoize.Memoized<Function>;

@@ -561,3 +561,3 @@ cache.updateAsyncCache(memoized);

const memoized = ((() => {}) as unknown) as Memoized<Function>;
const memoized = ((() => {}) as unknown) as MicroMemoize.Memoized<Function>;

@@ -564,0 +564,0 @@ cache.updateAsyncCache(memoized);

# micro-memoize CHANGELOG
## 4.0.3
- Namespace types under `MicroMemoize` namespace (which is how it was for `4.0.0`, but it got lost)
## 4.0.2
- Make Cache class consumable in types
## 4.0.1

@@ -4,0 +12,0 @@

@@ -44,3 +44,4 @@ 'use strict';

function isMemoized(fn) {
return typeof fn === 'function' && fn.isMemoized;
return (typeof fn === 'function' &&
fn.isMemoized);
}

@@ -328,4 +329,5 @@ /**

}
createMemoizedFunction._Cache = Cache;
module.exports = createMemoizedFunction;
//# sourceMappingURL=micro-memoize.cjs.js.map

@@ -42,3 +42,4 @@ /**

function isMemoized(fn) {
return typeof fn === 'function' && fn.isMemoized;
return (typeof fn === 'function' &&
fn.isMemoized);
}

@@ -326,4 +327,5 @@ /**

}
createMemoizedFunction._Cache = Cache;
export default createMemoizedFunction;
//# sourceMappingURL=micro-memoize.esm.js.map

@@ -48,3 +48,4 @@ (function (global, factory) {

function isMemoized(fn) {
return typeof fn === 'function' && fn.isMemoized;
return (typeof fn === 'function' &&
fn.isMemoized);
}

@@ -332,2 +333,3 @@ /**

}
createMemoizedFunction._Cache = Cache;

@@ -334,0 +336,0 @@ return createMemoizedFunction;

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self)["micro-memoize"]=t()}(this,function(){"use strict";var A={isEqual:!0,isMatchingKey:!0,isPromise:!0,maxSize:!0,onCacheAdd:!0,onCacheChange:!0,onCacheHit:!0,transformKey:!0};function I(e,t){return e===t||e!=e&&t!=t}function M(e,t){var n={};for(var i in e)n[i]=e[i];for(var i in t)n[i]=t[i];return n}var O=Function.prototype.bind.call(Function.prototype.call,Array.prototype.slice),b=(Object.defineProperty(e.prototype,"size",{get:function(){return this.keys.length},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"snapshot",{get:function(){return{keys:O(this.keys,0),size:this.size,values:O(this.values,0)}},enumerable:!0,configurable:!0}),e.prototype._getKeyIndexFromMatchingKey=function(e){var t=this.options,n=t.isMatchingKey,i=t.maxSize,o=this.keys;if(n(o[0],e))return 0;if(1<i)for(var s=o.length,r=1;r<s;r++)if(n(o[r],e))return r;return-1},e.prototype._getKeyIndexForMany=function(e){for(var t,n=this.options.isEqual,i=this.keys,o=i.length,s=e.length,r=0;r<o;r++)if((t=i[r]).length===s){for(var a=0;a<s&&n(t[a],e[a]);a++);if(a===s)return r}return-1},e.prototype._getKeyIndexForSingle=function(e){var t=this.keys[0],n=t.length;if(e.length!==n)return-1;for(var i=this.options.isEqual,o=0;o<n;o++)if(!i(t[o],e[o]))return-1;return 0},e.prototype.orderByLru=function(e,t,n){for(var i=n;i--;)this.keys[i+1]=this.keys[i],this.values[i+1]=this.values[i];this.keys[0]=e,this.values[0]=t;var o=this.options.maxSize;o<=n&&(this.keys.length=this.values.length=o)},e.prototype.updateAsyncCache=function(t){var n=this,e=this.options,i=e.onCacheChange,o=e.onCacheHit,s=this.keys[0],r=this.values[0];this.values[0]=r.then(function(e){return n.shouldUpdateOnHit&&o(n,n.options,t),n.shouldUpdateOnChange&&i(n,n.options,t),e}).catch(function(e){var t=n.getKeyIndex(s);throw-1!==t&&(n.keys.splice(t,1),n.values.splice(t,1)),e})},e);function e(e){this.keys=[],this.values=[];var t="function"==typeof(this.options=e).isMatchingKey;t?this.getKeyIndex=this._getKeyIndexFromMatchingKey:1<e.maxSize?this.getKeyIndex=this._getKeyIndexForMany:this.getKeyIndex=this._getKeyIndexForSingle,this.canTransformKey="function"==typeof e.transformKey,this.shouldCloneArguments=this.canTransformKey||t,this.shouldUpdateOnAdd="function"==typeof e.onCacheAdd,this.shouldUpdateOnChange="function"==typeof e.onCacheChange,this.shouldUpdateOnHit="function"==typeof e.onCacheHit}return function e(o,t){if(void 0===t&&(t={}),function(e){return"function"==typeof e&&e.isMemoized}(o))return e(o.fn,M(o.options,t));if("function"!=typeof o)throw new TypeError("You must pass a function to `memoize`.");function s(){var e=K?O(arguments,0):arguments;C&&(e=p(e));var t=v.length?g.getKeyIndex(e):-1;if(-1!==t)z&&d(g,l,s),t&&(g.orderByLru(v[t],m[t],t),k&&y(g,l,s));else{var n=o.apply(this,arguments),i=K?e:O(arguments,0);g.orderByLru(i,n,v.length),h&&g.updateAsyncCache(s),x&&f(g,l,s),k&&y(g,l,s)}return m[0]}var n=t.isEqual,i=void 0===n?I:n,r=t.isMatchingKey,a=t.isPromise,h=void 0!==a&&a,u=t.maxSize,c=void 0===u?1:u,f=t.onCacheAdd,y=t.onCacheChange,d=t.onCacheHit,p=t.transformKey,l=M({isEqual:i,isMatchingKey:r,isPromise:h,maxSize:c,onCacheAdd:f,onCacheChange:y,onCacheHit:d,transformKey:p},function(e){var t={};for(var n in e)A[n]||(t[n]=e[n]);return t}(t)),g=new b(l),v=g.keys,m=g.values,C=g.canTransformKey,K=g.shouldCloneArguments,x=g.shouldUpdateOnAdd,k=g.shouldUpdateOnChange,z=g.shouldUpdateOnHit;return s.cache=g,s.fn=o,s.isMemoized=!0,s.options=l,s}});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self)["micro-memoize"]=t()}(this,function(){"use strict";var z={isEqual:!0,isMatchingKey:!0,isPromise:!0,maxSize:!0,onCacheAdd:!0,onCacheChange:!0,onCacheHit:!0,transformKey:!0};function A(e,t){return e===t||e!=e&&t!=t}function I(e,t){var n={};for(var i in e)n[i]=e[i];for(var i in t)n[i]=t[i];return n}var M=Function.prototype.bind.call(Function.prototype.call,Array.prototype.slice),O=(Object.defineProperty(e.prototype,"size",{get:function(){return this.keys.length},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"snapshot",{get:function(){return{keys:M(this.keys,0),size:this.size,values:M(this.values,0)}},enumerable:!0,configurable:!0}),e.prototype._getKeyIndexFromMatchingKey=function(e){var t=this.options,n=t.isMatchingKey,i=t.maxSize,o=this.keys;if(n(o[0],e))return 0;if(1<i)for(var s=o.length,r=1;r<s;r++)if(n(o[r],e))return r;return-1},e.prototype._getKeyIndexForMany=function(e){for(var t,n=this.options.isEqual,i=this.keys,o=i.length,s=e.length,r=0;r<o;r++)if((t=i[r]).length===s){for(var a=0;a<s&&n(t[a],e[a]);a++);if(a===s)return r}return-1},e.prototype._getKeyIndexForSingle=function(e){var t=this.keys[0],n=t.length;if(e.length!==n)return-1;for(var i=this.options.isEqual,o=0;o<n;o++)if(!i(t[o],e[o]))return-1;return 0},e.prototype.orderByLru=function(e,t,n){for(var i=n;i--;)this.keys[i+1]=this.keys[i],this.values[i+1]=this.values[i];this.keys[0]=e,this.values[0]=t;var o=this.options.maxSize;o<=n&&(this.keys.length=this.values.length=o)},e.prototype.updateAsyncCache=function(t){var n=this,e=this.options,i=e.onCacheChange,o=e.onCacheHit,s=this.keys[0],r=this.values[0];this.values[0]=r.then(function(e){return n.shouldUpdateOnHit&&o(n,n.options,t),n.shouldUpdateOnChange&&i(n,n.options,t),e}).catch(function(e){var t=n.getKeyIndex(s);throw-1!==t&&(n.keys.splice(t,1),n.values.splice(t,1)),e})},e);function e(e){this.keys=[],this.values=[];var t="function"==typeof(this.options=e).isMatchingKey;t?this.getKeyIndex=this._getKeyIndexFromMatchingKey:1<e.maxSize?this.getKeyIndex=this._getKeyIndexForMany:this.getKeyIndex=this._getKeyIndexForSingle,this.canTransformKey="function"==typeof e.transformKey,this.shouldCloneArguments=this.canTransformKey||t,this.shouldUpdateOnAdd="function"==typeof e.onCacheAdd,this.shouldUpdateOnChange="function"==typeof e.onCacheChange,this.shouldUpdateOnHit="function"==typeof e.onCacheHit}function b(o,e){if(void 0===e&&(e={}),function(e){return"function"==typeof e&&e.isMemoized}(o))return b(o.fn,I(o.options,e));if("function"!=typeof o)throw new TypeError("You must pass a function to `memoize`.");function s(){var e=C?M(arguments,0):arguments;m&&(e=d(e));var t=g.length?l.getKeyIndex(e):-1;if(-1!==t)k&&y(l,p,s),t&&(l.orderByLru(g[t],v[t],t),x&&f(l,p,s));else{var n=o.apply(this,arguments),i=C?e:M(arguments,0);l.orderByLru(i,n,g.length),a&&l.updateAsyncCache(s),K&&c(l,p,s),x&&f(l,p,s)}return v[0]}var t=e.isEqual,n=void 0===t?A:t,i=e.isMatchingKey,r=e.isPromise,a=void 0!==r&&r,h=e.maxSize,u=void 0===h?1:h,c=e.onCacheAdd,f=e.onCacheChange,y=e.onCacheHit,d=e.transformKey,p=I({isEqual:n,isMatchingKey:i,isPromise:a,maxSize:u,onCacheAdd:c,onCacheChange:f,onCacheHit:y,transformKey:d},function(e){var t={};for(var n in e)z[n]||(t[n]=e[n]);return t}(e)),l=new O(p),g=l.keys,v=l.values,m=l.canTransformKey,C=l.shouldCloneArguments,K=l.shouldUpdateOnAdd,x=l.shouldUpdateOnChange,k=l.shouldUpdateOnHit;return s.cache=l,s.fn=o,s.isMemoized=!0,s.options=p,s}return b._Cache=O,b});

@@ -6,52 +6,49 @@ type Dictionary<Type> = {

export type Key = any[];
export type Value = any;
export declare namespace MicroMemoize {
export type Key = any[];
export type Value = any;
export type RawKey = Key | IArguments;
export type RawKey = Key | IArguments;
type CacheClass = import('./src/Cache').Cache;
export type Cache = import('./src/Cache').Cache;
export interface Cache extends CacheClass {
constructor: Cache;
new (options: Options): Cache;
}
export type EqualityComparator = (object1: any, object2: any) => boolean;
export type EqualityComparator = (object1: any, object2: any) => boolean;
export type MatchingKeyComparator = (key1: Key, key2: RawKey) => boolean;
export type MatchingKeyComparator = (key1: Key, key2: RawKey) => boolean;
export type CacheModifiedHandler = (
cache: Cache,
options: StandardOptions,
memoized: Function,
) => void;
export type CacheModifiedHandler = (
cache: Cache,
options: StandardOptions,
memoized: Function,
) => void;
export type KeyTransformer = (args: RawKey) => Key;
export type KeyTransformer = (args: RawKey) => Key;
export type KeyIndexGetter = (keyToMatch: RawKey) => number;
export type KeyIndexGetter = (keyToMatch: RawKey) => number;
export type StandardOptions = {
isEqual?: EqualityComparator;
isMatchingKey?: MatchingKeyComparator;
isPromise?: boolean;
maxSize?: number;
onCacheAdd?: CacheModifiedHandler;
onCacheChange?: CacheModifiedHandler;
onCacheHit?: CacheModifiedHandler;
transformKey?: KeyTransformer;
};
export type StandardOptions = {
isEqual?: EqualityComparator;
isMatchingKey?: MatchingKeyComparator;
isPromise?: boolean;
maxSize?: number;
onCacheAdd?: CacheModifiedHandler;
onCacheChange?: CacheModifiedHandler;
onCacheHit?: CacheModifiedHandler;
transformKey?: KeyTransformer;
};
export type Options = StandardOptions & Dictionary<any>;
export type Options = StandardOptions & Dictionary<any>;
export type Memoized<Fn extends Function> = Fn &
Dictionary<any> & {
cache: Cache;
fn: Fn;
isMemoized: true;
options: Options;
};
}
export type Memoized<Fn extends Function> = Fn &
Dictionary<any> & {
cache: Cache;
fn: Fn;
isMemoized: true;
options: Options;
};
export default function memoize<Fn extends Function>(
fn: Fn | Memoized<Fn>,
options?: Options,
): Memoized<Fn>;
fn: Fn | MicroMemoize.Memoized<Fn>,
options?: MicroMemoize.Options,
): MicroMemoize.Memoized<Fn>;

@@ -101,3 +101,3 @@ {

"types": "./index.d.ts",
"version": "4.0.2"
"version": "4.0.3"
}

@@ -10,2 +10,3 @@ # micro-memoize

- [Usage](#usage)
- [Types](#types)
- [Composition](#composition)

@@ -69,2 +70,10 @@ - [Options](#options)

### Types
If you need them, all types are available under the `MicroMemoize` namespace.
```ts
import { MicroMemoize } from "micro-memoize";
```
### Composition

@@ -390,4 +399,4 @@

{
keys: any[][], // available as Key[]
values: any[] // available as Value[]
keys: any[][], // available as MicroMemoize.Key[]
values: any[] // available as MicroMemoize.Value[]
}

@@ -394,0 +403,0 @@ ```

@@ -1,2 +0,2 @@

import { Key, KeyIndexGetter, Memoized, Options, Value } from './types';
import { MicroMemoize } from './types';

@@ -8,4 +8,4 @@ // utils

readonly canTransformKey: boolean;
readonly getKeyIndex: KeyIndexGetter;
readonly options: Options;
readonly getKeyIndex: MicroMemoize.KeyIndexGetter;
readonly options: MicroMemoize.Options;
readonly shouldCloneArguments: boolean;

@@ -16,6 +16,6 @@ readonly shouldUpdateOnAdd: boolean;

keys: Key[];
values: Value[];
keys: MicroMemoize.Key[];
values: MicroMemoize.Value[];
constructor(options: Options) {
constructor(options: MicroMemoize.Options) {
this.keys = [];

@@ -64,3 +64,3 @@ this.values = [];

*/
_getKeyIndexFromMatchingKey(keyToMatch: Key) {
_getKeyIndexFromMatchingKey(keyToMatch: MicroMemoize.Key) {
const { isMatchingKey, maxSize } = this.options;

@@ -95,3 +95,3 @@ const allKeys = this.keys;

*/
_getKeyIndexForMany(keyToMatch: Key) {
_getKeyIndexForMany(keyToMatch: MicroMemoize.Key) {
const { isEqual } = this.options;

@@ -135,3 +135,3 @@ const allKeys = this.keys;

*/
_getKeyIndexForSingle(keyToMatch: Key) {
_getKeyIndexForSingle(keyToMatch: MicroMemoize.Key) {
const existingKey = this.keys[0];

@@ -165,3 +165,7 @@ const keyLength = existingKey.length;

*/
orderByLru(key: Key, value: Value, startingIndex: number) {
orderByLru(
key: MicroMemoize.Key,
value: MicroMemoize.Value,
startingIndex: number,
) {
let index = startingIndex;

@@ -193,3 +197,3 @@

*/
updateAsyncCache(memoized: Memoized<Function>) {
updateAsyncCache(memoized: MicroMemoize.Memoized<Function>) {
const { onCacheChange, onCacheHit } = this.options;

@@ -196,0 +200,0 @@

@@ -5,3 +5,3 @@ // cache

// types
import { Memoized, StandardOptions } from './types';
import { MicroMemoize } from './types';

@@ -18,5 +18,5 @@ // utils

function createMemoizedFunction<Fn extends Function>(
fn: Fn | Memoized<Fn>,
options: StandardOptions = {},
): Memoized<Fn> {
fn: Fn | MicroMemoize.Memoized<Fn>,
options: MicroMemoize.StandardOptions = {},
): MicroMemoize.Memoized<Fn> {
if (isMemoized(fn)) {

@@ -39,3 +39,3 @@ return createMemoizedFunction(fn.fn, mergeOptions(fn.options, options));

transformKey,
}: StandardOptions = options;
}: MicroMemoize.StandardOptions = options;

@@ -120,2 +120,4 @@ const normalizedOptions = mergeOptions(

createMemoizedFunction._Cache = Cache;
export default createMemoizedFunction;

@@ -6,47 +6,44 @@ export type Dictionary<Type> = {

export type Key = any[];
export type Value = any;
export declare namespace MicroMemoize {
export type Key = any[];
export type Value = any;
export type RawKey = Key | IArguments;
export type RawKey = Key | IArguments;
type CacheClass = import('./Cache').Cache;
export type Cache = import('./Cache').Cache;
export interface Cache extends CacheClass {
constructor: Cache;
new (options: Options): Cache;
}
export type EqualityComparator = (object1: any, object2: any) => boolean;
export type EqualityComparator = (object1: any, object2: any) => boolean;
export type MatchingKeyComparator = (key1: Key, key2: RawKey) => boolean;
export type MatchingKeyComparator = (key1: Key, key2: RawKey) => boolean;
export type CacheModifiedHandler = (
cache: Cache,
options: StandardOptions,
memoized: Function,
) => void;
export type CacheModifiedHandler = (
cache: Cache,
options: StandardOptions,
memoized: Function,
) => void;
export type KeyTransformer = (args: RawKey) => Key;
export type KeyTransformer = (args: RawKey) => Key;
export type KeyIndexGetter = (keyToMatch: RawKey) => number;
export type KeyIndexGetter = (keyToMatch: RawKey) => number;
export type StandardOptions = {
isEqual?: EqualityComparator;
isMatchingKey?: MatchingKeyComparator;
isPromise?: boolean;
maxSize?: number;
onCacheAdd?: CacheModifiedHandler;
onCacheChange?: CacheModifiedHandler;
onCacheHit?: CacheModifiedHandler;
transformKey?: KeyTransformer;
};
export type StandardOptions = {
isEqual?: EqualityComparator;
isMatchingKey?: MatchingKeyComparator;
isPromise?: boolean;
maxSize?: number;
onCacheAdd?: CacheModifiedHandler;
onCacheChange?: CacheModifiedHandler;
onCacheHit?: CacheModifiedHandler;
transformKey?: KeyTransformer;
};
export type Options = StandardOptions & Dictionary<any>;
export type Options = StandardOptions & Dictionary<any>;
export type Memoized<Fn extends Function> = Fn &
Dictionary<any> & {
cache: Cache;
fn: Fn;
isMemoized: true;
options: Options;
};
export type Memoized<Fn extends Function> = Fn &
Dictionary<any> & {
cache: Cache;
fn: Fn;
isMemoized: true;
options: Options;
};
}

@@ -1,2 +0,2 @@

import { Dictionary, Memoized, Options, StandardOptions } from './types';
import { Dictionary, MicroMemoize } from './types';

@@ -26,3 +26,3 @@ /**

*/
export function getCustomOptions(options: Options) {
export function getCustomOptions(options: MicroMemoize.Options) {
const customOptions: Dictionary<any> = {};

@@ -48,4 +48,7 @@

*/
export function isMemoized(fn: any): fn is Memoized<Function> {
return typeof fn === 'function' && (fn as Memoized<Function>).isMemoized;
export function isMemoized(fn: any): fn is MicroMemoize.Memoized<Function> {
return (
typeof fn === 'function' &&
(fn as MicroMemoize.Memoized<Function>).isMemoized
);
}

@@ -78,6 +81,6 @@

export function mergeOptions(
existingOptions: Options,
newOptions: Options,
): Readonly<Options> {
const target: Options = {};
existingOptions: MicroMemoize.Options,
newOptions: MicroMemoize.Options,
): Readonly<MicroMemoize.Options> {
const target: MicroMemoize.Options = {};

@@ -84,0 +87,0 @@ for (const key in existingOptions) {

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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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