Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

items should be gneric not any and the output should be the same type as the inp... #136

Open
github-actions bot opened this issue Nov 7, 2023 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Nov 7, 2023

items should be gneric not any and the output should be the same type as the input

// TODO items should be gneric not any and the output should be the same type as the input

import type { SortProperty, StatusFilter } from '$lib/types/entry'
import type { SortDirection } from '$lib/types/enums'
import { format } from 'date-fns'

// solution inspired by https://www.designcise.com/web/tutorial/how-to-fix-replaceall-is-not-a-function-javascript-error
// implementation inspired by https://futurestud.io/tutorials/node-js-string-replace-all-appearances
// TODO replace with magic regex
export function getSlug(str: string) {
  if (!str) return ''
  const slug = str
    .trim()
    .toLowerCase()
    // remove all chars which aren't characters, numbers or spaces
    .replace(/[^a-zA-Z0-9\s]+/g, '')
    // replace all spaces with dashes
    .replace(/\s+/g, '-')
  return slug
}

// Won't be tested
// TODO items should be gneric not any and the output should be the same type as the input
export function getRandomItems(items: any[], amount: number) {
  return items.sort(() => 0.5 - Math.random()).slice(0, amount)
}

export function formatDate(date: Date): string {
  return format(new Date(date), 'yyyy-MM-dd')
}

export function sortAlphabetical(a: string, b: string): number {
  return a.localeCompare(b)
}

export function sortDate(a: Date, b: Date): number {
  return new Date(b).valueOf() - new Date(a).valueOf()
}

export function sortNumber(a: number, b: number): number {
  return b - a
}

export function enumToArray(rawEnum: SortDirection | StatusFilter | SortProperty): { display: string; key: string }[] {
  return Object.keys(rawEnum).map((key) => {
    return {
      display: key,
      key: rawEnum[key]
    }
  })
}

export function setParam(key: string, value: string) {
  const url = new URL(window.location.toString())
  url.searchParams.set(key, value)
  window.history.pushState({}, '', url.href)
}

2d25563af7bb9d2de4a8bc0c6593726558267551

@github-actions github-actions bot added the todo label Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants