From 126da2e1d461beebed3449a56b69dc8ca9c47dd7 Mon Sep 17 00:00:00 2001 From: shivam-sharma7 Date: Thu, 14 Nov 2024 06:52:36 +0530 Subject: [PATCH] feat(maths): module to allows users to perform mathematical operations --- docs/_sidebar.md | 1 + docs/guide/maths.md | 27 +++++++++++++++++++++++++++ src/index.ts | 1 + src/maths/basic.ts | 41 +++++++++++++++++++++++++++++++++++++++++ tests/math.test.ts | 28 ++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 docs/guide/maths.md create mode 100644 src/maths/basic.ts create mode 100644 tests/math.test.ts diff --git a/docs/_sidebar.md b/docs/_sidebar.md index a3a334e..c7b409a 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -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) diff --git a/docs/guide/maths.md b/docs/guide/maths.md new file mode 100644 index 0000000..7f59285 --- /dev/null +++ b/docs/guide/maths.md @@ -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. diff --git a/src/index.ts b/src/index.ts index c1448a0..75a5e79 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from './date/date.js'; export * from './productivity/productivity.js'; +export * from './maths/basic.js'; diff --git a/src/maths/basic.ts b/src/maths/basic.ts new file mode 100644 index 0000000..4698685 --- /dev/null +++ b/src/maths/basic.ts @@ -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 diff --git a/tests/math.test.ts b/tests/math.test.ts new file mode 100644 index 0000000..04bf864 --- /dev/null +++ b/tests/math.test.ts @@ -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)); + }); +});