Skip to content

Commit

Permalink
Added Literal#squish and #squish! to remove leading/trailing whitespa…
Browse files Browse the repository at this point in the history
…ce and multiple internal whitespace characters from the value of a literal.
  • Loading branch information
gkellogg committed Sep 4, 2016
1 parent 6919f96 commit 7fe2e4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/rdf/model/literal.rb
Expand Up @@ -408,6 +408,30 @@ def canonicalize!
self
end

##
# Returns the literal, first removing all whitespace on both ends of the value, and then changing remaining consecutive whitespace groups into one space each.
#
# Note that it handles both ASCII and Unicode whitespace.
#
# @see [String#squish](http://apidock.com/rails/String/squish)
# @return [RDF::Literal] a new literal based on `self`.
def squish(*other_string)
self.dup.squish!
end

##
# Performs a destructive {#squish}.
#
# @see [String#squish!](http://apidock.com/rails/String/squish%21)
# @return self
def squish!
@string = value.
gsub(/\A[[:space:]]+/, '').
gsub(/[[:space:]]+\z/, '').
gsub(/[[:space:]]+/, ' ')
self
end

##
# Escape a literal using ECHAR escapes.
#
Expand Down
27 changes: 26 additions & 1 deletion spec/model_literal_spec.rb
Expand Up @@ -248,7 +248,6 @@ def self.literals(*selector)
end
end


describe "#compatible?" do
{
%("abc") => {
Expand Down Expand Up @@ -286,6 +285,32 @@ def self.literals(*selector)
end
end

describe "#squish" do
let(:result) {"a b c"}
subject {RDF::Literal(" a\n b c\n ")}

it "squeezes properly" do
expect(subject.squish).to eq result
end

it "returns a new object" do
expect(subject.squish).not_to equal subject
end
end

describe "#squish!" do
let(:result) {"a b c"}
subject {RDF::Literal(" a\n b c\n ")}

it "squeezes properly" do
expect(subject.squish!).to eq result
end

it "returns itself" do
expect(subject.squish!).to equal subject
end
end

describe RDF::Literal::Boolean do
it_behaves_like 'RDF::Literal with datatype and grammar', "true", RDF::XSD.boolean
it_behaves_like 'RDF::Literal equality', "true", true
Expand Down

0 comments on commit 7fe2e4e

Please sign in to comment.