adding monkeytype
Some checks failed
Mark Stale PRs / stale (push) Has been cancelled

This commit is contained in:
Benjamin Falch
2026-04-23 13:53:44 +02:00
parent e214a2fd35
commit 2bc741fb78
1930 changed files with 7590652 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
import { describe, it, expect } from "vitest";
import * as Arrays from "../src/arrays";
describe("arrays", () => {
it("intersect", () => {
const testCases = [
{
a: [1],
b: [2],
removeDuplicates: false,
expected: [],
},
{
a: [1],
b: [1],
removeDuplicates: false,
expected: [1],
},
{
a: [1, 1],
b: [1],
removeDuplicates: true,
expected: [1],
},
{
a: [1, 1],
b: [1],
removeDuplicates: false,
expected: [1, 1],
},
{
a: [1],
b: [1, 2, 3],
removeDuplicates: false,
expected: [1],
},
{
a: [1, 1],
b: [1, 2, 3],
removeDuplicates: true,
expected: [1],
},
];
testCases.forEach(({ a, b, removeDuplicates, expected }) => {
expect(Arrays.intersect(a, b, removeDuplicates)).toEqual(expected);
});
});
});

View File

@@ -0,0 +1,223 @@
import { describe, it, expect, afterAll, vi } from "vitest";
import * as DateAndTime from "../src/date-and-time";
describe("date-and-time", () => {
afterAll(() => {
vi.useRealTimers();
});
it("getCurrentDayTimestamp", () => {
vi.useFakeTimers();
vi.setSystemTime(1652743381);
const currentDay = DateAndTime.getCurrentDayTimestamp();
expect(currentDay).toBe(1641600000);
});
it("getStartOfWeekTimestamp", () => {
const testCases = [
{
input: 1662400184017, // Mon Sep 05 2022 17:49:44 GMT+0000
expected: 1662336000000, // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: 1559771456000, // Wed Jun 05 2019 21:50:56 GMT+0000
expected: 1559520000000, // Mon Jun 03 2019 00:00:00 GMT+0000
},
{
input: 1465163456000, // Sun Jun 05 2016 21:50:56 GMT+0000
expected: 1464566400000, // Mon May 30 2016 00:00:00 GMT+0000
},
{
input: 1491515456000, // Thu Apr 06 2017 21:50:56 GMT+0000
expected: 1491177600000, // Mon Apr 03 2017 00:00:00 GMT+0000
},
{
input: 1462507200000, // Fri May 06 2016 04:00:00 GMT+0000
expected: 1462147200000, // Mon May 02 2016 00:00:00 GMT+0000
},
{
input: 1231218000000, // Tue Jan 06 2009 05:00:00 GMT+0000,
expected: 1231113600000, // Mon Jan 05 2009 00:00:00 GMT+0000
},
{
input: 1709420681000, // Sat Mar 02 2024 23:04:41 GMT+0000
expected: 1708905600000, // Mon Feb 26 2024 00:00:00 GMT+0000
},
];
testCases.forEach(({ input, expected }) => {
expect(DateAndTime.getStartOfWeekTimestamp(input)).toEqual(expected);
});
});
it("getCurrentWeekTimestamp", () => {
Date.now = vi.fn(() => 825289481000); // Sun Feb 25 1996 23:04:41 GMT+0000
const currentWeek = DateAndTime.getCurrentWeekTimestamp();
expect(currentWeek).toBe(824688000000); // Mon Feb 19 1996 00:00:00 GMT+0000
});
it("getStartOfDayTimestamp", () => {
const testCases = [
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 0,
expected: new Date("2023/06/16 00:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 1,
expected: new Date("2023/06/16 01:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: -1,
expected: new Date("2023/06/15 23:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: -4,
expected: new Date("2023/06/15 20:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 4,
expected: new Date("2023/06/16 04:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/17 03:00 UTC").getTime(),
offset: 4,
expected: new Date("2023/06/16 04:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 3,
expected: new Date("2023/06/16 03:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/17 01:00 UTC").getTime(),
offset: 3,
expected: new Date("2023/06/16 03:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
];
testCases.forEach(({ input, offset, expected }) => {
expect(
DateAndTime.getStartOfDayTimestamp(input, offset * 3600000),
).toEqual(expected);
});
});
it("isToday", () => {
const testCases = [
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 0,
expected: true,
},
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/17 1:00 UTC").getTime(),
offset: 0,
expected: false,
},
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/16 01:00 UTC").getTime(),
offset: 1,
expected: true,
},
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/17 01:00 UTC").getTime(),
offset: 2,
expected: true,
},
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/16 01:00 UTC").getTime(),
offset: 2,
expected: false,
},
{
now: new Date("2023/06/17 01:00 UTC").getTime(),
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 2,
expected: true,
},
{
now: new Date("2023/06/17 01:00 UTC").getTime(),
input: new Date("2023/06/17 02:00 UTC").getTime(),
offset: 2,
expected: false,
},
];
testCases.forEach(({ now, input, offset, expected }) => {
Date.now = vi.fn(() => now);
expect(DateAndTime.isToday(input, offset)).toEqual(expected);
});
});
it("isYesterday", () => {
const testCases = [
{
now: new Date("2023/06/15 15:00 UTC").getTime(),
input: new Date("2023/06/14 15:00 UTC").getTime(),
offset: 0,
expected: true,
},
{
now: new Date("2023/06/15 15:00 UTC").getTime(),
input: new Date("2023/06/15 15:00 UTC").getTime(),
offset: 0,
expected: false,
},
{
now: new Date("2023/06/15 15:00 UTC").getTime(),
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 0,
expected: false,
},
{
now: new Date("2023/06/15 15:00 UTC").getTime(),
input: new Date("2023/06/13 15:00 UTC").getTime(),
offset: 0,
expected: false,
},
{
now: new Date("2023/06/16 02:00 UTC").getTime(),
input: new Date("2023/06/15 02:00 UTC").getTime(),
offset: 4,
expected: true,
},
{
now: new Date("2023/06/16 02:00 UTC").getTime(),
input: new Date("2023/06/16 01:00 UTC").getTime(),
offset: 4,
expected: false,
},
{
now: new Date("2023/06/16 02:00 UTC").getTime(),
input: new Date("2023/06/15 22:00 UTC").getTime(),
offset: 4,
expected: false,
},
{
now: new Date("2023/06/16 04:00 UTC").getTime(),
input: new Date("2023/06/16 03:00 UTC").getTime(),
offset: 4,
expected: true,
},
{
now: new Date("2023/06/16 14:00 UTC").getTime(),
input: new Date("2023/06/16 12:00 UTC").getTime(),
offset: -11,
expected: true,
},
];
testCases.forEach(({ now, input, offset, expected }) => {
Date.now = vi.fn(() => now);
expect(DateAndTime.isYesterday(input, offset)).toEqual(expected);
});
});
});

View File

@@ -0,0 +1,147 @@
import { describe, it, expect } from "vitest";
import { parseWithSchema } from "../src/json";
import { z } from "zod";
describe("json", () => {
describe("parseWithSchema", () => {
const schema = z.object({
test: z.boolean().optional(),
name: z.string(),
nested: z.object({ foo: z.string() }).strict().optional(),
});
it("should throw with invalid json", () => {
expect(() => parseWithSchema("blah", schema)).toThrow(
new Error(
`Invalid JSON: Unexpected token 'b', "blah" is not valid JSON`,
),
);
});
it("should parse", () => {
const json = `{
"test":true,
"name":"bob",
"unknown":"unknown",
"nested":{
"foo":"bar"
}
}`;
expect(parseWithSchema(json, schema)).toStrictEqual({
test: true,
name: "bob",
nested: { foo: "bar" },
});
});
it("should throw with invalid schema", () => {
const json = `{
"test":"yes",
"nested":{
"foo":1
}
}`;
expect(() => parseWithSchema(json, schema)).toThrow(
new Error(
`JSON does not match schema: "test" expected boolean, received string, "name" required, "nested.foo" expected string, received number`,
),
);
});
it("should migrate if valid json", () => {
const json = `{
"name": 1
}`;
const result = parseWithSchema(json, schema, {
migrate: () => {
return {
name: "migrated",
test: false,
};
},
});
expect(result).toStrictEqual({
name: "migrated",
test: false,
});
});
it("should revert to fallback if invalid json", () => {
const json = `blah`;
const result = parseWithSchema(json, schema, {
fallback: {
name: "migrated",
test: false,
},
});
expect(result).toStrictEqual({
name: "migrated",
test: false,
});
});
it("should throw if migration fails", () => {
const json = `{
"name": 1
}`;
expect(() => {
parseWithSchema(json, schema, {
//@ts-expect-error need to test migration failure
migrate: () => {
return {
name: null,
test: "Hi",
};
},
});
}).toThrow(
new Error(
`Migrated value does not match schema: "test" expected boolean, received string, "name" expected string, received null`,
),
);
});
it("should revert to fallback if migration fails", () => {
const json = `{
"name": 1
}`;
const result = parseWithSchema(json, schema, {
fallback: {
name: "fallback",
test: false,
},
//@ts-expect-error need to test migration failure
migrate: () => {
return {
name: null,
test: "Hi",
};
},
});
expect(result).toStrictEqual({
name: "fallback",
test: false,
});
});
it("migrate function should receive value", () => {
const json = `{
"test":"test"
}`;
const result = parseWithSchema(json, schema, {
migrate: (value) => {
expect(value).toEqual({ test: "test" });
return {
name: "valid",
};
},
});
expect(result).toStrictEqual({
name: "valid",
});
});
});
});

View File

@@ -0,0 +1,157 @@
import { describe, it, expect } from "vitest";
import * as Numbers from "../src/numbers";
describe("numbers", () => {
describe("roundTo1", () => {
it("should correctly round", () => {
const tests = [
{
in: 0.0,
out: 0,
},
{
in: 0.01,
out: 0.0,
},
{
in: 0.09,
out: 0.1,
},
{
in: 0.123,
out: 0.1,
},
{
in: 0.456,
out: 0.5,
},
{
in: 0.789,
out: 0.8,
},
];
tests.forEach((test) => {
expect(Numbers.roundTo1(test.in)).toBe(test.out);
});
});
it("mapRange", () => {
const testCases = [
{
input: {
value: 123,
inMin: 0,
inMax: 200,
outMin: 0,
outMax: 1000,
clamp: false,
},
expected: 615,
},
{
input: {
value: 123,
inMin: 0,
inMax: 200,
outMin: 1000,
outMax: 0,
clamp: false,
},
expected: 385,
},
{
input: {
value: 10001,
inMin: 0,
inMax: 10000,
outMin: 0,
outMax: 1000,
clamp: false,
},
expected: 1000.1,
},
{
input: {
value: 10001,
inMin: 0,
inMax: 10000,
outMin: 0,
outMax: 1000,
clamp: true,
},
expected: 1000,
},
];
testCases.forEach(({ input, expected }) => {
expect(
Numbers.mapRange(
input.value,
input.inMin,
input.inMax,
input.outMin,
input.outMax,
input.clamp,
),
).toEqual(expected);
});
});
});
describe("isSafeNumber", () => {
describe("should correctly identify safe numbers", () => {
const testCases = [
//safe
{ input: 0, expected: true },
{ input: 1, expected: true },
{ input: -1, expected: true },
{ input: 0.5, expected: true },
{ input: -0.5, expected: true },
//not safe
{ input: NaN, expected: false },
{ input: Infinity, expected: false },
{ input: -Infinity, expected: false },
{ input: "string", expected: false },
{ input: null, expected: false },
{ input: undefined, expected: false },
{ input: true, expected: false },
{ input: false, expected: false },
];
it.for(testCases)(
"should return $expected for $input",
({ input, expected }) => {
expect(Numbers.isSafeNumber(input)).toEqual(expected);
},
);
});
});
describe("safeNumber", () => {
describe("should correctly identify safe numbers", () => {
const testCases = [
//safe
{ input: 0, expected: 0 },
{ input: 1, expected: 1 },
{ input: -1, expected: -1 },
{ input: 0.5, expected: 0.5 },
{ input: -0.5, expected: -0.5 },
//not safe
{ input: NaN, expected: undefined },
{ input: Infinity, expected: undefined },
{ input: -Infinity, expected: undefined },
{ input: "string", expected: undefined },
{ input: null, expected: undefined },
{ input: undefined, expected: undefined },
{ input: true, expected: undefined },
{ input: false, expected: undefined },
];
it.for(testCases)(
"should return $expected for $input",
({ input, expected }) => {
expect(Numbers.safeNumber(input as number)).toEqual(expected);
},
);
});
});
});

View File

@@ -0,0 +1,41 @@
import { describe, it, expect } from "vitest";
import { not } from "../src/predicates";
describe("predicates", () => {
describe("not", () => {
it("should not a simple boolean function", () => {
const isTrue = (): boolean => true;
const isFalse = not(isTrue);
expect(isFalse()).toBe(false);
});
it("should not a numeric predicate", () => {
const isPositive = (num: number): boolean => num > 0;
const isNotPositive = not(isPositive);
expect(isNotPositive(-5)).toBe(true);
expect(isNotPositive(10)).toBe(false);
});
it("should not a predicate taking multiple arguments", () => {
const containsLetter = (
str1: string,
str2: string,
letter: string,
): boolean => str1.includes(letter) || str2.includes(letter);
const doesNotContainLetter = not(containsLetter);
expect(doesNotContainLetter("hello", "world", "x")).toBe(true);
expect(doesNotContainLetter("apple", "banana", "a")).toBe(false);
});
it("should preserve type safety", () => {
const isEven = (num: number): boolean => num % 2 === 0;
const isOdd = not(isEven);
expect(isOdd(3)).toBe(true);
expect(isOdd(4)).toBe(false);
});
});
});

View File

@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { kebabToCamelCase } from "../src/strings";
describe("strings", () => {
describe("kebabToCamelCase", () => {
it("should convert kebab case to camel case", () => {
expect(kebabToCamelCase("hello-world")).toEqual("helloWorld");
expect(kebabToCamelCase("helloWorld")).toEqual("helloWorld");
expect(
kebabToCamelCase("one-two-three-four-five-six-seven-eight-nine-ten"),
).toEqual("oneTwoThreeFourFiveSixSevenEightNineTen");
});
});
});

View File

@@ -0,0 +1,92 @@
import { describe, it, expect } from "vitest";
import { tryCatch, tryCatchSync } from "../src/trycatch";
describe("tryCatch", () => {
it("should return data on successful promise resolution", async () => {
const result = await tryCatch(Promise.resolve("success"));
expect(result.data).toBe("success");
expect(result.error).toBeNull();
});
it("should return error on promise rejection", async () => {
const testError = new Error("test error");
const result = await tryCatch(Promise.reject(testError));
expect(result.data).toBeNull();
expect(result.error).toBe(testError);
});
it("should handle custom error types", async () => {
class CustomError extends Error {
code: string;
constructor(message: string, code: string) {
super(message);
this.code = code;
}
}
const customError = new CustomError("custom error", "E123");
const result = await tryCatch<string, CustomError>(
Promise.reject(customError),
);
expect(result.data).toBeNull();
expect(result.error).toBe(customError);
expect(result.error?.code).toBe("E123");
});
it("should handle exceptions in async functions", async () => {
const testError = new Error("test error");
const fn = async (): Promise<void> => {
throw testError;
};
const result = await tryCatch(fn());
expect(result.data).toBeNull();
expect(result.error).toBe(testError);
});
});
describe("tryCatchSync", () => {
it("should return data on successful function execution", () => {
const result = tryCatchSync(() => "success");
expect(result.data).toBe("success");
expect(result.error).toBeNull();
});
it("should return error when function throws", () => {
const testError = new Error("test error");
const result = tryCatchSync(() => {
throw testError;
});
expect(result.data).toBeNull();
expect(result.error).toBe(testError);
});
it("should handle complex data structures", () => {
const complexData = {
foo: "bar",
numbers: [1, 2, 3],
nested: { value: true },
};
const result = tryCatchSync(() => complexData);
expect(result.data).toEqual(complexData);
expect(result.error).toBeNull();
});
it("should handle custom error types", () => {
class CustomError extends Error {
code: string;
constructor(message: string, code: string) {
super(message);
this.code = code;
}
}
const customError = new CustomError("custom error", "E123");
const result = tryCatchSync<string, CustomError>(() => {
throw customError;
});
expect(result.data).toBeNull();
expect(result.error).toBe(customError);
expect(result.error?.code).toBe("E123");
});
});

View File

@@ -0,0 +1,7 @@
{
"extends": "@monkeytype/typescript-config/base.json",
"compilerOptions": {
"noEmit": true
},
"include": ["./**/*.ts", "./**/*.spec.ts", "./setup-tests.ts"]
}