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

@wixc3/common

Package Overview
Dependencies
Maintainers
67
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wixc3/common - npm Package Compare versions

Comparing version 15.1.1 to 16.0.0

9

dist/cjs/functions.d.ts

@@ -48,2 +48,11 @@ /**

export declare function enforceSequentialExecution<P, T extends (...args: any[]) => Promise<Awaited<P>>>(fn: T): T;
/**
*
* @param fn a function to memoize
* @param argsHash a function that returns a string hash for the arguments, defaults to JSON.stringify
* @returns a memoized version of `fn`
*/
export declare function memoize<T extends (...args: any[]) => any>(fn: T, argsHash?: (args: Parameters<T>) => string): T & {
__cache: Map<string, ReturnType<T>>;
};
//# sourceMappingURL=functions.d.ts.map

20

dist/cjs/functions.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.enforceSequentialExecution = exports.delayed = exports.once = exports.asyncNoop = exports.noop = void 0;
exports.memoize = exports.enforceSequentialExecution = exports.delayed = exports.once = exports.asyncNoop = exports.noop = void 0;
const promise_assist_1 = require("promise-assist");

@@ -89,2 +89,20 @@ /**

exports.enforceSequentialExecution = enforceSequentialExecution;
/**
*
* @param fn a function to memoize
* @param argsHash a function that returns a string hash for the arguments, defaults to JSON.stringify
* @returns a memoized version of `fn`
*/
function memoize(fn, argsHash = JSON.stringify) {
const __cache = new Map();
return Object.assign(((...args) => {
const key = argsHash(args);
if (!__cache.has(key)) {
__cache.set(key, fn(...args));
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return __cache.get(key);
}), { __cache });
}
exports.memoize = memoize;
//# sourceMappingURL=functions.js.map

@@ -48,2 +48,11 @@ /**

export declare function enforceSequentialExecution<P, T extends (...args: any[]) => Promise<Awaited<P>>>(fn: T): T;
/**
*
* @param fn a function to memoize
* @param argsHash a function that returns a string hash for the arguments, defaults to JSON.stringify
* @returns a memoized version of `fn`
*/
export declare function memoize<T extends (...args: any[]) => any>(fn: T, argsHash?: (args: Parameters<T>) => string): T & {
__cache: Map<string, ReturnType<T>>;
};
//# sourceMappingURL=functions.d.ts.map

@@ -81,2 +81,19 @@ import { sleep } from 'promise-assist';

}
/**
*
* @param fn a function to memoize
* @param argsHash a function that returns a string hash for the arguments, defaults to JSON.stringify
* @returns a memoized version of `fn`
*/
export function memoize(fn, argsHash = JSON.stringify) {
const __cache = new Map();
return Object.assign(((...args) => {
const key = argsHash(args);
if (!__cache.has(key)) {
__cache.set(key, fn(...args));
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return __cache.get(key);
}), { __cache });
}
//# sourceMappingURL=functions.js.map

2

package.json
{
"name": "@wixc3/common",
"version": "15.1.1",
"version": "16.0.0",
"description": "Common utils, usable in all environments",

@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js",

@@ -93,1 +93,25 @@ import { sleep } from 'promise-assist';

}
/**
*
* @param fn a function to memoize
* @param argsHash a function that returns a string hash for the arguments, defaults to JSON.stringify
* @returns a memoized version of `fn`
*/
export function memoize<T extends (...args: any[]) => any>(
fn: T,
argsHash: (args: Parameters<T>) => string = JSON.stringify
): T & { __cache: Map<string, ReturnType<T>> } {
const __cache = new Map<string, ReturnType<T>>();
return Object.assign(
((...args: Parameters<T>) => {
const key = argsHash(args);
if (!__cache.has(key)) {
__cache.set(key, fn(...args) as ReturnType<T>);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return __cache.get(key);
}) as T,
{ __cache }
);
}
import chai, { expect } from 'chai';
import { delayed, enforceSequentialExecution, once } from '..';
import { delayed, enforceSequentialExecution, memoize, once } from '..';
import Sinon, { stub } from 'sinon';

@@ -87,1 +87,40 @@ import sinonChai from 'sinon-chai';

});
describe('memoize', () => {
it('runs fn only once per given args', () => {
let callCount = 0;
const fn = (num: number) => {
callCount++;
return `${num}-${callCount}`;
};
const memoized = memoize(fn);
expect(memoized(1)).to.equal('1-1');
expect(memoized(1)).to.equal('1-1');
expect(memoized(2)).to.equal('2-2');
expect(memoized(2)).to.equal('2-2');
});
it('with a custom hash', () => {
let callCount = 0;
const fn = (num: number, ..._args: any[]) => {
callCount++;
return `${num}-${callCount}`;
};
const hashOnlyFirstArg = (args: any[]) => `${args[0]}`;
const memoized = memoize(fn, hashOnlyFirstArg);
expect(memoized(1, 1)).to.equal('1-1');
expect(memoized(1, 2)).to.equal('1-1');
expect(memoized(2, 1)).to.equal('2-2');
expect(memoized(2, 2)).to.equal('2-2');
});
it('uses __cache property', () => {
let callCount = 0;
const fn = (num: number) => {
callCount++;
return `${num}-${callCount}`;
};
const memoized = memoize(fn);
expect(memoized(1)).to.equal('1-1');
memoized.__cache.clear();
expect(memoized(1)).to.equal('1-2');
});
});

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