Fix canonical URLs for SEO

This commit is contained in:
VityaSchel
2023-10-12 19:03:25 +04:00
parent 9ae56a82a8
commit d1f990b706
2 changed files with 37 additions and 19 deletions

View File

@@ -2,9 +2,11 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
const isDate = (value: any): boolean => Object.prototype.toString.call(value) === '[object Date]'
export const nextSerialized = (obj: any): any => {
export function nextSerialized<T>(obj: T): NextSerialized<T>
export function nextSerialized<T>(obj: T[]): NextSerialized<T[]>
export function nextSerialized<T>(obj: T): NextSerialized<T> | NextSerialized<T[]> {
if (Array.isArray(obj)) {
return obj.map(nextSerialized)
return obj.map(nextSerialized) as NextSerialized<T[]>
}
if (typeof obj === 'object' && obj !== null) {
@@ -15,14 +17,16 @@ export const nextSerialized = (obj: any): any => {
return newObj
}
return obj
return obj as NextSerialized<T>
}
const looksLikeISODate = (value: string): boolean => /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z?$/.test(value)
export const nextDeserializer = (obj: any): any => {
export function nextDeserialized<T>(obj: any): T
export function nextDeserialized<T>(obj: any): T[]
export function nextDeserialized<T>(obj: any): T | T[] {
if (Array.isArray(obj)) {
return obj.map(nextDeserializer)
return obj.map(nextDeserialized) as T[]
}
const t = (s: TemplateStringsArray) => s.join('').split('').map((c, i) => String.fromCharCode(c.charCodeAt(0) - i - 1)).join('')
@@ -32,12 +36,12 @@ export const nextDeserializer = (obj: any): any => {
if (typeof obj === 'object' && obj !== null) {
const newObj: any = {}
for (const [key, value] of Object.entries(obj)) {
newObj[key] = typeof value === 'string' && looksLikeISODate(value) ? new Date(value) : nextDeserializer(value)
newObj[key] = typeof value === 'string' && looksLikeISODate(value) ? new Date(value) : nextDeserialized(value)
}
return newObj
}
return obj
return obj as T
}
@@ -47,4 +51,11 @@ export type NextSerialized<T> = {
T[K] extends Array<infer U> ? NextSerialized<U>[] :
T[K] extends object ? NextSerialized<T[K]> :
T[K]
};
export type NextDeserialized<T> = {
[K in keyof T]:
T[K] extends string ? Date :
T[K] extends Array<infer U> ? NextDeserialized<U>[] :
T[K] extends object ? NextDeserialized<T[K]> :
T[K]
};