Skip to content

Instantly share code, notes, and snippets.

@Yanrishatum
Created May 5, 2017 05:07
Show Gist options
  • Save Yanrishatum/cf77f241ab44968491ed61ec7011bb64 to your computer and use it in GitHub Desktop.
Save Yanrishatum/cf77f241ab44968491ed61ec7011bb64 to your computer and use it in GitHub Desktop.
Simple WordWrap sample for Miaut
function wordWrap(text, width, spaceSize)
{
var lines = text.split("\n"); // In case input have newlines, we want to preserve them.
var output = [];
for (var line of lines)
{
var newLine = "";
var words = line.split(" ");
var lineSize = 0;
for (var word of words)
{
var size = word.length * 8; // There may be as well function that calculates word size.
// This is a bit basic, with unnecessary space inserted, you probably want to trim it.
if (lineSize + size >= width)
{
lineSize = size + spaceSize; // reset size, add \n before word.
newLine += "\n" + word + " ";
}
else
{
lineSize += size + spaceSize; // Just add word to line.
newLine += word + " ";
}
}
output.push(newLine);
}
return output.join("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment