Skip to content

Commit 8b77172

Browse files
authored
Merge pull request #58 from excid3/deduplicate-new-elements
Deduplicate new elements for unique lists
2 parents e326fc3 + 6733315 commit 8b77172

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/kredis/types/unique_list.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@ class Kredis::Types::UniqueList < Kredis::Types::List
55
attr_accessor :typed, :limit
66

77
def prepend(elements)
8+
elements = Array(elements).uniq
9+
return if elements.empty?
10+
811
multi do
912
remove elements
1013
super
1114
ltrim 0, (limit - 1) if limit
12-
end if Array(elements).flatten.any?
15+
end
1316
end
1417

1518
def append(elements)
19+
elements = Array(elements).uniq
20+
return if elements.empty?
21+
1622
multi do
1723
remove elements
1824
super
1925
ltrim -limit, -1 if limit
20-
end if Array(elements).flatten.any?
26+
end
2127
end
2228
alias << append
2329
end

test/types/unique_list_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,14 @@ class UniqueListTest < ActiveSupport::TestCase
6363
@list.prepend(%w[ 6 7 8 ])
6464
assert_equal %w[ 8 7 6 5 4 ], @list.elements
6565
end
66+
67+
test "appending array with duplicates" do
68+
@list.append(%w[ 1 1 1 ])
69+
assert_equal %w[ 1 ], @list.elements
70+
end
71+
72+
test "prepending array with duplicates" do
73+
@list.prepend(%w[ 1 1 1 ])
74+
assert_equal %w[ 1 ], @list.elements
75+
end
6676
end

0 commit comments

Comments
 (0)