Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added count function #72

Merged
merged 3 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 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,5 @@
---
desc: Calls a function and returns result and the times the funciton is called.
---

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`.
ebe25 marked this conversation as resolved.
Show resolved Hide resolved
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)
})


})
30 changes: 30 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,30 @@
/***
* Invokes the function passed with args and counts how many times the function is excueted.
*
* @param {fn:Function} - The function to be called.
* @returns - A new function that excutes the given function and returns the result.
* @returns {getCount:Function} - A method that returns the count of exceution of the passed function.
***/


ebe25 marked this conversation as resolved.
Show resolved Hide resolved



const count = <T>(fn: (...args: any[]) => T) => {
let callCount = 0;

const wrapper= (...args: any[]): T => {
callCount++;
console.log(`Original function called ${callCount} times`);
ebe25 marked this conversation as resolved.
Show resolved Hide resolved
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;