Skip to content

Instantly share code, notes, and snippets.

@ihorkatkov
Created July 22, 2015 18:20
Show Gist options
  • Save ihorkatkov/bed0356af4c697eeb3eb to your computer and use it in GitHub Desktop.
Save ihorkatkov/bed0356af4c697eeb3eb to your computer and use it in GitHub Desktop.
[JS] Функция усечения строки. Проверяет длину строки str, и если она превосходит maxlength — заменяет конец str на "...", так что ее длина становится равна maxlength. Результатом функции будет усечённая строка.
function truncate(str, maxlength) {
if (str.length > maxlength) {
return str.slice(0, maxlength - 3) + '...';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment