Skip to content

Instantly share code, notes, and snippets.

@varyonic
Created August 21, 2014 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varyonic/ccda540c417a6bd49aec to your computer and use it in GitHub Desktop.
Save varyonic/ccda540c417a6bd49aec to your computer and use it in GitHub Desktop.
makes titleize respect hypens
# inspired by https://gist.github.com/zachrose/7549941
module ActiveSupport::Inflector
def nice_title(phrase)
return phrase if phrase =~ /^-+$/
phrase.split('-').map { |part|
if part.chars.count == part.bytes.count
part.titleize
else
part.split(' ').map { |word| word.mb_chars.titleize }.join(' ')
end
}.join('-')
end
end
# encoding: utf-8
# https://gist.github.com/zachrose/7549941
require 'spec_helper'
require 'active_support'
require 'inflector_extensions'
include ActiveSupport::Inflector
describe "nice_title" do
it "makes titleize respect hypens" do
nice_title('man from the boondocks').should == "Man From The Boondocks"
nice_title('x-men: the last stand').should == "X-Men: The Last Stand"
nice_title('TheManWithoutAPast').should == "The Man Without A Past"
nice_title('raiders_of_the_lost_ark').should == "Raiders Of The Lost Ark"
nice_title('-').should == "-"
nice_title('-----').should == "-----"
nice_title('JUANA MARÍA MELÉNDEZ').should == "Juana María Meléndez"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment