lucky_case 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,413 @@
1
+
2
+ require_relative '../lucky_case'
3
+
4
+ # LuckyCase version to add methods directly to string by monkey patching
5
+ #
6
+ # can be included this way by
7
+ # require 'lucky_case/string'
8
+ #
9
+
10
+ class String
11
+
12
+ # Get type of case of string (one key of LuckyCase.CASES)
13
+ #
14
+ # If more than one case matches, the first match wins.
15
+ # Match prio is the order of the regex in LuckyCase.CASES
16
+ #
17
+ # If you want or need to know all cases, use plural version of this method
18
+ #
19
+ # If you want to check explicitly for one case, use its check method,
20
+ # e.g. snake_case? for snake_case, etc...
21
+ #
22
+ # @param [Boolean] allow_prefixed_underscores
23
+ # @return [Symbol,nil] symbol of type, nil if no match
24
+ def lucky_case(allow_prefixed_underscores: true)
25
+ LuckyCase.case self, allow_prefixed_underscores: allow_prefixed_underscores
26
+ end
27
+
28
+ # Get types of cases of string (keys of LuckyCase.CASES)
29
+ #
30
+ # @param [Boolean] allow_prefixed_underscores
31
+ # @return [Array<Symbol>,nil] symbols of types, nil if no one matches
32
+ def lucky_cases(allow_prefixed_underscores: true)
33
+ LuckyCase.cases self, allow_prefixed_underscores: allow_prefixed_underscores
34
+ end
35
+
36
+ # Convert a string into the given case type
37
+ #
38
+ # @param [Symbol,String] case_type
39
+ # @param [Boolean] preserve_prefixed_underscores
40
+ # @return [String]
41
+ def convert_case(case_type, preserve_prefixed_underscores: true)
42
+ LuckyCase.convert_case self, case_type, preserve_prefixed_underscores: preserve_prefixed_underscores
43
+ end
44
+
45
+ def convert_case!(case_type, preserve_prefixed_underscores: true)
46
+ set_self_value self.convert_case(case_type, preserve_prefixed_underscores: preserve_prefixed_underscores)
47
+ end
48
+
49
+ #----------------------------------------------------------------------------------------------------
50
+ # UPPER CASE
51
+ #----------------------------------------------------------------------------------------------------
52
+
53
+ # Converts all characters inside the string
54
+ # into upper case
55
+ #
56
+ # @example conversion
57
+ # 'this-isAnExample_string' => 'THIS-ISANEXAMPLE_STRING'
58
+ #
59
+ # @return [String]
60
+ def upper_case()
61
+ LuckyCase.upper_case self
62
+ end
63
+
64
+ def upper_case!()
65
+ set_self_value self.upper_case
66
+ end
67
+
68
+ # Checks if all characters inside the string are upper case
69
+ #
70
+ # @return [Boolean]
71
+ def upper_case?()
72
+ LuckyCase.upper_case? self
73
+ end
74
+
75
+ #----------------------------------------------------------------------------------------------------
76
+ # LOWER CASE
77
+ #----------------------------------------------------------------------------------------------------
78
+
79
+ # Converts all characters inside the string
80
+ # into lower case
81
+ #
82
+ # @example conversion
83
+ # 'this-isAnExample_string' => 'this-isanexample_string'
84
+ #
85
+ # @return [String]
86
+ def lower_case()
87
+ LuckyCase.lower_case self
88
+ end
89
+
90
+ def lower_case!()
91
+ set_self_value self.lower_case
92
+ end
93
+
94
+ # Checks if all characters inside the string are lower case
95
+ #
96
+ # @return [Boolean]
97
+ def lower_case?()
98
+ LuckyCase.lower_case? self
99
+ end
100
+
101
+ #----------------------------------------------------------------------------------------------------
102
+ # SNAKE CASE
103
+ #----------------------------------------------------------------------------------------------------
104
+
105
+ # Converts the given string from any case
106
+ # into snake case
107
+ #
108
+ # @example conversion
109
+ # 'this-isAnExample_string' => 'this_is_an_example_string'
110
+ #
111
+ # @param [Boolean] preserve_prefixed_underscores
112
+ # @return [String]
113
+ def snake_case(preserve_prefixed_underscores: true)
114
+ LuckyCase.snake_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
115
+ end
116
+
117
+ def snake_case!(preserve_prefixed_underscores: true)
118
+ set_self_value self.snake_case preserve_prefixed_underscores: preserve_prefixed_underscores
119
+ end
120
+
121
+ # Checks if the string is snake case
122
+ #
123
+ # @param [Boolean] allow_prefixed_underscores
124
+ # @return [Boolean]
125
+ def snake_case?(allow_prefixed_underscores: true)
126
+ LuckyCase.snake_case? self, allow_prefixed_underscores: allow_prefixed_underscores
127
+ end
128
+
129
+ # Converts the given string from any case
130
+ # into upper snake case
131
+ #
132
+ # @example conversion
133
+ # 'this-isAnExample_string' => 'THIS_IS_AN_EXAMPLE_STRING'
134
+ #
135
+ # @param [Boolean] preserve_prefixed_underscores
136
+ # @return [String]
137
+ def upper_snake_case(preserve_prefixed_underscores: true)
138
+ LuckyCase.upper_snake_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
139
+ end
140
+
141
+ def upper_snake_case!(preserve_prefixed_underscores: true)
142
+ set_self_value self.upper_snake_case preserve_prefixed_underscores: preserve_prefixed_underscores
143
+ end
144
+
145
+ # Checks if the string is upper snake case
146
+ #
147
+ # @param [Boolean] allow_prefixed_underscores
148
+ # @return [Boolean]
149
+ def upper_snake_case?(allow_prefixed_underscores: true)
150
+ LuckyCase.upper_snake_case? self, allow_prefixed_underscores: allow_prefixed_underscores
151
+ end
152
+
153
+ #----------------------------------------------------------------------------------------------------
154
+ # PASCAL CASE
155
+ #----------------------------------------------------------------------------------------------------
156
+
157
+ # Converts the given string from any case
158
+ # into pascal case
159
+ #
160
+ # @example conversion
161
+ # 'this-isAnExample_string' => 'ThisIsAnExampleString'
162
+ #
163
+ # @param [Boolean] preserve_prefixed_underscores
164
+ # @return [String]
165
+ def pascal_case(preserve_prefixed_underscores: true)
166
+ LuckyCase.pascal_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
167
+ end
168
+
169
+ def pascal_case!(preserve_prefixed_underscores: true)
170
+ set_self_value self.pascal_case preserve_prefixed_underscores: preserve_prefixed_underscores
171
+ end
172
+
173
+ # Checks if the string is upper pascal case
174
+ #
175
+ # @param [Boolean] allow_prefixed_underscores
176
+ # @return [Boolean]
177
+ def pascal_case?(allow_prefixed_underscores: true)
178
+ LuckyCase.pascal_case? self, allow_prefixed_underscores: allow_prefixed_underscores
179
+ end
180
+
181
+ #----------------------------------------------------------------------------------------------------
182
+ # CAMEL CASE
183
+ #----------------------------------------------------------------------------------------------------
184
+
185
+ # Converts the given string from any case
186
+ # into camel case
187
+ #
188
+ # @example conversion
189
+ # 'this-isAnExample_string' => 'thisIsAnExampleString'
190
+ #
191
+ # @param [Boolean] preserve_prefixed_underscores
192
+ # @return [String]
193
+ def camel_case(preserve_prefixed_underscores: true)
194
+ LuckyCase.camel_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
195
+ end
196
+
197
+ def camel_case!(preserve_prefixed_underscores: true)
198
+ set_self_value self.camel_case preserve_prefixed_underscores: preserve_prefixed_underscores
199
+ end
200
+
201
+ # Checks if the string is camel case
202
+ #
203
+ # @param [Boolean] allow_prefixed_underscores
204
+ # @return [Boolean]
205
+ def camel_case?(allow_prefixed_underscores: true)
206
+ LuckyCase.camel_case? self, allow_prefixed_underscores: allow_prefixed_underscores
207
+ end
208
+
209
+ #----------------------------------------------------------------------------------------------------
210
+ # DASH CASE
211
+ #----------------------------------------------------------------------------------------------------
212
+
213
+ # Converts the given string from any case
214
+ # into dash case
215
+ #
216
+ # @example conversion
217
+ # 'this-isAnExample_string' => 'this-is-an-example-string'
218
+ #
219
+ # @param [Boolean] preserve_prefixed_underscores
220
+ # @return [String]
221
+ def dash_case(preserve_prefixed_underscores: true)
222
+ LuckyCase.dash_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
223
+ end
224
+
225
+ def dash_case!(preserve_prefixed_underscores: true)
226
+ set_self_value self.dash_case preserve_prefixed_underscores: preserve_prefixed_underscores
227
+ end
228
+
229
+ # Checks if the string is dash case
230
+ #
231
+ # @param [Boolean] allow_prefixed_underscores
232
+ # @return [Boolean]
233
+ def dash_case?(allow_prefixed_underscores: true)
234
+ LuckyCase.dash_case? self, allow_prefixed_underscores: allow_prefixed_underscores
235
+ end
236
+
237
+ # Converts the given string from any case
238
+ # into upper dash case
239
+ #
240
+ # @example conversion
241
+ # 'this-isAnExample_string' => 'THIS-IS-AN-EXAMPLE-STRING'
242
+ #
243
+ # @param [Boolean] preserve_prefixed_underscores
244
+ # @return [String]
245
+ def upper_dash_case(preserve_prefixed_underscores: true)
246
+ LuckyCase.upper_dash_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
247
+ end
248
+
249
+ def upper_dash_case!(preserve_prefixed_underscores: true)
250
+ set_self_value self.upper_dash_case preserve_prefixed_underscores: preserve_prefixed_underscores
251
+ end
252
+
253
+ # Checks if the string is upper dash case
254
+ #
255
+ # @param [Boolean] allow_prefixed_underscores
256
+ # @return [Boolean]
257
+ def upper_dash_case?(allow_prefixed_underscores: true)
258
+ LuckyCase.upper_dash_case? self, allow_prefixed_underscores: allow_prefixed_underscores
259
+ end
260
+
261
+ #----------------------------------------------------------------------------------------------------
262
+ # TRAIN CASE
263
+ #----------------------------------------------------------------------------------------------------
264
+
265
+ # Converts the given string from any case
266
+ # into train case
267
+ #
268
+ # @example conversion
269
+ # 'this-isAnExample_string' => 'This-Is-An-Example-String'
270
+ #
271
+ # @param [Boolean] preserve_prefixed_underscores
272
+ # @return [String]
273
+ def train_case(preserve_prefixed_underscores: true)
274
+ LuckyCase.train_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
275
+ end
276
+
277
+ def train_case!(preserve_prefixed_underscores: true)
278
+ set_self_value self.train_case preserve_prefixed_underscores: preserve_prefixed_underscores
279
+ end
280
+
281
+ # Checks if the string is train case
282
+ #
283
+ # @param [Boolean] allow_prefixed_underscores
284
+ # @return [Boolean]
285
+ def train_case?(allow_prefixed_underscores: true)
286
+ LuckyCase.train_case? self, allow_prefixed_underscores: allow_prefixed_underscores
287
+ end
288
+
289
+ #----------------------------------------------------------------------------------------------------
290
+ # CAPITALIZE
291
+ #----------------------------------------------------------------------------------------------------
292
+
293
+ # Converts the first character to capital
294
+ #
295
+ # @param [Boolean] skip_prefixed_underscores
296
+ # @return [String]
297
+ def capital(skip_prefixed_underscores: false)
298
+ LuckyCase.capitalize self, skip_prefixed_underscores: skip_prefixed_underscores
299
+ end
300
+
301
+ # Converts the first character to capital
302
+ #
303
+ # @param [Boolean] skip_prefixed_underscores
304
+ # @return [String]
305
+ def capitalize(skip_prefixed_underscores: false)
306
+ self.capital skip_prefixed_underscores: skip_prefixed_underscores
307
+ end
308
+
309
+ def capital!(skip_prefixed_underscores: false)
310
+ set_self_value self.capitalize skip_prefixed_underscores: skip_prefixed_underscores
311
+ end
312
+
313
+ def capitalize!(skip_prefixed_underscores: false)
314
+ self.capital! skip_prefixed_underscores: skip_prefixed_underscores
315
+ end
316
+
317
+ # Checks if the strings first character is a capital letter
318
+ #
319
+ # @param [Boolean] skip_prefixed_underscores
320
+ # @return [Boolean]
321
+ def capital?(skip_prefixed_underscores: false)
322
+ LuckyCase.capital? self, skip_prefixed_underscores: skip_prefixed_underscores
323
+ end
324
+
325
+ # Checks if the strings first character is a capital letter
326
+ #
327
+ # @param [Boolean] skip_prefixed_underscores
328
+ # @return [Boolean]
329
+ def capitalized?(skip_prefixed_underscores: false)
330
+ self.capital? skip_prefixed_underscores: skip_prefixed_underscores
331
+ end
332
+
333
+ #----------------------------------------------------------------------------------------------------
334
+ # MIXED CASE
335
+ #----------------------------------------------------------------------------------------------------
336
+
337
+ # Converts the given string from any case
338
+ # into mixed case
339
+ #
340
+ # @example conversion
341
+ # 'this-isAnExample_string' => 'This-Is_anExample-string'
342
+ #
343
+ # @param [Boolean] preserve_prefixed_underscores
344
+ # @return [String]
345
+ def mixed_case(preserve_prefixed_underscores: true)
346
+ LuckyCase.mixed_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
347
+ end
348
+
349
+ def mixed_case!(preserve_prefixed_underscores: true)
350
+ set_self_value self.mixed_case preserve_prefixed_underscores: preserve_prefixed_underscores
351
+ end
352
+
353
+ # Checks if the string is a valid mixed case (without special characters!)
354
+ #
355
+ # @return [Boolean]
356
+ def mixed_case?()
357
+ LuckyCase.mixed_case? self
358
+ end
359
+
360
+ #----------------------------------------------------------------------------------------------------
361
+ # SWAP CASE
362
+ #----------------------------------------------------------------------------------------------------
363
+
364
+ # Swaps character cases in string
365
+ #
366
+ # lower case to upper case
367
+ # upper case to lower case
368
+ # dash to underscore
369
+ # underscore to dash
370
+ #
371
+ # @example conversion
372
+ # 'this-isAnExample_string' => 'THIS_ISaNeXAMPLE-STRING'
373
+ #
374
+ # @param [Boolean] preserve_prefixed_underscores
375
+ # @return [String]
376
+ def swap_case(preserve_prefixed_underscores: false)
377
+ LuckyCase.swap_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
378
+ end
379
+
380
+ #----------------------------------------------------------------------------------------------------
381
+
382
+ def swap_case!(preserve_prefixed_underscores: false)
383
+ set_self_value self.swap_case preserve_prefixed_underscores: preserve_prefixed_underscores
384
+ end
385
+
386
+ #----------------------------------------------------------------------------------------------------
387
+ # CONSTANTIZE
388
+ #----------------------------------------------------------------------------------------------------
389
+
390
+ # Converts the string from any case
391
+ # into pascal case and casts it into a constant
392
+ #
393
+ # @example conversion
394
+ # 'this-isAnExample_string' => ThisIsAnExampleString
395
+ # 'this/is_an/example_path' => This::IsAn::ExamplePath
396
+ #
397
+ # @param [Boolean] preserve_prefixed_underscores
398
+ # @return [Constant]
399
+ def constantize()
400
+ LuckyCase.constantize self
401
+ end
402
+
403
+ #----------------------------------------------------------------------------------------------------
404
+ # HELPERS
405
+ #----------------------------------------------------------------------------------------------------
406
+
407
+ private
408
+
409
+ def set_self_value(val)
410
+ self.gsub!(self, val)
411
+ end
412
+
413
+ end
@@ -0,0 +1,3 @@
1
+ module LuckyCase
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lucky_case/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lucky_case"
8
+ spec.version = LuckyCase::VERSION
9
+ spec.authors = ["Matthäus J. N. Beyrle"]
10
+ spec.email = ["lucky_case.gemspec@mail.magynhard.de"]
11
+
12
+ spec.summary = %q{The lucky ruby gem to identify and convert strings from any letter case to another}
13
+ spec.homepage = "https://github.com/magynhard/lucky_case"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.required_ruby_version = '>= 2.3'
33
+
34
+ spec.add_development_dependency 'bundler', '>= 2.0'
35
+ spec.add_development_dependency 'rake', '>= 10.0'
36
+ spec.add_development_dependency 'rspec', '>= 3.0'
37
+ end