Skip to content

Instantly share code, notes, and snippets.

@amccloud
Created June 19, 2014 20:39
Show Gist options
  • Save amccloud/3fc684661c642cde7e2f to your computer and use it in GitHub Desktop.
Save amccloud/3fc684661c642cde7e2f to your computer and use it in GitHub Desktop.
module TypographyHelper
WIDOW_EXPRESSION = /([^\s])\s+([^\s]+)\s*$/
def widont(text, count=1)
count.times.inject(text) do |text|
text.sub(WIDOW_EXPRESSION, '\1 \2')
end
end
end
require 'spec_helper'
describe TypographyHelper do
specify '#widont' do
expect(helper.widont('')).to eq ''
expect(helper.widont('text')).to eq 'text'
expect(helper.widont('text with widow')).to eq 'text with widow'
expect(helper.widont('text with widow', 1)).to eq 'text with widow'
expect(helper.widont('text with widow', 2)).to eq 'text with widow'
expect(helper.widont('text with widow', 3)).to eq 'text with widow'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment