mirror of
https://github.com/theoludwig/theoludwig.git
synced 2025-05-29 22:37:44 +02:00
perf!: monorepo setup + fully static + webp images
BREAKING CHANGE: minimum supported Node.js >= 22.0.0 and pnpm >= 9.5.0
This commit is contained in:
17
packages/utils/src/constants.ts
Normal file
17
packages/utils/src/constants.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import packageJSON from "../package.json"
|
||||
|
||||
export const VERSION =
|
||||
process.env["NODE_ENV"] === "development"
|
||||
? "0.0.0-development"
|
||||
: packageJSON.version
|
||||
|
||||
export const GIT_REPO_LINK = "https://github.com/theoludwig/theoludwig"
|
||||
|
||||
export const BIRTH_DATE_DAY = "31"
|
||||
export const BIRTH_DATE_MONTH = "03"
|
||||
export const BIRTH_DATE_YEAR = "2003"
|
||||
export const BIRTH_DATE_STRING =
|
||||
`${BIRTH_DATE_DAY}/${BIRTH_DATE_MONTH}/${BIRTH_DATE_YEAR}` as const
|
||||
export const BIRTH_DATE_ISO_8601 =
|
||||
`${BIRTH_DATE_YEAR}-${BIRTH_DATE_MONTH}-${BIRTH_DATE_DAY}` as const
|
||||
export const BIRTH_DATE = new Date(BIRTH_DATE_ISO_8601)
|
24
packages/utils/src/dates.ts
Normal file
24
packages/utils/src/dates.ts
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Returns a date as a string value in ISO 8601 format (without time information).
|
||||
*
|
||||
* @param date
|
||||
* @returns
|
||||
* @example getISODate(new Date("2012-05-23")) // "2012-05-23"
|
||||
*/
|
||||
export const getISODate = (date: Date): string => {
|
||||
return date.toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the age of a person based on their birth date.
|
||||
* @param birthDate
|
||||
*/
|
||||
export const getAge = (birthDate: Date): number => {
|
||||
const today = new Date()
|
||||
let age = today.getFullYear() - birthDate.getFullYear()
|
||||
const month = today.getMonth() - birthDate.getMonth()
|
||||
if (month < 0 || (month === 0 && today.getDate() < birthDate.getDate())) {
|
||||
age--
|
||||
}
|
||||
return age
|
||||
}
|
3
packages/utils/src/strings.ts
Normal file
3
packages/utils/src/strings.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export const capitalize = (string: string): string => {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1)
|
||||
}
|
36
packages/utils/src/tests/constants.test.ts
Normal file
36
packages/utils/src/tests/constants.test.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest"
|
||||
|
||||
describe("VERSION", () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs()
|
||||
vi.resetModules()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('should return "0.0.0-development" when NODE_ENV is development', async () => {
|
||||
// Arrange - Given
|
||||
vi.stubEnv("NODE_ENV", "development")
|
||||
|
||||
// Act - When
|
||||
const { VERSION } = await import("../constants.js")
|
||||
|
||||
// Assert - Then
|
||||
const expected = "0.0.0-development"
|
||||
expect(VERSION).toEqual(expected)
|
||||
})
|
||||
|
||||
it("should return the version from package.json when NODE_ENV is not development", async () => {
|
||||
// Arrange - Given
|
||||
vi.stubEnv("NODE_ENV", "production")
|
||||
vi.mock("../../package.json", () => {
|
||||
return { default: { version: "1.0.0" } }
|
||||
})
|
||||
|
||||
// Act - When
|
||||
const { VERSION } = await import("../constants.js")
|
||||
|
||||
// Assert - Then
|
||||
const expected = "1.0.0"
|
||||
expect(VERSION).toEqual(expected)
|
||||
})
|
||||
})
|
79
packages/utils/src/tests/dates.test.ts
Normal file
79
packages/utils/src/tests/dates.test.ts
Normal file
@ -0,0 +1,79 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
||||
|
||||
import { getAge, getISODate } from "../dates.js"
|
||||
|
||||
describe("getISODate", () => {
|
||||
it("should return the correct date in ISO format (e.g: 2012-05-23)", () => {
|
||||
// Arrange - Given
|
||||
const input = new Date("2012-05-23")
|
||||
|
||||
// Act - When
|
||||
const output = getISODate(input)
|
||||
|
||||
// Assert - Then
|
||||
const expected = "2012-05-23"
|
||||
expect(output).toEqual(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe("getAge", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it("should return the correct age based on the birth date", () => {
|
||||
// Arrange - Given
|
||||
vi.setSystemTime(new Date("2018-03-20"))
|
||||
const birthDate = new Date("1980-02-20")
|
||||
|
||||
// Act - When
|
||||
const output = getAge(birthDate)
|
||||
|
||||
// Assert - Then
|
||||
const expected = 38
|
||||
expect(output).toEqual(expected)
|
||||
})
|
||||
|
||||
it("should return the correct age based on the birth date when the birthday has not happened yet", () => {
|
||||
// Arrange - Given
|
||||
vi.setSystemTime(new Date("2018-03-20"))
|
||||
const birthDate = new Date("1980-07-20")
|
||||
|
||||
// Act - When
|
||||
const output = getAge(birthDate)
|
||||
|
||||
// Assert - Then
|
||||
const expected = 37
|
||||
expect(output).toEqual(expected)
|
||||
})
|
||||
|
||||
it("should return the correct age based on the birth date when the birthday is today", () => {
|
||||
// Arrange - Given
|
||||
vi.setSystemTime(new Date("2018-03-20"))
|
||||
const birthDate = new Date("1980-03-20")
|
||||
|
||||
// Act - When
|
||||
const output = getAge(birthDate)
|
||||
|
||||
// Assert - Then
|
||||
const expected = 38
|
||||
expect(output).toEqual(expected)
|
||||
})
|
||||
|
||||
it("should return the correct age based on the birth date when the birthday has not happened yet, but will happen this month", () => {
|
||||
// Arrange - Given
|
||||
vi.setSystemTime(new Date("2018-03-20"))
|
||||
const birthDate = new Date("1980-03-25")
|
||||
|
||||
// Act - When
|
||||
const output = getAge(birthDate)
|
||||
|
||||
// Assert - Then
|
||||
const expected = 37
|
||||
expect(output).toEqual(expected)
|
||||
})
|
||||
})
|
41
packages/utils/src/tests/strings.test.ts
Normal file
41
packages/utils/src/tests/strings.test.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { capitalize } from "../strings.js"
|
||||
|
||||
describe("capitalize", () => {
|
||||
it("should capitalize the first letter of a string", () => {
|
||||
// Arrange - Given
|
||||
const input = "hello, world!"
|
||||
|
||||
// Act - When
|
||||
const output = capitalize(input)
|
||||
|
||||
// Assert - Then
|
||||
const expected = "Hello, world!"
|
||||
expect(output).toEqual(expected)
|
||||
})
|
||||
|
||||
it("should return an empty string when the input is an empty string", () => {
|
||||
// Arrange - Given
|
||||
const input = ""
|
||||
|
||||
// Act - When
|
||||
const output = capitalize(input)
|
||||
|
||||
// Assert - Then
|
||||
const expected = ""
|
||||
expect(output).toEqual(expected)
|
||||
})
|
||||
|
||||
it("should return the same string when the first letter is already capitalized", () => {
|
||||
// Arrange - Given
|
||||
const input = "Hello, world!"
|
||||
|
||||
// Act - When
|
||||
const output = capitalize(input)
|
||||
|
||||
// Assert - Then
|
||||
const expected = "Hello, world!"
|
||||
expect(output).toEqual(expected)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user