Skip to content

Commit

Permalink
v1 breaking changes (#17)
Browse files Browse the repository at this point in the history
* break randomInt

* v1 more

* files

* readme

* changelog

* fix random Int

* miss

* randomDate

* ch
  • Loading branch information
ogroppo authored Jul 5, 2024
1 parent c8fc978 commit 89f07bd
Show file tree
Hide file tree
Showing 23 changed files with 164 additions and 101 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# deverything

## 1.0.0

### Major Changes

- Breaking changes on args
- percentageChange
- randomInt
- randomPositiveInt
- randomNegativeInt
- randomDate
- randomMaxDate

## 0.51.1

### Patch Changes
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Contributions always welcome!
- `max()`
- `min()`
- `multiply()`
- `normaliseArray()`
- `normaliseNumber()`
- `percentageChange()`
- `sum()`

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "0.51.1",
"version": "1.0.0",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand All @@ -27,9 +27,10 @@
"checks",
"dates",
"fake",
"formatters",
"generator",
"helpers",
"formatters",
"math",
"numbers",
"random",
"testing",
Expand Down
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: 6 additions & 4 deletions src/formatters/formatNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ import { randomInt } from "../random/randomInt";

describe("formatNumber", () => {
test("should return the same number if test is under a thousand", () => {
const value = randomInt(0, 999);
const value = randomInt({ min: 0, max: 999 });
expect(formatNumber(value, { compact: true })).toBe(`${value}`);
});

test("should return a string in compact K notation if value is one thousand or above", () => {
const value = randomInt(1000, 9999);
const value = randomInt({ min: 1000, max: 9999 });
expect(formatNumber(value, { compact: true })).toContain("K");
});

test("should return a string in compact M notation if value is one million or above", () => {
const value = randomInt(1000000, 9999999);
const value = randomInt({ min: 1000000, max: 9999999 });
expect(formatNumber(value, { compact: true })).toContain("M");
});

test("should return a string with thousand separator but no compact notation", () => {
const formattedValue = formatNumber(randomInt(1000000, 9999999));
const formattedValue = formatNumber(
randomInt({ min: 1000000, max: 9999999 })
);
expect(formattedValue).not.toContain("M");
expect(formattedValue).toContain(",");
});
Expand Down
2 changes: 2 additions & 0 deletions src/math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ export * from "./isStrictlyBetween";
export * from "./max";
export * from "./min";
export * from "./multiply";
export * from "./normaliseArray";
export * from "./normaliseNumber";
export * from "./percentageChange";
export * from "./sum";
14 changes: 14 additions & 0 deletions src/math/normaliseArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { max } from "./max";
import { min } from "./min";
import { normaliseNumber } from "./normaliseNumber";

/**
* Normalises an array of numbers
* @example normaliseArray([1, 2, 3]) => [0, 0.5, 1]
*/
export const normaliseArray = (values: number[]) => {
const minValue = min(values);
const maxValue = max(values);

return values.map((value) => normaliseNumber(value, minValue, maxValue));
};
9 changes: 9 additions & 0 deletions src/math/normaliseNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
*
* @example normaliseNumber(50, 0, 100) => 0.5
*/
export const normaliseNumber = (
value: number,
minValue: number,
maxValue: number
) => (value - minValue) / (maxValue - minValue);
31 changes: 7 additions & 24 deletions src/math/percentageChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,12 @@ import { percentageChange } from "./percentageChange";

describe("percentageChange", () => {
test("simple", async () => {
expect(
percentageChange({
current: 10,
previous: 12,
})
).toBe(-16.67);
expect(
percentageChange({
current: 0,
previous: 12,
})
).toBe(0);
expect(
percentageChange({
current: 0,
previous: 0,
})
).toBe(0);
expect(
percentageChange({
current: 99,
previous: 0,
})
).toBe(0);
expect(percentageChange(-0.1, 0.2)).toBe(0);
expect(percentageChange(0.2, 0.1)).toBe(-0.5);
expect(percentageChange(0.1, 0.2)).toBe(1);
expect(percentageChange(0.3, 0.333)).toBe(0.11);
expect(percentageChange(0, 0.12)).toBe(0);
expect(percentageChange(0, 0)).toBe(0);
expect(percentageChange(0.99, 0)).toBe(0);
});
});
26 changes: 12 additions & 14 deletions src/math/percentageChange.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { isPositiveInt } from "../validators";

export const percentageChange = ({
previous,
current,
}: {
previous: number;
current: number;
}): number => {
if (!isPositiveInt(previous) || !isPositiveInt(current)) return 0;
/**
*
* @param previous Positive percentage i.e. 0.1 for 10%
* @param current Positive percentage i.e. 0.2 for 20%
* @returns
*/
export const percentageChange = (previous: number, current: number): number => {
if (previous < 0 || current < 0) 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
};
2 changes: 1 addition & 1 deletion src/random/randomArrayItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export const randomArrayItem = <T>(
return last(array);
}

return array[randomInt(0, lastIndex(array))];
return array[randomInt({ min: 0, max: lastIndex(array) })];
};
4 changes: 2 additions & 2 deletions src/random/randomBool.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { randomInt } from "./randomInt";
import { randomArrayItem } from "./randomArrayItem";

export const randomBool = () => !!randomInt(0, 1);
export const randomBool = () => randomArrayItem([true, false]);
2 changes: 1 addition & 1 deletion src/random/randomChar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomInt } from "./randomInt";

export const randomChar = () => {
return String.fromCharCode(randomInt(97, 122));
return String.fromCharCode(randomInt({ min: 97, max: 122 }));
};
14 changes: 12 additions & 2 deletions src/random/randomDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ describe(`randomDate`, () => {
expect(randomDate().getTime()).toBeGreaterThan(0);
});
it(`args`, () => {
expect(randomDate("2010", "2011").toISOString().substring(0, 3)).toBe(
"201"
expect(
randomDate({ startDate: "2010", endDate: "2011" })
.toISOString()
.substring(0, 3)
).toBe("201");
});
it(`no start`, () => {
expect(randomDate({ endDate: "2011" }).getFullYear()).toBeLessThan(2011);
});
it(`no end`, () => {
expect(randomDate({ startDate: "2012" }).getFullYear()).toBeGreaterThan(
2011
);
});
});
Expand Down
44 changes: 24 additions & 20 deletions src/random/randomDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import {
MILLISECONDS_IN_MINUTE,
} from "../constants/time";
import { parseDate } from "../helpers/parseDate";
import { DateLike, DateRange } from "../types";
import { DateRange } from "../types";
import { isFutureDate, isPastDate } from "../validators";
import { randomInt } from "./randomInt";

const nowPlusMs = (ms: number) => new Date(new Date().getTime() + ms);
const datePlusDecade = (date: Date = new Date()) =>
datePlusMs(date, MILLISECONDS_IN_DECADE);

export const randomDate = (startDate?: DateLike, endDate?: DateLike) => {
const dateMinusDecade = (date: Date = new Date()) =>
datePlusMs(date, -MILLISECONDS_IN_DECADE);

const datePlusMs = (date: Date, ms: number) => new Date(date.getTime() + ms);

export const randomDate = ({ startDate, endDate }: Partial<DateRange> = {}) => {
const parsedStartDate = parseDate(startDate);
const parsedEndDate = parseDate(endDate);

Expand All @@ -19,24 +25,22 @@ export const randomDate = (startDate?: DateLike, endDate?: DateLike) => {
}

const finalStartDate =
parsedStartDate ||
(parsedEndDate
? new Date(parsedEndDate.getTime() - MILLISECONDS_IN_DECADE)
: nowPlusMs(-MILLISECONDS_IN_DECADE));
parsedStartDate || //
dateMinusDecade(parsedEndDate); // uses now if undefined

const finalEndDate =
parsedEndDate ||
(parsedStartDate
? new Date(parsedStartDate.getTime() + MILLISECONDS_IN_DECADE)
: nowPlusMs(MILLISECONDS_IN_DECADE));
parsedEndDate || //
datePlusDecade(parsedStartDate); // uses now if undefined

return new Date(randomInt(finalStartDate.getTime(), finalEndDate.getTime()));
return new Date(
randomInt({ min: finalStartDate.getTime(), max: finalEndDate.getTime() })
);
};

export const randomMaxDate = (start?: Date, end?: Date) => {
const startDate = start || new Date(-MAX_DATE_MILLISECONDS);
const endDate = end || new Date(MAX_DATE_MILLISECONDS);
return randomDate(startDate, endDate);
export const randomMaxDate = ({ startDate, endDate }: Partial<DateRange>) => {
startDate = startDate || new Date(-MAX_DATE_MILLISECONDS);
endDate = endDate || new Date(MAX_DATE_MILLISECONDS);
return randomDate({ startDate, endDate });
};

export const randomFutureDate = ({
Expand All @@ -51,9 +55,9 @@ export const randomFutureDate = ({
}

const finalStartDate =
parseDate(startDate) || nowPlusMs(5 * MILLISECONDS_IN_MINUTE); // Add a safe margin in the future (i.e. lagging tests)
parseDate(startDate) || datePlusMs(new Date(), 5 * MILLISECONDS_IN_MINUTE); // Add a safe margin in the future (i.e. lagging tests)

return randomDate(finalStartDate, endDate);
return randomDate({ startDate: finalStartDate, endDate });
};

export const randomPastDate = ({
Expand All @@ -68,12 +72,12 @@ export const randomPastDate = ({
}

const finalEndDate = parseDate(endDate) || new Date();
return randomDate(startDate, finalEndDate);
return randomDate({ startDate, endDate: finalEndDate });
};

export const randomDateRange = () => {
const startDate = randomDate();
const endDate = randomDate(startDate);
const endDate = randomDate({ startDate });

return {
endDate,
Expand Down
2 changes: 1 addition & 1 deletion src/random/randomIP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { array } from "../helpers/array";
import { randomInt } from "./randomInt";

export const randomIP = () => {
return array(4, () => randomInt(0, 255).toString()).join(".");
return array(4, () => randomInt({ min: 0, max: 255 }).toString()).join(".");
};
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);
});
});
Loading

0 comments on commit 89f07bd

Please sign in to comment.