/* jshint node:true */

'use strict';

function format(s, args){
    var START = '{{', END = '}}', key = null;
    var idx = 0, lastIdx = 0, endIdx = 0;
    var result = '';
    while(true){
        idx = s.indexOf(START, lastIdx);
        if (idx > -1){
            result += s.substr(lastIdx, idx-lastIdx);
            idx = idx+START.length;
            
            endIdx = s.indexOf(END, idx);
            if (endIdx === -1){
                throw new Error('Invalid arg format. Missing "' + END + '".');
            }

            key = s.substr(idx, endIdx - idx);
            result += args[key] ? args[key].toString() : '';
            lastIdx = endIdx + END.length;
        }
        else {
            break;
        }
    }
    result += s.substr(lastIdx);
    return result;
}

String.prototype.endsWith = function endsWith(endStr){
    if (this === null || endStr === null){ return false; }
    return (this.indexOf(endStr) + endStr.length) === this.length;
};

String.prototype.startsWith = function startsWith(startStr){
    if (this === null || startStr === null){ return false; }
    return (this.indexOf(startStr) === 0);
};

module.exports = {
    format: format
};
@@ -338,28 +338,16 @@
 length;%0A
-            
 %0A       
@@ -773,16 +773,127 @@
 ult;%0A%7D%0A%0A
+// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith%0Aif (typeof 
 String.p
@@ -910,26 +910,29 @@
 ndsWith 
+!=
 = 
+'
 function
  endsWit
@@ -927,157 +927,601 @@
 tion
- endsWith(endStr)%7B%0A    if (this =
+') %7B%0A  String.prototype.endsWith = function(searchString, position) %7B%0A    var subjectString = this.toString();%0A    if (typeof position !
 == 
+'
 nu
-ll %7C%7C endStr === null)%7B return false; %7D%0A    return (this.indexOf(endStr) + endStr.length) === this.length;%0A%7D;%0A%0A
+mber' %7C%7C%0A      !isFinite(position) %7C%7C%0A      Math.floor(position) !== position %7C%7C%0A      position %3E subjectString.length) %7B%0A      position = subjectString.length;%0A    %7D%0A    position -= searchString.length;%0A    var lastIndex = subjectString.lastIndexOf(searchString, position);%0A    return lastIndex !== -1 && lastIndex === position;%0A  %7D;%0A%7D%0A%0A// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith%0Aif (typeof 
 Stri
@@ -1544,26 +1544,29 @@
 rtsWith 
+!=
 = 
+'
 function
  startsW
@@ -1561,91 +1561,109 @@
 tion
- startsWith(startStr)%7B%0A    if (this === null %7C%7C startStr === null)%7B return false; %7D
+') %7B%0A  String.prototype.startsWith = function(searchString, position) %7B%0A    position = position %7C%7C 0;
 %0A   
@@ -1674,42 +1674,75 @@
 urn 
-(
 this.
-indexOf(startStr) === 0);%0A%7D;
+substr(position, searchString.length) === searchString;%0A  %7D;%0A%7D
 %0A%0Amo

Fix string polyfills