-
-
Save Serrin/c4e057951c931952353708532d3309a4 to your computer and use it in GitHub Desktop.
Функція повертає скорочений до заданої кількості символів рядок із трьома крапками у кінці.
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
// https://gist.github.com/i-havr/deae1ca8874c01d0ccc1e396714af687 | |
// v0 | |
const LIMIT = 30; | |
const endStringWithThreeDots = (str) => { | |
if (str.length <= LIMIT) { | |
return str; | |
} else { | |
const updatedString = str.slice(0, LIMIT - 3) + "..."; | |
return updatedString; | |
} | |
}; | |
// shorter version: | |
const endStringWithThreeDots = (str, limit = 30) => | |
(str.length <= limit) ? str : str.slice(0, limit - 3) + "..."; | |
// unicode compatible: | |
const endStringWithThreeDots = (str, limit = 30) => | |
(str.length <= limit) ? str : Array.from(str).slice(0, limit - 3).join("") + "..."; | |
// unicode compatible fixed: | |
function endStringWithThreeDots (str, limit = 30) { | |
var A = Array.from(str); | |
return (A.length <= limit) ? str : A.slice(0, limit - 3).join("") + "..."; | |
} | |
console.log(endStringWithThreeDots("dgdfsdf")); | |
console.log(endStringWithThreeDots("dgdfsdf",5)); | |
console.log(endStringWithThreeDots("dgdfsddsfdsffdsafjkdsakdshakhdaskhfkadsjhfkdsfjkdsggdsjfddsf")); | |
console.log(endStringWithThreeDots("dgdfsddsfdsffdsafjkdsakdshakhdaskhfkadsjhfkdsfjkdsggdsjfddsf",15)); | |
/* | |
dgdfsdf | |
dg... | |
dgdfsddsfdsffdsafjkdsakdsha... | |
dgdfsddsfdsf... | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment