-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from ebe25/dev
feat: added count function
- Loading branch information
Showing
9 changed files
with
4,104 additions
and
4,623 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
src/www/src/registry/functions/functional/count/count.example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/www/src/registry/functions/functional/count/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |