Commit 4f233d37 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Rubified first_sentence method.

Using \A and \Z is more clear, as these match the beginning and
end of string, while ^ and $ match beginning and end of line.

Removed unneeded variable.

Added rspec to make sure it all works as intended.
parent e5ffa377
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ module JsDuck
    end

    def first_sentence(str)
      r = str.sub(/^(.+?\.)\s.*$/m, "\\1")
      str.sub(/\A(.+?\.)\s.*\Z/m, "\\1")
    end

  end

spec/members_spec.rb

0 → 100644
+32 −0
Original line number Diff line number Diff line
require "jsduck/class"
require "jsduck/members"

describe JsDuck::Members do

  before do
    @members = JsDuck::Members.new
  end

  describe "first_sentence" do
    it "extracts first sentence" do
      @members.first_sentence("Hi John. This is me.").should == "Hi John."
    end
    it "extracts first sentence of multiline text" do
      @members.first_sentence("Hi\nJohn.\nThis\nis\nme.").should == "Hi\nJohn."
    end
    it "returns everything if no dots in text" do
      @members.first_sentence("Hi John this is me").should == "Hi John this is me"
    end
    it "returns everything if no dots in text" do
      @members.first_sentence("Hi John this is me").should == "Hi John this is me"
    end
    it "ignores dots inside words" do
      @members.first_sentence("Hi John th.is is me").should == "Hi John th.is is me"
    end
    it "ignores first empty sentence" do
      @members.first_sentence(". Hi John. This is me.").should == ". Hi John."
    end
  end

end