﻿/*
* @project YASMIJ.js, "Yet another simplex method implementation in Javascript"
*/
// Equation Class
var Equation = function(){
	this.comparison;
	this.leftSide;
	this.rightSide;
	this.terms;
};
Equation.hasManyCompares = function(str){
	var RE_compares = /[<>]=?|=/g;
	var matches = (""+str).replace(/\s/g,"").match(RE_compares) || [];
	return 1 < matches.length;
};
Equation.hasOtherMathSigns = function( str ){
	return /[\*\/%]/.test(str);
};
Equation.hasIncompleteBinaryOperator = function( str ){
	var noSpaceStr = (""+str).replace( /\s/g, "" );
	var RE_hasNoPlusOrMinus = /^[^\+\-\d]+$/, RE_noLeftAndRightTerms = /[\+\-][><=\+\-]|[><=\+\-]$/;	
	return RE_hasNoPlusOrMinus.test(noSpaceStr) || RE_noLeftAndRightTerms.test(noSpaceStr);
};
Equation.getErrorMessage = function( str ){
	var errMsg;
	if( Equation.hasManyCompares( str ) ){
		errMsg = "Only 1 comparision (<,>,=, >=, <=) is allow in a equation.";
	}
	if( !errMsg && Equation.hasOtherMathSigns( str ) ){
		errMsg = "Only addition and subtraction is allowed. Get rid of math operators.";
	}
	if( !errMsg && Equation.hasIncompleteBinaryOperator( str ) ){
		errMsg = "Math operators must be in between terms. Good:(a+b=c). Bad:(a b+=c)";
	}
	return errMsg;
};
Equation.checkInput = function( str ){
	var errMsg = Equation.getErrorMessage( str );
	if( errMsg ){
		throw new Error( errMsg );
	}
};
Equation.parse = function( str ){
	Equation.checkInput( str );
	if( !str ){
		return {lhs:{0:1}};
	}
	if( !isNaN( str ) ){
		return { lhs: {1: parseFloat(str) } }
	}
};
/*
// Simplex Class
var Simplex = function(){
	this.input = new Input();
	this.output = new Output();
	this.tableau = new Tableau();
	this.state;
};
Simplex.prototype = {
	toString:function(){
	},
	setInput:function( obj ){
		this.input.parse( obj );
	}
};
// Input Class
var Input = function(){
	this.z;
	this.raw;
	this.type;
	this.variables;
	this.constraints;
};
Input.checkForRequirements = function( obj ){
	var errMsg = "Input Object";
	if( !obj || typeof obj !== "object" ){
		errMsg = "must be an object with properies `z`, `constraints` and `type`.";
	}
	return errMsg;
}
Input.prototype = {
	toString:function(){
	},
	parse:function( obj ){
		this.raw = obj;
		var zEquation = new Equation(obj.z);
		this.z = obj.z;
	}
};
// Matrix Class
var Matrix = function(){
	this.columnLength;
	this.rowLength;
	this.matrix;
};
// Tableau Class
var Tableau = function(){
	this.matrix;
	this.limit;
	this.state;
};

// Output Class
var Output = function(){
	this.matrix;
	this.result;
};
Output.prototype = {
	toString:function(){
	}
};
*/
















@@ -1,9 +1,8 @@
-%EF%BB%BF
 /*%0D%0A
+
 * @p
@@ -522,16 +522,30 @@
 %7B%0D%0A%09var 
+hasError, %0D%0A%09%09
 noSpaceS
@@ -578,24 +578,21 @@
 /g, %22%22 )
-;%0D%0A%09var 
+,%0D%0A%09%09
 RE_hasNo
@@ -617,16 +617,18 @@
 %5C+%5C-
-%5Cd
 %5D+$/, 
+%0D%0A%09%09
 RE_n
@@ -681,18 +681,56 @@
 %5D$/;
-%09
 %0D%0A%09
-return
+hasError =  /%5Cs/.test( (%22%22 + str).trim() ) &&
  RE_
@@ -762,16 +762,39 @@
 paceStr)
+;%0D%0A%09hasError = hasError
  %7C%7C RE_n
@@ -827,24 +827,43 @@
 SpaceStr);%0D%0A
+%09return hasError;%0D%0A
 %7D;%0D%0AEquation
@@ -1484,24 +1484,435 @@
 );%0D%0A%09%7D%0D%0A%7D;%0D%0A
+Equation.convertExpressionToObject = function (str) %7B%0D%0A%09var noSpacesStr = (%22%22 + str).replace(/%5Cs/g, ''),%0D%0A%09RE_findTerms = /%5B%5C-%5D?%5B%5E%5C-%5C+=%3E%3C%5D+/g,%0D%0A%09matches = noSpacesStr.match(RE_findTerms),%0D%0A%09term,%0D%0A%09obj = %7B%7D, i = matches.length;%0D%0A%09while( i-- )%7B%0D%0A%09%09term = matches%5B i %5D;%0D%0A%09%09val = parseFloat( term );%0D%0A%09%09obj%5B term.match(/%5B%5E%5Cd%5C-%5C+%5D+$/) %5D = parseFloat( term ) %7C%7C ( /%5E-/.test( term ) ? -1 : 1);%0D%0A%09%7D%0D%0A%09return obj;%0D%0A%7D;%0D%0A
 Equation.par
@@ -2069,20 +2069,100 @@
 r) %7D %7D%0D%0A
-
 %09%7D%0D%0A
+%09var obj = %7B%0D%0A%09%09lhs: Equation.convertExpressionToObject(str)%0D%0A%09%7D%0D%0A%09return obj;%0D%0A
 %7D;%0D%0A/*%0D%0A

Update master