Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Created May 6, 2013 14:40
Show Gist options
  • Save rmosolgo/5525569 to your computer and use it in GitHub Desktop.
Save rmosolgo/5525569 to your computer and use it in GitHub Desktop.
JavaScript to split a string into an array of strings close to the specified length.
split_to_fit = (string, allowable_width) ->
# console.log("splitting '#{string}' to fit #{allowable_width}")
splitters = [' ', ',', '-', '.'] # allowable splitters
new_strings = []
for w in [allowable_width..0]
# console.log("trying width=#{w}, which returns '#{string[w]}'")
if string[w] in splitters
# console.log("found a splitter at #{w} in #{string}")
if string[w] is ' '
end_of_word = w-1
else
end_of_word = w
new_strings.push string[0..end_of_word]
shorter_string = string[w+1..]
if shorter_string.length > allowable_width
next_split = split_to_fit(shorter_string, allowable_width)
# console.log("split_to_fit is joining in", next_split, " to ", new_strings)
new_strings = new_strings.concat(next_split)
# console.log("after joining in", next_split, ", split_to_fit has ", new_strings)
else
new_strings.push shorter_string
break
else if w is 0
# If you didn't find a break on the inside,
# then get desperate and search for a break on the outside.
for w2 in [0..string.length]
if string[w2] in splitters
if string[w2] is ' '
end_of_word = w2-1
else
end_of_word = w2
new_strings.push string[0..end_of_word]
shorter_string = string[w2+1..]
if shorter_string.length > allowable_width
next_split = split_to_fit(shorter_string, allowable_width)
# console.log("split_to_fit is joining in", next_split, " to ", new_strings)
new_strings = new_strings.concat(next_split)
# console.log("after joining in", next_split, ", split_to_fit has ", new_strings)
else
new_strings.push shorter_string
break
else if w2 == string.length
# If you still don't find one, then return the string without a break.
# console.log("no split found in '#{string}', returning it anyways!")
new_strings.push string
# console.log('split_to_fit is returning', new_strings)
return new_strings
// compiled from split_to_fit.coffee
split_to_fit = function(string, allowable_width) {
var end_of_word, new_strings, next_split, shorter_string, splitters, w, w2, _i, _j, _ref, _ref1, _ref2;
splitters = [' ', ',', '-', '.'];
new_strings = [];
for (w = _i = allowable_width; allowable_width <= 0 ? _i <= 0 : _i >= 0; w = allowable_width <= 0 ? ++_i : --_i) {
if (_ref = string[w], __indexOf.call(splitters, _ref) >= 0) {
if (string[w] === ' ') {
end_of_word = w - 1;
} else {
end_of_word = w;
}
new_strings.push(string.slice(0, +end_of_word + 1 || 9e9));
shorter_string = string.slice(w + 1);
if (shorter_string.length > allowable_width) {
next_split = split_to_fit(shorter_string, allowable_width);
new_strings = new_strings.concat(next_split);
} else {
new_strings.push(shorter_string);
}
break;
} else if (w === 0) {
for (w2 = _j = 0, _ref1 = string.length; 0 <= _ref1 ? _j <= _ref1 : _j >= _ref1; w2 = 0 <= _ref1 ? ++_j : --_j) {
if (_ref2 = string[w2], __indexOf.call(splitters, _ref2) >= 0) {
if (string[w2] === ' ') {
end_of_word = w2 - 1;
} else {
end_of_word = w2;
}
new_strings.push(string.slice(0, +end_of_word + 1 || 9e9));
shorter_string = string.slice(w2 + 1);
if (shorter_string.length > allowable_width) {
next_split = split_to_fit(shorter_string, allowable_width);
new_strings = new_strings.concat(next_split);
} else {
new_strings.push(shorter_string);
}
break;
} else if (w2 === string.length) {
new_strings.push(string);
}
}
}
}
return new_strings;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment