Created
May 15, 2017 13:59
-
-
Save andrIvash/0066904fd4df9d7059c2993e9b25eda1 to your computer and use it in GitHub Desktop.
truncate - перевод строки
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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