diff --git a/src/client/navigation.ts b/src/client/navigation.ts index 9f296b8..66f535b 100644 --- a/src/client/navigation.ts +++ b/src/client/navigation.ts @@ -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'); @@ -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)); } diff --git a/src/common/slug.ts b/src/common/slug.ts index eff61f1..0983da5 100644 --- a/src/common/slug.ts +++ b/src/common/slug.ts @@ -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; } }