lucky_case 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f4d90caf6443b20241256aa6721241b6c1cffe91ee094f1803bd4b4a8fa9bc4
4
- data.tar.gz: 1d6ee16099adf04b3d3b5103f8b808a955797df41a61715c30bcb0951900eeb9
3
+ metadata.gz: afbd0b3dbbd7c21f311666d8c0ccabecab4ba9947df2da05e2013638516c89e2
4
+ data.tar.gz: 47050573fc4b1d3fcde64ab975bda715fbfa98256631ca663ee9ea3f5c13b58d
5
5
  SHA512:
6
- metadata.gz: 4d4525d94e1eb4060030da2f1d071e05801a0ec81c9f6484af3cb89dd349207b735344782ce888153abc308560d1c11235d0041c22204660a2db4b695c9df354
7
- data.tar.gz: 04e76e361b15ebff0e1fd36451ee36ad927dbb791ae58824d06046f31885223c2957c6d3dac2f96620d9f8544d007444358efc6f1424592c14c4b9fb6919d1eb
6
+ metadata.gz: 7245455162da41c94380f0738ee3bc6485053191af5fff2fbc2c62c1322ebcb8f1cac30803da544d938da8a8be1730e617ccc3101a61c68e08279acef43727c1
7
+ data.tar.gz: 1048bf41fd2c4d4bb3c1282d30b0fe5d795dabb7cc3415a5bbfc1dc4cc70b9c8eb928b9d437cefb624c66df2cf940b860db9b45dfd291230ba8776b82a17c6a0
data/README.md CHANGED
@@ -30,13 +30,17 @@ You can either use the static LuckyCase class with its method or optionally monk
30
30
  require 'lucky_case'
31
31
 
32
32
  # converters
33
- LuckyCase.snake_case('ExamplePascalString') # => 'example_pascal_string'
34
- LuckyCase.upper_snake_case('Example-Train-String') # => 'EXAMPLE_TRAIN_STRING'
35
- LuckyCase.pascal_case('example_snake_string') # => 'ExampleSnakeString'
36
- LuckyCase.camel_case('example-dash-string') # => 'exampleDashString'
37
- LuckyCase.dash_case('ExamplePascalString') # => 'example-pascal-string'
38
- LuckyCase.upper_dash_case('ExamplePascalString') # => 'EXAMPLE-PASCAL-STRING'
39
- LuckyCase.train_case('example_snake_string') # => 'Example-Snake-String'
33
+ LuckyCase.snake_case('PascalToSnake') # => 'pascal_to_snake'
34
+ LuckyCase.upper_snake_case('Train-To-Upper-Snake') # => 'TRAIN_TO_UPPER_SNAKE'
35
+ LuckyCase.pascal_case('snake_to_pascal') # => 'SnakeToPascal'
36
+ LuckyCase.camel_case('dash-to-camel-case') # => 'dashToCamelCase'
37
+ LuckyCase.dash_case('PascalToDashCase') # => 'pascal-to-dash-case'
38
+ LuckyCase.upper_dash_case('PascalToUpperDash') # => 'PASCAL-TO-UPPER-DASH'
39
+ LuckyCase.train_case('snake_to_train_case') # => 'Snake-To-Train-Case'
40
+ LuckyCase.word_case('PascalToWordCase') # => 'pascal to word case'
41
+ LuckyCase.upper_word_case('PascalToUpperWord') # => 'PASCAL TO UPPER WORD'
42
+ LuckyCase.capital_word_case('snake_to_capital_word') # => 'Snake To Capital Word'
43
+ LuckyCase.sentence_case('snake_to_sentence_case') # => 'Snake to sentence case'
40
44
  LuckyCase.mixed_case('example_snake_string') # => 'Example-snake_STRING'
41
45
  # converter by type
42
46
  LuckyCase.convert_case('some_snake', :pascal_case) # => 'SomeSnake'
@@ -56,12 +60,16 @@ LuckyCase.case('this_can_only_be_snake_case') # => :snake_case
56
60
  LuckyCase.cases('multiple') # => [ :snake_case, :camel_case, :dash_case ]
57
61
  # checkers
58
62
  LuckyCase.snake_case?('valid_snake_case') # => true
59
- LuckyCase.upper_snake_case?('inValidSnakeCase') # => false
63
+ LuckyCase.upper_snake_case?('UPPER_SNAKE') # => true
60
64
  LuckyCase.pascal_case?('PascalCase') # => true
61
65
  LuckyCase.camel_case?('camelCase') # => true
62
66
  LuckyCase.dash_case?('dash-case') # => true
63
67
  LuckyCase.upper_dash_case?('DASH-CASE') # => true
64
68
  LuckyCase.train_case?('Train-Case') # => true
69
+ LuckyCase.word_case?('word case') # => true
70
+ LuckyCase.upper_word_case?('UPPER WORD CASE') # => true
71
+ LuckyCase.capital_word_case?('Capital Word Case') # => true
72
+ LuckyCase.sentence_case?('Sentence case string') # => true
65
73
  LuckyCase.mixed_case?('mixed_Case') # => true
66
74
  LuckyCase.upper_case?('UPPER50984') # => true
67
75
  LuckyCase.lower_case?('lower_cheese') # => true
@@ -18,6 +18,10 @@ module LuckyCase
18
18
  dash_case: /^([[:lower:]]){1}[[:lower:]\-0-9]*[[:lower:]0-9]+$/,
19
19
  upper_dash_case: /^([[:upper:]]){1}[[:upper:]\-0-9]*[[:upper:]0-9]+$/,
20
20
  train_case: /^([[:upper:]][[:lower:]0-9]*\-|[0-9]+\-)*([[:upper:]][[:lower:]0-9]*)$/,
21
+ word_case: /^[[:lower:]]{1}[[:lower:] 0-9]+$/,
22
+ upper_word_case: /^[[:upper:]]{1}[[:upper:] 0-9]+$/,
23
+ capital_word_case: /^([[:upper:]][[:lower:]0-9]*\ |[0-9]+\ )*([[:upper:]][[:lower:]0-9]*)$/,
24
+ sentence_case: /^[[:upper:]]{1}[[:lower:] 0-9]+$/,
21
25
  mixed_case: /^[[:upper:][:lower:]][[:upper:][:lower:]_\-0-9]*$/,
22
26
  }
23
27
 
@@ -391,6 +395,146 @@ module LuckyCase
391
395
  _case_match? s, :train_case
392
396
  end
393
397
 
398
+ #----------------------------------------------------------------------------------------------------
399
+ # WORD CASE
400
+ #----------------------------------------------------------------------------------------------------
401
+
402
+ # Converts the given string from any case
403
+ # into word case
404
+ #
405
+ # @example conversion
406
+ # 'this-isAnExample_string' => 'this is an example string'
407
+ #
408
+ # @param [String] string to convert
409
+ # @param [Boolean] preserve_prefixed_underscores
410
+ # @return [String]
411
+ def self.word_case(string, preserve_prefixed_underscores: true)
412
+ a = split_case_string string
413
+ converted = a.join(' ')
414
+ if preserve_prefixed_underscores
415
+ underscores_at_start(string) + converted
416
+ else
417
+ converted
418
+ end
419
+ end
420
+
421
+ # Checks if the string is word case
422
+ #
423
+ # @param [String] string to check
424
+ # @param [Boolean] allow_prefixed_underscores
425
+ # @return [Boolean]
426
+ def self.word_case?(string, allow_prefixed_underscores: true)
427
+ s = if allow_prefixed_underscores
428
+ cut_underscores_at_start string
429
+ else
430
+ string
431
+ end
432
+ _case_match? s, :word_case
433
+ end
434
+
435
+ # Converts the given string from any case
436
+ # into upper word case
437
+ #
438
+ # @example conversion
439
+ # 'this-isAnExample_string' => 'THIS IS AN EXAMPLE STRING'
440
+ #
441
+ # @param [String] string to convert
442
+ # @param [Boolean] preserve_prefixed_underscores
443
+ # @return [String]
444
+ def self.upper_word_case(string, preserve_prefixed_underscores: true)
445
+ a = split_case_string string
446
+ converted = a.map { |e| upper_case e }.join(' ')
447
+ if preserve_prefixed_underscores
448
+ underscores_at_start(string) + converted
449
+ else
450
+ converted
451
+ end
452
+ end
453
+
454
+ # Checks if the string is upper word case
455
+ #
456
+ # @param [String] string to check
457
+ # @param [Boolean] allow_prefixed_underscores
458
+ # @return [Boolean]
459
+ def self.upper_word_case?(string, allow_prefixed_underscores: true)
460
+ s = if allow_prefixed_underscores
461
+ cut_underscores_at_start string
462
+ else
463
+ string
464
+ end
465
+ _case_match? s, :upper_word_case
466
+ end
467
+
468
+ # Converts the given string from any case
469
+ # into capital word case
470
+ #
471
+ # @example conversion
472
+ # 'this-isAnExample_string' => 'This Is An Example String'
473
+ #
474
+ # @param [String] string to convert
475
+ # @param [Boolean] preserve_prefixed_underscores
476
+ # @return [String]
477
+ def self.capital_word_case(string, preserve_prefixed_underscores: true)
478
+ a = split_case_string string
479
+ converted = a.map { |e| capital e }.join(' ')
480
+ if preserve_prefixed_underscores
481
+ underscores_at_start(string) + converted
482
+ else
483
+ converted
484
+ end
485
+ end
486
+
487
+ # Checks if the string is capital word case
488
+ #
489
+ # @param [String] string to check
490
+ # @param [Boolean] allow_prefixed_underscores
491
+ # @return [Boolean]
492
+ def self.capital_word_case?(string, allow_prefixed_underscores: true)
493
+ s = if allow_prefixed_underscores
494
+ cut_underscores_at_start string
495
+ else
496
+ string
497
+ end
498
+ _case_match? s, :capital_word_case
499
+ end
500
+
501
+ #----------------------------------------------------------------------------------------------------
502
+ # SENTENCE CASE
503
+ #----------------------------------------------------------------------------------------------------
504
+
505
+ # Converts the given string from any case
506
+ # into sentence case
507
+ #
508
+ # @example conversion
509
+ # 'this-isAnExample_string' => 'This is an example string'
510
+ #
511
+ # @param [String] string to convert
512
+ # @param [Boolean] preserve_prefixed_underscores
513
+ # @return [String]
514
+ def self.sentence_case(string, preserve_prefixed_underscores: true)
515
+ a = split_case_string string
516
+ converted = capital(a.join(' '))
517
+ if preserve_prefixed_underscores
518
+ underscores_at_start(string) + converted
519
+ else
520
+ converted
521
+ end
522
+ end
523
+
524
+ # Checks if the string is sentence case
525
+ #
526
+ # @param [String] string to check
527
+ # @param [Boolean] allow_prefixed_underscores
528
+ # @return [Boolean]
529
+ def self.sentence_case?(string, allow_prefixed_underscores: true)
530
+ s = if allow_prefixed_underscores
531
+ cut_underscores_at_start string
532
+ else
533
+ string
534
+ end
535
+ _case_match? s, :sentence_case
536
+ end
537
+
394
538
  #----------------------------------------------------------------------------------------------------
395
539
  # CAPITALIZE
396
540
  #----------------------------------------------------------------------------------------------------
@@ -539,7 +683,7 @@ module LuckyCase
539
683
  # @param [Boolean] preserve_prefixed_underscores
540
684
  # @return [Constant]
541
685
  def self.constantize(string)
542
- s = string.gsub('/','::')
686
+ s = string.gsub('/', '::')
543
687
  constants = if s.include? '::'
544
688
  s.split('::')
545
689
  else
@@ -627,6 +771,7 @@ module LuckyCase
627
771
  def self.split_case_string(string)
628
772
  s = cut_underscores_at_start string
629
773
  s = s.gsub(/([[:upper:]])/, '_\1') unless upper_case? s # prepend all upper characters with underscore
774
+ s = s.gsub(' ', '_') # replace all spaces with underscore
630
775
  s = s.gsub('-', '_') # replace all dashes with underscore
631
776
  s = cut_underscores_at_start s
632
777
  s.downcase.split('_').reject(&:empty?) # split everything by underscore
@@ -286,6 +286,110 @@ class String
286
286
  LuckyCase.train_case? self, allow_prefixed_underscores: allow_prefixed_underscores
287
287
  end
288
288
 
289
+ #----------------------------------------------------------------------------------------------------
290
+ # WORD CASE
291
+ #----------------------------------------------------------------------------------------------------
292
+
293
+ # Converts the given string from any case
294
+ # into word case
295
+ #
296
+ # @example conversion
297
+ # 'this-isAnExample_string' => 'this is an example string'
298
+ #
299
+ # @param [Boolean] preserve_prefixed_underscores
300
+ # @return [String]
301
+ def word_case(preserve_prefixed_underscores: true)
302
+ LuckyCase.word_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
303
+ end
304
+
305
+ def word_case!(preserve_prefixed_underscores: true)
306
+ set_self_value self.word_case preserve_prefixed_underscores: preserve_prefixed_underscores
307
+ end
308
+
309
+ # Checks if the string is word case
310
+ #
311
+ # @param [Boolean] allow_prefixed_underscores
312
+ # @return [Boolean]
313
+ def word_case?(allow_prefixed_underscores: true)
314
+ LuckyCase.word_case? self, allow_prefixed_underscores: allow_prefixed_underscores
315
+ end
316
+
317
+ # Converts the given string from any case
318
+ # into upper word case
319
+ #
320
+ # @example conversion
321
+ # 'this-isAnExample_string' => 'THIS IS AN EXAMPLE STRING'
322
+ #
323
+ # @param [Boolean] preserve_prefixed_underscores
324
+ # @return [String]
325
+ def upper_word_case(preserve_prefixed_underscores: true)
326
+ LuckyCase.upper_word_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
327
+ end
328
+
329
+ def upper_word_case!(preserve_prefixed_underscores: true)
330
+ set_self_value self.upper_word_case preserve_prefixed_underscores: preserve_prefixed_underscores
331
+ end
332
+
333
+ # Checks if the string is upper word case
334
+ #
335
+ # @param [Boolean] allow_prefixed_underscores
336
+ # @return [Boolean]
337
+ def upper_word_case?(allow_prefixed_underscores: true)
338
+ LuckyCase.upper_word_case? self, allow_prefixed_underscores: allow_prefixed_underscores
339
+ end
340
+
341
+ # Converts the given string from any case
342
+ # into capital word case
343
+ #
344
+ # @example conversion
345
+ # 'this-isAnExample_string' => 'This Is An Example String'
346
+ #
347
+ # @param [Boolean] preserve_prefixed_underscores
348
+ # @return [String]
349
+ def capital_word_case(preserve_prefixed_underscores: true)
350
+ LuckyCase.capital_word_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
351
+ end
352
+
353
+ def capital_word_case!(preserve_prefixed_underscores: true)
354
+ set_self_value self.capital_word_case preserve_prefixed_underscores: preserve_prefixed_underscores
355
+ end
356
+
357
+ # Checks if the string is capital word case
358
+ #
359
+ # @param [Boolean] allow_prefixed_underscores
360
+ # @return [Boolean]
361
+ def capital_word_case?(allow_prefixed_underscores: true)
362
+ LuckyCase.capital_word_case? self, allow_prefixed_underscores: allow_prefixed_underscores
363
+ end
364
+
365
+ #----------------------------------------------------------------------------------------------------
366
+ # SENTENCE CASE
367
+ #----------------------------------------------------------------------------------------------------
368
+
369
+ # Converts the given string from any case
370
+ # into sentence case
371
+ #
372
+ # @example conversion
373
+ # 'this-isAnExample_string' => 'This is an example string'
374
+ #
375
+ # @param [Boolean] preserve_prefixed_underscores
376
+ # @return [String]
377
+ def sentence_case(preserve_prefixed_underscores: true)
378
+ LuckyCase.sentence_case self, preserve_prefixed_underscores: preserve_prefixed_underscores
379
+ end
380
+
381
+ def sentence_case!(preserve_prefixed_underscores: true)
382
+ set_self_value self.sentence_case preserve_prefixed_underscores: preserve_prefixed_underscores
383
+ end
384
+
385
+ # Checks if the string is sentence case
386
+ #
387
+ # @param [Boolean] allow_prefixed_underscores
388
+ # @return [Boolean]
389
+ def sentence_case?(allow_prefixed_underscores: true)
390
+ LuckyCase.sentence_case? self, allow_prefixed_underscores: allow_prefixed_underscores
391
+ end
392
+
289
393
  #----------------------------------------------------------------------------------------------------
290
394
  # CAPITALIZE
291
395
  #----------------------------------------------------------------------------------------------------
@@ -1,3 +1,3 @@
1
1
  module LuckyCase
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucky_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthäus J. N. Beyrle