Skip to content

Commit

Permalink
sort with usenull
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Jul 22, 2021
1 parent 58dcf55 commit be2c1c5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/kiba/extend/transforms/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def process(row)
private

def process_for_sort(val)
return val.gsub('%NULLVALUE%', 'zzzzzzzzzzzzzz').downcase.gsub(/[^[:alnum:][:space:]]/, '') if @usenull

val.downcase.gsub(/[^[:alnum:][:space:]]/, '')
end

Expand Down
2 changes: 1 addition & 1 deletion lib/kiba/extend/transforms/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def field_values(row:, fields:, discard: %i[nil empty delim], delim: DELIM, usen
# @return [Array(Symbol)] of names of fields that should be kept, based on given discard
# and usenull param values and the field values
private_class_method def keep_fields(field_vals, discard, delim, usenull)
field_vals.transform_values!{ |val| val.gsub('%NULLVALUE%', '') } if usenull
field_vals = field_vals.transform_values{ |val| val.gsub('%NULLVALUE%', '') } if usenull
field_vals = field_vals.reject{ |field, val| val.empty? } if discard.any?(:empty)
field_vals = field_vals.reject{ |field, val| delim_only?(val, delim) } if discard.any?(:delim)
field_vals.keys
Expand Down
32 changes: 32 additions & 0 deletions spec/kiba/extend/transforms/clean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@
end
end
end

context 'when usenull = true' do
let(:usenull) { true }
it 'sorts as expected' do
expected = [
'Organization;Person;unmapped',
';',
nil,
'',
'notmapped;Person',
'apple;%NULLVALUE%',
'oatmeal;%NULLVALUE%'
]
expect(result.map{ |res| res[:type] }).to eq(expected)
end

context 'when direction = :desc' do
let(:direction) { :desc }
it 'sorts as expected' do
expected = [
'unmapped;Person;Organization',
';',
nil,
'',
'Person;notmapped',
'%NULLVALUE%;apple',
'%NULLVALUE%;oatmeal'
]
expect(result.map{ |res| res[:type] }).to eq(expected)
end
end
end
end

describe 'ClearFields' do
Expand Down

0 comments on commit be2c1c5

Please sign in to comment.