UNPKG

@scayle/storefront-core

Version:

Collection of essential utilities to work with the Storefront API

107 lines (106 loc) 3.52 kB
export const getPayloadDate = (date) => { if (!date) { return date; } const inputDateParts = date.split("."); return `${inputDateParts[2]}-${inputDateParts[1]}-${inputDateParts[0]}`; }; export const dateValidator = (date) => { if (!date) { return true; } const formattedDate = getPayloadDate(date); if (!formattedDate) { return false; } const inputDateParts = formattedDate.split("-").map((datePart) => +datePart); const [year, month, day] = inputDateParts; if (day <= 0 || day > 31 || month <= 0 || month > 12 || year < 1900) { return false; } const convertedDate = new Date(formattedDate); return year === convertedDate.getFullYear() && month === convertedDate.getMonth() + 1 && day === convertedDate.getDate(); }; export const formatDate = (date) => { if (!date) { return ""; } const [datePart] = date.split("T", 2); const splitDateArray = datePart?.split("-", 3); if (splitDateArray?.length !== 3) { return ""; } const [yyyy, mm, dd] = splitDateArray; return `${dd.padStart(2, "0")}.${mm.padStart(2, "0")}.${yyyy}`; }; export const convertDate = (date) => { if (!date) { return ""; } const splitDateArray = date?.split(".", 3); if (splitDateArray?.length !== 3) { return ""; } const [dd, mm, yyyy] = splitDateArray; return `${dd.padStart(2, "0")}.${mm.padStart(2, "0")}.${yyyy}`; }; export const getPayloadPhone = (phone) => { if (!phone) { return phone; } const inputPhoneParts = phone.split(" "); return `${inputPhoneParts[0].replace("+", "00")}/${[ ...inputPhoneParts?.[1]?.trim() || [], ...inputPhoneParts?.[2]?.trim() || [] ].join("")}`; }; export const phoneValidator = (phone) => { if (!phone) { return true; } const inputPhoneParts = getPayloadPhone(phone)?.split("/") || []; if (inputPhoneParts.length <= 1) { return false; } return inputPhoneParts[1]?.length > 4; }; export const formatPhone = (phone) => { if (!phone) { return ""; } const inputPhoneParts = phone.split("/"); if (inputPhoneParts.length === 1) { return phone; } const internationalCode = `+${+inputPhoneParts[0]}`; const number = `${inputPhoneParts[1].substring( 0, 4 )} ${inputPhoneParts[1].substring(4, inputPhoneParts[1].length)}`; return `${internationalCode} ${number}`; }; export const convertPhone = (phone, internationalPrefix = "49") => { if (!phone) { return phone; } const phoneNumber = []; if (phone.startsWith(`+${internationalPrefix}`) || phone.startsWith(`00${internationalPrefix}`)) { phoneNumber.push(`+${internationalPrefix}`); const remaining = phone.startsWith(`+${internationalPrefix}`) ? phone.split(`+${internationalPrefix}`)[1]?.split(" ")?.join("") : phone.split(`00${internationalPrefix}`)[1]?.split(" ")?.join(""); phoneNumber.push(remaining.substring(0, 4)); phoneNumber.push(remaining.substring(4, remaining.length)); return phoneNumber.join(" "); } else if (phone.startsWith("0")) { phoneNumber.push(`+${internationalPrefix}`); const remaining = phone.substring(1)?.split(" ")?.join(""); phoneNumber.push(remaining.substring(0, 4)); phoneNumber.push(remaining.substring(4, remaining.length)); return phoneNumber.join(" "); } else { phoneNumber.push(`+${internationalPrefix}`); const remaining = phone?.split(" ")?.join(""); phoneNumber.push(remaining.substring(0, 4)); phoneNumber.push(remaining.substring(4, phone.length)); return phoneNumber.join(" "); } };