Skip to content

Commit

Permalink
feat(maths): module to allows users to perform mathematical operations
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-sharma7 committed Nov 14, 2024
1 parent 384d020 commit 126da2e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [Water Intake Reminder](./guide/waterIntakeReminder.md)
- [Sleep Time Advisor](./guide/sleepTimeAdvisor.md)
- [Energy Tracker](./guide/energyTracker.md)
- [Maths Operations](./guide/maths.md)

</details>

Expand Down
27 changes: 27 additions & 0 deletions docs/guide/maths.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Maths

## Description

This feature allows you to perform mathematical operations such as addition, subtraction, multiplication, and division. And we will work on more operations in the future.

## Usage

```javascript
import { sum, subtract, multiplication , divide } 'world-clockify';
const sum = sum(1, 2, 3, 4);
const difference = subtract(10, 5);
const product = multiplication (2, 3, 4);
const quotient = divide(20, 2, 5);
```

## Expected Output

```bash
10
5
24
```

## Arguments

Every function can take multiple arguments.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './date/date.js';
export * from './productivity/productivity.js';
export * from './maths/basic.js';
41 changes: 41 additions & 0 deletions src/maths/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Note: All function will accept unlimited number of arguments.
* Return sum of numbers
*/

export const sum = (...args: any[]): number => {
if (args.length === 0) return 0;
return args.reduce((acc, num) => acc + num, 0);
};

/**
* Return substraction
*/

export const subtract = (...args: number[]): number => {
if (args.length === 0) return 0;
const [first = 0, ...rest] = args; // Default to 0 if args is empty

return rest.reduce((acc, num) => acc - num, first);
};

/**
* Return multiplication
*/

export const multiplication = (...args: number[]): number => {
if (args.length === 0) return 0;
if (args.length === 0) return 1;
return args.reduce((acc, num) => acc * num, 1);
};

/**
* Return division
*/

export const divide = (...args: number[]): number => {
const [first = 1, ...rest] = args; // Start with the first element as initial value
return rest.reduce((acc, num) => (num !== 0 ? acc / num : acc), first);
};

// TODO : Add more math operations
28 changes: 28 additions & 0 deletions tests/math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, it, expect } from 'vitest';
import { sum, subtract, multiplication, divide } from '../src/maths/basic.js';

describe('Math module helper', () => {
it('should pass sum', () => {
let a = 10;
let b = 20;
let c = 30;
expect(sum(a, b, c));
});
it('should pass substract', () => {
let a = 10;
let b = 20;
let c = 30;
expect(subtract(a, b, c));
});
it('should pass multiplication', () => {
let a = 10;
let b = 20;
let c = 30;
expect(multiplication(a, b, c));
});
it('should pass divison', () => {
let a = 10;
let b = 20;
expect(divide(a, b));
});
});

0 comments on commit 126da2e

Please sign in to comment.