Skip to content

Commit

Permalink
break randomInt
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Apr 5, 2024
1 parent 728a521 commit 5f9c598
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 30 deletions.
12 changes: 12 additions & 0 deletions src/formatters/formatLatin.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
4 changes: 4 additions & 0 deletions src/formatters/formatLatin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const formatLatin = (text: string): string => {
let normalized = text.normalize("NFKD");
return normalized;
};
10 changes: 10 additions & 0 deletions src/math/normaliseArray.ts
Original file line number Diff line number Diff line change
@@ -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));
};
5 changes: 5 additions & 0 deletions src/math/normaliseNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const normaliseNumber = (
value: number,
minValue: number,
maxValue: number
) => (value - minValue) / (maxValue - minValue);
8 changes: 7 additions & 1 deletion src/math/percentageChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/math/percentageChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
6 changes: 3 additions & 3 deletions src/random/randomInt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
52 changes: 30 additions & 22 deletions src/random/randomInt.ts
Original file line number Diff line number Diff line change
@@ -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() + "%";

0 comments on commit 5f9c598

Please sign in to comment.