Skip to content

Commit

Permalink
Multiply
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Oct 8, 2023
1 parent e4d2d2d commit e16af15
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 36 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 0.31.0

### Minor Changes

- multiply

## 0.30.0

### Minor Changes
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ Contributions always welcome!

### Math

- `average()`
- `max()`
- `min()`
- `sum()`
- `average()` numbers of an array
- `max()` numbers of an array
- `min()` numbers of an array
- `multiply()` numbers of an array
- `sum()` numbers an array

### Helpers

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "0.30.0",
"version": "0.31.0",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
1 change: 1 addition & 0 deletions src/math/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./average";
export * from "./max";
export * from "./min";
export * from "./multiply";
export * from "./sum";
10 changes: 10 additions & 0 deletions src/math/multiply.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, test } from "@jest/globals";
import { multiply } from "./multiply";

describe("multiply", () => {
test("simple", async () => {
expect(multiply([1, 2])).toBe(2);
expect(multiply([1, 0])).toBe(0);
expect(multiply([0.5, 0.5])).toBe(0.25);
});
});
3 changes: 3 additions & 0 deletions src/math/multiply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const multiply = (numbers: number[]) => {
return numbers.reduce((total, num) => total * num, 1);
};
22 changes: 1 addition & 21 deletions src/math/sum.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import { describe, expect, test } from "@jest/globals";
import { sum, sumBy } from "./sum";
import { sum } from "./sum";

describe("sum", () => {
test("simple", async () => {
expect(sum([1, 2])).toBe(3);
});
});

describe("sumBy", () => {
test("simple", async () => {
expect(
sumBy([{ a: 1 }, { a: 2 }], (item) => {
return item.a;
})
).toBe(3);
});
test("string", async () => {
expect(sumBy([{ a: 1 }, { a: 2 }], "a")).toBe(3);
});
test("complex", async () => {
expect(
sumBy([{ a: 1 }, { b: { a: 2 } }], (item) => {
return item.a || item.b?.a;
})
).toBe(3);
});
});
10 changes: 0 additions & 10 deletions src/math/sum.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
import { PropertyAccessor, getProp } from "../_internals/getProp";
import { PlainObject } from "../types";

export const sum = (numbers: number[]) => {
return numbers.reduce((total, num) => total + num, 0);
};

export const sumBy = <T extends PlainObject>(
items: T[],
prop: PropertyAccessor<T>
) => {
return items.reduce((total, item) => total + getProp<T>(item, prop), 0);
};

0 comments on commit e16af15

Please sign in to comment.