Skip to content

Commit

Permalink
improvements for jquery versions
Browse files Browse the repository at this point in the history
  • Loading branch information
reiz committed Apr 14, 2012
1 parent 13341b0 commit 09fd2f0
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Get newest.

You should add this line to your Gemfile

`gem 'naturalsorter', '0.3.1'`
`gem 'naturalsorter', '0.3.4'`

and run this command in your app root directory

Expand Down
76 changes: 76 additions & 0 deletions lib/#natcmp.rb#
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# natcmp.rb
#
# Natural order comparison of two strings
# e.g. "my_prog_v1.1.0" < "my_prog_v1.2.0" < "my_prog_v1.10.0"
# which does not follow alphabetically
#
# Based on Martin Pool's "Natural Order String Comparison" originally written in C
# http://sourcefrog.net/projects/natsort/
#
# This implementation is Copyright (C) 2003 by Alan Davies
# <cs96and_AT_yahoo_DOT_co_DOT_uk>
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.

class Natcmp

# 'Natural order' comparison of two strings
def self.natcmp(str1, str2, caseInsensitive=false)
str1, str2 = str1.dup, str2.dup
compareExpression = /^(\D*)(\d*)(.*)$/

if caseInsensitive
str1.downcase!
str2.downcase!
end

# Remove all whitespace
str1.gsub!(/\s*/, '')
str2.gsub!(/\s*/, '')

while (str1.length > 0) or (str2.length > 0) do
# Extract non-digits, digits and rest of string
str1 =~ compareExpression
chars1, num1, str1 = $1.dup, $2.dup, $3.dup

str2 =~ compareExpression
chars2, num2, str2 = $1.dup, $2.dup, $3.dup

# Compare the non-digits
case (chars1 <=> chars2)
when 0 # Non-digits are the same, compare the digits...
# If either number begins with a zero, then compare alphabetically,
# otherwise compare numerically
if (num1[0] != 48) and (num2[0] != 48)
num1, num2 = num1.to_i, num2.to_i
end

case (num1 <=> num2)
when -1 then return -1
when 1 then return 1
end
when -1 then return -1
when 1 then return 1
end # case

end # while

# Strings are naturally equal
return 0
end

end
2 changes: 1 addition & 1 deletion lib/naturalsorter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Naturalsorter
VERSION = "0.3.1"
VERSION = "0.3.4"
end
32 changes: 30 additions & 2 deletions lib/versioncmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def self.compare(a, b)
return result if (result != 0)
next
else
result = Versioncmp.check_jquery_versioning(part1, part2)
if result != nil
return result
end
return 1 if (part1.match(/^[0-9]+$/) != nil && part2.match(/^[0-9]+$/) == nil)
return -1;
end
Expand All @@ -77,6 +81,30 @@ def self.compareString(a, b)
return -1 if a < b
return 1
end

def self.check_jquery_versioning(part1, part2)
# --- START ---- special case for awesome jquery shitty verison numbers
if ( part1.match(/^[0-9]+[a-zA-Z]+[0-9]+$/) != nil && part2.match(/^[0-9]+$/) != nil )
part1_1 = part1.match(/^[0-9]+/)
result = Versioncmp.compareInt(part1_1[0], part2)
if result != 0
return result
end
return -1
end

if ( part2.match(/^[0-9]+[a-zA-Z]+[0-9]+$/) != nil && part1.match(/^[0-9]+$/) != nil )
part2_1 = part2.match(/^[0-9]+/)
result = Versioncmp.compareInt(part1, part2_1[0])
if result != 0
return result
end
return 1
end

return nil
# --- END ---- special case for awesome jquery shitty verison numbers
end

def self.checkForRC(a, b)
big = String.new(a)
Expand Down Expand Up @@ -145,8 +173,8 @@ def self.getAPiece(offset, cake)
for z in 0..100
offsetz = offset + z
break if offsetz > cake.length()
p = cake[offset..offset + z]
if ( p.match(/^[0-9]+$/) == nil )
p = cake[ offset..offset + z ]
if ( p.match(/^\w+\.$/) != nil )
break
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/versioncmp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,40 @@
Versioncmp.compare("1.1", "20040121.140929").should eql(1)
end

it "1.7b2 is smaller than 1.7" do
Versioncmp.compare("1.7b2", "1.7").should eql(-1)
end

it "1.7 is bigger than 1.7b2" do
Versioncmp.compare("1.7", "1.7b2").should eql(1)
end

it "1.7 is bigger than 1.7rc2" do
Versioncmp.compare("1.7", "1.7rc2").should eql(1)
end

it "1.7 is bigger than 1.7RC2" do
Versioncmp.compare("1.7", "1.7RC2").should eql(1)
end

it "1.7 is bigger than 1.7a" do
Versioncmp.compare("1.7", "1.7a").should eql(1)
end

it "1.7 is bigger than 1.7b" do
Versioncmp.compare("1.7", "1.7b").should eql(1)
end

it "1.7b is bigger than 1.7a" do
Versioncmp.compare("1.7", "1.7b").should eql(1)
end

it "1.7.1rc1 is smaller than 1.7.2" do
Versioncmp.compare("1.7.1rc1", "1.7.2").should eql(-1)
end

it "1.7.2 is bigger than 1.7.1rc1" do
Versioncmp.compare("1.7.2", "1.7.1rc1").should eql(1)
end

end

0 comments on commit 09fd2f0

Please sign in to comment.