-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
285f6ad
commit 01c54ef
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# based on https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/keys.rb#L116 | ||
# I chose to copy this method instead of adding activesupport as a dependency | ||
# because we want to have the least number of dependencies | ||
module FormatParser | ||
class HashUtils | ||
def self.deep_transform_keys(object, &block) | ||
case object | ||
when Hash | ||
object.each_with_object({}) do |(key, value), result| | ||
result[yield(key)] = deep_transform_keys(value, &block) | ||
end | ||
when Array | ||
object.map { |e| deep_transform_keys(e, &block) } | ||
else | ||
object | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'spec_helper' | ||
|
||
describe FormatParser::HashUtils do | ||
describe '.deep_transform_keys' do | ||
it 'transforms all the keys in a hash' do | ||
hash = { aa: 1, 'bb' => 2 } | ||
result = described_class.deep_transform_keys(hash, &:to_s) | ||
|
||
expect(result).to eq('aa' => 1, 'bb' => 2) | ||
end | ||
|
||
it 'transforms all the keys in a array of hashes' do | ||
array = [{ aa: 1, bb: 2 }, { cc: 3, dd: [{c: 2, d: 3}] }] | ||
result = described_class.deep_transform_keys(array, &:to_s) | ||
|
||
expect(result).to eq( | ||
[{'aa' => 1, 'bb' => 2}, {'cc' => 3, 'dd' => [{'c' => 2, 'd' => 3}]}] | ||
) | ||
end | ||
|
||
it 'transforms all the keys in a hash recursively' do | ||
hash = { aa: 1, bb: { cc: 22, dd: 3 } } | ||
result = described_class.deep_transform_keys(hash, &:to_s) | ||
|
||
expect(result).to eq('aa' => 1, 'bb' => { 'cc' => 22, 'dd' => 3}) | ||
end | ||
|
||
it 'does nothing for an non array/hash object' do | ||
object = Object.new | ||
result = described_class.deep_transform_keys(object, &:to_s) | ||
|
||
expect(result).to eq(object) | ||
end | ||
end | ||
end |