-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bebb7a8
commit bd3f888
Showing
4 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class PermalinkNormalizer | ||
attr_accessor :text, :max_length | ||
|
||
def initialize(text, max_length: 50) | ||
self.text = text | ||
self.max_length = max_length | ||
end | ||
|
||
def normalize | ||
text | ||
.gsub(/((?!\p{Alnum}|\p{So}).)+/, "-") | ||
.delete_prefix("-") | ||
.delete_suffix("-") | ||
.downcase[0..max_length - 1] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# coding: utf-8 | ||
require 'rails_helper' | ||
|
||
RSpec.describe PermalinkNormalizer do | ||
describe "#normalize" do | ||
subject { PermalinkNormalizer.new(text, max_length: 50).normalize } | ||
|
||
let(:text) { "Hello world!" } | ||
|
||
it { is_expected.to eql("hello-world") } | ||
|
||
context "with a very long title" do | ||
let(:text) { "x" * 200 } | ||
|
||
it { is_expected.to eql("x" * 50) } | ||
end | ||
|
||
context "with bad characters at start and end" do | ||
let(:text) { "~ hello world ~" } | ||
|
||
it { is_expected.to eql("hello-world") } | ||
end | ||
|
||
context "with numbers" do | ||
let(:text) { "3 days ago" } | ||
|
||
it { is_expected.to eql("3-days-ago") } | ||
end | ||
|
||
context "with diacritics" do | ||
let(:text) { "naïve café" } | ||
|
||
it { is_expected.to eql("naïve-café") } | ||
end | ||
|
||
context "with kanji and kana" do | ||
let(:text) { "初めまして、ダニエルです。" } | ||
|
||
it { is_expected.to eql("初めまして-ダニエルです") } | ||
end | ||
|
||
context "with emoji" do | ||
let(:text) { "💎 is a gemstone 😃" } | ||
|
||
it { is_expected.to eql("💎-is-a-gemstone-😃") } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters