Skip to content

Commit

Permalink
refactor code for readability and replace inject with sum
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianmariecom committed Sep 11, 2024
1 parent 276c9a8 commit 78e042f
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/dorian/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def initialize
alias: :o
},
parallel: {
alias: :p,
alias: :p
},
parallel_type: {
alias: :pt,
Expand Down Expand Up @@ -422,11 +422,11 @@ def command_each
end

def command_merge
outputs(map(everything) { |thing| lines(reads(thing)) }.inject(&:+))
outputs(map(everything) { |thing| lines(reads(thing)) }.sum)
end

def command_pluck
outputs(map(everything) { |thing| pluck(lines(reads(thing))) }.inject(&:+))
outputs(map(everything) { |thing| pluck(lines(reads(thing))) }.sum)
end

def command_tally
Expand Down Expand Up @@ -1068,22 +1068,23 @@ def match_filetypes?(path)

def pluck(object)
map(wrap(object).from_deep_struct) do |element|
results = arguments.map do |argument|
if element.is_a?(Array) && argument.to_i.to_s == argument
element[argument.to_i]
elsif element.is_a?(Hash) && element.keys.include?(argument)
{ argument => element[argument] }
else
evaluates(ruby: argument, it: element.to_deep_struct).returned
results =
arguments.map do |argument|
if element.is_a?(Array) && argument.to_i.to_s == argument
element[argument.to_i]
elsif element.is_a?(Hash) && element.key?(argument)
{ argument => element[argument] }
else
evaluates(ruby: argument, it: element.to_deep_struct).returned
end
end
end

if results.all? { |result| result.is_a?(Hash) }
if results.all?(Hash)
results.inject(&:merge).to_deep_struct
else
results.map do |result|
result.is_a?(Hash) ? result.values.first : result
end.to_deep_struct
results
.map { |result| result.is_a?(Hash) ? result.values.first : result }
.to_deep_struct
end
end
end
Expand Down

0 comments on commit 78e042f

Please sign in to comment.