From c23274d6a65af281ba092d6d97701f323052ece7 Mon Sep 17 00:00:00 2001 From: Orlando Date: Sun, 27 Aug 2023 18:19:10 +0100 Subject: [PATCH] Add math --- CHANGELOG.md | 6 ++++++ README.md | 8 ++++++++ package.json | 4 ++-- src/index.ts | 1 + src/math/average.test.ts | 13 +++++++++++++ src/math/average.ts | 10 ++++++++++ src/math/index.ts | 4 ++++ src/math/max.ts | 4 ++++ src/math/min.ts | 4 ++++ src/math/sum.test.ts | 28 ++++++++++++++++++++++++++++ src/math/sum.ts | 13 +++++++++++++ src/validators/index.ts | 1 + src/validators/isLastIndex.ts | 3 +++ 13 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/math/average.test.ts create mode 100644 src/math/average.ts create mode 100644 src/math/index.ts create mode 100644 src/math/max.ts create mode 100644 src/math/min.ts create mode 100644 src/math/sum.test.ts create mode 100644 src/math/sum.ts create mode 100644 src/validators/isLastIndex.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 31434a9..3aa4c8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.27.0 + +### Minor Changes + +- add math + ## 0.26.0 ### Minor Changes diff --git a/README.md b/README.md index bf30792..c99e576 100644 --- a/README.md +++ b/README.md @@ -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()` @@ -48,6 +49,13 @@ Contributions always welcome! - `isURL()` - `isUUID()` +### Math + +- `average()` +- `max()` +- `min()` +- `sum()` + ### Helpers - `array()` create an arbitrary array based on a function diff --git a/package.json b/package.json index 92bbc68..960003d 100644 --- a/package.json +++ b/package.json @@ -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, diff --git a/src/index.ts b/src/index.ts index fbb6582..23d7b2c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export * from "./checks"; export * from "./helpers"; +export * from "./math"; export * from "./random"; export * from "./types"; export * from "./validators"; diff --git a/src/math/average.test.ts b/src/math/average.test.ts new file mode 100644 index 0000000..1bb516f --- /dev/null +++ b/src/math/average.test.ts @@ -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); + }); +}); diff --git a/src/math/average.ts b/src/math/average.ts new file mode 100644 index 0000000..bc92b0e --- /dev/null +++ b/src/math/average.ts @@ -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; +}; diff --git a/src/math/index.ts b/src/math/index.ts new file mode 100644 index 0000000..25fe110 --- /dev/null +++ b/src/math/index.ts @@ -0,0 +1,4 @@ +export * from "./average"; +export * from "./max"; +export * from "./min"; +export * from "./sum"; diff --git a/src/math/max.ts b/src/math/max.ts new file mode 100644 index 0000000..e16f2ad --- /dev/null +++ b/src/math/max.ts @@ -0,0 +1,4 @@ +export const max = (values: number[]): number => { + const maxValue = Math.max(...values); + return maxValue; +}; diff --git a/src/math/min.ts b/src/math/min.ts new file mode 100644 index 0000000..1a20730 --- /dev/null +++ b/src/math/min.ts @@ -0,0 +1,4 @@ +export const min = (values: number[]): number => { + const minValue = Math.min(...values); + return minValue; +}; diff --git a/src/math/sum.test.ts b/src/math/sum.test.ts new file mode 100644 index 0000000..9b17188 --- /dev/null +++ b/src/math/sum.test.ts @@ -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); + }); +}); diff --git a/src/math/sum.ts b/src/math/sum.ts new file mode 100644 index 0000000..83008ac --- /dev/null +++ b/src/math/sum.ts @@ -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 = ( + items: T[], + prop: PropertyAccessor +) => { + return items.reduce((total, item) => total + getProp(item, prop), 0); +}; diff --git a/src/validators/index.ts b/src/validators/index.ts index 226a418..6753696 100644 --- a/src/validators/index.ts +++ b/src/validators/index.ts @@ -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"; diff --git a/src/validators/isLastIndex.ts b/src/validators/isLastIndex.ts new file mode 100644 index 0000000..56b083f --- /dev/null +++ b/src/validators/isLastIndex.ts @@ -0,0 +1,3 @@ +export const isLastIndex = (index: number, array: any[]) => { + return index === array.length - 1; +};