Sha256: 4fedbe0c4e0fa69a84aefc54473f3e4b2ce11287539ae70321109e5cbc385e3f
Contents?: true
Size: 1.9 KB
Versions: 2
Compression:
Stored size: 1.9 KB
Contents
module Paxx require 'babosa' class NameNormalizer attr_reader :name, :first_name, :last_name, :full_name def initialize src_name @name = src_name @first_name, @last_name = splitt end def valid !(name.to_s.strip.length == 0) end def normalize(txt=name) txt.to_s.gsub("\n", ' ').squeeze(' ').strip end def subst_norwegian_chars(txt) [["æ", "ae"], ["ø", "oe"], ["å", "aa"]].each do |int| txt = txt.gsub(int[0], int[1]) end [["Æ", "AE"], ["Ø", "OE"], ["Å", "AA"]].each do |int| txt = txt.gsub(int[0], int[1]) end txt end def camelize(txt=name) txt.split(/[^a-zøæåØÆÅ0-9]/i).map { |w| w.capitalize }.join if txt end def splitt words = name.to_s.split() if words.count > 1 last_name = words.last words.pop first_name = words.join(" ") else first_name = name last_name="" end [camelize(first_name), camelize(last_name)] end def as_id(txt=name) subst_norwegian_chars(txt.delete(" ").delete("-")).downcase if txt end def compose_short_ref first,last "#{first}#{last}" end def last_part name,counter if name && counter < name.length name[0..counter] else "#{name}#{counter-name.length+1}" end end def as_short_ref counter = 0 unique = !block_given? xref = "" start = camelize(as_id(first_name)) begin shortref = compose_short_ref start,xref if !unique unique = yield shortref if !unique counter += 1 xref = last_part(camelize(as_id(last_name)),counter) end end end while !unique && counter < 100 shortref end def as_slug(txt=name) txt.to_s.to_slug.normalize(transliterations: :norwegian).to_s end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
paxx-0.1.5 | lib/paxx/names/name_normalizer.rb |
paxx-0.1.4 | lib/paxx/names/name_normalizer.rb |