export const slugify = (str: string) =>
  str
    .trim()
    .toString()
    .toLowerCase()
    .replace(/\s+/g, '-')
    .replace(/\+/g, '-p')
    .replace(/#/g, '-sharp')
    .replace(/[^\w\-]+/g, '')

export const shorten = (name: string) => {
  let n = slugify(name).replace('script', '-s').replace('python', 'py').replace('markdown', 'md')
  const nov = (s: string) => s[0] + s.substr(1).replace(/[aeiouy-]/g, '')
  if (n.replace(/-/g, '').length <= 4) {
    return n.replace(/-/g, '')
  }
  if (n.split('-').length >= 2) {
    return n
      .split('-')
      .map((x) => nov(x.substr(0, 2)))
      .join('')
      .substr(0, 4)
  }
  n = nov(n)
  if (n.length <= 4) {
    return n
  }
  return n.substr(0, 2) + n.substr(n.length - 2, 2)
}

export const byId = (id: string) => document.getElementById(id)
