Skip to content

Commit

Permalink
Add math
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Aug 27, 2023
1 parent 0f3f1c1 commit c23274d
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 2 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.27.0

### Minor Changes

- add math

## 0.26.0

### Minor Changes
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Contributions always welcome!
- `isPastDate()`
- `isStringDate()` also checks if the string passed is a **valid** date
- `isKey()` is a real key of an object
- `isLastIndex()` is the index is the last item of array
- `isNumber()` if the arg is number, and also usable (no infinity)
- `isInt()`
- `isEven()`
Expand All @@ -48,6 +49,13 @@ Contributions always welcome!
- `isURL()`
- `isUUID()`

### Math

- `average()`
- `max()`
- `min()`
- `sum()`

### Helpers

- `array()` create an arbitrary array based on a function
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "deverything",
"version": "0.26.0",
"version": "0.27.0",
"description": "Everything you need for Dev",
"main": "dist/index.js",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"sideEffects": false,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./checks";
export * from "./helpers";
export * from "./math";
export * from "./random";
export * from "./types";
export * from "./validators";
13 changes: 13 additions & 0 deletions src/math/average.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, test } from "@jest/globals";
import { average } from "./average";

describe("average", () => {
test("no arg", async () => {
expect(average([])).toBe(NaN);
});

test("args", async () => {
expect(average([1, 2])).toBe(1.5);
expect(average([1, 2, 3, 4])).toBe(2.5);
});
});
10 changes: 10 additions & 0 deletions src/math/average.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Calculates the average of a list of numbers.
* @example
* average([1, 2, 3, 4, 5]); // 3
* average(1, 2, 3, 4, 5); // 3
*/
export const average = (numbers: number[]): number => {
const sum = numbers.reduce((total, num) => total + num, 0);
return sum / numbers.length;
};
4 changes: 4 additions & 0 deletions src/math/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./average";
export * from "./max";
export * from "./min";
export * from "./sum";
4 changes: 4 additions & 0 deletions src/math/max.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const max = (values: number[]): number => {
const maxValue = Math.max(...values);
return maxValue;
};
4 changes: 4 additions & 0 deletions src/math/min.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const min = (values: number[]): number => {
const minValue = Math.min(...values);
return minValue;
};
28 changes: 28 additions & 0 deletions src/math/sum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, test } from "@jest/globals";
import { sum, sumBy } 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);
});
});
13 changes: 13 additions & 0 deletions src/math/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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);
};
1 change: 1 addition & 0 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./isFunction";
export * from "./isFutureDate";
export * from "./isJsDate";
export * from "./isKey";
export * from "./isLastIndex";
export * from "./isNumber";
export * from "./isNumeric";
export * from "./isNumericId";
Expand Down
3 changes: 3 additions & 0 deletions src/validators/isLastIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isLastIndex = (index: number, array: any[]) => {
return index === array.length - 1;
};

0 comments on commit c23274d

Please sign in to comment.