ricardobalk / ricardobalk.nl

My personal website, made with Nuxt 3 (Vue 3), TypeScript & TailwindCSS
https://ricardobalk.nl
ISC License
0 stars 0 forks source link

Refactor breadcrumbs functionality #86

Closed ricardobalk closed 3 years ago

ricardobalk commented 3 years ago

The breadcrumbs functionality, shown below, was written by someone else and is not up to my preferred standards. Don't get me wrong, it works, but it can be done in a much better way. I would like to have the code in TypeScript rather than JavaScript.

https://github.com/ricardobalk/website/blob/1c10d86a4d81e4062f16b44ba75d34f771b3e333/src/.vuepress/theme/components/Breadcrumbs.js#L1-L25

There are some lines that caught my attention, for example...

It would be much better to sanitise the string first, in other words - remove trailing slashes, before using split and automatically converting it into an array. That way, we can get rid of the popping. Clean input, clean output. šŸ‘šŸ¼

Something like:

const currentPage = (this.$page.path.charAt(parts.length - 1) === '/') ? this.$page.path.slice(0, -1) : this.$page.path; // Current page, relative to domain, without trailing slash.
const parts = currentPage.split("/");
// ...

Maybe even ES6+:

const currentPage = this.$page.path.endsWith('/') ? this.$page.path.slice(0, -1) : this.$page.path; // Current page, relative to domain, without trailing slash.
const parts = currentPage.split("/");

There are many more things I see in this small piece of code that can be written in a more efficient way. So let's refactor it.