jrTilak / lazykit

Refine your JavaScript workflows with Lazykit. A concentrated collection of lean utility functions, not a bloated library.
https://lazykit.thapatilak.com.np
MIT License
6 stars 2 forks source link

count: count the number of times a function is called #63

Closed jrTilak closed 3 months ago

ebe25 commented 4 months ago

hey @jrTilak could you elaborate on this issue a little bit, would like to contribute .

jrTilak commented 4 months ago

Certainly! The issue is about creating a higher-order function, named count, which accepts another function as its argument. This function is typically referred to as func.

Inside the count function, a variable is initialized to keep track of the number of times the wrapped function (func) is called.

The count function then returns a new function, often named wrapper, which encapsulates the original function (func).

When the wrapper function is called, it increments the count variable, indicating that the original function (func) has been invoked. It then proceeds to execute the original function (func) with any arguments passed to the wrapper function.

Optionally, the wrapper function can also return the result of the original function (func).

Additionally, the count function should provide a way to access the count of function calls from outside the wrapper function. This can be achieved by adding a property or method to the wrapper function, allowing external code to query the number of function calls.

This is basic implementation you can refer but you can implement the function on yoru own way as you want

function count(func) {
  let count = 0; // Variable to track the count

  // Inner function that will be returned
  function wrapper(...args) {
    count++; // Increment count each time the wrapped function is called
    console.log("Function called ", count, " times."); // Output the count
    const result = func(...args); // Call the original function with any arguments passed
    console.log("Return value:", result); // Output the return value of the function
    return result; // Return the result of the original function
  }

  // Add a property to the wrapper function to access the count
  wrapper.getCount = () => count;

  // Return the wrapper function
  return wrapper;
}

// Example usage:
function add(a, b) {
  return a + b;
}

const countedAdd = count(add);

console.log(countedAdd(1, 2)); // Output: Function called 1 times. Return value: 3
console.log(countedAdd(3, 4)); // Output: Function called 2 times. Return value: 7

console.log("Number of times function called:", countedAdd.getCount()); // Output: Number of times function called: 2

Main requirement

  1. We should be able to call the function passed to count function
  2. We should be able to access the variable that how many time the original function is called.
ebe25 commented 4 months ago

cool i get the idea, going through the code base and the contributing doc, will raise a pr as latest. :)

jrTilak commented 4 months ago

cool i get the idea, going through the code base and the contributing doc, will raise a pr as latest. :)

Thanks for your interest in contribution.

ebe25 commented 4 months ago

hey tilak, i have written the registry method . One thing why does the www app keep getting a hydration error?

jrTilak commented 4 months ago

hey tilak, i have written the registry method . One thing why does the www app keep getting a hydration error?

I am not sure about that but you can skip that part. It always gives hydration error whenever i implement dark-light mode in next.js

That error won't matter, you can skip that.

ebe25 commented 4 months ago

hey tilak, : No such filemsg: 2: .: cannot open .husky/_/husky.sh husky - commit-msg script failed (code 2) getting this error while committing I am on. What configuration you have, I am currently on a wsl. any tips?

jrTilak commented 4 months ago

This error looks new to me.

Try running

pnpm run prepare
ebe25 commented 4 months ago

hey man, cli cmd doesnt seems to be working after the first util method being installed. Could take a look ? i mean should raise this issue with step to reproduce seems like a bug.

jrTilak commented 3 months ago

Closed as per #72