mirror of
https://github.com/danbee/slugtastic
synced 2025-03-04 08:49:05 +00:00
Added basic transliteration with IConv.
This commit is contained in:
parent
38893fa515
commit
44d9f806cb
@ -2,8 +2,12 @@ require "slugtastic/version"
|
||||
require "slugtastic/model_additions"
|
||||
require "slugtastic/railtie" if defined? Rails
|
||||
|
||||
# TODO: iconv will be deprecated in the future.
|
||||
require 'iconv'
|
||||
|
||||
module Slugtastic
|
||||
def self.generate_slug(string)
|
||||
string.downcase.gsub(/ /, '_').gsub(/[^a-z0-9\-_]/, '') unless string.nil?
|
||||
return if string.nil?
|
||||
Iconv.iconv("ASCII//TRANSLIT//IGNORE", "UTF-8", string).join.downcase.gsub(/ /, '_').gsub(/[^a-z0-9\-_]/, '')
|
||||
end
|
||||
end
|
||||
|
||||
@ -3,6 +3,10 @@ require 'spec_helper'
|
||||
|
||||
describe Slugtastic do
|
||||
describe ".generate_slug" do
|
||||
it "returns empty if the input string is empty" do
|
||||
Slugtastic.generate_slug("").should eq ""
|
||||
end
|
||||
|
||||
it "generates a slug from a simple string" do
|
||||
Slugtastic.generate_slug("A simple string.").should eq "a_simple_string"
|
||||
end
|
||||
@ -14,5 +18,10 @@ describe Slugtastic do
|
||||
it "handles strings with other characters in them" do
|
||||
Slugtastic.generate_slug("A string, with /All sorts!").should eq "a_string_with_all_sorts"
|
||||
end
|
||||
|
||||
it "handles basic transliteration" do
|
||||
Slugtastic.generate_slug("Un été À la maison.").should eq "un_ete_a_la_maison"
|
||||
Slugtastic.generate_slug("Ātri brūna lapsa").should eq "atri_bruna_lapsa"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user