From d265df7746f490e9971686dd092ef203b39f1822 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Fri, 3 Jun 2022 21:56:36 +0800 Subject: [PATCH] feat(shared): add slash utils --- packages/shared/src/client/index.ts | 4 ++++ packages/shared/src/shared/utils/url.ts | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/shared/src/client/index.ts b/packages/shared/src/client/index.ts index 4aa8fd935..c802b1615 100644 --- a/packages/shared/src/client/index.ts +++ b/packages/shared/src/client/index.ts @@ -7,5 +7,9 @@ export { getTag, isAbsoluteUrl, isUrl, + ensureEndingSlash, + ensureLeadingSlash, + removeEndingSlash, + removeLeadingSlash, timeTransformer, } from "../shared"; diff --git a/packages/shared/src/shared/utils/url.ts b/packages/shared/src/shared/utils/url.ts index 20079c0e0..4ca92d8ad 100644 --- a/packages/shared/src/shared/utils/url.ts +++ b/packages/shared/src/shared/utils/url.ts @@ -22,3 +22,15 @@ export const isUrl = (test: string): boolean => { }; export const isAbsoluteUrl = (test: string): boolean => test.startsWith("/"); + +export const ensureEndingSlash = (str: string): string => + str.replace(/\/?$/, "/"); + +export const ensureLeadingSlash = (str: string): string => + str.replace(/^\/?/, "/"); + +export const removeEndingSlash = (str: string): string => + str.replace(/\/$/, ""); + +export const removeLeadingSlash = (str: string): string => + str.replace(/^\//, "");