npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

genfunc

v0.0.0

Published

A library for creating, caching, delaying, and wrapping functions with ease in JavaScript and TypeScript.

Downloads

8

Readme

genfunc

This library provides a collection of high-performance, framework-agnostic functions designed to simplify common programming tasks. Whether you need to create functions from string templates, delay function execution, cache results, or wrap functions with additional behavior, this library offers a flexible and easy-to-use solution.

Table of Contents:

genFn

Description:

Creates a function from a string template. The function is dynamically generated from the provided template and bound to an optional context object.

Parameters:

  • template: string
    The function template as a string. This string should be a valid JavaScript expression or block of code.

  • context: Record<string, any> (optional)
    An optional context object to bind to the created function.

Returns:

  • Function
    The created function.

Basic Usage:

const sumFn = genFn('a + b', { a: 2, b: 3 });
console.log(sumFn()); // Output: 5

Using Context:

const multiplier = genFn('a * b', { a: 4, b: 5 });
console.log(multiplier()); // Output: 20

wrapFn

Description:

Creates a function that wraps the provided function with additional behavior. The wrapper function can modify the arguments, this context, or the return value of the original function.

Parameters:

  • func: Function
    The function to wrap.

  • wrapper: Function
    The wrapper function that takes the original function as its first argument and allows additional behavior. It has the signature:

    (next: (...args: Parameters<T>) => ReturnType<T>, thisArg: unknown, ...args: Parameters<T>) => ReturnType<T>

Returns:

  • Function
    The wrapped function.

Wrapping a Function:

const originalFn = (a, b) => a + b;
const wrappedFn = wrapFn(originalFn, (next, thisArg, a, b) => {
    return next(a * 2, b * 2);
});

console.log(wrappedFn(1, 2)); // Output: 6 (1*2 + 2*2)

Accessing this Context:

const obj = {
    factor: 2,
    multiply(a, b) {
        return a * b * this.factor;
    }
};

const wrappedMultiply = wrapFn(obj.multiply, (next, thisArg, a, b) => {
    return next(a + 1, b + 1);
});

console.log(wrappedMultiply.call(obj, 2, 3)); // Output: 24 ((2+1) * (3+1) * 2)

cacheFn

Description:

Creates a function that caches the results of the original function based on the arguments provided. This is useful for memoizing expensive computations.

Parameters:

  • func: Function
    The function to cache.

Returns:

  • Function
    The cached function.

Basic Caching:

const expensiveFn = (x) => x * 2;
const cachedFn = cacheFn(expensiveFn);

console.log(cachedFn(2)); // Output: 4
console.log(cachedFn(2)); // Output: 4 (cached result)

Caching with Different Arguments:

const expensiveFn = (x) => x * 2;
const cachedFn = cacheFn(expensiveFn);

console.log(cachedFn(2)); // Output: 4
console.log(cachedFn(3)); // Output: 6

delayFn

Description:

Creates a function that delays the execution of the provided function by a specified number of milliseconds.

Parameters:

  • func: Function
    The function to delay.

  • delay: number
    The delay in milliseconds.

Returns:

  • Function
    The delayed function.

Basic Delay:

const mockFn = jest.fn();
const delayedFn = delayFn(mockFn, 1000);

delayedFn('test');
expect(mockFn).not.toHaveBeenCalled();

jest.advanceTimersByTime(1000);
expect(mockFn).toHaveBeenCalledWith('test');

constantFn

Description:

Creates a function that returns a fixed value.

Parameters:

  • value: T
    The fixed value to return.

Returns:

  • Function
    The function that returns the fixed value.

Returning a Fixed Value:

const constantFive = constantFn(5);
console.log(constantFive()); // Output: 5

const constantString = constantFn('hello');
console.log(constantString()); // Output: 'hello'

Usage Examples

Here's a summary of how to use all the provided functions:

// genFn
const sumFn = genFn('a + b', { a: 5, b: 10 });
console.log(sumFn()); // Output: 15

// wrapFn
const addFn = (a, b) => a + b;
const wrappedAdd = wrapFn(addFn, (next, _thisArg, a, b) => next(a * 2, b * 2));
console.log(wrappedAdd(1, 2)); // Output: 6

// cacheFn
const computeFn = (x) => x * 3;
const cachedCompute = cacheFn(computeFn);
console.log(cachedCompute(4)); // Output: 12
console.log(cachedCompute(4)); // Output: 12 (cached)

// delayFn
const delayedFn = delayFn(() => console.log('Delayed'), 500);
delayedFn(); // Will log 'Delayed' after 500ms

// constantFn
const getNumber = constantFn(42);
console.log(getNumber()); // Output: 42

Summary Table

| Function | Description | Parameters | Returns | Example Usage | |---------------|-----------------------------------------------------|----------------------------------------------------------------------------|-----------------|--------------------------------------| | genFn | Creates a function from a string template. | template: string, context?: Record<string, any> | Function | genFn('a + b', { a: 5, b: 10 }) | | wrapFn | Wraps a function with additional behavior. | func: Function, wrapper: Function | Function | wrapFn(fn, wrapper) | | cacheFn | Caches results of a function based on arguments. | func: Function | Function | cacheFn(fn) | | delayFn | Delays the execution of a function. | func: Function, delay: number | Function | delayFn(fn, 1000) | | constantFn | Creates a function that returns a fixed value. | value: T | Function | constantFn(42) |

Contribution

Contributions are welcome! If you have suggestions or improvements, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Write tests to cover your changes.
  4. Submit a pull request with a description of your changes.

Please ensure that your code follows the existing coding style and that all tests pass before submitting a pull request. We appreciate your contributions!

License

This is licensed under the MIT License © Shafin (@besaoct).