Created
June 4, 2022 13:29
-
-
Save dkacper/752352d1359254a25c762599e1fdce7d to your computer and use it in GitHub Desktop.
A pure function that adds nbsp between one-character word and a previous word.
This file contains 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
export const nbspScript = (text: string) => { | |
const input = text.split(' '); | |
let output = input[input.length - 1]; | |
for (let index = 0; index < input.length; index++) { | |
const reversedIndex = input.length - index - 1; | |
const word = input[reversedIndex]; | |
if (reversedIndex !== input.length - 1) { | |
if (word.length > 1) { | |
output = `${word} ${output}`; | |
} else { | |
output = `${word}\u00A0${output}`; | |
} | |
} | |
} | |
return output; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment