{"size": 2344, "ext": "js", "max_stars_repo_path": "JavaScript Advanced/04.Object Composition/Exercises/05.String extension.js", "max_stars_repo_name": "svetlimladenov/JavaScript-Core", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "JavaScript Advanced/04.Object Composition/Exercises/05.String extension.js", "max_issues_repo_name": "svetlimladenov/JavaScript-Core", "max_issues_repo_issues_event_min_datetime": "2020-09-04T19:19:58.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T10:06:01.000Z", "max_forks_repo_path": "JavaScript Advanced/04.Object Composition/Exercises/05.String extension.js", "max_forks_repo_name": "svetlimladenov/JavaScript-Core", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 10, "max_forks_count": null, "avg_line_length": 28.5853658537, "max_line_length": 98, "alphanum_fraction": 0.5362627986}
(() => {
    String.prototype.ensureStart = function (str) {
        const currentStr = this.valueOf();
        if (currentStr.indexOf(str) === 0){
            return currentStr;
        }

        return str + currentStr;
    };

    String.prototype.ensureEnd = function (str) {
        const currentStr = this.valueOf();
        if (currentStr.endsWith(str)){
            return currentStr;
        }

        return currentStr + str;
    };

    String.prototype.isEmpty  = function () {
        return this.valueOf().length === 0;
    };

    String.prototype.truncate = function (n) {
        const currentStr = this.valueOf();
        if (n >= currentStr.length){
            return currentStr;
        }
        if (n + 3 < 4)
        {
            return '.'.repeat(n);
        }

        let splitedStr = currentStr.split(' ');
        if (splitedStr.length > 1) {
            let lastPart = splitedStr.pop();
            let newLenght = splitedStr.join(' ').length;
            while (newLenght + 3 > n){
                splitedStr.pop();
                newLenght = splitedStr.join(' ').length;
            }
            let dots = 3;
            let dotChar = '.';
            return splitedStr.join(' ') + dotChar.repeat(dots);
        }else{
            let lastStringPartLeft = splitedStr[0];
            let returnValue = lastStringPartLeft.slice(0,n - 3);
            returnValue = returnValue + '.'.repeat(3);
            return returnValue;
        }
    };

    String.format = function(string){
        let tempStr = string;
        let tempArguments = arguments;
        let tempParameters = Array.from(tempArguments).slice(1, Array.from(tempArguments).length);

        Array.from(tempParameters).forEach((p, index) => {
            let parameter = "{" + index.toString() + "}";
            if(tempStr.includes(parameter)){
                tempStr = tempStr.replace(parameter, p);
            }
        });

        return tempStr;
    }
})();

// let str = 'my string';
// str = str.ensureStart('my');
// str = str.ensureStart('hello ');
// str = str.truncate(16);
// str = str.truncate(14);
// str = str.truncate(8);
// str = str.truncate(4);
// str = str.truncate(2);
// console.log(str);

var testString = 'the quick brown fox jumps over the lazy dog';

testString = testString.truncate(10);
console.log(testString);