Skip to content

Instantly share code, notes, and snippets.

@rubenCodeforges
Created October 30, 2014 09:36
Show Gist options
  • Save rubenCodeforges/c77b32d794272051e797 to your computer and use it in GitHub Desktop.
Save rubenCodeforges/c77b32d794272051e797 to your computer and use it in GitHub Desktop.
JS create excerpt example
var string = "some text here with some amount of chars lets rteplaece it like a excerpt";
var description = getExcerpt(string,10);
// принимает строку и лимит после скольки знаков искать пробел и обрезать
function getExcerpt( str, limit ){
var fullText = str;
var shortText = str;
shortText = shortText.substr( 0, shortText.lastIndexOf( ' ', limit ) ) + '...';
var returnString = {
fullText: fullText,
shortText: shortText
};
return returnString;
}
// функция возвратит объект , свойство shortText короткое описание обрезанное по limit и пробелу fullText полный текст
console.log(description.shortText);
console.log(description.fullText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment