Permalink
Browse files

add more methods for versions

  • Loading branch information...
reiz
reiz committed Apr 4, 2012
1 parent 1ee15b0 commit 45cbde98b576d0baefcc4704792e30ad8d34e6ce
Showing with 129 additions and 38 deletions.
  1. +48 −12 lib/naturalsorter.rb
  2. +0 −4 lib/versioncmp.rb
  3. +81 −22 spec/naturalsorter_spec.rb
View
@@ -43,6 +43,22 @@ def self.sort_desc(array, caseinsesitive)
array.sort { |a, b| Natcmp.natcmp(a,b,caseinsesitive) }
end
+ # 'Natural order' sort for an array of objects.
+ def self.sort_by_method(array, method, caseinsesitive)
+ if (array.nil? || array.empty?)
+ return nil
+ end
+ array.sort { |a,b| Natcmp.natcmp(a.send(method),b.send(method),caseinsesitive) }
+ end
+
+ def self.sort_desc_by_method(array, method, caseinsesitive)
+ if (array.nil? || array.empty?)
+ return nil
+ end
+ array.sort { |a, b| Natcmp.natcmp(a.send(method),b.send(method),caseinsesitive) }
+ end
+
+
def self.sort_version(array, direction)
return nil if (array.nil? || array.empty?)
if direction.eql? "asc"
@@ -52,14 +68,6 @@ def self.sort_version(array, direction)
end
end
- # 'Natural order' sort for an array of objects.
- def self.sort_by_method(array, method, caseinsesitive)
- if (array.nil? || array.empty?)
- return nil
- end
- array.sort { |a,b| Natcmp.natcmp(a.send(method),b.send(method),caseinsesitive) }
- end
-
def self.sort_version_by_method(array, method, direction)
return nil if (array.nil? || array.empty?)
if direction.eql? "asc"
@@ -69,11 +77,39 @@ def self.sort_version_by_method(array, method, direction)
end
end
- def self.sort_desc_by_method(array, method, caseinsesitive)
- if (array.nil? || array.empty?)
- return nil
+ def self.get_newest_version(first, second)
+ array = [first, second]
+ array = array.sort { |a,b| Versioncmp.compare( a, b ) }
+ array.last
+ end
+
+ def self.is_version_current?(version, current_version)
+ version = version.gsub("~>", "")
+ versions = version.split(".")
+ currents = current_version.split(".")
+ min_length = versions.size
+ if currents.size < min_length
+ min_length = currents.size
end
- array.sort { |a, b| Natcmp.natcmp(a.send(method),b.send(method),caseinsesitive) }
+ min_length = min_length - 2
+ if min_length < 0
+ min_length = 0
+ end
+ (0..min_length).each do |z|
+ ver = versions[z]
+ cur = currents[z]
+ if (cur > ver)
+ return false
+ end
+ end
+ if currents.size < versions.size
+ ver = versions[min_length + 1]
+ cur = currents[min_length + 1]
+ if cur > ver
+ return false
+ end
+ end
+ true
end
end
View
@@ -38,7 +38,6 @@ def self.compare(a, b)
part1 = Versioncmp.getAPiece(offset1, a);
part2 = Versioncmp.getAPiece(offset2, b);
-
offset1 += part1.length() + 1;
offset2 += part2.length() + 1;
@@ -83,18 +82,15 @@ def self.compareString(a, b)
end
def self.checkForRC(a, b)
- # p "checkForRc #{a} #{b}"
big = String.new(a)
small = String.new(b)
if (a.length() < b.length())
big = String.new(b)
small = String.new(a)
end
if (Versioncmp.isRc(big))
- # p " it is RC #{big}"
bigwithoutRc = big.gsub(/\.RC[1-9]*$/, "").gsub(/\.rc[1-9]*$/, "")
if (Versioncmp.compareString(bigwithoutRc, small) == 0)
- # p " it is RC #{bigwithoutRc} -1"
return Versioncmp.getRcValue(a, b)
end
elsif (Versioncmp.isBeta(big))
View
@@ -2,40 +2,99 @@
describe Naturalsorter::Sorter do
- it "cba is abc" do
- Naturalsorter::Sorter.sort(["c", "b", "a"], true).should eql(["a", "b", "c"])
+ describe "sort" do
+ it "cba is abc" do
+ Naturalsorter::Sorter.sort(["c", "b", "a"], true).should eql(["a", "b", "c"])
+ end
+ it "c400b5a1 is a1b5c400" do
+ Naturalsorter::Sorter.sort(["a400", "a5", "a1"], true).should eql(["a1", "a5", "a400"])
+ end
end
- it "c400b5a1 is a1b5c400" do
- Naturalsorter::Sorter.sort(["a400", "a5", "a1"], true).should eql(["a1", "a5", "a400"])
+ describe "sort_by_method" do
+ it "c400b5a1 is a1b5c400" do
+ Naturalsorter::Sorter.sort_by_method(["a400", "a5", "a1"], "to_s", true).should eql(["a1", "a5", "a400"])
+ end
end
- it "c400b5a1 is a1b5c400" do
- Naturalsorter::Sorter.sort_by_method(["a400", "a5", "a1"], "to_s", true).should eql(["a1", "a5", "a400"])
- end
- it "1.2, 1.1 is 1.1, 1.2" do
- Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0"], "asc").should eql(["0.1", "0.4", "1.0", "1.1", "1.2"])
- end
+ describe "sort_version" do
- it "1.2, 1.1 is 1.1, 1.2" do
- Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0", "1.0.RC1"], "asc").should eql(["0.1", "0.4", "1.0.RC1", "1.0", "1.1", "1.2"])
- end
+ it "1.1, 1.0 is 1.0, 1.1" do
+ Naturalsorter::Sorter.sort_version(["1.1", "1.0"], "asc").should eql(["1.0", "1.1"])
+ end
+
+ it "1.0, 1.1 is 1.0, 1.1" do
+ Naturalsorter::Sorter.sort_version(["1.0", "1.1"], "asc").should eql(["1.0", "1.1"])
+ end
+
+ it "4.5, 1.0 is 1.0, 4.5" do
+ Naturalsorter::Sorter.sort_version(["4.5", "1.0"], "asc").should eql(["1.0", "4.5"])
+ end
+
+ it "1.0, 4.5 is 1.0, 4.5" do
+ Naturalsorter::Sorter.sort_version(["1.0", "4.5"], "asc").should eql(["1.0", "4.5"])
+ end
- it "1.2, 1.1 is 1.1, 1.2" do
- Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0", "1.0.RC1"], "desc").should eql(["1.2", "1.1", "1.0", "1.0.RC1", "0.4", "0.1"])
+ it "1.2, 1.1 is 1.1, 1.2" do
+ Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0"], "asc").should eql(["0.1", "0.4", "1.0", "1.1", "1.2"])
+ end
+
+ it "1.2, 1.1 is 1.1, 1.2" do
+ Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0", "1.0.RC1"], "asc").should eql(["0.1", "0.4", "1.0.RC1", "1.0", "1.1", "1.2"])
+ end
+
+ it "1.2, 1.1 is 1.1, 1.2" do
+ Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0", "1.0.RC1"], "desc").should eql(["1.2", "1.1", "1.0", "1.0.RC1", "0.4", "0.1"])
+ end
+
+ it "1.2, 1.1 is 1.1, 1.2" do
+ Naturalsorter::Sorter.sort_version(["1.1", "1.2", "1.0"], "asc").should eql(["1.0", "1.1", "1.2"])
+ end
+
+ it "sort with RC" do
+ Naturalsorter::Sorter.sort_version(["1.1", "1.1.RC1"], "asc").should eql(["1.1.RC1", "1.1"])
+ end
+
+ it "sort with RC" do
+ Naturalsorter::Sorter.sort_version(["1.1.RC1", "1.1", "1.0"], "asc").should eql(["1.0", "1.1.RC1", "1.1"])
+ end
+
end
- it "1.2, 1.1 is 1.1, 1.2" do
- Naturalsorter::Sorter.sort_version(["1.1", "1.2", "1.0"], "asc").should eql(["1.0", "1.1", "1.2"])
+ describe "is_version_current?" do
+ it "returns true" do
+ Naturalsorter::Sorter.is_version_current?("1.1.1", "1.1.9").should be_true
+ end
+ it "returns false" do
+ Naturalsorter::Sorter.is_version_current?("1.1.1", "1.2.0").should be_false
+ end
+ it "returns false" do
+ Naturalsorter::Sorter.is_version_current?("1.1.1", "1.2").should be_false
+ end
+ it "returns false" do
+ Naturalsorter::Sorter.is_version_current?("1.1.1", "2.0").should be_false
+ end
+ it "returns false" do
+ Naturalsorter::Sorter.is_version_current?("1.1.1", "2").should be_false
+ end
end
- it "sort with RC" do
- Naturalsorter::Sorter.sort_version(["1.1", "1.1.RC1"], "asc").should eql(["1.1.RC1", "1.1"])
- end
+ describe "get_newest_version" do
+
+ it "returns 2.0" do
+ Naturalsorter::Sorter.get_newest_version("1.1", "2.0").should eql("2.0")
+ end
+ it "returns 1.1" do
+ Naturalsorter::Sorter.get_newest_version("1.1", "1.0").should eql("1.1")
+ end
+ it "returns 4.5" do
+ Naturalsorter::Sorter.get_newest_version("4.5", "1.0").should eql("4.5")
+ end
+ it "returns 1.0" do
+ Naturalsorter::Sorter.get_newest_version("1.0", "1.0").should eql("1.0")
+ end
- it "sort with RC" do
- Naturalsorter::Sorter.sort_version(["1.1.RC1", "1.1", "1.0"], "asc").should eql(["1.0", "1.1.RC1", "1.1"])
end
end

0 comments on commit 45cbde9

Please sign in to comment.