Skip to content

Commit

Permalink
chore(core-utils): cleanup and add tests
Browse files Browse the repository at this point in the history
Remove unused modules and add some test coverage. The tests
are not exhaustive, but any test coverage is good test coverage.
  • Loading branch information
wycats committed Dec 21, 2023
1 parent 0ad7df5 commit b8819e0
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 218 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,33 @@
"@starbeam-dev/compile": "^1.0.2",
"@starbeam-dev/core": "^1.0.2",
"@starbeam-dev/eslint-plugin": "^1.0.4",
"@types/eslint": "^8.44.9",
"@types/eslint": "^8.56.0",
"@types/node": "^20.10.5",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"@vitest/ui": "^1.0.4",
"@vitest/ui": "^1.1.0",
"eslint": "^8.56.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-jsonc": "^2.11.1",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-jsonc": "^2.11.2",
"eslint-plugin-prettier": "^5.1.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-unused-imports": "^3.0.0",
"esyes": "^1.0.1",
"fast-glob": "^3.3.2",
"happy-dom": "^12.10.3",
"jsdom": "^23.0.1",
"knip": "^3.8.2",
"knip": "^3.8.4",
"node-gyp": "^10.0.1",
"prettier": "^3.1.1",
"tstyche": "1.0.0-beta.6",
"turbo": "^1.11.2",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vitest": "^1.0.4"
"vitest": "^1.1.0"
},
"nodemonConfig": {
"watch": [
Expand Down
8 changes: 0 additions & 8 deletions packages/universal/core-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,4 @@ export {
readonly,
} from "./src/object.js";
export { type Matcher, Overload, type TypedOverload } from "./src/overload.js";
export { isEmptyMatch, matchPattern, Pattern } from "./src/regexp.js";
export {
asIntIndex,
isPresentString,
stringify,
TO_STRING,
} from "./src/string.js";
export { exhaustive } from "./src/types.js";
export { isPresent } from "./src/value.js";
48 changes: 46 additions & 2 deletions packages/universal/core-utils/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type PresentArrayFor<T extends unknown[] | readonly unknown[] | undefined> =
T extends (infer Item)[]
? [Item, ...Item[]]
: T extends readonly (infer Item)[]
? readonly [Item, ...Item[]]
: never;
? readonly [Item, ...Item[]]
: never;

export function isPresentArray<
T extends unknown[] | readonly unknown[] | undefined,
Expand Down Expand Up @@ -280,3 +280,47 @@ export function isArray(value: unknown): value is unknown[];
export function isArray(value: unknown): boolean {
return Array.isArray(value);
}

if (import.meta.vitest) {
const { describe, test, expect } = import.meta.vitest;

describe("getLastIndex", () => {
test("should return the last index of a non-empty array", () => {
const array = [1, 2, 3];
expect(getLastIndex(array)).toBe(2);
});

test("should return undefined for an empty array", () => {
const array: number[] = [];
expect(getLastIndex(array)).toBeUndefined();
});
});

describe("removeItemAt", () => {
test("should remove item at a given index from the array", () => {
const array = [1, 2, 3];
removeItemAt(array, 1);
expect(array).toEqual([1, 3]);
});
});

describe("removeItem", () => {
test("should remove a given item from the array", () => {
const array = [1, 2, 3];
removeItem(array, 2);
expect(array).toEqual([1, 3]);
});
});

describe("isArray", () => {
test("should return true if the input is an array", () => {
const array = [1, 2, 3];
expect(isArray(array)).toBe(true);
});

test("should return false if the input is not an array", () => {
const notArray = "not an array";
expect(isArray(notArray)).toBe(false);
});
});
}
14 changes: 14 additions & 0 deletions packages/universal/core-utils/src/array.tst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, test } from "tstyche";

import { isPresentArray } from "./array.js";

describe("type checks", () => {
describe("isPresentArray", () => {
test("empty array", () => {
const array = ["hello"] as const;
if (isPresentArray(array)) {
expect(array).type.toEqual<readonly ["hello"]>();
}
});
});
});
35 changes: 35 additions & 0 deletions packages/universal/core-utils/src/iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,38 @@ export function iterableHasItems<T>(iterable: Iterable<T>): boolean {
const iterator = iterable[Symbol.iterator]();
return !iterator.next().done;
}

if (import.meta.vitest) {
const { test, describe, expect } = import.meta.vitest;

describe("reverse", () => {
test("should reverse an array", () => {
const input = ["a", "b", "c"];
const output = reverse(input);
expect(output).toEqual(["c", "b", "a"]);
});

test("should reverse a set", () => {
const input = new Set(["a", "b", "c"]);
const output = reverse(input);
expect(output).toEqual(["c", "b", "a"]);
});

test("should return an empty array when input is empty", () => {
expect(reverse([])).toEqual([]);
expect(reverse(new Set())).toEqual([]);
});
});

describe("iterableHasItems", () => {
test("should return true when iterable has items", () => {
expect(iterableHasItems(["a", "b", "c"])).toBe(true);
expect(iterableHasItems(new Set(["a", "b", "c"]))).toBe(true);
});

test("should return false when iterable is empty", () => {
expect(iterableHasItems([])).toBe(false);
expect(iterableHasItems(new Set())).toBe(false);
});
});
}
25 changes: 25 additions & 0 deletions packages/universal/core-utils/src/iterable.tst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, test } from "tstyche";

import type { PresentArray } from "./array.js";
import { iterableHasItems, reverse } from "./iterable.js";

describe("type checks", () => {
describe("reverse", () => {
test("PresentArray", () => {
const array = ["a", "b", "c"] as const;
expect(reverse(array)).type.toEqual<PresentArray<"b" | "a" | "c">>();
});

test("Set", () => {
const set = new Set(["a", "b", "c"]);
expect(reverse(set)).type.toEqual<string[]>();
});
});

describe("iterableHasItems", () => {
test("PresentArray", () => {
const array = ["a", "b", "c"] as const;
expect(iterableHasItems(array)).type.toEqual<true>();
});
});
});
80 changes: 0 additions & 80 deletions packages/universal/core-utils/src/regexp.ts

This file was deleted.

34 changes: 0 additions & 34 deletions packages/universal/core-utils/src/string.ts

This file was deleted.

16 changes: 0 additions & 16 deletions packages/universal/core-utils/src/types.ts

This file was deleted.

67 changes: 67 additions & 0 deletions packages/universal/core-utils/src/value.tst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, expect, test } from "tstyche";

import { isPresent } from "./value.js";

describe("type checks", () => {
test("truthy | null | undefined", () => {
{
const x: number | null = 1;

if (isPresent(x)) {
expect(x).type.toEqual<number>();
}
}

{
const x: number | undefined = 1;

if (isPresent(x)) {
expect(x).type.toEqual<number>();
}
}

{
const x: number | void = 1;

if (isPresent(x)) {
expect(x).type.toEqual<number>();
}
}

{
const x: number = 1;

if (isPresent(x)) {
expect(x).type.toEqual<number>();
}
}

{
const x: number | null | undefined = 1;

if (isPresent(x)) {
expect(x).type.toEqual<number>();
}
}
});

test("falsey | null | undefined", () => {
{
// even though 0 is falsy, it is still present
const x: number | null = 0;

if (isPresent(x)) {
expect(x).type.toEqual<number>();
}
}

{
// even though "" is falsy, it is still present
const x: string | null = "";

if (isPresent(x)) {
expect(x).type.toEqual<string>();
}
}
});
});
Loading

0 comments on commit b8819e0

Please sign in to comment.