const _ = require( 'underscore' );

// Ensure both arguments are strings and that str begins with starts.
function startsWith( str, starts ) {
	return 0 === ( '' + str ).lastIndexOf( '' + starts, 0 );
}

module.exports = {
	isAb( diff ) {
		let ab = false;
		try {
			diff = diff.split( '\n' );
			_.each( diff, ( line ) => {
				if ( startsWith( line, 'diff --git a/' ) ) {
					throw true;
				}
				if ( startsWith( line, 'Index: trunk/wp-' ) ) {
					throw true;
				}
			} );
		} catch ( e ) {
			ab = e;
		}

		return ab;
	},

	/**
	 * Check to see if we should apply the diff from the src dir
	 *
	 * @param {string} diff A string diff
	 * @return {boolean} true if we should go into src to apply the diff
	 */
	moveToSrc( diff ) {
		let src = false;
		const wpDashExceptions = [
			'.editorconfig',
			'.gitignore',
			'.jshintrc',
			'.travis.yml',
			'Gruntfile.js',
			'package.json',
			'phpunit.xml.dist',
			'wp-cli.yml',
			'wp-config-sample.php',
			'wp-tests-config-sample.php',
		];
		const noWpDashExceptions = [
			'index.php',
			'license.txt',
			'readme.html',
			'xmlrpc.php',
		];

		try {
			diff = diff.split( '\n' );
			_.each( diff, ( line ) => {
				// these are often the first line
				if ( startsWith( line, 'Index: src/' ) ||
					startsWith( line, 'Index: tests/' ) ||
					startsWith( line, 'Index: tools/' ) ||
					startsWith( line, 'diff --git src' ) ||
					startsWith( line, 'diff --git test' ) ||
					startsWith( line, 'diff --git tools' ) ||
					startsWith( line, 'diff --git a/src' ) ||
					startsWith( line, 'diff --git a/test' ) ||
					startsWith( line, 'diff --git a/tools' )

				) {
					throw false;
				}

				_.each( wpDashExceptions, ( exception ) => {
					if ( startsWith( line, 'Index: ' + exception ) ||
						startsWith( line, 'diff --git ' + exception ) ||
						startsWith( line, 'diff --git a/' + exception )
					) {
						throw false;
					}
				} );

				_.each( noWpDashExceptions, ( exception ) => {
					if ( startsWith( line, 'Index: ' + exception ) ||
						startsWith( line, 'diff --git ' + exception ) ||
						startsWith( line, 'diff --git a/' + exception )
					) {
						throw true;
					}
				} );

				if ( startsWith( line, 'Index: wp-' ) ||
					startsWith( line, 'Index: trunk/wp-' ) ||
					startsWith( line, 'diff --git wp-' ) ||
					startsWith( line, 'diff --git a/wp-' )
				) {
					throw true;
				}
			} );
			throw true;
		} catch ( l ) {
			src = l;
		}
		return src;
	},

};

@@ -1,40 +1,4 @@
-const _ = require( 'underscore' );%0A%0A
 // E
@@ -217,39 +217,32 @@
 lse;%0A%09%09try %7B%0A%09%09%09
-diff = 
 diff.split( '%5Cn'
@@ -243,34 +243,25 @@
 ( '%5Cn' )
-;%0A%09%09%09_.each( diff,
+.forEach(
  ( line 
@@ -1063,15 +1063,8 @@
 %0A%09%09%09
-diff = 
 diff
@@ -1081,26 +1081,17 @@
 n' )
-;%0A%09%09%09_.each( diff,
+.forEach(
  ( l
@@ -1590,24 +1590,16 @@
 %09%7D%0A%0A%09%09%09%09
-_.each( 
 wpDashEx
@@ -1598,33 +1598,41 @@
 wpDashExceptions
-,
+.forEach(
  ( exception ) =
@@ -1852,16 +1852,8 @@
 %09%09%09%09
-_.each( 
 noWp
@@ -1866,17 +1866,25 @@
 ceptions
-,
+.forEach(
  ( excep

Use native Array#forEach instead of _.each