'use strict';

/**
 * Internal dependencies
 */
let NumberRange = require( './number-range' );

function NormalDistribution( mean, sd ) {
	this.mean = mean;
	this.sd = sd;
}

NormalDistribution.prototype = {
	SD_TO_GRAPH: 4,
	POINTS_PER_CURVE: 1000,

	getX: function( z ) {
		return this.mean + z * this.sd;
	},

	getRange: function( z ) {
		return new NumberRange( this.getX( -z ), this.getX( z ) );
	},

	getYAxisRange: function() {
		return new NumberRange( 0, this.getDensity( this.mean ) );
	},

	// See: See: http://en.wikipedia.org/wiki/Normal_distribution
	getDensity: function( x ) {
		let a = 1 / ( this.sd * Math.sqrt( 2 * Math.PI ) ),
			b = Math.exp( -( Math.pow( x - this.mean, 2) ) / ( 2 * Math.pow( this.sd, 2 ) ) );

		return a * b;
	},

	getPeakDensity: function() {
		return this.getDensity( this.mean );
	},

	getCurveXValues: function() {
		let xRange = this.xRange,
			range = new NumberRange( xRange.min, xRange.max, this.POINTS_PER_CURVE );

		return range.getValues();
	},

	getCurveYValues: function() {
		return this.xValues.map( function( xValue ) {
			return this.getDensity( xValue );
		}, this );
	},

	getXBetween: function( minExclusive, maxInclusive ) {
		return this.xValues.filter( function( xValue ) {
			return xValue > minExclusive && xValue <= maxInclusive;
		} );
	},

	getYForXBetween: function( minExclusive, maxInclusive ) {
		let xValues = this.getXBetween( minExclusive, maxInclusive );
		return xValues.map( function( xValue ) {
			return this.getDensity( xValue );
		}, this );
	},
};

Object.defineProperties( NormalDistribution.prototype, {
	'variance': {
		get: function() {
			return Math.pow( this.sd, 2 );
		}
	},
	'xRange': {
		get: function() {
			if ( this._xRange === undefined ) {
				this._xRange = this.getRange( this.SD_TO_GRAPH );
			}
			return this._xRange;
		}
	},
	'xValues': {
		get: function() {
			if ( this._xValues === undefined ) {
				let range = new NumberRange( this.xRange.min, this.xRange.max, this.POINTS_PER_CURVE );
				this._xValues = range.getValues();
			}
			return this._xValues;
		}
	},
	'yValues': {
		get: function() {
			if ( this._yValues === undefined ) {
				this._yValues = this.xValues.map( function( xValue ) {
					return this.getDensity( xValue );
				}, this );
			}
			return this._yValues;
		}
	}
} );

module.exports = NormalDistribution;

@@ -89,24 +89,76 @@
 ge' );%0A%0A
-function
+const SD_TO_GRAPH = 4;%0Aconst POINTS_PER_CURVE = 1000;%0A%0Aclass
  NormalD
@@ -168,16 +168,31 @@
 ribution
+ %7B%0A%09constructor
 ( mean, 
@@ -198,16 +198,17 @@
  sd ) %7B%0A
+%09
 %09this.me
@@ -218,16 +218,17 @@
 = mean;%0A
+%09
 %09this.sd
@@ -238,102 +238,17 @@
 sd;%0A
-%7D%0A%0ANormalDistribution.prototype = %7B%0A%09SD_TO_GRAPH: 4,%0A%09POINTS_PER_CURVE: 1000,%0A%0A%09getX: function
+%09%7D%0A%0A%09getX
 ( z 
@@ -283,25 +283,24 @@
  this.sd;%0A%09%7D
-,
 %0A%0A%09getRange:
@@ -298,26 +298,16 @@
 getRange
-: function
 ( z ) %7B%0A
@@ -365,25 +365,24 @@
 X( z ) );%0A%09%7D
-,
 %0A%0A%09getYAxisR
@@ -385,26 +385,16 @@
 xisRange
-: function
 () %7B%0A%09%09r
@@ -453,17 +453,16 @@
  ) );%0A%09%7D
-,
 %0A%0A%09// Se
@@ -529,26 +529,16 @@
 tDensity
-: function
 ( x ) %7B%0A
@@ -692,25 +692,24 @@
 rn a * b;%0A%09%7D
-,
 %0A%0A%09getPeakDe
@@ -713,26 +713,16 @@
 kDensity
-: function
 () %7B%0A%09%09r
@@ -755,25 +755,24 @@
 s.mean );%0A%09%7D
-,
 %0A%0A%09getCurveX
@@ -773,34 +773,24 @@
 CurveXValues
-: function
 () %7B%0A%09%09let x
@@ -854,37 +854,32 @@
 in, xRange.max, 
-this.
 POINTS_PER_CURVE
@@ -909,25 +909,24 @@
 Values();%0A%09%7D
-,
 %0A%0A%09getCurveY
@@ -931,26 +931,16 @@
 eYValues
-: function
 () %7B%0A%09%09r
@@ -1032,25 +1032,24 @@
 , this );%0A%09%7D
-,
 %0A%0A%09getXBetwe
@@ -1046,34 +1046,24 @@
 %09getXBetween
-: function
 ( minExclusi
@@ -1202,17 +1202,16 @@
 %09%7D );%0A%09%7D
-,
 %0A%0A%09getYF
@@ -1220,26 +1220,16 @@
 XBetween
-: function
 ( minExc
@@ -1416,12 +1416,10 @@
 ;%0A%09%7D
-,
 %0A%7D
-;
 %0A%0AOb
@@ -1656,21 +1656,16 @@
 tRange( 
-this.
 SD_TO_GR
@@ -1849,21 +1849,16 @@
 ge.max, 
-this.
 POINTS_P

Convert Normal Distribution class to ES6