Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/lowmess/lameass.recipes. A recipe site, but without the ads and 3,000 word essays lameass.recipes
nextjs sanity-io theme-ui typescript
Something went wrong. Try again.
817 B · 21 lines
TypeScript
at develop
12345678910111213141516171819202122const unwidow = (str: string): string => { if (typeof str !== 'string') return str
// Remove extra whitespace characters before starting. const clean = str.trim().replace(/\s+/g, ' ')
// We only care about strings with at least 4 words, so if we don't find at // least 3 spaces in the string we can just return the cleaned up string. const count = clean.match(/\s/g) || [] if (count.length < 4) return clean
// If the last word is long enough, just return the cleaned string. if (clean.split(' ').pop().length > 15) return clean
// Regex matches the last instance of a whitespace character that is followed // by a non-whitespace character. It then replaces the former with a // non-breaking space character (\u00A0). return clean.replace(/\s+([\S]*)(\s*)$/g, '\u00A0$1$2')}
export default unwidow