﻿//--------------------------------------------------------------
// Class: String
// Author: ReelGreetings.com
// Description: Prototypes the String class
// Properties: Length
// Methods: Find,IndexOfAny,FindAny,Contains,StartsWith,EndWith,
//          ToInt,ToFloat,Hyphenate,Trim,TrimStart,TrimEnd,
//          Clean,RemoveLines,Insert,PadLeft,PadRight,
//          Remove,RemoveAfter,RemoveBefore,RemoveAt
//---------------------------------------------------------------

// Return: int
String.prototype.Find = function(str,blnIgnoreCase) {
    if(blnIgnoreCase)
    {
       return this.toLowerCase().indexOf(str.toLowerCase())
    }
   return this.indexOf(str)
}

// Params: string[]
// Return: int[]
String.prototype.IndexOfAny = function(arrKeywords,blnIgnoreCase)
{
   var str = null;
   var strKeyword = null;
   var arrIndexes = new Array();
   for(i = 0; i < arrKeywords.length; i++)
   {
      if(blnIgnoreCase)
      {
         str = this.toLowerCase();
         strKeyword = arrKeywords[i].toLowerCase();
      }
      else
      {
         str = this;
         strKeyword = arrKeywords[i];
      }            
      if(str.indexOf(strKeyword) != -1)
      {
         arrIndexes[arrIndexes.length] = i;
      }

   }
   return arrIndexes;
}

// Params: string[]
// Return: string[]
String.prototype.FindAny = function(arrKeywords,blnIgnoreCase)
{
   var arrMatches = new Array();
   for(i = 0; i < arrKeywords.length; i++)
   {
      if(blnIgnoreCase)
      {
         str = this.toLowerCase();
         strKeyword = arrKeywords[i].toLowerCase();
      }
      else
      {
         str = this;
         strKeyword = arrKeywords[i];
      }            
      if(str.indexOf(strKeyword) != -1)
      {
         arrMatches[arrMatches.length] = arrKeywords[i];
      }

   }
   return arrMatches;
}

// Return: boolean
String.prototype.Contains = function(str,blnIgnoreCase) {
    if(blnIgnoreCase)
    {
       return this.Find(this,true) != -1;
    }
   return this.Find(this,false) != -1;
}

// Return: boolean
String.prototype.StartsWith = function(c) {
   return this.indexOf(c) == 0;
}

// Return: boolean
String.prototype.EndWith = function(c) {
   return (this.indexOf(c) == (this.length - 1));
}

// Params: (), (int base)
// Return: int, -1 if failed to parse
String.prototype.ToInt = function() {
   var intparsed_int = (arguments.length == 1)? parseInt(this,arguments[0]) : parseInt(this);
   return (isNaN(intparsed_int))? -1 : intparsed_int;
}

// Params: (), (int decimal places)
// Return: int, -1 if failed to parse
String.prototype.ToFloat = function() {
   var str = this;
   if(arguments.length == 1)
   {
      var intIndex = this.indexOf('.');
      if(intIndex != -1)
      {
         if(intIndex + arguments[0] > this.length)
            throw "(Exception: s_tf0) Index Out of Range";
         str = this.substring(0,intIndex + arguments[0]);
      }
   }
   var intparsed_float = parseFloat(str);
   return (isNaN(intparsed_float))? -1 : intparsed_float;
}

// Params:    
//            0: "space" 
//            1 : "lower" 
//            2 : "upper"
//            3 : "num" 
//            4 : "all" 
// Return: string
String.prototype.Hyphenate = function() {
   var regex = /\s/g;
   if(arguments.length == 1)
   {
      var eType = arguments[0];
      switch(eType)
      {
         case 0:
             regex = /\s/g;
             break;
         case 1:
             regex = /[a-z]/g;
             break;
         case 2:
             regex = /[A-Z]/g;
             break;
         case 3:
             regex = /[0-9]/g;
             break;
         case 4:
             regex = /\s[a-z][A-Z][0-9]/g;
             break;
         default: 
             throw "(Exception: s_h0) Invalid enumeration value";
      }
   }
   return this.replace(regex, 
                       function(match) { 
                             return '-' + match.charAt(0);
                       }
           );
}

// Return: string
String.prototype.Trim = function() {
   return this.replace(/^\s+|\s+$/g, '');
}

// Return: string
String.prototype.TrimStart = function() {
   return this.replace(/^\s+/,"");
}

// Return: string
String.prototype.TrimEnd = function() {
   return this.replace(/\s+$/,"");
}

// Return: string
String.prototype.Clean = function() {
   return this.replace(/\s{2,}/g," ").Trim();
}

// Return: int
String.prototype.Length = function() {
   return this.length;
}

// Return: string
String.prototype.RemoveLines = function() {
   return this.replace(/(\n\r|\n|\r)/gm,"");
}

// Return: string
String.prototype.Insert = function(str,intIndex) {
   if(this.length < intIndex)
      throw "(Exception: s_i0) Index Out of Range";
   var strStart = this.substring(0,intIndex);
   var strEnd = this.substring(intIndex,this.length);
   return strStart + str + strEnd;
}

// Return: string
String.prototype.PadLeft = function(str,intFrequency) {
   var strStart = "";
   for(i = 0; i < intFrequency + 1; i++)
   {
      strStart = strStart + str;
   }
   return strStart + this;
}

// Return: string
String.prototype.PadRight = function(str,intFrequency) {
   var strEnd = "";
   for(i = 0; i < intFrequency + 1; i++)
   {
      strEnd = strEnd + str;
   }
   return this + strEnd;
}


// Return: string
String.prototype.Remove = function(str) {
  this.replace(str,"");
}

// Return: string
String.prototype.RemoveAfter = function(intIndex) {
   if(this.length < intIndex)
      throw "(Exception: s_r0) Index Out of Range";
   return this.substring(0,Index);
}

// Return: string
String.prototype.RemoveBefore = function(intIndex) {
   if(this.length < intIndex)
      throw "(Exception: s_r0) Index Out of Range";
   return this.substring(intIndex,this.length);
}

// Return: string
String.prototype.RemoveAt = function(str,intIndex) {
   if(this.length < intIndex)
      throw "(Exception: s_r0) Index Out of Range";
   var strStart = this.substring(0,intIndex - 1);
   var strEnd = this.substring(intIndex + 1, this.length);
   return strStart + strEnd;
}