Skip to content

Commit

Permalink
feat: use helpers for routes, and DTO for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed May 23, 2023
1 parent cb646dc commit d51be19
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 184 deletions.
72 changes: 0 additions & 72 deletions assets/js/RouterHelper.js

This file was deleted.

23 changes: 23 additions & 0 deletions assets/js/RouterHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Tag from 'assets/js/tag.dto'
import { RouteLocationRaw } from 'vue-router'

export function generatePostsRoute(domain?: string, page?: number, tags?: Tag[]) {
const route: RouteLocationRaw = {
path: '/posts',
query: {}
}

if (domain != null) {
route.query.domain = domain
}

if (page != null) {
route.query.page = page.toString()
}

if (tags != null && Array.isArray(tags) && tags.length) {
route.query.tags = tags.map((tag) => encodeURI(tag.name)).join('|')
}

return route
}
39 changes: 0 additions & 39 deletions assets/js/SeoHelper.js

This file was deleted.

40 changes: 40 additions & 0 deletions assets/js/SeoHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Tag from 'assets/js/tag.dto'

export function tagArrayToTitle(tags: Tag[]) {
if (!tags.length) {
return null
}

return (
tags
.map((tag) => tag.name)
// .map((tag) => capitalize(tag))
.map((tag) => normalizeStringForTitle(tag))
.join(', ')
)
}

export function normalizeStringForTitle(string: string) {
if (!string) {
return null
}

string = string.trim()

// Delete negative tag
if (string.startsWith('-')) {
return null
}

// Replace underscores with spaces
string = string.replace(/_/g, ' ')

// Delete parentheses
string = string.replace(/\(/g, '')
string = string.replace(/\)/g, '')

// Delete colons
string = string.replace(/:/g, '')

return string
}
17 changes: 17 additions & 0 deletions assets/js/tag.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface ITag {
name: string
count?: number
}

// For default values
export class TagDTO implements ITag {
name: string = ''
count?: number
}

export default class Tag extends TagDTO {
constructor(dto: TagDTO) {
super()
Object.assign(this, dto)
}
}
Loading

0 comments on commit d51be19

Please sign in to comment.