import { ReadConverter } from '../types/index'
import { readName, readValue } from './converter'

type GetReturn<T, R> = [T] extends [undefined]
  ? object & { [property: string]: string }
  : R | undefined

export default function <T extends string | undefined>(
  key: T,
  converter: ReadConverter<any> = readValue
): GetReturn<T, typeof converter> {
  // To prevent the for loop in the first place assign an empty array
  // in case there are no cookies at all.
  const cookies: string[] =
    document.cookie.length > 0 ? document.cookie.split('; ') : []
  const jar: any = {}
  for (let i = 0; i < cookies.length; i++) {
    const parts: string[] = cookies[i].split('=')
    const value: string = parts.slice(1).join('=')

    try {
      const foundKey: string = readName(parts[0])
      jar[foundKey] = converter(value, foundKey)

      if (key === foundKey) {
        break
      }
    } catch (e) {}
  }

  return key != null ? jar[key] : jar
}

import { ReadConverter } from '../types/index'
import { readName, readValue } from './converter'

type GetReturn<T, R> = [T] extends [undefined]
  ? object & { [property: string]: string }
  : R | undefined

export default function <T extends string | undefined>(
  key: T,
  converter: ReadConverter<any> = readValue
): GetReturn<T, typeof converter> {
  const scan = /(?:^|; )([^=]*)=([^;]*)/g
  const jar: any = {}
  let match
  while ((match = scan.exec(document.cookie)) != null) {
    try {
      const foundKey = readName(match[1])
      const value = converter(match[2], foundKey)
      jar[foundKey] = value
      if (key === foundKey) {
        break
      }
    } catch (e) {}
  }

  return key != null ? jar[key] : jar
}

Change cookie lookup to using regex
Change cookie lookup to using regex

Now we don't need to consider an edge case in rather outdated Internet
Explorer we can safely use a regex for this task.

Also reduces the module size..
