{"size": 518, "ext": "js", "max_stars_repo_path": "utils/elementToText.js", "max_stars_repo_name": "theo-mesnil/styled-components-website", "max_stars_repo_stars_event_min_datetime": "2017-04-18T19:31:16.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-26T02:14:50.000Z", "max_issues_repo_path": "utils/elementToText.js", "max_issues_repo_name": "theo-mesnil/styled-components-website", "max_issues_repo_issues_event_min_datetime": "2017-04-17T16:15:35.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-25T14:42:56.000Z", "max_forks_repo_path": "utils/elementToText.js", "max_forks_repo_name": "theo-mesnil/styled-components-website", "max_forks_repo_forks_event_min_datetime": "2017-04-16T23:36:37.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-24T11:20:05.000Z"}
{"max_stars_count": 635, "max_issues_count": 797, "max_forks_count": 549, "avg_line_length": 24.6666666667, "max_line_length": 69, "alphanum_fraction": 0.638996139}
import { isValidElement } from 'react';

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

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

  return '';
};

const elementToText = (node) => _format(elementToTextRec(node));

export default elementToText;
