Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions rb/helpers/case_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'active_support/all'

# CaseFormatter allows you to convert from and to snake_case and camelCase
# It support nested elements as well, such as Arrays and Hashes
module CaseFormatter
extend ActiveSupport::Concern

included do
def snake_to_camel_case(obj)
case obj
when String
obj.camelize(:lower)
when Symbol
obj.to_s.camelize(:lower).to_sym
when Hash
obj.deep_transform_keys { |key| key.to_s.camelize(:lower).to_sym }
when Array
obj.map { |item| snake_to_camel_case(item) }
else
raise ArgumentError, "Unexpected type for case conversion: #{obj.class}"
end
end

def camel_to_snake_case(obj)
case obj
when String
obj.underscore
when Symbol
obj.to_s.underscore.to_sym
when Hash
obj.deep_transform_keys { |key| key.to_s.underscore.to_sym }
when Array
obj.map { |item| camel_to_snake_case(item) }
else
raise ArgumentError, "Unexpected type for case conversion: #{obj.class}"
end
end
end
end
115 changes: 115 additions & 0 deletions rb/spec/case_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# frozen_string_literal: true

require_relative '../helpers/case_formatter'

describe CaseFormatter do
let(:instance) { Class.new { include CaseFormatter }.new }

describe '#snake_to_camel_case' do
context 'when input is a string' do
it 'converts snake_case to camelCase' do
expect(instance.snake_to_camel_case('hello_world')).to eq('helloWorld')
end
end

context 'when input is a symbol' do
it 'converts snake_case to camelCase' do
expect(instance.snake_to_camel_case(:hello_world)).to eq(:helloWorld)
end
end

context 'when input is a hash' do
it 'converts all keys from snake_case to camelCase' do
input = { first_name: 'John', last_name: 'Doe' }
expected = { firstName: 'John', lastName: 'Doe' }
expect(instance.snake_to_camel_case(input)).to eq(expected)
end

it 'converts nested hash keys' do
input = { user_data: { first_name: 'John', address_info: { street_name: 'Main' } } }
expected = { userData: { firstName: 'John', addressInfo: { streetName: 'Main' } } }
expect(instance.snake_to_camel_case(input)).to eq(expected)
end
end

context 'when input is an array' do
it 'converts all elements that can be converted' do
input = ['hello_world', { user_name: 'john' }, :test_case]
expected = ['helloWorld', { userName: 'john' }, :testCase]
expect(instance.snake_to_camel_case(input)).to eq(expected)
end
end

context 'when input is another type' do
it 'raises ArgumentError for nil' do
expect { instance.snake_to_camel_case(nil) }
.to raise_error(ArgumentError, 'Unexpected type for case conversion: NilClass')
end

it 'raises ArgumentError for numbers' do
expect { instance.snake_to_camel_case(123) }
.to raise_error(ArgumentError, 'Unexpected type for case conversion: Integer')
end

it 'raises ArgumentError for custom objects' do
custom_object = double
expect { instance.snake_to_camel_case(custom_object) }
.to raise_error(ArgumentError)
end
end
end

describe '#camel_to_snake_case' do
context 'when input is a string' do
it 'converts camelCase to snake_case' do
expect(instance.camel_to_snake_case('helloWorld')).to eq('hello_world')
end
end

context 'when input is a symbol' do
it 'converts camelCase to snake_case' do
expect(instance.camel_to_snake_case(:helloWorld)).to eq(:hello_world)
end
end

context 'when input is a hash' do
it 'converts all keys from camelCase to snake_case' do
input = { firstName: 'John', lastName: 'Doe' }
expected = { first_name: 'John', last_name: 'Doe' }
expect(instance.camel_to_snake_case(input)).to eq(expected)
end

it 'converts nested hash keys' do
input = { userData: { firstName: 'John', addressInfo: { streetName: 'Main' } } }
expected = { user_data: { first_name: 'John', address_info: { street_name: 'Main' } } }
expect(instance.camel_to_snake_case(input)).to eq(expected)
end
end

context 'when input is an array' do
it 'converts all elements that can be converted' do
input = ['helloWorld', { userName: 'john' }, :testCase]
expected = ['hello_world', { user_name: 'john' }, :test_case]
expect(instance.camel_to_snake_case(input)).to eq(expected)
end
end

context 'when input is another type' do
it 'raises ArgumentError for nil' do
expect { instance.snake_to_camel_case(nil) }
.to raise_error(ArgumentError, 'Unexpected type for case conversion: NilClass')
end

it 'raises ArgumentError for numbers' do
expect { instance.snake_to_camel_case(123) }
.to raise_error(ArgumentError, 'Unexpected type for case conversion: Integer')
end

it 'raises ArgumentError for custom objects' do
custom_object = double
expect { instance.snake_to_camel_case(custom_object) }
.to raise_error(ArgumentError)
end
end
end
end
11 changes: 11 additions & 0 deletions zsh/.zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ ZSH_THEME="gozilla"

plugins=(git ssh-agent zsh-autosuggestions)

# Suffix Aliases
alias -s rb=code

# Checkout
alias myscripts="cd ~/scripts"
alias rbscripts="cd ~/scripts/rb"
Expand All @@ -26,3 +29,11 @@ alias zshconfig="code ~/.zshrc"

# Easter eggs
alias rubydance="ruby ~/ruby_egg.rb"

# Lines of code
alias count_rb="find app -iname "*.rb" -type f -exec cat {} \;| wc -l"
alias count_jsx="find src -iname "*.jsx" -type f -exec cat {} \;| wc -l"
alias count_js="find src -iname "*.js" -type f -exec cat {} \;| wc -l"

# Rails
alias rails_stats="bundle exec rake stats"