Skip to content

Commit

Permalink
Merge pull request #74 from jrTilak/satnize/count
Browse files Browse the repository at this point in the history
chore: sanitize count
  • Loading branch information
Tilak Thapa authored May 12, 2024
2 parents 8197581 + c8630c2 commit 00e47d8
Show file tree
Hide file tree
Showing 9 changed files with 4,244 additions and 3,424 deletions.
7,010 changes: 3,918 additions & 3,092 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions src/www/src/configs/nav-links.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@
"label": "Arrays",
"url": "/docs/functions/arrays",
"methods": [
{
"label": "chunk",
"url": "/docs/functions/arrays/chunk"
},
{
"label": "compact",
"url": "/docs/functions/arrays/compact"
},
{
"label": "insert",
"url": "/docs/functions/arrays/insert"
},
{
"label": "partition",
"url": "/docs/functions/arrays/partition"
Expand Down Expand Up @@ -74,6 +62,18 @@
{
"label": "zip",
"url": "/docs/functions/arrays/zip"
},
{
"label": "chunk",
"url": "/docs/functions/arrays/chunk"
},
{
"label": "compact",
"url": "/docs/functions/arrays/compact"
},
{
"label": "insert",
"url": "/docs/functions/arrays/insert"
}
]
},
Expand Down
24 changes: 12 additions & 12 deletions src/www/src/configs/prev-next-button-links.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@
"label": "Functions",
"url": "/docs/functions"
},
{
"label": "chunk",
"url": "/docs/functions/arrays/chunk"
},
{
"label": "compact",
"url": "/docs/functions/arrays/compact"
},
{
"label": "insert",
"url": "/docs/functions/arrays/insert"
},
{
"label": "partition",
"url": "/docs/functions/arrays/partition"
Expand Down Expand Up @@ -67,6 +55,18 @@
"label": "zip",
"url": "/docs/functions/arrays/zip"
},
{
"label": "chunk",
"url": "/docs/functions/arrays/chunk"
},
{
"label": "compact",
"url": "/docs/functions/arrays/compact"
},
{
"label": "insert",
"url": "/docs/functions/arrays/insert"
},
{
"label": "callAfter",
"url": "/docs/functions/functional/callAfter"
Expand Down
516 changes: 258 additions & 258 deletions src/www/src/configs/registry.json

Large diffs are not rendered by default.

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

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

const countAddFn = count(add);
countAddFn(1, 2);
countAddFn(3, 4);

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
// Expected Output: 2
2 changes: 1 addition & 1 deletion src/www/src/registry/functions/functional/count/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ desc: returns the result of a function and the number of times that function is

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.
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, analytics, resource management.
57 changes: 27 additions & 30 deletions src/www/src/registry/functions/functional/count/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import { expect, test, describe } from 'vitest'
import count from "."
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);
})
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 case 2: Testing the getCount function
test("returns 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)
})


})
//test the getCount method
expect(countedFunction.getCount()).toBe(6);
});
});
11 changes: 5 additions & 6 deletions src/www/src/registry/functions/functional/count/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
/**
* Invokes the function passed with arguments and
* 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.
* @returns {Function} getCount - A method that returns the count of execution of the passed function.
*/
const count = <T>(fn: (...args: any[]) => T) => {
const count = <A extends any[], R>(fn: (...args: A) => R) => {
let callCount = 0;

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

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

wrapper.getCount = getCount;

return wrapper;
Expand Down
5 changes: 2 additions & 3 deletions src/www/src/registry/functions/functional/count/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ 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.",
propDesc: "The function to count the number of times it is called.",
type: "Function",
}
},
];

export default Props;

0 comments on commit 00e47d8

Please sign in to comment.