<commit_before>// @flow
import React from 'react';
import { map, reduce } from 'lodash';

import saveFile from '@ncigdc/utils/filesaver';
import { mapStringArrayToTsvString } from '@ncigdc/utils/toTsvString';
import Button from '@ncigdc/uikit/Button';
import { visualizingButton } from '@ncigdc/theme/mixins';
import { Tooltip } from '@ncigdc/uikit/Tooltip';

type TProps = {
  selector: string,
  filename: string,
  style: Object,
};

const getSingleHeader = (headThs: Array<NodeList>) =>
  reduce(
    headThs[0],
    (acc, th) =>
      th.rowSpan === 2 ? [...acc, th] : [...acc, ...map(headThs[1], t => t)],
    [],
  );

const DownloadTableToTsvButton = ({ filename, selector, style = {} }: TProps) =>
  <Tooltip Component={<span>Export current view</span>}>
    <Button
      style={{ ...visualizingButton, ...style }}
      onClick={() => {
        const tableEl = document.querySelector(selector);
        const headTrs = tableEl.querySelector('thead').querySelectorAll('tr');
        const headThs = map(headTrs, h => h.querySelectorAll('th'));
        const thEls = headThs.length === 2
          ? getSingleHeader(headThs)
          : tableEl.querySelectorAll('th');
        const thText = map(thEls, el => el.innerText).map(t =>
          t.replace('\n', ' '),
        );
        const trs = tableEl.querySelector('tbody').querySelectorAll('tr');
        const tdText = map(trs, t => {
          const tds = t.querySelectorAll('td');
          return reduce(
            tds,
            (acc, td) => {
              const markedForTsv = td.querySelector('.for-tsv-export');
              const exportText = markedForTsv
                ? markedForTsv.innerText
                : td.innerText;
              const joinedText = exportText
                .trim()
                .replace(/\u00A0/g, ' ')
                .split('\n')
                .join(',');
              const colspan = td.getAttribute('colspan');
              const fittedToColspan = colspan
                ? [joinedText, ...Array(colspan - 1)]
                : [joinedText];
              return [...acc, ...fittedToColspan];
            },<commit_msg>🐛 [PRTL-1372] remove more whitespace for tsv download<commit_after>// @flow
import React from 'react';
import { map, reduce } from 'lodash';

import saveFile from '@ncigdc/utils/filesaver';
import { mapStringArrayToTsvString } from '@ncigdc/utils/toTsvString';
import Button from '@ncigdc/uikit/Button';
import { visualizingButton } from '@ncigdc/theme/mixins';
import { Tooltip } from '@ncigdc/uikit/Tooltip';

type TProps = {
  selector: string,
  filename: string,
  style: Object,
};

const getSingleHeader = (headThs: Array<NodeList>) =>
  reduce(
    headThs[0],
    (acc, th) =>
      th.rowSpan === 2 ? [...acc, th] : [...acc, ...map(headThs[1], t => t)],
    [],
  );

const DownloadTableToTsvButton = ({ filename, selector, style = {} }: TProps) =>
  <Tooltip Component={<span>Export current view</span>}>
    <Button
      style={{ ...visualizingButton, ...style }}
      onClick={() => {
        const tableEl = document.querySelector(selector);
        const headTrs = tableEl.querySelector('thead').querySelectorAll('tr');
        const headThs = map(headTrs, h => h.querySelectorAll('th'));
        const thEls = headThs.length === 2
          ? getSingleHeader(headThs)
          : tableEl.querySelectorAll('th');
        const thText = map(thEls, el => el.innerText).map(t =>
          t.replace(/\s+/g, ' '),
        );
        const trs = tableEl.querySelector('tbody').querySelectorAll('tr');
        const tdText = map(trs, t => {
          const tds = t.querySelectorAll('td');
          return reduce(
            tds,
            (acc, td) => {
              const markedForTsv = td.querySelector('.for-tsv-export');
              const exportText = markedForTsv
                ? markedForTsv.innerText
                : td.innerText;
              const joinedText = exportText
                .trim()
                .split(/\s*\n\s*/)
                .join(',')
                .replace(/[\s\u00A0]+/g, ' ');
              const colspan = td.getAttribute('colspan');
              const fittedToColspan = colspan
                ? [joinedText, ...Array(colspan - 1)]
                : [joinedText];
              return [...acc, ...fittedToColspan];
            },
{'fraction_non_alphanumeric': 0.15585315169706765, 'fraction_numerical': 0.004617871161394597, 'mean_word_length': 2.162043795620438, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5525224', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1320, 'n_words': 279}