Skip to content

Commit

Permalink
fixed sorting Integer values
Browse files Browse the repository at this point in the history
  • Loading branch information
prashanth-sams committed Dec 9, 2019
1 parent ab38a1c commit f5288d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/client-api/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,27 @@ def validate_list(res, *options)
@resp = @resp.send(:[], method)
end

@cls = []
@resp.map do |val|
unit.map do |list|
val = val.send(:[], list)
end
@value << val
begin
@cls << (val.scan(/^\d+$/).any? ? Integer : val.class)
rescue NoMethodError
@cls << val.class
end
end

@value =
if @cls.all? {|e| e == Integer}
@value.map(&:to_i)
elsif (@cls.all? {|e| e == String}) || (@cls.include? String)
@value.map(&:to_s)
else
@value
end
expect(@value).to eq(@value.sort) if sort == 'ascending'
expect(@value).to eq(@value.sort.reverse) if sort == 'descending'

Expand Down
2 changes: 1 addition & 1 deletion lib/client-api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ClientApi
VERSION = "0.3.7".freeze
VERSION = "0.3.8".freeze
end
9 changes: 9 additions & 0 deletions spec/client/sorting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
"posts": [
{
"id": 1,
"int": "1",
"title": "Post 1"
},{
"id": 2,
"int": "2",
"title": "Post 2"
},{
"id": 3,
"int": "3",
"title": "Post 3"
},{
"id": 4,
"int": "4",
"title": "Post 4"
},{
"id": 5,
"int": "5",
"title": "Post 5"
}
],
Expand All @@ -32,6 +37,10 @@
"key": "posts",
"unit": "id",
"sort": "ascending"
},{
"key": "posts",
"unit": "int",
"sort": "ascending"
}
)
end
Expand Down

0 comments on commit f5288d0

Please sign in to comment.