Hey is a decentralized and permissionless social media app built with Lens Protocol 🌿

Refactor truncateByWords helper for improved logic and readability

yoginth.com 8d5626af e8e6b7d4

verified
+3 -6
+3 -6
apps/web/src/helpers/truncateByWords.ts
··· 1 - const truncateByWords = (string: string, count: number): string => { 2 - const strArr = string.split(" "); 3 - if (strArr.length > count) { 4 - return `${strArr.slice(0, count).join(" ")}…`; 5 - } 6 - return string; 1 + const truncateByWords = (str: string, count: number): string => { 2 + const words = str.trim().split(/\s+/); 3 + return words.length > count ? `${words.slice(0, count).join(" ")}…` : str; 7 4 }; 8 5 9 6 export default truncateByWords;