45 lines
907 B
TypeScript
45 lines
907 B
TypeScript
// utils/urlEncoder.ts
|
|
|
|
export const encodeData = (url: string): string => {
|
|
if (url.startsWith("https%3A%2F%2F")) {
|
|
return url;
|
|
}
|
|
|
|
return encodeURIComponent(url);
|
|
};
|
|
|
|
export const decodeData = (url: string): string => {
|
|
if (url.startsWith("https://")) {
|
|
return url;
|
|
}
|
|
|
|
const str = decodeURIComponent(url);
|
|
|
|
if (str.startsWith("https%3A%2F%2F")) {
|
|
return decodeData(str);
|
|
}
|
|
|
|
return decodeURIComponent(url);
|
|
};
|
|
|
|
export const encodeKvURL = (url: string): string => {
|
|
if (url.startsWith("https%3A%2F%2F")) {
|
|
return url;
|
|
}
|
|
|
|
return encodeURIComponent(url).replace(
|
|
/[!'()*]/g,
|
|
(c) => "%" + c.charCodeAt(0).toString(16).toUpperCase(),
|
|
);
|
|
};
|
|
|
|
export const decodeKvURL = (encodedURL: string): string => {
|
|
const str = decodeURIComponent(encodedURL.replace(/\+/g, " "));
|
|
|
|
if (str.startsWith("https%3A%2F%2F")) {
|
|
return decodeKvURL(str);
|
|
}
|
|
|
|
return str;
|
|
};
|