<commit_before>/** @jsx React.DOM */

/**
 * Module dependencies
 */

var React = require('react');
var sdk = require('require-sdk')('https://www.youtube.com/iframe_api', 'YT');
var loadTrigger = sdk.trigger();

function noop() {}

// YT API requires global ready event handler
window.onYouTubeIframeAPIReady = function () {
  loadTrigger();
  delete window.onYouTubeIframeAPIReady;
};

/**
 * Separates video ID from valid YouTube URL
 *
 * @param {string} url
 * @return {string}
 */

function getVideoId(url) {
  var regex = /(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/;
  if (url) return url.match(regex)[5];
}

/**
 * Simple wrapper over YouTube JavaScript API
 */

module.exports = React.createClass({
  propTypes: {
    id: React.PropTypes.string,
    url: React.PropTypes.string,
    autoplau: React.PropTypes.boolean,
    playing: React.PropTypes.func,
    stopped: React.PropTypes.func,
    ended: React.PropTypes.func
  },

  getDefaultProps: function() {
    return {
      id: 'react-yt-player',
      url: undefined,
      autoplay: false,
      playing: noop,
      stopped: noop,
      ended: noop
    };
  },
  
  componentDidMount: function() {
    var _this = this;
    // called once API has loaded.
    sdk(function(err, youtube) {
      var player = new youtube.Player(_this.props.id, {
        videoId: getVideoId(_this.props.url),
        events: {
          'onStateChange': _this._handlePlayerStateChange
        }
      });

      _this.setState({player: player});
    });
  },

  componentWillUpdate: function(nextProps) {
    if (this.props.url !== nextProps.url) {
      this._loadNewUrl(nextProps.url);
    }
  },

  /**
   * Start a new video
   *
   * @param {string} url
   */
  
  _loadNewUrl: function(url) {
    this.props.autoplay
      ? this.state.player.loadVideoById(getVideoId(url))
      : this.state.player.cueVideoById(getVideoId(url));
  },

  /**
   * Respond to player events
   *
   * 0 = ended
   * 1 = playing
   * 2 = paused
   *
   * @param {object} event
   */
  
  _handlePlayerStateChange: function(event) {
    switch(event.data) {
      case 0: 
        this.props.ended();
        break;

      case 1:
        this.props.playing();
        break;

      case 2:
        this.props.stopped();
        break;

      default: 
        return;
    }
  },

  render: function() {
    return (
      <div id={this.props.id}></div>
    );
  }
});
<commit_msg>gardening<commit_after>/** @jsx React.DOM */

/**
 * Module dependencies
 */

var React = require('react');
var sdk = require('require-sdk')('https://www.youtube.com/iframe_api', 'YT');
var loadTrigger = sdk.trigger();

// YT API requires global ready event handler
window.onYouTubeIframeAPIReady = function () {
  loadTrigger();
  delete window.onYouTubeIframeAPIReady;
};

function noop() {}

/**
 * Separates video ID from valid YouTube URL
 *
 * @param {string} url
 * @return {string}
 */

function getVideoId(url) {
  var regex = /(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/;
  if (url) return url.match(regex)[5];
}

/**
 * Simple wrapper over YouTube JavaScript API
 */

module.exports = React.createClass({
  propTypes: {
    id: React.PropTypes.string,
    url: React.PropTypes.string,
    autoplau: React.PropTypes.boolean,
    playing: React.PropTypes.func,
    stopped: React.PropTypes.func,
    ended: React.PropTypes.func
  },

  getDefaultProps: function() {
    return {
      id: 'react-yt-player',
      url: undefined,
      autoplay: false,
      playing: noop,
      stopped: noop,
      ended: noop
    };
  },

  /**
   * Once YouTube API had loaded, a new YT.Player
   * instance will be created and its events bound.
   */
  
  componentDidMount: function() {
    var _this = this;
    // called once API has loaded.
    sdk(function(err, youtube) {
      var player = new youtube.Player(_this.props.id, {
        videoId: getVideoId(_this.props.url),
        events: {
          'onStateChange': _this._handlePlayerStateChange
        }
      });

      _this.setState({player: player});
    });
  },

  componentWillUpdate: function(nextProps) {
    if (this.props.url !== nextProps.url) {
      this._loadNewUrl(nextProps.url);
    }
  },

  /**
   * Start a new video
   *
   * @param {string} url
   */
  
  _loadNewUrl: function(url) {
    this.props.autoplay
      ? this.state.player.loadVideoById(getVideoId(url))
      : this.state.player.cueVideoById(getVideoId(url));
  },

  /**
   * Respond to player events
   *
   * @param {object} event
   */
  
  _handlePlayerStateChange: function(event) {
    switch(event.data) {
      case 0: 
        this.props.ended();
        break;

      case 1:
        this.props.playing();
        break;

      case 2:
        this.props.stopped();
        break;

      default: 
        return;
    }
  },

  render: function() {
    return (
      <div id={this.props.id}></div>
    );
  }
});

{'fraction_non_alphanumeric': 0.1533009315512353, 'fraction_numerical': 0.0022276225192385582, 'mean_word_length': 2.4490223463687153, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1928071', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1653, 'n_words': 334}