ncase 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16a2690323ff720a8937d24dd52c62d8d3e66811f8d9cc9ce97dedb55caee3f3
4
- data.tar.gz: b0b4e213140f6140c953484e37ffa020b394b5ca0bbda1ace27ff71c15c9c1ee
3
+ metadata.gz: f63a81646702a0bf54bb8987867f86144a6fc4bf816893bad066ef84ef46d4ae
4
+ data.tar.gz: e1a824fd7c86e5245f39c8d30f5391aed2672247598b0255ada6728e18b79e96
5
5
  SHA512:
6
- metadata.gz: 9b60a5f4275a83b679caa3cc8c479a1a8ec74535be18fb5c5880351be1db47848c25ea446614caa0930b06562748f1c544a48fd2f2e258ee4c491d8c3a3fa134
7
- data.tar.gz: 5ace2c70da311791b66f53e65e08b83aa031a663469094d6e96d155c69f7baaa21f285c9aa6d5aa5f83d503cb3f20037ac6c9ac0521780d89ed5e6289b11f143
6
+ metadata.gz: 0bb39994226927772dc6a1cb7037d4bdb56cf27573436fbebbcbfc8e1a6bd45cf49ac9ee8ebf39b94c23f0a2d6d68713d09a51eebaf134516b313e733c3beef0
7
+ data.tar.gz: 2a391f0aa7a8de40ee82b8e51788ac57111c9bd4bf089866a5ddb8fdb0a68c76cb46e7ceac76d48d285869ddb03e07aaf5eeb49f11694d66c30bcd865a6bf50d
data/bin/ncase CHANGED
@@ -1,33 +1,34 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- $LOAD_PATH.unshift("#{__dir__}/../lib") if __dir__ =~ /proj|src/i
4
+ $LOAD_PATH.unshift("#{__dir__}/../lib") if /proj|src/i.match?(__dir__)
5
5
 
6
6
  require "ncase"
7
7
  require "optparse"
8
8
 
9
9
  separator = nil
10
- to_case = :random_case
10
+ to_case = :random
11
+
11
12
  OptionParser.new do |opts|
12
13
  opts.banner = "Usage: ncase [OPTIONS] [TEXT]"
13
14
  opts.version = Ncase::VERSION
14
15
 
15
- opts.on("-F", "--separator REGEXP", "Use REGEXP as the separator") {|v| separator = v}
16
-
17
- opts.on("-c", "--camel-case", "Enforce camelCase") {to_case = :camel_case}
18
- opts.on("-P", "--pascal-case", "Enforce PascalCase") {to_case = :pascal_case}
19
- opts.on("-k", "--kebab-case", "Enforce kebab-case") {to_case = :kebab_case}
20
- opts.on("-K", "--upper-kebab-case", "Enforce KEBAB-CASE") {to_case = :upper_kebab_case}
21
- opts.on("-l", "--lower-case", "Enforce `lower case`") {to_case = :lower_case}
22
- opts.on("-U", "--upper-case", "Enforce `UPPER CASE`") {to_case = :upper_case}
23
- opts.on("-s", "--snake-case", "Enforce snake_case") {to_case = :snake_case}
24
- opts.on("-S", "--upper-snake-case", "Enforce SNAKE_CASE") {to_case = :upper_snake_case}
25
- opts.on("-t", "--title-case", "Enforce `Title Case`") {to_case = :title_case}
26
- opts.on("-T", "--inver-title-case", "Enforce `tITLE cASE`") {to_case = :inver_title_case}
27
- opts.on("-r", "--random-case", "Enforce `rAnDOm CaSe`") {to_case = :random_case}
16
+ opts.on("-F", "--separator REGEXP", "Use REGEXP as the separator") { |v| separator = v }
17
+
18
+ opts.on("-c", "--camel-case", "Enforce camelCase") { to_case = :camel }
19
+ opts.on("-P", "--pascal-case", "Enforce PascalCase") { to_case = :pascal }
20
+ opts.on("-s", "--snake-case", "Enforce snake_case") { to_case = :snake }
21
+ opts.on("-S", "--upper-snake-case", "Enforce SNAKE_CASE") { to_case = :upper_snake }
22
+ opts.on("-k", "--kebab-case", "Enforce kebab-case") { to_case = :kebab }
23
+ opts.on("-K", "--upper-kebab-case", "Enforce KEBAB-CASE") { to_case = :upper_kebab }
24
+ opts.on("-l", "--lower-case", "Enforce `lower case`") { to_case = :lower }
25
+ opts.on("-U", "--upper-case", "Enforce `UPPER CASE`") { to_case = :upper }
26
+ opts.on("-t", "--title-case", "Enforce `Title Case`") { to_case = :title }
27
+ opts.on("-T", "--inver-title-case", "Enforce `tITLE cASE`") { to_case = :inver_title }
28
+ opts.on("-r", "--random-case", "Enforce `rAnDOm CaSe`") { to_case = :random }
28
29
  end.parse!
29
30
 
30
31
  separator &&= Regexp.new(separator)
31
- in_lines = if ARGV.empty? then readlines else [ARGV.join(" ")] end
32
- out_lines = in_lines.map {|l| Ncase::Words.new(l, separator: separator).__send__ to_case}
33
- puts out_lines
32
+
33
+ input = ARGV.empty? ? readlines : [ARGV.join(" ")]
34
+ input.each { |line| puts Ncase.public_send(to_case, line, separator: separator) }
data/lib/ncase/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ncase
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/ncase/words.rb CHANGED
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Support library for +bin/ncase+.
4
3
  module Ncase
5
- # Implements efficient conversion of a string into a multitude of case
6
- # styles.
4
+ # Implements efficient conversion of a string into multiple case styles.
7
5
  #
8
6
  # By default will guess the separator in the string:
9
7
  #
@@ -13,115 +11,107 @@ module Ncase
13
11
  # more frequent is a separator.
14
12
  # 3. A case-switch is a separator.
15
13
  #
16
- # @example Convert a string into +PascalCase+
14
+ # @example Convert a string into +PascalCase+ and +snake_case+
17
15
  # require "ncase"
18
16
  #
19
17
  # w = Ncase::Words.new("this is a test string")
20
18
  # p w.pascal_case # => "ThisIsATestString"
19
+ # p w.snake_case # => "this_is_a_test_string"
21
20
  class Words
22
- # @param s [String] the string to convert
21
+ # @param str [String] the string to work with
23
22
  # @param separator [Regexp] the pattern to split the string into words.
24
23
  # If +nil+, it will guess the separator as described in {Words}.
25
24
  # @see String#split
26
- def initialize(s, separator: nil)
27
- ss = s.strip
28
- separator ||= guess_separator(ss)
29
- @words = ss.split(separator)
25
+ def initialize(str, separator: nil)
26
+ sstr = str.strip
27
+ separator ||= guess_separator(sstr)
28
+ @words = sstr.split(separator)
30
29
  end
31
30
 
32
31
  # @return [String] the +camelCase+ representation of the string
33
32
  def camel_case
34
- first_word = @words.first
35
- if first_word
36
- @words.drop(1)
37
- .map {|s| s.capitalize}
38
- .unshift(first_word.downcase)
39
- .join
40
- else
41
- ""
42
- end
33
+ return "" if @words.empty?
34
+
35
+ @words.each(&:capitalize!)
36
+ @words.first.downcase!
37
+ @words.join
43
38
  end
44
39
 
45
40
  # @return [String] the +PascalCase+ representation of the string
46
41
  def pascal_case
47
- @words.map {|s| s.capitalize}.join
42
+ @words.each(&:capitalize!).join
48
43
  end
49
44
 
50
45
  # @return [String] the +kebab-case+ representation of the string
51
46
  def kebab_case
52
- @words.map {|s| s.downcase}.join("-")
47
+ @words.each(&:downcase!).join("-")
53
48
  end
54
49
 
55
50
  # @return [String] the +KEBAB-CASE+ representation of the string
56
51
  def upper_kebab_case
57
- @words.map {|s| s.upcase}.join("-")
52
+ @words.each(&:upcase!).join("-")
58
53
  end
59
54
 
60
55
  # @return [String] the +lower case+ representation of the string
61
56
  def lower_case
62
- @words.map {|s| s.downcase}.join(" ")
57
+ @words.each(&:downcase!).join(" ")
63
58
  end
64
59
 
65
60
  # @return [String] the +UPPER CASE+ representation of the string
66
61
  def upper_case
67
- @words.map {|s| s.upcase}.join(" ")
62
+ @words.each(&:upcase!).join(" ")
68
63
  end
69
64
 
70
65
  # @return [String] the +snake_case+ representation of the string
71
66
  def snake_case
72
- @words.map {|s| s.downcase}.join("_")
67
+ @words.each(&:downcase!).join("_")
73
68
  end
74
69
 
75
70
  # @return [String] the +SNAKE_CASE+ representation of the string
76
71
  def upper_snake_case
77
- @words.map {|s| s.upcase}.join("_")
72
+ @words.each(&:upcase!).join("_")
78
73
  end
79
74
 
80
75
  # @return [String] the +Title Case+ representation of the string
81
76
  def title_case
82
- @words.map {|s| s.capitalize}.join(" ")
77
+ @words.each(&:capitalize!).join(" ")
83
78
  end
84
79
 
85
80
  # @return [String] the +tITLE cASE+ representation of the string
86
81
  def inver_title_case
87
- title_case.swapcase
82
+ @words.each(&:capitalize!).each(&:swapcase!).join(" ")
88
83
  end
89
84
 
90
85
  # @return [String] a +rAnDOm CaSe+ representation of the string
91
86
  def random_case
92
87
  @words.join(" ")
93
- .chars
94
- .map {|c| if rand(2) == 0 then c.downcase else c.upcase end}
95
- .join
88
+ .chars
89
+ .each { |c| rand(2).zero? ? c.downcase! : c.upcase! }
90
+ .join
96
91
  end
97
92
 
98
- SPACE_SEP_REGEXP = /\s+/
99
- HYPHEN_SEP_REGEXP = /-/
100
- UNDERSCORE_SEP_REGEXP = /_/
93
+ SPACE_SEP_REGEXP = /\s+/.freeze
94
+ HYPHEN_SEP_REGEXP = /-/.freeze
95
+ UNDERSCORE_SEP_REGEXP = /_/.freeze
101
96
  CASE_SEP_REGEXP = / (?<=[[:lower:]]) (?=[[:upper:]]) # z|A
102
97
  | (?<=[[:upper:]]) (?=[[:upper:]] [[:lower:]]) # A|Bc
103
- /x
98
+ /x.freeze
104
99
  private_constant :SPACE_SEP_REGEXP, :HYPHEN_SEP_REGEXP, :UNDERSCORE_SEP_REGEXP, :CASE_SEP_REGEXP
105
100
 
106
101
  private
107
102
 
103
+ # @param str [String] the string to guess the separator
108
104
  # @return [Regexp] the most likely separator for the string
109
- def guess_separator(s)
110
- if s.include?(" ")
111
- SPACE_SEP_REGEXP
112
- else
113
- num_both = s.count("-_")
114
- if num_both > 0
115
- num_hyphens = s.count("-")
116
- if num_hyphens * 2 >= num_both
117
- HYPHEN_SEP_REGEXP
118
- else
119
- UNDERSCORE_SEP_REGEXP
120
- end
121
- else
122
- CASE_SEP_REGEXP
123
- end
105
+ def guess_separator(str)
106
+ return SPACE_SEP_REGEXP if str.include?(" ")
107
+
108
+ num_both = str.count("-_")
109
+ if num_both.positive?
110
+ return HYPHEN_SEP_REGEXP if str.count("-") * 2 >= num_both
111
+
112
+ return UNDERSCORE_SEP_REGEXP
124
113
  end
114
+ CASE_SEP_REGEXP
125
115
  end
126
116
  end
127
117
  end
data/lib/ncase.rb CHANGED
@@ -2,3 +2,21 @@
2
2
 
3
3
  require_relative "ncase/version"
4
4
  require_relative "ncase/words"
5
+
6
+ # Contains convenience methods for one-off use.
7
+ module Ncase
8
+ module_function
9
+
10
+ # Delegate +<name>+ to +Words#<name>_case+ if it's defined.
11
+ def method_missing(name, *args)
12
+ if (method = Words.public_instance_method("#{name}_case"))
13
+ method.bind(Words.new(*args)).call
14
+ else
15
+ super
16
+ end
17
+ end
18
+
19
+ def respond_to_missing?(name, *)
20
+ Words.public_instance_method("#{name}_case") || super
21
+ end
22
+ end
metadata CHANGED
@@ -1,58 +1,114 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ncase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stas Miasnikoŭ
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-02 00:00:00.000000000 Z
11
+ date: 2021-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.11'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.11'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '12.3'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '12.3'
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
41
97
  - !ruby/object:Gem::Dependency
42
98
  name: yard
43
99
  requirement: !ruby/object:Gem::Requirement
44
100
  requirements:
45
- - - "~>"
101
+ - - ">="
46
102
  - !ruby/object:Gem::Version
47
- version: '0.9'
103
+ version: '0'
48
104
  type: :development
49
105
  prerelease: false
50
106
  version_requirements: !ruby/object:Gem::Requirement
51
107
  requirements:
52
- - - "~>"
108
+ - - ">="
53
109
  - !ruby/object:Gem::Version
54
- version: '0.9'
55
- description:
110
+ version: '0'
111
+ description:
56
112
  email:
57
113
  - miasnikou@yandex.by
58
114
  executables:
@@ -69,7 +125,7 @@ licenses:
69
125
  - ISC
70
126
  metadata:
71
127
  source_code_uri: https://github.com/xmyst/ncase
72
- post_install_message:
128
+ post_install_message:
73
129
  rdoc_options: []
74
130
  require_paths:
75
131
  - lib
@@ -77,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
133
  requirements:
78
134
  - - ">="
79
135
  - !ruby/object:Gem::Version
80
- version: '0'
136
+ version: '2.6'
81
137
  required_rubygems_version: !ruby/object:Gem::Requirement
82
138
  requirements:
83
139
  - - ">="
@@ -85,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
141
  version: '0'
86
142
  requirements: []
87
143
  rubygems_version: 3.0.3
88
- signing_key:
144
+ signing_key:
89
145
  specification_version: 4
90
146
  summary: Enforce a case style.
91
147
  test_files: []