Skip to content

Instantly share code, notes, and snippets.

@andrIvash
Created May 15, 2017 13:59
Show Gist options
  • Select an option

  • Save andrIvash/0066904fd4df9d7059c2993e9b25eda1 to your computer and use it in GitHub Desktop.

Select an option

Save andrIvash/0066904fd4df9d7059c2993e9b25eda1 to your computer and use it in GitHub Desktop.
truncate - перевод строки
function truncate(str, maxlength) {
if (str.length > maxlength) {
var arr = str.split(''),
count = arr.length / maxlength | 0,
result = '';
for (var i=1; i<= count; i++) {
var part = '';
if(arr[i * maxlength] !== ' ') {
part = arr.splice(0, i * maxlength + 1);
} else {
part = arr.splice(0, i * maxlength);
}
result += part.join('').trim() + '\n';
}
return result;
}
return str;
}
console.log( truncate('Вот, что мне хотелось бы сказать на эту тему:', 20) );
console.log( truncate('Всем привет!', 20) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment