Skip to content

Commit 1775b56

Browse files
authored
Merge pull request #26 from klmhyeonwoo/feature/utils-workspace-yeom
feat: add multiply util function
2 parents 1a6a0f9 + 4d641ff commit 1775b56

5 files changed

Lines changed: 155 additions & 61 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const frozen = objectUtil.deepFreeze({ a: { b: 1 } });
4343
// Number utilities
4444
const total = numberUtil.sum(1, 2, 3, 4, 5); // 15
4545
const difference = numberUtil.subtract(10, 3); // 7
46+
const product = numberUtil.multiply(2, 3, 4); // 24
4647

4748
// Validation utilities
4849
const isValid = validationUtil.checkEmail("user@example.com"); // true
@@ -77,6 +78,7 @@ const theme = cookieUtil.getCookie("theme");
7778

7879
- `sum(...numbers: number[]): number` - Calculates sum of numbers
7980
- `subtract(...numbers: number[]): number` - Calculates subtraction of numbers
81+
- `multiply(...numbers: number[]): number` - Calculates multiplication of numbers
8082

8183
### ValidationUtil
8284

package/numberUtil/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as sum } from "./sum";
22
export { default as subtract } from "./subtract";
3+
export { default as multiply } from "./multiply";
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, test, expect } from "vitest";
2+
import multiply from ".";
3+
4+
describe("multiply", () => {
5+
describe("기본 케이스", () => {
6+
test("두 양수를 올바르게 곱한다", () => {
7+
expect(multiply(2, 3)).toBe(6);
8+
});
9+
10+
test("여러 숫자를 올바르게 곱한다", () => {
11+
expect(multiply(2, 3, 4)).toBe(24);
12+
});
13+
14+
test("인수 중 0이 포함되면 0을 반환한다", () => {
15+
expect(multiply(5, 10, 0, 20)).toBe(0);
16+
});
17+
18+
test("음수를 올바르게 처리한다", () => {
19+
expect(multiply(-5, 10)).toBe(-50);
20+
expect(multiply(-5, -10)).toBe(50);
21+
});
22+
23+
test("부동소수점을 올바르게 처리한다", () => {
24+
expect(multiply(0.5, 10)).toBe(5);
25+
});
26+
});
27+
28+
describe("엣지 케이스", () => {
29+
test("인수가 없을 때 1을 반환한다", () => {
30+
expect(multiply()).toBe(1);
31+
});
32+
33+
test("인수가 하나일 때 해당 숫자 자신을 반환한다", () => {
34+
expect(multiply(7)).toBe(7);
35+
expect(multiply(-100)).toBe(-100);
36+
});
37+
38+
test("인수 중 NaN이 포함되면 NaN을 반환한다", () => {
39+
expect(multiply(NaN)).toBeNaN();
40+
expect(multiply(NaN, 7)).toBeNaN();
41+
expect(multiply(5, NaN, 10)).toBeNaN();
42+
});
43+
44+
test("Infinity(무한대)를 올바르게 처리한다", () => {
45+
expect(multiply(Infinity, 2)).toBe(Infinity);
46+
expect(multiply(Infinity, -2)).toBe(-Infinity);
47+
expect(multiply(Infinity, 0)).toBeNaN();
48+
});
49+
});
50+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function multiply(...args: number[]): number {
2+
return args.reduce((acc, curr) => acc * curr, 1);
3+
}

0 commit comments

Comments
 (0)