import get from "lodash.get"; import { WordpressPodcastEpisode, WordpressBlogPost, WordpressSingleBlogPostResponse } from "typings/custom"; export const toJson = (value: string | object | null) => typeof value === "string" ? JSON.parse(value) : value; export const normalizeToArray = ( value: string[] | string | null, splitKey: string = "," ): Array => { if (!value) { return []; } if (typeof value === "string") { try { value = JSON.parse(value); } catch (e) { value = (value as string).split(splitKey); } } return (Array.isArray(value) ? value : [value]) as Array; }; const getLength = (duration: string) => { if (!duration || !duration.includes(":")) { return undefined; } return `${get(duration.split(":"), "[1]")} min`; }; export const normalizeEpisode = ({ url, id, title, description, date, podcastDuration }: WordpressPodcastEpisode) => ({ body: description, datetime: date, href: url, id, length: getLength(podcastDuration), title }); export const normalizePost = ({ authors = [], date, description, featuredImage, id, title, url }: WordpressBlogPost) => ({ body: description, datetime: date, href: url, authors: authors, imgAlt: "Blogpost image", imgSrc: featuredImage, id, title }); export const normalizeSinglePostResponse = ({ id, authors = [], date, link, featured_image, yoast_head_json }: WordpressSingleBlogPostResponse) => ({ body: yoast_head_json?.description, datetime: date, href: link, authors: authors, imgAlt: "Blogpost image", imgSrc: featured_image, id, title: yoast_head_json?.title }); export const normalizeBoolean = ( value: string | boolean | number | undefined ): boolean => (typeof value === "string" ? value === "true" : !!value); /** * Cleans characters that are invalid for DOM IDs * @param {string} id id to normalize */ export const normalizeDomId = (id: string): string => { return id.replace(/^[^a-z]+|[^\w.-]+/gi, ""); }; export const safeDecodeURI = (value: string): string => { try { // Check if the value contains URI encoding patterns return typeof value === "string" && value.includes("%") ? decodeURIComponent(value) : value; } catch (error) { // In case of any errors, return the original value return value; } };