diff --git a/rb/helpers/case_formatter.rb b/rb/helpers/case_formatter.rb new file mode 100644 index 0000000..6cbb25b --- /dev/null +++ b/rb/helpers/case_formatter.rb @@ -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 diff --git a/rb/spec/case_formatter_spec.rb b/rb/spec/case_formatter_spec.rb new file mode 100644 index 0000000..6af1631 --- /dev/null +++ b/rb/spec/case_formatter_spec.rb @@ -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 diff --git a/zsh/.zshrc b/zsh/.zshrc index 6f7893e..274c319 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -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" @@ -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"