Skip to content

Instantly share code, notes, and snippets.

@alanhoff
Created June 16, 2014 21:23
Show Gist options
  • Select an option

  • Save alanhoff/ac659e6c18b0c3bc294e to your computer and use it in GitHub Desktop.

Select an option

Save alanhoff/ac659e6c18b0c3bc294e to your computer and use it in GitHub Desktop.
var text = 'Seitan XOXO meh cornhole, before they sold out mustache scenester photo booth. IPhone selvage Pitchfork aesthetic. Polaroid post-ironic Thundercats quinoa, roof party PBR mumblecore cred +1 semiotics scenester freegan organic Godard. Cred Banksy meggings actually, Helvetica kogi gluten-free fap ethical slow-carb flexitarian before they sold out four loko. Fixie ugh yr single-origin coffee mustache. Mumblecore cardigan Helvetica mlkshk iPhone. Biodiesel synth pug Banksy mustache food truck McSweeney\'s, Intelligentsia post-ironic whatever ethnic dreamcatcher.';
var cut = function(text, max, delimiter){
return text.split(' ').reduce(function(arr, word){
if(!Array.isArray(arr))
arr = [new Buffer(arr)];
if(Buffer.concat([arr[arr.length -1], new Buffer(' ' + word)]).length > max)
arr.push(new Buffer(word));
else
arr[arr.length - 1] = Buffer.concat([arr[arr.length -1], new Buffer(' ' + word)]);
return arr;
}).reduce(function(buff, item){
return Buffer.concat([buff, new Buffer(delimiter), item]);
});
};
console.log(cut(text, 44, '\n').toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment