-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase_maker.rb
More file actions
85 lines (73 loc) · 1.71 KB
/
case_maker.rb
File metadata and controls
85 lines (73 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def head(string)
string[0]
end
def tail(string)
string[1..-1]
end
def title(string)
string.downcase.split(' ').map(&:capitalize).join(' ')
end
def remove_spaces(string)
string.gsub(' ', '')
end
def pascal(string)
remove_spaces(title(string))
end
def camel(string)
head(string) + tail(pascal(string))
end
def uppercase_all_vowels(string)
string.downcase.gsub(/[aeiou]/, &:upcase)
end
def uppercase_all_consonants(string)
string.downcase.gsub(/[^aeiou]/, &:upcase)
end
def make_case(string, list_type)
case_type = list_type.is_a?(Array) ? list_type : [list_type]
input = string
case_type.each do |type|
case type
when 'snake'
input = input.gsub(' ', '_')
when 'kebab'
input = input.gsub(' ', '-')
when 'camel'
input = camel(input)
when 'title'
input = title(input)
when 'pascal'
input = pascal(input)
when 'vowel'
input = uppercase_all_vowels(input)
when 'consonant'
input = uppercase_all_consonants(input)
when 'upper'
input = input.upcase
end
end
input
end
print "camel:"
puts make_case("this is a string", "camel")
print "Pascal:"
puts make_case("this is a string", "pascal")
print "snake:"
puts make_case("this is a string", "snake")
print "kebab:"
puts make_case("this is a string", "kebab")
print "title:"
puts make_case("this is a string", "title")
print "vowel:"
puts make_case("this is a string", "vowel")
print "consonant:"
puts make_case("this is a string", "consonant")
puts make_case("this is a string", ["upper", "snake"])
# Expected Output
# thisIsAString camel
# ThisIsAString pascal
# this_is_a_string snake
# this-is-a-string kebab
# This Is A String
# thIs Is A strIng
# THiS iS a STRiNG
# THIS_IS_A_STRING