Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Implement minMax util
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Foo committed Aug 27, 2022
1 parent d7a5990 commit 9399c5f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-emus-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@busyxiang/utilities-js': patch
---

Implement minMax util
10 changes: 9 additions & 1 deletion src/number.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { average } from './number';
import { average, minMax } from './number';

describe('average', () => {
test('it should return average value', () => {
Expand All @@ -13,3 +13,11 @@ describe('average', () => {
expect(aver).toBe(2);
});
});

describe('minMax', () => {
test('it should return correct min max value', () => {
const value = minMax(10, 0, 5);

expect(value).toBe(5);
});
});
8 changes: 6 additions & 2 deletions src/number.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const average = (...nums: number[]) => {
return nums.reduce((acc, value) => acc + value, 0) / nums.length;
export const average = (...values: number[]) => {
return values.reduce((acc, value) => acc + value, 0) / values.length;
};

export const minMax = (value: number, min: number, max: number) => {
return Math.min(Math.max(value, min), max);
};

0 comments on commit 9399c5f

Please sign in to comment.