-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(FEAT) Ensure hash key casing methods work on arrays #15
(FEAT) Ensure hash key casing methods work on arrays #15
Conversation
Added a maint commit to clarify the parameter names for the hash methods. |
lib/pwsh/util.rb
Outdated
if hash.is_a?(Hash) | ||
hash.inject({}) do |memo, (k, v)| # rubocop:disable Style/EachWithObject | ||
def symbolize_hash_keys(object) | ||
if object.is_a?(Hash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit weird that this function has a different code pattern than the pascal_case and snake_case variants. Makes it hard to see whether they're all three doing the same or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and if they're actually all the same shape, then you could replace them with a apply_key_mutator(f, o)
function so that
def pascal_case_hash_keys(object)
apply_key_mutator(&:pascal_case, o)
end
etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced with a private method.
Prior to this commit the hash key casing methods, snake_case_hash_keys and pascal_case_hash_keys, only properly handled hashes, not hashes inside arrays or other objects. Now, if passed an array, the methods will iterate over each item in the array, recursively casing any keys discovered in hashes. Additionally, they will not error if passed any objects which are not hashes or arrays; instead, they will return that object as-is.
This commit standardizes the hash util methods on taking object as a parameter as they handle more data types than just hashes. It also conforms the hash util methods to use the same underlying structure by utilizing a new private method, apply_key_mutator, to do the heavy lifting. The other hash utility methods now create a proc for the conforming of the keys and pass that to the mutator.
This commit updates the casing methods to handle symbols if passed them - they will return a string if given a string and a symbol if given a symbol, properly cased. This does away with the weird requirement to cast the keys to strings, case them, and resymbolize them in the hash mutation procs.
Prior to this commit the hash key casing methods,
snake_case_hash_keys
andpascal_case_hash_keys
, only properly handled hashes, not hashes inside arrays or other objects.Now, if passed an array, the methods will iterate over each item in the array, recursively casing any keys discovered in hashes. Additionally, they will not error if passed any objects which are not hashes or arrays; instead, they will return that object as-is.