Skip to content

Instantly share code, notes, and snippets.

@i-havr
Created June 9, 2023 10:49
Show Gist options
  • Save i-havr/deae1ca8874c01d0ccc1e396714af687 to your computer and use it in GitHub Desktop.
Save i-havr/deae1ca8874c01d0ccc1e396714af687 to your computer and use it in GitHub Desktop.
Функція повертає скорочений до заданої кількості символів рядок із трьома крапками у кінці.
const LIMIT = 30;
const endStringWithThreeDots = (str) => {
if (str.length <= LIMIT) {
return str;
} else {
const updatedString = str.slice(0, LIMIT - 3) + "...";
return updatedString;
}
};
@Serrin
Copy link

Serrin commented Jun 9, 2023

// shorter version:
const endStringWithThreeDots = (str, limit = 30) =>
  (str.length <= limit) ? str : str.slice(0, limit - 3) + "...";

// unicode compatible:
function endStringWithThreeDots (str, limit = 30) {
  var A = Array.from(str);
  return (A.length <= limit) ? str : A.slice(0, limit - 3).join("") + "...";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment