mirror of
https://github.com/mastodon/mastodon.git
synced 2024-10-21 00:56:14 +00:00
e7aa2be828
* Change how hashtags are normalized * Fix tests
26 lines
455 B
Ruby
26 lines
455 B
Ruby
# frozen_string_literal: true
|
|
|
|
class HashtagNormalizer
|
|
def normalize(str)
|
|
remove_invalid_characters(ascii_folding(lowercase(cjk_width(str))))
|
|
end
|
|
|
|
private
|
|
|
|
def remove_invalid_characters(str)
|
|
str.gsub(/[^[:alnum:]#{Tag::HASHTAG_SEPARATORS}]/, '')
|
|
end
|
|
|
|
def ascii_folding(str)
|
|
ASCIIFolding.new.fold(str)
|
|
end
|
|
|
|
def lowercase(str)
|
|
str.mb_chars.downcase.to_s
|
|
end
|
|
|
|
def cjk_width(str)
|
|
str.unicode_normalize(:nfkc)
|
|
end
|
|
end
|