<commit_before>/**
 * @copyright   2016, <NAME>
 * @license     https://opensource.org/licenses/MIT
 * @flow
 */

import React from 'react';
import Matcher from '../Matcher';
import Emoji from '../components/Emoji';
import { SHORTNAME_TO_UNICODE, EMOJI_PATTERN, EMOJI_SHORTNAME_PATTERN } from '../data/emoji';

import type { MatchResponse, EmojiProps, EmojiOptions } from '../types';

const EMOJI_REGEX = new RegExp(EMOJI_PATTERN);
const EMOJI_SHORTNAME_REGEX = new RegExp(EMOJI_SHORTNAME_PATTERN, 'i');

export default class EmojiMatcher extends Matcher<EmojiOptions> {
  options: EmojiOptions;

  /**
   * {@inheritDoc}
   */
  factory(match: string, props: Object = {}): React.Element<EmojiProps> {
    if (this.options.renderUnicode) {
      return props.unicode;
    }

    return (
      <Emoji {...props} />
    );
  }

  /**
   * @{inheritDoc}
   */
  getTagName(): string {
    return 'span';
  }

  /**
   * {@inheritDoc}
   */
  match(string: string): ?MatchResponse {
    let response = null;

    // Should we convert shortnames to unicode?
    if (this.options.convertShortName && string.indexOf(':') >= 0) {
      response = this.doMatch(string, EMOJI_SHORTNAME_REGEX, matches => ({
        shortName: matches[0].toLowerCase(),
      }));

      if (response) {
        const unicode = SHORTNAME_TO_UNICODE[response.shortName];

        // Invalid shortname
        if (!unicode) {
          response = null;

        // We want to render using the unicode value
        } else {
          response.unicode = unicode;
        }
      }
    }

    // Should we convert unicode to SVG/PNG?
    if (this.options.convertUnicode && !response) {
      response = this.doMatch(string, EMOJI_REGEX, matches => ({
        unicode: matches[0],
      }));
    }

    return response;
  }
}<commit_msg>Updated EmojiMatcher to verify the unicode character is legit.<commit_after>/**
 * @copyright   2016, <NAME>
 * @license     https://opensource.org/licenses/MIT
 * @flow
 */

import React from 'react';
import Matcher from '../Matcher';
import Emoji from '../components/Emoji';
import {
  EMOJI_PATTERN,
  EMOJI_SHORTNAME_PATTERN,
  SHORTNAME_TO_UNICODE,
  UNICODE_TO_SHORTNAME,
} from '../data/emoji';

import type { MatchResponse, EmojiProps, EmojiOptions } from '../types';

const EMOJI_REGEX = new RegExp(EMOJI_PATTERN);
const EMOJI_SHORTNAME_REGEX = new RegExp(EMOJI_SHORTNAME_PATTERN, 'i');

export default class EmojiMatcher extends Matcher<EmojiOptions> {
  options: EmojiOptions;

  /**
   * {@inheritDoc}
   */
  factory(match: string, props: Object = {}): React.Element<EmojiProps> {
    if (this.options.renderUnicode) {
      return props.unicode;
    }

    return (
      <Emoji {...props} />
    );
  }

  /**
   * @{inheritDoc}
   */
  getTagName(): string {
    return 'span';
  }

  /**
   * {@inheritDoc}
   */
  match(string: string): ?MatchResponse {
    let response = null;

    // Should we convert shortnames to unicode?
    if (this.options.convertShortName && string.indexOf(':') >= 0) {
      response = this.doMatch(string, EMOJI_SHORTNAME_REGEX, matches => ({
        shortName: matches[0].toLowerCase(),
      }));

      if (response) {
        const unicode = SHORTNAME_TO_UNICODE[response.shortName];

        // Invalid shortname
        if (!unicode) {
          response = null;

        // We want to render using the unicode value
        } else {
          response.unicode = unicode;
        }
      }
    }

    // Should we convert unicode to SVG/PNG?
    if (this.options.convertUnicode && !response) {
      response = this.doMatch(string, EMOJI_REGEX, matches => ({
        unicode: matches[0],
      }));

      if (response && !UNICODE_TO_SHORTNAME[response.unicode]) {
        return null;
      }
    }

    return response;
  }
}
{'fraction_non_alphanumeric': 0.13527080581241743, 'fraction_numerical': 0.0036988110964332895, 'mean_word_length': 2.6721629485935985, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '5410171', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1231, 'n_words': 272}