Skip to content

Commit

Permalink
Merge pull request #72 from ebe25/dev
Browse files Browse the repository at this point in the history
feat: added count function
  • Loading branch information
Tilak Thapa authored May 12, 2024
2 parents bc41e14 + 53ba38c commit 8197581
Show file tree
Hide file tree
Showing 9 changed files with 4,104 additions and 4,623 deletions.
7,791 changes: 3,567 additions & 4,224 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions src/www/src/configs/nav-links.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@
"label": "Functional",
"url": "/docs/functions/functional",
"methods": [
{
"label": "callAfter",
"url": "/docs/functions/functional/callAfter"
},
{
"label": "callBefore",
"url": "/docs/functions/functional/callBefore"
},
{
"label": "count",
"url": "/docs/functions/functional/count"
},
{
"label": "nTimes",
"url": "/docs/functions/functional/nTimes"
Expand All @@ -100,14 +112,6 @@
{
"label": "timeout",
"url": "/docs/functions/functional/timeout"
},
{
"label": "callAfter",
"url": "/docs/functions/functional/callAfter"
},
{
"label": "callBefore",
"url": "/docs/functions/functional/callBefore"
}
]
},
Expand Down
20 changes: 12 additions & 8 deletions src/www/src/configs/prev-next-button-links.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
"label": "zip",
"url": "/docs/functions/arrays/zip"
},
{
"label": "callAfter",
"url": "/docs/functions/functional/callAfter"
},
{
"label": "callBefore",
"url": "/docs/functions/functional/callBefore"
},
{
"label": "count",
"url": "/docs/functions/functional/count"
},
{
"label": "nTimes",
"url": "/docs/functions/functional/nTimes"
Expand All @@ -87,14 +99,6 @@
"label": "timeout",
"url": "/docs/functions/functional/timeout"
},
{
"label": "callAfter",
"url": "/docs/functions/functional/callAfter"
},
{
"label": "callBefore",
"url": "/docs/functions/functional/callBefore"
},
{
"label": "and",
"url": "/docs/functions/gates/and"
Expand Down
803 changes: 420 additions & 383 deletions src/www/src/configs/registry.json

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/www/src/registry/functions/functional/count/count.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import count from "."

const add =(a:number, b:number)=>{
return a+b;
}

const countAddFn = count(add);
countAddFn(1,2);
// Expected Output: Original function called 1 times
countAddFn(3,4);
// Expected Output: Original function called 2 times
console.log(countAddFn.getCount());
// Expected Output: 2
7 changes: 7 additions & 0 deletions src/www/src/registry/functions/functional/count/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
desc: returns the result of a function and the number of times that function is invoked.
---

The `count` function invokes another function passed as an `arg` and returns both the `result` of the invoked function and the `count of how many times the function has been called`.

This is useful in when you need to keep a tab on how many times a function is being called which can be helpful in monitoring,debugging,testing,anayltics,resource management.
33 changes: 33 additions & 0 deletions src/www/src/registry/functions/functional/count/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect, test, describe } from 'vitest'
import count from "."

describe('count', () => {
//Test case 1: Testing the wrapper function
test('calls the function passed and returns the result', ()=>{
const mockFunction = (a: number, b: number): number => {
return a + b;
};
const countedFunction = count(mockFunction);
expect(countedFunction(1,2)).toBe(3);
})


// Test case 2: Testing the getCount function
test('resturns the count of function calls', ()=>{
const mockFunction = (a: number, b: number): number => {
return Math.abs(a - b);
};
const countedFunction = count(mockFunction);
countedFunction(4,6);
countedFunction(10,6);
countedFunction(4,5);
countedFunction(4,13);
countedFunction(155,6);
countedFunction(109,126);

//test the getCount method
expect(countedFunction.getCount()).toBe(6)
})


})
26 changes: 26 additions & 0 deletions src/www/src/registry/functions/functional/count/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Invokes the function passed with arguments and
* counts how many times the function is executed.
*
* @param {Function} fn - The function to be called.
* @returns - result: the result of the passed function invocation.
* This function also has a getCount method attached.
* @returns {Function} getCount - A method that returns the count of execution of the passed function.
*/
const count = <T>(fn: (...args: any[]) => T) => {
let callCount = 0;

const wrapper = (...args: any[]): T => {
callCount++;
const result = fn(...args);
return result;
};

const getCount: () => number = () => callCount;

wrapper.getCount = getCount;

return wrapper;
};

export default count;
14 changes: 14 additions & 0 deletions src/www/src/registry/functions/functional/count/props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IRegistryFunctionPropTable } from "@/types/registry.types";

const Props: IRegistryFunctionPropTable[] = [
{
title: "function",
required: true,
defaultValue: undefined,
propDesc:
"The function calculates count of exceution of func passed. The function receives a function as an argument.",
type: "Function",
}
];

export default Props;

0 comments on commit 8197581

Please sign in to comment.