// http://wiki.ecmascript.org/doku.php?id=strawman:string_padding
'use strict';
var $def = require('./$.def')
  , $ = require('./$')
  , repeat = require('../fn/string/repeat');

module.exports = function $pad(that, minLength, fillChar, left) {
    // 2. Let S be ToString(O).
    var S = String(that);

    if ( minLength === undefined ) {
      // 4. If intMinLength is undefined, return S.
      return S;
    } else {
      // 4. Let intMinLength be ToInteger(minLength).
      var intMinLength = $.toInteger(minLength);
    }

    // 5. Let fillLen be the number of characters in S minus intMinLength.
    var fillLen = intMinLength - S.length;

    if ( fillLen < 0 || fillLen === Infinity ) {
      // 6. If fillLen < 0, then throw a RangeError exception.
      // 7. If fillLen is +∞, then throw a RangeError exception.
      throw new RangeError('Cannot satisfy string length ' +
                            minLength + ' for string ' + S);
    }

    // 8. Let sFillStr be the string represented by fillStr.
    // 9. If sFillStr is undefined, let sFillStr be a space character.
    var sFillStr = fillChar === undefined ? ' ' : String(fillChar);

    // 10. Let sFillVal be a String made of sFillStr, repeated until fillLen is met.
    var sFillVal = repeat(sFillStr, fillLen / sFillStr.length);

    // truncate if we overflowed
    if ( sFillVal.length > fillLen ) {
      sFillVal = sFillVal.slice(0, fillLen);
    }

    if ( left ) {
      // 11. Return a string made from sFillVal, followed by S.
      return sFillVal.concat(S);
    } else {
      // 11. Return a String made from S, followed by sFillVal.
      return S.concat(sFillVal);
    }
  };
};

@@ -80,38 +80,8 @@
 %0Avar
- $def = require('./$.def')%0A  ,
  $ =
@@ -208,18 +208,16 @@
 left) %7B%0A
-  
   // 2. 
@@ -238,18 +238,16 @@
 ing(O).%0A
-  
   var S 
@@ -261,26 +261,24 @@
 g(that);%0A%0A  
- 
- 
 if ( minLeng
@@ -294,26 +294,24 @@
 defined ) %7B%0A
-  
     // 4. If
@@ -348,26 +348,24 @@
 turn S.%0A    
- 
- 
 return S;%0A  
@@ -362,18 +362,16 @@
 turn S;%0A
-  
   %7D else
@@ -369,26 +369,24 @@
 %0A  %7D else %7B%0A
-  
     // 4. Le
@@ -429,18 +429,16 @@
 h).%0A    
-  
 var intM
@@ -472,25 +472,21 @@
 ength);%0A
-  
   %7D%0A%0A
-  
   // 5. 
@@ -550,18 +550,16 @@
 Length.%0A
-  
   var fi
@@ -590,26 +590,24 @@
 .length;%0A%0A  
- 
- 
 if ( fillLen
@@ -635,26 +635,24 @@
 nfinity ) %7B%0A
-  
     // 6. If
@@ -704,18 +704,16 @@
 on.%0A    
- 
- 
 // 7. If
@@ -767,18 +767,16 @@
 on.%0A    
-  
 throw ne
@@ -822,18 +822,16 @@
 gth ' +%0A
-  
         
@@ -881,25 +881,21 @@
 ' + S);%0A
-  
   %7D%0A%0A
-  
   // 8. 
@@ -945,18 +945,16 @@
 illStr.%0A
-  
   // 9. 
@@ -1012,26 +1012,24 @@
 haracter.%0A  
- 
- 
 var sFillStr
@@ -1083,18 +1083,16 @@
 ar);%0A%0A  
- 
- 
 // 10. L
@@ -1164,18 +1164,16 @@
 is met.%0A
-  
   var sF
@@ -1229,18 +1229,16 @@
 th);%0A%0A  
- 
- 
 // trunc
@@ -1258,18 +1258,16 @@
 rflowed%0A
-  
   if ( s
@@ -1291,26 +1291,24 @@
 fillLen ) %7B%0A
-  
     sFillVal
@@ -1342,17 +1342,13 @@
 n);%0A
-  
   %7D%0A%0A
-  
   if
@@ -1359,26 +1359,24 @@
 eft ) %7B%0A    
-  
 // 11. Retur
@@ -1421,26 +1421,24 @@
 d by S.%0A    
-  
 return sFill
@@ -1454,18 +1454,16 @@
 t(S);%0A  
- 
- 
 %7D else %7B
@@ -1463,18 +1463,16 @@
  else %7B%0A
-  
     // 1
@@ -1529,18 +1529,16 @@
 al.%0A    
- 
- 
 return S
@@ -1562,16 +1562,9 @@
 ;%0A  
-  %7D%0A  %7D;
+%7D
 %0A%7D;%0A

make lint-free