<commit_before>(function ($) {
  $.fn.twitter = function (doi) {
    var tweet, tweetText, totalTweets, initData, minDisplayTweets, maxDisplayTweets, dataSort, datePrefix, dataPrefix, tweetDate, tweetDateOther, tweetInfo, tweetActionLink, tweetAvatar, tweetPlaceholder, tweetId, replyLink, retweetLink, favoriteLink, tweetActions, tweetUserName, tweetHandle, wholeTweet, listBody, tweetAvatarParse;

    function validateDOI(doi) {
      if (doi == null) {
        throw new Error('DOI is null.');
      }

      doi = encodeURI(doi);

      return doi.replace(new RegExp('/', 'g'), '%2F').replace(new RegExp(':', 'g'), '%3A');
    }

    this.getSidebarTweets = function (doi) {
      doi = validateDOI(doi);
      var config, requestUrl, errorText;

      config = ALM_CONFIG;
      // if twitter stream stops working, check the api for parameter changes: http://alm.plos.org/docs/api
      requestUrl = config.host + '?api_key=' + config.apiKey + '&ids=' + doi + '&info=detail&source_id=twitter';

      errorText = '<li>Our system is having a bad day. We are working on it. Please check back later.</li>';

      $.ajax({
        url:         requestUrl,
        dataType:    'jsonp',
        contentType: "text/json; charset=utf-8",
        type:        "GET"
      }).done(function (data) {

        initData = data.data[0];
        totalTweets = initData.sources[0].metrics.total;

        if (totalTweets === 0) {
        //no tweets to display, do nothing further
        } else {

          minDisplayTweets = 2;
          maxDisplayTweets = 5;
          dataSort = initData.sources[0].events;

          //parse the date to be able to sort by date
          this.parseTwitterDate = function (tweetdate) {
            //running regex to grab everything after the time
            var newdate = tweetdate.replace(/(\d{1,2}[:]\d{2}[:]\d{2}) (.*)/, '$2 $1');
            //moving the time code to the end
            newdate = newdate.replace(/(\+\S+) (.*)/, '$2 $1');

            return new Date(Date.parse(newdate));
          }
          //sort by date from Ambra
          this.sort_tweets_by_date = function (a, b) {
            var aDt = isNaN(a.event.created_at) ? this.parseTwitterDate(a.event.created_at) : a.event.created_at;
            var bDt = isNaN(b.event.created_at) ? this.parseTwitterDate(b.event.created_at) : b.event.created_at;

            return (new Date(bDt).getTime()) - (new Date(aDt).getTime());
          }
          //pull the data & run the sort function
          dataSort = dataSort.sort(jQuery.proxy(this.sort_tweets_by_date, this));
          // only show 5, so cut the json results to 5
          if (dataSort.length > maxDisplayTweets) {
            dataSort = dataSort.slice(0, 5);
          } else { }

          $.each(dataSort, function (index) {
            dataPrefix = dataSort[index].event;
            datePrefix = dataSort[index];
            //run through dataPass to get all the data
            dataPass(dataPrefix, datePrefix);
            //show only 2 and then 5
            if (index < minDisplayTweets) {
              wholeTweet = '<li>' + listBody + '</li>';
            } else {
              wholeTweet = '<li class="more-tweets">' + listBody + '</li>';
            }

            $('#tweetList').append(wholeTweet);
            checkAvatar(listBody);

          });

          //display tweets
          $('.twitter-container').css('display', 'block');

          // display the more tweets if there are any.
          if (totalTweets > minDisplayTweets) {
            var show_link = more_tweets();
            return show_link;
          }

        }

      }).fail(function () {
        $('.twitter-container').css('display', 'block');
        $('#tweetList').append(errorText);
      });

    };

    function checkAvatar(listappend) {
      var checkImg = $(listappend).find('.imgLoad');
      $(checkImg).on('error', changeAvatar);
    }

    function changeAvatar(event) {
      if (event) {
        var newthing = $(this).attr('src', tweetPlaceholder);
        return $('.imgholder').html(newthing);<commit_msg>DPRO-738: account for initData undefined<commit_after>(function ($) {
  $.fn.twitter = function (doi) {
    var tweet, tweetText, totalTweets, initData, minDisplayTweets, maxDisplayTweets, dataSort, datePrefix, dataPrefix, tweetDate, tweetDateOther, tweetInfo, tweetActionLink, tweetAvatar, tweetPlaceholder, tweetId, replyLink, retweetLink, favoriteLink, tweetActions, tweetUserName, tweetHandle, wholeTweet, listBody, tweetAvatarParse;

    function validateDOI(doi) {
      if (doi == null) {
        throw new Error('DOI is null.');
      }

      doi = encodeURI(doi);

      return doi.replace(new RegExp('/', 'g'), '%2F').replace(new RegExp(':', 'g'), '%3A');
    }

    this.getSidebarTweets = function (doi) {
      doi = validateDOI(doi);
      var config, requestUrl, errorText;

      config = ALM_CONFIG;
      // if twitter stream stops working, check the api for parameter changes: http://alm.plos.org/docs/api
      requestUrl = config.host + '?api_key=' + config.apiKey + '&ids=' + doi + '&info=detail&source_id=twitter';

      errorText = '<li>Our system is having a bad day. We are working on it. Please check back later.</li>';

      $.ajax({
        url:         requestUrl,
        dataType:    'jsonp',
        contentType: "text/json; charset=utf-8",
        type:        "GET"
      }).done(function (data) {

        initData = data.data[0];

        
        if (initData === undefined) {
        // no data, do nothing further
        } else {

          totalTweets = initData.sources[0].metrics.total;

          if (totalTweets === 0) {
            //no tweets to display, do nothing further
          } else {
            minDisplayTweets = 2;
            maxDisplayTweets = 5;
            dataSort = initData.sources[0].events;

            //parse the date to be able to sort by date
            this.parseTwitterDate = function (tweetdate) {
              //running regex to grab everything after the time
              var newdate = tweetdate.replace(/(\d{1,2}[:]\d{2}[:]\d{2}) (.*)/, '$2 $1');
              //moving the time code to the end
              newdate = newdate.replace(/(\+\S+) (.*)/, '$2 $1');

              return new Date(Date.parse(newdate));
            }
            //sort by date from Ambra
            this.sort_tweets_by_date = function (a, b) {
              var aDt = isNaN(a.event.created_at) ? this.parseTwitterDate(a.event.created_at) : a.event.created_at;
              var bDt = isNaN(b.event.created_at) ? this.parseTwitterDate(b.event.created_at) : b.event.created_at;

              return (new Date(bDt).getTime()) - (new Date(aDt).getTime());
            }
            //pull the data & run the sort function
            dataSort = dataSort.sort(jQuery.proxy(this.sort_tweets_by_date, this));
            // only show 5, so cut the json results to 5
            if (dataSort.length > maxDisplayTweets) {
              dataSort = dataSort.slice(0, 5);
            } else { }

            $.each(dataSort, function (index) {
              dataPrefix = dataSort[index].event;
              datePrefix = dataSort[index];
              //run through dataPass to get all the data
              dataPass(dataPrefix, datePrefix);
              //show only 2 and then 5
              if (index < minDisplayTweets) {
                wholeTweet = '<li>' + listBody + '</li>';
              } else {
                wholeTweet = '<li class="more-tweets">' + listBody + '</li>';
              }

              $('#tweetList').append(wholeTweet);
              checkAvatar(listBody);

            });

            //display tweets
            $('.twitter-container').css('display', 'block');

            // display the more tweets if there are any.
            if (totalTweets > minDisplayTweets) {
              var show_link = more_tweets();
              return show_link;
            }

          }
        }
      }).fail(function () {
        $('.twitter-container').css('display', 'block');
        $('#tweetList').append(errorText);
      });

    };

    function checkAvatar(listappend) {
      var checkImg = $(listappend).find('.imgLoad');
      $(checkImg).on('error', changeAvatar);
    }

    function changeAvatar(event) {
      if (event) {
        var newthing = $(this).attr('src', tweetPlaceholder);
        return $('.imgholder').html(newthing);
{'fraction_non_alphanumeric': 0.13711977186311788, 'fraction_numerical': 0.005822243346007605, 'mean_word_length': 2.0741417092768444, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '3406176', 'n_tokens_mistral': 2591, 'n_tokens_neox': 2453, 'n_words': 612}