Skip to content

Commit db3809c

Browse files
committed
✨ Add SequenceSet#append, to keep unsorted order
1 parent 2df9106 commit db3809c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/net/imap/sequence_set.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ class IMAP
226226
# - #add?: If the given object is not an element in the set, adds it and
227227
# returns +self+; otherwise, returns +nil+.
228228
# - #merge: Merges multiple elements into the set; returns +self+.
229+
# - #append: Adds a given object to the set, appending it to the existing
230+
# string, and returns +self+.
229231
# - #string=: Assigns a new #string value and replaces #elements to match.
230232
# - #replace: Replaces the contents of the set with the contents
231233
# of a given object.
@@ -660,6 +662,18 @@ def add(object)
660662
end
661663
alias << add
662664

665+
# Adds a range or number to the set and returns +self+.
666+
#
667+
# Unlike #add, #merge, or #union, the new value is appended to #string.
668+
# This may result in a #string which has duplicates or is out-of-order.
669+
def append(object)
670+
tuple = input_to_tuple object
671+
entry = tuple_to_str tuple
672+
tuple_add tuple
673+
@string = -(string ? "#{@string},#{entry}" : entry)
674+
self
675+
end
676+
663677
# :call-seq: add?(object) -> self or nil
664678
#
665679
# Adds a range or number to the set and returns +self+. Returns +nil+

test/net/imap/test_sequence_set.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ def compare_to_reference_set(nums, set, seqset)
288288
assert_equal SequenceSet["1:*"], SequenceSet.new("5:*") << (1..4)
289289
end
290290

291+
test "#append" do
292+
assert_equal "1,5", SequenceSet.new("1").append("5").string
293+
assert_equal "*,1", SequenceSet.new("*").append(1).string
294+
assert_equal "1:6,4:9", SequenceSet.new("1:6").append("4:9").string
295+
assert_equal "1:4,5:*", SequenceSet.new("1:4").append(5..).string
296+
assert_equal "5:*,1:4", SequenceSet.new("5:*").append(1..4).string
297+
end
298+
291299
test "#merge" do
292300
seqset = -> { SequenceSet.new _1 }
293301
assert_equal seqset["1,5"], seqset["1"].merge("5")

0 commit comments

Comments
 (0)