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

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;
Loading