<commit_before>// @flow
import React from "react";
import { EditorState } from "draft-js";

type Props = {|
  getEditorState: () => EditorState,
  // eslint-disable-next-line @thibaudcolas/cookbook/react/require-default-props
  maxLength?: ?number,
|};

// const countParagraphs = (str) => (str ? str.match(/\n+/g).length + 1 : 0);
// const countSentences = (str) =>
//   str ? (str.match(/[.?!…]+./g) || []).length + 1 : 0;
// const countWords = (str) =>
//   str ? (str.replace(/['";:,.?¿\-!¡]+/g, "").match(/\S+/g) || []).length : 0;
/**
 * Count characters in a string, with special processing to account for astral symbols in UCS-2. See:
 * - https://github.com/RadLikeWhoa/Countable/blob/master/Countable.js#L29
 * - https://mathiasbynens.be/notes/javascript-unicode
 */
const countChars = (str) => {
  if (str) {
    // Impossible for `match` to be null here since this matches all characters.
    // $FlowFixMe
    return str.match(/./gu).length;
  }

  return 0;
};

/**
 * Shows the editor’s character count, with a calculation of unicode characters
 * matching that of `maxlength` attributes.
 */
const CharCount = ({ getEditorState, maxLength = null }: Props) => {
  const editorState = getEditorState();
  const content = editorState.getCurrentContent();
  const text = content.getPlainText();
  // $FlowFixMe
  const suffix = typeof maxLength !== "undefined" ? `/${maxLength}` : "";

  return (
    <div className="Draftail-ToolbarButton CharCount">{`${countChars(
      text,
    )}${suffix}`}</div>
  );
};

export default CharCount;<commit_msg>Fix CharCount display value<commit_after>// @flow
import React from "react";
import { EditorState } from "draft-js";

type Props = {|
  getEditorState: () => EditorState,
  // eslint-disable-next-line @thibaudcolas/cookbook/react/require-default-props
  maxLength?: ?number,
|};

// const countParagraphs = (str) => (str ? str.match(/\n+/g).length + 1 : 0);
// const countSentences = (str) =>
//   str ? (str.match(/[.?!…]+./g) || []).length + 1 : 0;
// const countWords = (str) =>
//   str ? (str.replace(/['";:,.?¿\-!¡]+/g, "").match(/\S+/g) || []).length : 0;
/**
 * Count characters in a string, with special processing to account for astral symbols in UCS-2. See:
 * - https://github.com/RadLikeWhoa/Countable/blob/master/Countable.js#L29
 * - https://mathiasbynens.be/notes/javascript-unicode
 */
const countChars = (str) => {
  if (str) {
    // Impossible for `match` to be null here since this matches all characters.
    // $FlowFixMe
    return str.match(/./gu).length;
  }

  return 0;
};

/**
 * Shows the editor’s character count, with a calculation of unicode characters
 * matching that of `maxlength` attributes.
 */
const CharCount = ({ getEditorState, maxLength = null }: Props) => {
  const editorState = getEditorState();
  const content = editorState.getCurrentContent();
  const text = content.getPlainText();
  // $FlowFixMe
  const suffix = maxLength ? `/${maxLength}` : "";

  return (
    <div className="Draftail-ToolbarButton CharCount">{`${countChars(
      text,
    )}${suffix}`}</div>
  );
};

export default CharCount;
{'fraction_non_alphanumeric': 0.18535175072277546, 'fraction_numerical': 0.0057822036620623195, 'mean_word_length': 3.4677187948350072, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7637306', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1026, 'n_words': 256}