Skip to content

Deduplicate new elements for unique lists #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/kredis/types/unique_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ class Kredis::Types::UniqueList < Kredis::Types::List
attr_accessor :typed, :limit

def prepend(elements)
elements = Array(elements).uniq
return if elements.empty?

multi do
remove elements
super
ltrim 0, (limit - 1) if limit
end if Array(elements).flatten.any?
end
end

def append(elements)
elements = Array(elements).uniq
return if elements.empty?

multi do
remove elements
super
ltrim -limit, -1 if limit
end if Array(elements).flatten.any?
end
end
alias << append
end
10 changes: 10 additions & 0 deletions test/types/unique_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ class UniqueListTest < ActiveSupport::TestCase
@list.prepend(%w[ 6 7 8 ])
assert_equal %w[ 8 7 6 5 4 ], @list.elements
end

test "appending array with duplicates" do
@list.append(%w[ 1 1 1 ])
assert_equal %w[ 1 ], @list.elements
end

test "prepending array with duplicates" do
@list.prepend(%w[ 1 1 1 ])
assert_equal %w[ 1 ], @list.elements
end
end