Skip to content

Commit

Permalink
chore: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
halvaradop committed Oct 2, 2024
1 parent 713c5df commit ce3e58d
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions docs/components/Code/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ const allFrameworks = {
[ExpressCode.name]: "Express",
}

/**
* Replace all non-alphabetic characters with a hyphen
*
* @param url - URL to parse
* @returns - A string parsed from the URL
*/
const parseParams = (url: string): string => {
return url
.toLowerCase()
.replace(/\s/g, "")
.replaceAll(/\(/g, "-")
.replaceAll(/\)/g, "")
let parsedUrl = url.toLowerCase().replaceAll(/[^a-zA-z]+/g, "-")

Check warning

Code scanning / CodeQL

Overly permissive regular expression range Medium documentation

Suspicious character range that overlaps with a-z in the same character class, and is equivalent to [A-Z[]^_`a-z].
return parsedUrl.endsWith("-") ? parsedUrl.slice(0, -1) : parsedUrl
}

export function Code({ children }: ChildrenProps) {
Expand Down Expand Up @@ -68,23 +71,15 @@ export function Code({ children }: ChildrenProps) {
}

useEffect(() => {
const getCookitFramework = window.localStorage.getItem(AUTHJS_TAB_KEY)
if (!getCookitFramework) {
updateFrameworkStorage("next.js")
const length = Object.keys(renderedFrameworks).length
const getFrameworkStorage = window.localStorage.getItem(AUTHJS_TAB_KEY)
const indexFramework = parseInt(getFrameworkStorage ?? "0") % length
if (!getFrameworkStorage) {
window.localStorage.setItem(AUTHJS_TAB_KEY, "0")
} else {
const indexCookieFramework = parseInt(getCookitFramework)
if (indexCookieFramework > Object.keys(renderedFrameworks).length - 1) {
updateFrameworkStorage("next.js")
window.localStorage.setItem(AUTHJS_TAB_KEY, "0")
} else {
updateFrameworkStorage(
parseParams(
Object.values(renderedFrameworks)[parseInt(getCookitFramework)]
)
)
}
}
updateFrameworkStorage(
parseParams(Object.values(renderedFrameworks)[indexFramework])
)
}, [router.pathname, renderedFrameworks])

return (
Expand Down

0 comments on commit ce3e58d

Please sign in to comment.