fork download
  1. require 'rspec/autorun'
  2.  
  3. describe '#cleanup_array' do
  4. it 'removes duplicates' do
  5. array_with_duplicates = [1, 2, 4, 4, 3, 5, 1]
  6. expect(cleanup_array(array_with_duplicates)).to eq [1, 2, 3, 4, 5]
  7. end
  8.  
  9. it 'removes nil values' do
  10. array_with_nils = [1, 2, nil, 4, 3, 5, nil]
  11. expect(cleanup_array(array_with_nils)).to eq [1, 2, 3, 4, 5]
  12. end
  13.  
  14. it 'sorts array of integers' do
  15. unsorted_int_arr = [3, 2, 4, 2]
  16. expect(cleanup_array(unsorted_int_arr)).to eq [2, 3, 4]
  17. end
  18.  
  19. it 'sorts array of strings' do
  20. unsorted_str_arr = ["c\n", 'some new', 'omg', 'ca', 'omg']
  21. expect(cleanup_array(unsorted_str_arr)).to eq ["c\n", 'ca', 'omg', 'some new']
  22. end
  23.  
  24. it 'sorts integers like strings' do
  25. arr = ['1a', '23', '13', 1, 16, 111]
  26. expect(cleanup_array(arr)).to eq [1, 111, '13', 16, '1a', '23']
  27. end
  28.  
  29. it 'sorts other objects as strings' do
  30. time_obj = Time.now
  31. arr = [5, 'awww', time_obj, '5', 'xd', '1a']
  32. expect(cleanup_array(arr)).to eq ['1a', time_obj, 5, '5', 'awww', 'xd']
  33. end
  34.  
  35. it 'sorts nested array as string' do
  36. arr = [5, 'awww', 'xd', nil, ['lol', nil, 'fyi'], '1a', 3, '5', 'xd']
  37. expect(cleanup_array(arr)).to eq ['1a', 3, 5, '5', 'awww', ['lol', nil, 'fyi'], 'xd']
  38. end
  39.  
  40. it 'orders ascending by default' do
  41. array_with_duplicates = [1, 2, 4, 4, 3, 5, 11]
  42. expect(cleanup_array(array_with_duplicates)).to eq [1, 2, 3, 4, 5, 11]
  43. end
  44.  
  45. it 'orders descending when passed a kwarg' do
  46. array_with_duplicates = [1, 2, 4, 4, 3, 5, 11]
  47. expect(cleanup_array(array_with_duplicates, order: :desc)).to eq [11, 5, 4, 3, 2, 1]
  48. end
  49.  
  50. it 'allows to pass a custom sort lambda' do
  51. array_with_duplicates = [[19, 2], [41, 8], [4, 17]]
  52. expect(cleanup_array(array_with_duplicates, sort_key: ->(e) { e.max })).to eq [[4, 17], [19, 2], [41, 8]]
  53. end
  54.  
  55. it 'is a part of Array class' do
  56. expect(['a', '3', 1].cleanup_array(order: :desc, sort_key: lambda { |e|
  57. e.to_i
  58. })).to eq(['3', 1, 'a'])
  59. end
  60. end
  61.  
  62. # def cleanup_array(arr, opts = {})
  63. # arr.compact! # Remove nil values
  64. # arr.uniq! # Remove duplicates
  65.  
  66. # if opts[:sort_key]
  67. # arr.sort_by!(&opts[:sort_key]) # Use custom sort key function if provided
  68. # else
  69. # all_ints = arr.all? {|el| Integer(el) rescue false}
  70. # arr.sort_by! do |e|
  71. # if all_ints
  72. # e.to_i # Sort positive integers
  73. # elsif e.is_a?(Array)
  74. # e.map(&:to_s).join('_')
  75. # else
  76. # e.to_s # Sort other elements as strings
  77. # end
  78. # end
  79. # end
  80.  
  81. # arr.reverse! if opts[:order] == :desc # Reverse only for explicit descending order
  82. # arr
  83. # end
  84.  
  85. def cleanup_array(arr, opts = {})
  86. # Determine if all elements are integers and get the sort key function
  87. arr = arr.compact.uniq
  88. all_ints = arr.all? { |el| el.is_a?(Integer) rescue false }
  89. sort_key = opts[:sort_key]
  90.  
  91. # Use a single sort_by call
  92. arr.sort_by! do |e|
  93. if sort_key
  94. sort_key.call(e)
  95. elsif all_ints
  96. e.to_i
  97. elsif e.is_a?(Array)
  98. e.map(&:to_s).join('_')
  99. else
  100. e.to_s
  101. end
  102. end
  103.  
  104. # Reverse only for explicit descending order
  105. arr.reverse! if opts[:order] == :desc
  106.  
  107. arr
  108. end
  109.  
  110.  
  111. class Array
  112. def cleanup_array(opts = {})
  113. sort_order = opts[:order] || :asc
  114. sort_key = opts[:sort_key] || ->(e) { e.to_s }
  115.  
  116. sorted_array = compact.sort_by(&sort_key)
  117. sorted_array.reverse! if sort_order == :desc
  118. sorted_array
  119. end
  120. end
  121.  
Success #stdin #stdout 0.08s 13232KB
stdin
Standard input is empty
stdout
...........

Finished in 0.0037 seconds (files took 0.06277 seconds to load)
11 examples, 0 failures