Skip to content

Instantly share code, notes, and snippets.

@dkacper
Created June 4, 2022 13:29
Show Gist options
  • Save dkacper/752352d1359254a25c762599e1fdce7d to your computer and use it in GitHub Desktop.
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.
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