feat(#1): Set up monorepo scaffold with pnpm workspaces + TurboRepo
Implements the foundational project structure including: - pnpm workspaces configuration - TurboRepo for build orchestration - NestJS 11.1.12 API (apps/api) - Next.js 16.1.6 web app (apps/web) - Shared packages (config, shared, ui) - TypeScript strict mode configuration - ESLint + Prettier setup - Vitest for unit testing (19 passing tests) Fixes #1 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
61
packages/shared/src/utils/index.test.ts
Normal file
61
packages/shared/src/utils/index.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isDefined, parseDate, sleep, slugify } from "./index.js";
|
||||
|
||||
describe("parseDate", () => {
|
||||
it("should return undefined for null or undefined", () => {
|
||||
expect(parseDate(null)).toBeUndefined();
|
||||
expect(parseDate(undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return the same Date object if passed a Date", () => {
|
||||
const date = new Date("2024-01-15");
|
||||
expect(parseDate(date)).toBe(date);
|
||||
});
|
||||
|
||||
it("should parse valid date strings", () => {
|
||||
const result = parseDate("2024-01-15");
|
||||
expect(result).toBeInstanceOf(Date);
|
||||
expect(result?.toISOString()).toContain("2024-01-15");
|
||||
});
|
||||
|
||||
it("should return undefined for invalid date strings", () => {
|
||||
expect(parseDate("not-a-date")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("slugify", () => {
|
||||
it("should convert text to lowercase slug", () => {
|
||||
expect(slugify("Hello World")).toBe("hello-world");
|
||||
});
|
||||
|
||||
it("should handle special characters", () => {
|
||||
expect(slugify("Hello, World!")).toBe("hello-world");
|
||||
});
|
||||
|
||||
it("should trim leading and trailing hyphens", () => {
|
||||
expect(slugify(" Hello World ")).toBe("hello-world");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sleep", () => {
|
||||
it("should resolve after the specified time", async () => {
|
||||
const start = Date.now();
|
||||
await sleep(50);
|
||||
const elapsed = Date.now() - start;
|
||||
expect(elapsed).toBeGreaterThanOrEqual(45);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isDefined", () => {
|
||||
it("should return false for null and undefined", () => {
|
||||
expect(isDefined(null)).toBe(false);
|
||||
expect(isDefined(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return true for defined values", () => {
|
||||
expect(isDefined(0)).toBe(true);
|
||||
expect(isDefined("")).toBe(true);
|
||||
expect(isDefined(false)).toBe(true);
|
||||
expect(isDefined({})).toBe(true);
|
||||
});
|
||||
});
|
||||
35
packages/shared/src/utils/index.ts
Normal file
35
packages/shared/src/utils/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Safely parse a date string or return undefined
|
||||
*/
|
||||
export function parseDate(value: string | Date | undefined | null): Date | undefined {
|
||||
if (!value) return undefined;
|
||||
if (value instanceof Date) return value;
|
||||
const parsed = new Date(value);
|
||||
return isNaN(parsed.getTime()) ? undefined : parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a simple slug from a string
|
||||
*/
|
||||
export function slugify(text: string): string {
|
||||
return text
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^\w\s-]/g, "")
|
||||
.replace(/[\s_-]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleep for a given number of milliseconds
|
||||
*/
|
||||
export function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a value is defined (not null or undefined)
|
||||
*/
|
||||
export function isDefined<T>(value: T | null | undefined): value is T {
|
||||
return value !== null && value !== undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user