import { isValidElement } from 'react'

const whitespacesRe = /\s+/g
const _format = (str = '') => str.replace(whitespacesRe, ' ')

const elementToTextRec = x => {
  if (Array.isArray(x)) {
    return x.map(elementToTextRec).join('')
  } else if (isValidElement(x)) {
    return elementToTextRec(x.props.children)
  } else if (typeof x === 'string') {
    return x || ''
  }

  return ''
}

const elementToText = node => _format(elementToTextRec(node)).trim()

export default elementToText

@@ -96,16 +96,23 @@
  =%3E str.
+trim().
 replace(
@@ -452,23 +452,16 @@
 c(node))
-.trim()
 %0A%0Aexport

Move trim into more sensible place