Shave extra characters from text with one line of code

Truncate text to give symmetric ui design layout. Mostly seen with blog articles description.

This code is inspired from ShaveJS and uses that approach in one line.

First of all, here's the code in one line.

const shave=(s, n)=>(s.length>n) ? s.substr(0, n-2)+'..' : s;

This function takes two parameters, string and length. Then truncates (or shaves) the string to that length with ellipsis at end.

Now for readability, more elaborated version.

function shave(string, length){
    return (string.length>length) ? string.substr(0, n-2)+'..' : string;
}

Psst. this is also used in this blog and length of text is 100 characters (yep, I counted).

See you later or maybe on Twitter