diff --git a/src/formatters/formatLatin.test.ts b/src/formatters/formatLatin.test.ts new file mode 100644 index 0000000..1b9b571 --- /dev/null +++ b/src/formatters/formatLatin.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, test } from "@jest/globals"; +import { formatLatin } from "./formatLatin"; + +const accentString = + "áéíóúüñ¿¡àâèêëîïôûàãàãçàèéìòóùäöüßáéíóúáéíóúąćęłńóśźżáčďéěíňóřšťúůýžćčđšžáéíóúüαβγδεζηθικλμνξοπρστυφχψωçğıİöşüابتثجحخدذرزسشصضطظعغ"; +const latinString = "aeiou"; + +describe("formatLatin", () => { + test("should return a-z", () => { + expect(formatLatin(accentString)).toBe(latinString); + }); +}); diff --git a/src/formatters/formatLatin.ts b/src/formatters/formatLatin.ts new file mode 100644 index 0000000..c346ab7 --- /dev/null +++ b/src/formatters/formatLatin.ts @@ -0,0 +1,4 @@ +export const formatLatin = (text: string): string => { + let normalized = text.normalize("NFKD"); + return normalized; +}; diff --git a/src/math/normaliseArray.ts b/src/math/normaliseArray.ts new file mode 100644 index 0000000..24c56f4 --- /dev/null +++ b/src/math/normaliseArray.ts @@ -0,0 +1,10 @@ +import { max } from "./max"; +import { min } from "./min"; +import { normaliseNumber } from "./normaliseNumber"; + +export const normaliseArray = (values: number[]) => { + const minValue = min(values); + const maxValue = max(values); + + return values.map((value) => normaliseNumber(value, minValue, maxValue)); +}; diff --git a/src/math/normaliseNumber.ts b/src/math/normaliseNumber.ts new file mode 100644 index 0000000..96a34c7 --- /dev/null +++ b/src/math/normaliseNumber.ts @@ -0,0 +1,5 @@ +export const normaliseNumber = ( + value: number, + minValue: number, + maxValue: number +) => (value - minValue) / (maxValue - minValue); diff --git a/src/math/percentageChange.test.ts b/src/math/percentageChange.test.ts index 8771ef5..7b96222 100644 --- a/src/math/percentageChange.test.ts +++ b/src/math/percentageChange.test.ts @@ -8,7 +8,13 @@ describe("percentageChange", () => { current: 10, previous: 12, }) - ).toBe(-16.67); + ).toBe(-0.1667); + expect( + percentageChange({ + current: 12, + previous: 10, + }) + ).toBe(0.2); expect( percentageChange({ current: 0, diff --git a/src/math/percentageChange.ts b/src/math/percentageChange.ts index 3b5dfd5..1d95a4c 100644 --- a/src/math/percentageChange.ts +++ b/src/math/percentageChange.ts @@ -9,8 +9,8 @@ export const percentageChange = ({ }): number => { if (!isPositiveInt(previous) || !isPositiveInt(current)) return 0; if (current === 0 && previous === 0) return 0; - if (current === 0 && previous !== 0) return -100; - if (current !== 0 && previous === 0) return 100; - const perChange = ((current - previous) * 100) / previous; - return parseFloat(perChange.toFixed(2)); + if (current === 0 && previous !== 0) return -1; + if (current !== 0 && previous === 0) return 1; + const perChange = (current - previous) / previous; + return parseFloat(perChange.toFixed(4)); // 4 decimal places so when formatting to % two decimal places are shown }; diff --git a/src/random/randomInt.test.ts b/src/random/randomInt.test.ts index b8e45d2..b9c5efc 100644 --- a/src/random/randomInt.test.ts +++ b/src/random/randomInt.test.ts @@ -8,8 +8,8 @@ describe("randomInt", () => { }); test("args", async () => { - expect(randomInt(12, 20)).toBeGreaterThanOrEqual(12); - expect(randomInt(12, 12)).toBeLessThanOrEqual(12); - expect(randomInt(11, 12)).toBeLessThanOrEqual(13); + expect(randomInt({ min: 12, max: 20 })).toBeGreaterThanOrEqual(12); + expect(randomInt({ min: 12, max: 12 })).toBeLessThanOrEqual(12); + expect(randomInt({ min: 11, max: 12 })).toBeLessThanOrEqual(13); }); }); diff --git a/src/random/randomInt.ts b/src/random/randomInt.ts index f3717c2..706b958 100644 --- a/src/random/randomInt.ts +++ b/src/random/randomInt.ts @@ -1,35 +1,43 @@ -export const randomInt = (min: number = -100, max: number = 100): number => { +export const randomInt = ({ + min = -100, + max = 100, +}: { + min?: number; + max?: number; +} = {}): number => { min = Math.ceil(min); // in case is a float max = Math.floor(max); // in case is a float return Math.floor(Math.random() * (max - min + 1) + min); }; -export const randomPositiveInt = (max: number = 100): number => - randomInt(1, max); - -export const randomNegativeInt = (min: number = -100): number => - randomInt(min, -1); - -export const randomMaxSafeInt = () => - randomInt(-Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER); - -export const randomMaxInt = () => - randomInt(-Number.MAX_VALUE, Number.MAX_VALUE); - -export const randomPercentage = ({ - min, - max, +export const randomPositiveInt = ({ + min = 1, + max = 100, }: { min?: number; max?: number; -} = {}) => randomInt(min ?? -100, max ?? 100); +} = {}): number => + randomInt({ + min, + max, + }); -export const randomPositivePercentage = ({ - min, - max, +export const randomNegativeInt = ({ + min = -100, + max = -1, }: { min?: number; max?: number; -} = {}) => randomInt(min ?? 1, max ?? 100); +} = {}): number => + randomInt({ + min, + max, + }); + +export const randomMaxSafeInt = () => + randomInt({ min: -Number.MAX_SAFE_INTEGER, max: Number.MAX_SAFE_INTEGER }); + +export const randomMaxInt = () => + randomInt({ min: -Number.MAX_VALUE, max: Number.MAX_VALUE }); -export const randomFormattedPercentage = () => randomPercentage() + "%"; +export const randomFormattedPercentage = () => randomInt() + "%";