Skip to content
Browse files

new version. opt. for timestamps in version

  • Loading branch information...
1 parent 97a78c0 commit ea06b244f78fd283d838a8ecba4f1140d0402757 reiz committed Apr 6, 2012
Showing with 25 additions and 2 deletions.
  1. +1 −1 README.markdown
  2. +1 −1 lib/naturalsorter/version.rb
  3. +11 −0 lib/versioncmp.rb
  4. +4 −0 spec/naturalsorter_spec.rb
  5. +8 −0 spec/versioncmp_spec.rb
View
2 README.markdown
@@ -49,7 +49,7 @@ Get newest.
You should add this line to your Gemfile
-`gem 'naturalsorter', '0.2.2'`
+`gem 'naturalsorter', '0.2.3'`
and run this command in your app root directory
View
2 lib/naturalsorter/version.rb
@@ -1,3 +1,3 @@
module Naturalsorter
- VERSION = "0.2.2"
+ VERSION = "0.2.3"
end
View
11 lib/versioncmp.rb
@@ -37,10 +37,21 @@ def self.compare(a, b)
part1 = Versioncmp.getAPiece(offset1, a);
part2 = Versioncmp.getAPiece(offset2, b);
+ if part1.length() == 8 && part1.match(/^[0-9]+$/) != nil && part2.length() < 8
+ return -1
+ end
+ if part2.length() == 8 && part2.match(/^[0-9]+$/) != nil && part1.length() < 8
+ return 1
+ end
+
offset1 += part1.length() + 1;
offset2 += part2.length() + 1;
if ( part1.match(/^[0-9]+$/) != nil && part2.match(/^[0-9]+$/) != nil )
+
+
+
+
ai = part1.to_i;
bi = part2.to_i;
result = Versioncmp.compareInt(ai, bi);
View
4 spec/naturalsorter_spec.rb
@@ -64,6 +64,10 @@
it "1.2, 1.1 is 1.1, 1.2" do
Naturalsorter::Sorter.sort_version_desc(["0.4", "0.1", "1.1", "1.1.1", "1.2", "1.2.1", "1.0", "1.0.RC1"]).should eql(["1.2.1", "1.2", "1.1.1", "1.1", "1.0", "1.0.RC1", "0.4", "0.1"])
end
+
+ it "sorts this to the end 20040121.140929" do
+ Naturalsorter::Sorter.sort_version_desc(["0.4", "0.1", "20040121.140929", "1.1.1", "1.2", "1.2.1", "1.0", "1.0.RC1"]).should eql(["1.2.1", "1.2", "1.1.1", "1.0", "1.0.RC1", "0.4", "0.1", "20040121.140929"])
+ end
it "1.2, 1.1 is 1.1, 1.2" do
Naturalsorter::Sorter.sort_version(["1.1", "1.2", "1.0"]).should eql(["1.0", "1.1", "1.2"])
View
8 spec/versioncmp_spec.rb
@@ -45,5 +45,13 @@
it "bigger RC than 1.0" do
Versioncmp.compare("1.1.RC1", "1.1").should eql(-1)
end
+
+ it "20040121.140929 is smaller than 1.1" do
+ Versioncmp.compare("20040121.140929", "1.1").should eql(-1)
+ end
+
+ it "1.1 is bigger than 20040121.140929" do
+ Versioncmp.compare("1.1", "20040121.140929").should eql(1)
+ end
end

0 comments on commit ea06b24

Please sign in to comment.
Something went wrong with that request. Please try again.