<commit_before>import React, { Component, PropTypes } from 'react'
import classNames from 'classnames'
import autoBind from 'auto-bind'

export default class Swipe extends Component {
  constructor (props) {
    super(props)
    this.state = {
      currentIndex: props.initialIndex,
      width: 0,
      scrollLeft: 0,
      drag: 0
    }

    autoBind(this)
  }

  componentDidMount () {
    this.setWidth()
    this.initLazyLoad()

    if (this.props.autoPlay) this.autoPlay(this.props)

    if (this.props.responsive) {
      window.addEventListener('resize', this.setWidth, false)
    }
  }

  componentWillReceiveProps (nextProps) {
    if (this.props.autoPlay !== nextProps.autoPlay) {
      nextProps.autoPlay ? this.autoPlay(nextProps) : this.pause()
    }
  }

  setWidth () {
    if (this.swipeRef) {
      this.setState({
        width: this.swipeRef.clientWidth
      })
    }
  }

  handleTouchStart (e) {
    if (this.hasSingleImage()) return
    this.clientX = e.touches[0].clientX
    document.addEventListener('touchmove', this.handleTouchMove, false)
  }

  handleTouchEnd () {
    if (this.hasSingleImage()) return
    const {drag, currentIndex, width} = this.state
    const {threshold} = this.props

    const initialIndex = currentIndex

    if (Math.abs(drag) > threshold * width) {
      this.setState(() => {
        if (drag < 0 && currentIndex > 0) {
          return {
            // goto previous slide
            currentIndex: currentIndex - 1
          }
        } else if (drag > 0 && ((currentIndex + 1) < this.props.children.length)) {
          return {
            // goto next slide
            currentIndex: currentIndex + 1
          }
        }
      }, () => {
        this.onChange(initialIndex)
      })
    }

    this.setState({
      drag: 0
    })

    document.removeEventListener('touchmove', this.handleTouchMove, false)
  }

  initLazyLoad () {
    const {currentIndex} = this.state
    const {overScan, children} = this.props
    this[`imageRef${currentIndex}`].load()
    if (overScan === 1) {
      if (currentIndex > 0) this[`imageRef${currentIndex - 1}`].load()
      if (currentIndex + 1 < children.length) this[`imageRef${currentIndex + 1}`].load()
    }
  }

  handleTouchMove (e) {
    const dx = this.clientX - e.touches[0].clientX

    this.setState({
      drag: dx
    })
  }

  gotoPrev () {
    const {currentIndex} = this.state
    if (currentIndex > 0) {
      this.gotoSlide(currentIndex - 1, false)
    }
  }

  gotoNext () {
    const {currentIndex} = this.state
    if (currentIndex + 1 < this.props.children.length) {
      this.gotoSlide(currentIndex + 1, false)
    }
  }

  gotoSlide (i, isManual = true) {
    if (isManual) this.pause()
    const initial = this.state.currentIndex
    this.setState({
      currentIndex: i
    }, () => (this.onChange(initial)))
  }

  autoPlay (props) {
    const {children, autoPlayInterval} = props
    this.pause()

    if (this.hasSingleImage()) return

    this.autoPlayId = setInterval(() => {
      const {currentIndex} = this.state
      if (currentIndex + 1 < children.length) {
        this.gotoNext()
      } else {
        this.gotoSlide(0, currentIndex)
      }
    }, autoPlayInterval)
  }

  pause () {
    clearInterval(this.autoPlayId)
  }

  onChange (initialIndex) {
    this.props.onSwipe({
      currentIndex: this.state.currentIndex,
      initialIndex
    })
    this.initLazyLoad()
  }

  hasSingleImage () {
    return !(this.props.children.length > 1)
  }

  handleClick () {
    this.props.onClick({
      index: this.state.currentIndex
    })
  }

  componentWillUnmount () {<commit_msg>style(comments): changed .length to ===1<commit_after>import React, { Component, PropTypes } from 'react'
import classNames from 'classnames'
import autoBind from 'auto-bind'

export default class Swipe extends Component {
  constructor (props) {
    super(props)
    this.state = {
      currentIndex: props.initialIndex,
      width: 0,
      scrollLeft: 0,
      drag: 0
    }

    autoBind(this)
  }

  componentDidMount () {
    this.setWidth()
    this.initLazyLoad()

    if (this.props.autoPlay) this.autoPlay(this.props)

    if (this.props.responsive) {
      window.addEventListener('resize', this.setWidth, false)
    }
  }

  componentWillReceiveProps (nextProps) {
    if (this.props.autoPlay !== nextProps.autoPlay) {
      nextProps.autoPlay ? this.autoPlay(nextProps) : this.pause()
    }
  }

  setWidth () {
    if (this.swipeRef) {
      this.setState({
        width: this.swipeRef.clientWidth
      })
    }
  }

  handleTouchStart (e) {
    if (this.hasSingleImage()) return
    this.clientX = e.touches[0].clientX
    document.addEventListener('touchmove', this.handleTouchMove, false)
  }

  handleTouchEnd () {
    if (this.hasSingleImage()) return
    const {drag, currentIndex, width} = this.state
    const {threshold} = this.props

    const initialIndex = currentIndex

    if (Math.abs(drag) > threshold * width) {
      this.setState(() => {
        if (drag < 0 && currentIndex > 0) {
          return {
            // goto previous slide
            currentIndex: currentIndex - 1
          }
        } else if (drag > 0 && ((currentIndex + 1) < this.props.children.length)) {
          return {
            // goto next slide
            currentIndex: currentIndex + 1
          }
        }
      }, () => {
        this.onChange(initialIndex)
      })
    }

    this.setState({
      drag: 0
    })

    document.removeEventListener('touchmove', this.handleTouchMove, false)
  }

  initLazyLoad () {
    const {currentIndex} = this.state
    const {overScan, children} = this.props
    this[`imageRef${currentIndex}`].load()
    if (overScan === 1) {
      if (currentIndex > 0) this[`imageRef${currentIndex - 1}`].load()
      if (currentIndex + 1 < children.length) this[`imageRef${currentIndex + 1}`].load()
    }
  }

  handleTouchMove (e) {
    const dx = this.clientX - e.touches[0].clientX

    this.setState({
      drag: dx
    })
  }

  gotoPrev () {
    const {currentIndex} = this.state
    if (currentIndex > 0) {
      this.gotoSlide(currentIndex - 1, false)
    }
  }

  gotoNext () {
    const {currentIndex} = this.state
    if (currentIndex + 1 < this.props.children.length) {
      this.gotoSlide(currentIndex + 1, false)
    }
  }

  gotoSlide (i, isManual = true) {
    if (isManual) this.pause()
    const initial = this.state.currentIndex
    this.setState({
      currentIndex: i
    }, () => (this.onChange(initial)))
  }

  autoPlay (props) {
    const {children, autoPlayInterval} = props
    this.pause()

    if (this.hasSingleImage()) return

    this.autoPlayId = setInterval(() => {
      const {currentIndex} = this.state
      if (currentIndex + 1 < children.length) {
        this.gotoNext()
      } else {
        this.gotoSlide(0, currentIndex)
      }
    }, autoPlayInterval)
  }

  pause () {
    clearInterval(this.autoPlayId)
  }

  onChange (initialIndex) {
    this.props.onSwipe({
      currentIndex: this.state.currentIndex,
      initialIndex
    })
    this.initLazyLoad()
  }

  hasSingleImage () {
    return this.props.children.length === 1
  }

  handleClick () {
    this.props.onClick({
      index: this.state.currentIndex
    })
  }

  componentWillUnmount () {
{'fraction_non_alphanumeric': 0.13069741900054915, 'fraction_numerical': 0.006727073036792971, 'mean_word_length': 2.4105805243445695, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 22, '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': '6496008', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2294, 'n_words': 464}