Merged in feat/sw-2863-move-contentstack-router-to-trpc-package (pull request #2389)
feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
This commit is contained in:
25
packages/common/utils/chunk.test.ts
Normal file
25
packages/common/utils/chunk.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect } from "vitest"
|
||||
|
||||
import { chunk } from "./chunk"
|
||||
|
||||
describe("chunk", () => {
|
||||
it("should split an array into equally sized chunks of specified size", () => {
|
||||
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
const size = 3
|
||||
const expected = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9],
|
||||
]
|
||||
|
||||
expect(chunk(array, size)).toEqual(expected)
|
||||
})
|
||||
|
||||
it("should split an array into equally sized chunks of specified size", () => {
|
||||
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
const size = 4
|
||||
const expected = [[1, 2, 3, 4], [5, 6, 7, 8], [9]]
|
||||
|
||||
expect(chunk(array, size)).toEqual(expected)
|
||||
})
|
||||
})
|
||||
11
packages/common/utils/chunk.ts
Normal file
11
packages/common/utils/chunk.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Splits an array into chunks of a specified size
|
||||
*/
|
||||
export function chunk<T>(array: T[], size: number): T[][] {
|
||||
const result: T[][] = []
|
||||
for (let i = 0; i < array.length; i += size) {
|
||||
result.push(array.slice(i, i + size))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
29
packages/common/utils/languages.ts
Normal file
29
packages/common/utils/languages.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "../constants/language"
|
||||
|
||||
export const languageSchema = z.preprocess(
|
||||
(arg) => (typeof arg === "string" ? arg.toLowerCase() : arg),
|
||||
z.nativeEnum(Lang)
|
||||
)
|
||||
|
||||
export function isValidLang(lang?: string): lang is Lang {
|
||||
const result = languageSchema.safeParse(lang)
|
||||
return result.success
|
||||
}
|
||||
|
||||
export function findLang(pathname: string): Lang | undefined {
|
||||
const langFromPath = Object.values(Lang).find(
|
||||
(l) => pathname.startsWith(`/${l}/`) || pathname === `/${l}`
|
||||
)
|
||||
|
||||
return isValidLang(langFromPath) ? langFromPath : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to convert lang string to Lang enum.
|
||||
*/
|
||||
export function toLang(lang: string): Lang | undefined {
|
||||
const lowerCaseLang = lang.toLowerCase()
|
||||
return Object.values(Lang).find((l) => l === lowerCaseLang)
|
||||
}
|
||||
11
packages/common/utils/url.ts
Normal file
11
packages/common/utils/url.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export function removeMultipleSlashes(pathname: string) {
|
||||
return pathname.replaceAll(/\/\/+/g, "/")
|
||||
}
|
||||
|
||||
export function removeTrailingSlash(pathname: string) {
|
||||
if (pathname.endsWith("/")) {
|
||||
// Remove the trailing slash
|
||||
return pathname.slice(0, -1)
|
||||
}
|
||||
return pathname
|
||||
}
|
||||
17
packages/common/utils/zod/arrayValidator.ts
Normal file
17
packages/common/utils/zod/arrayValidator.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { nullableStringValidator } from "./stringValidator"
|
||||
|
||||
import type { ZodObject, ZodRawShape } from "zod"
|
||||
|
||||
export function nullableArrayObjectValidator<T extends ZodRawShape>(
|
||||
schema: ZodObject<T>
|
||||
) {
|
||||
return schema
|
||||
.array()
|
||||
.nullish()
|
||||
.transform((arr) => (arr ? arr.filter(Boolean) : []))
|
||||
}
|
||||
|
||||
export const nullableArrayStringValidator = nullableStringValidator
|
||||
.array()
|
||||
.nullish()
|
||||
.transform((arr) => (arr ? arr.filter(Boolean) : []))
|
||||
12
packages/common/utils/zod/numberValidator.ts
Normal file
12
packages/common/utils/zod/numberValidator.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const nullableNumberValidator = z
|
||||
.number()
|
||||
.nullish()
|
||||
.transform((num) => (typeof num === "number" ? num : 0))
|
||||
|
||||
export const nullableIntValidator = z
|
||||
.number()
|
||||
.int()
|
||||
.nullish()
|
||||
.transform((num) => (typeof num === "number" ? num : 0))
|
||||
18
packages/common/utils/zod/stringValidator.ts
Normal file
18
packages/common/utils/zod/stringValidator.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const nullableStringValidator = z
|
||||
.string()
|
||||
.nullish()
|
||||
.transform((str) => (str ? str : ""))
|
||||
|
||||
export const nullableStringEmailValidator = z
|
||||
.string()
|
||||
.email()
|
||||
.nullish()
|
||||
.transform((str) => (str ? str : ""))
|
||||
|
||||
export const nullableStringUrlValidator = z
|
||||
.string()
|
||||
.url()
|
||||
.nullish()
|
||||
.transform((str) => (str ? str : ""))
|
||||
Reference in New Issue
Block a user