Source: app/util/messageParser/detectTag.js

import { SEGMENT_TAG, SEGMENT_URL } from '../../data/enum/SegmentType';
import { TAG_REGEX } from '../../data/regexPatterns';

/**
 * Detects an user tagging in each SEGMENT_URL message segment provided to `input`. If a user tag
 * is detected, will convert it to a SEGMENT_TAG and attach a user tagging object.
 * @param input {Array<Object>} An array of message segments
 * @returns {Array<Object>} The processed array of message segments
 */
const detectTag = () => input =>
  input.map(segment => {
    const { type, data } = segment;

    switch (type) {
      case SEGMENT_URL: {
        if (TAG_REGEX.test(data.text)) {
          return {
            type: SEGMENT_TAG,
            data: {
              text: data.text,
            },
          };
        }

        return segment;
      }
      default:
        return segment;
    }
  });

export default detectTag;