Skip to content

Commit

Permalink
feat: Make Slug.fromString static
Browse files Browse the repository at this point in the history
  • Loading branch information
ozxybox committed Jan 10, 2024
1 parent f228427 commit 6d2defe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/client/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ function linkClickHandler(e) {
if (url.host === location.host) {
document.body.classList.remove('nav-show');

navigate(new Slug().fromString(url.pathname.slice(1)));
navigate(Slug.fromString(url.pathname.slice(1)));

if (e.currentTarget.parentNode.classList.contains('categories') && e.currentTarget.innerText !== 'Home') {
document.body.classList.add('nav-showTopics', 'nav-show');
Expand Down Expand Up @@ -246,5 +246,5 @@ function updateAllLinkListeners() {
* Returns the current location as a slug
*/
export function getLocationSlug(): Slug {
return new Slug().fromString(location.pathname.slice(1));
return Slug.fromString(location.pathname.slice(1));
}
25 changes: 16 additions & 9 deletions src/common/slug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,27 @@ export class Slug {
return str;
}

/**
* Clones the slug into a new object
* @returns A new slug with the same data
*/
clone(): Slug {
return new Slug(this.game, this.category, this.topic, this.article);
}

/**
* Separates the slug into an easily digestible object
* @param slug The slug you are trying to parse
* @param str The slug you are trying to parse
* @returns A representation of the path as a slug
*/
fromString(slug: string): Slug {
static fromString(str: string): Slug {
const slug = new Slug();

// Split the string and use it as our value
const split = slug.split('/');
this.set(split[0], split[1], split[2], split[3]);
const split = str.split('/');
slug.set(split[0], split[1], split[2], split[3]);

// Return this for ease of use
return this;
}

clone(): Slug {
return new Slug(this.game, this.category, this.topic, this.article);
return slug;
}
}

0 comments on commit 6d2defe

Please sign in to comment.