import isEmpty from './isEmpty'

interface Stringify {
  (v: any): string
}

const stringify: Stringify = (v) => {
  if (isEmpty(v)) {
    return ''
  }

  if (Array.isArray(v)) {
    return v
      .filter((v) => !isEmpty(v))
      .map((v) => stringify(v))
      .join(',')
  }

  const rawStringified = String(v)

  if (rawStringified === '[object Object]') {
    return JSON.stringify(v)
  }

  return rawStringified
}

export default stringify
