ncase 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 16a2690323ff720a8937d24dd52c62d8d3e66811f8d9cc9ce97dedb55caee3f3
4
+ data.tar.gz: b0b4e213140f6140c953484e37ffa020b394b5ca0bbda1ace27ff71c15c9c1ee
5
+ SHA512:
6
+ metadata.gz: 9b60a5f4275a83b679caa3cc8c479a1a8ec74535be18fb5c5880351be1db47848c25ea446614caa0930b06562748f1c544a48fd2f2e258ee4c491d8c3a3fa134
7
+ data.tar.gz: 5ace2c70da311791b66f53e65e08b83aa031a663469094d6e96d155c69f7baaa21f285c9aa6d5aa5f83d503cb3f20037ac6c9ac0521780d89ed5e6289b11f143
data/bin/ncase ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift("#{__dir__}/../lib") if __dir__ =~ /proj|src/i
5
+
6
+ require "ncase"
7
+ require "optparse"
8
+
9
+ separator = nil
10
+ to_case = :random_case
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: ncase [OPTIONS] [TEXT]"
13
+ opts.version = Ncase::VERSION
14
+
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}
28
+ end.parse!
29
+
30
+ 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
data/lib/ncase.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ncase/version"
4
+ require_relative "ncase/words"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ncase
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Support library for +bin/ncase+.
4
+ module Ncase
5
+ # Implements efficient conversion of a string into a multitude of case
6
+ # styles.
7
+ #
8
+ # By default will guess the separator in the string:
9
+ #
10
+ # 1. If the string contains spaces '+\x20+', any sequence of whitespace is a
11
+ # separator.
12
+ # 2. If the string contains hyphens '+-+' or underscores '+_+', whichever is
13
+ # more frequent is a separator.
14
+ # 3. A case-switch is a separator.
15
+ #
16
+ # @example Convert a string into +PascalCase+
17
+ # require "ncase"
18
+ #
19
+ # w = Ncase::Words.new("this is a test string")
20
+ # p w.pascal_case # => "ThisIsATestString"
21
+ class Words
22
+ # @param s [String] the string to convert
23
+ # @param separator [Regexp] the pattern to split the string into words.
24
+ # If +nil+, it will guess the separator as described in {Words}.
25
+ # @see String#split
26
+ def initialize(s, separator: nil)
27
+ ss = s.strip
28
+ separator ||= guess_separator(ss)
29
+ @words = ss.split(separator)
30
+ end
31
+
32
+ # @return [String] the +camelCase+ representation of the string
33
+ 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
43
+ end
44
+
45
+ # @return [String] the +PascalCase+ representation of the string
46
+ def pascal_case
47
+ @words.map {|s| s.capitalize}.join
48
+ end
49
+
50
+ # @return [String] the +kebab-case+ representation of the string
51
+ def kebab_case
52
+ @words.map {|s| s.downcase}.join("-")
53
+ end
54
+
55
+ # @return [String] the +KEBAB-CASE+ representation of the string
56
+ def upper_kebab_case
57
+ @words.map {|s| s.upcase}.join("-")
58
+ end
59
+
60
+ # @return [String] the +lower case+ representation of the string
61
+ def lower_case
62
+ @words.map {|s| s.downcase}.join(" ")
63
+ end
64
+
65
+ # @return [String] the +UPPER CASE+ representation of the string
66
+ def upper_case
67
+ @words.map {|s| s.upcase}.join(" ")
68
+ end
69
+
70
+ # @return [String] the +snake_case+ representation of the string
71
+ def snake_case
72
+ @words.map {|s| s.downcase}.join("_")
73
+ end
74
+
75
+ # @return [String] the +SNAKE_CASE+ representation of the string
76
+ def upper_snake_case
77
+ @words.map {|s| s.upcase}.join("_")
78
+ end
79
+
80
+ # @return [String] the +Title Case+ representation of the string
81
+ def title_case
82
+ @words.map {|s| s.capitalize}.join(" ")
83
+ end
84
+
85
+ # @return [String] the +tITLE cASE+ representation of the string
86
+ def inver_title_case
87
+ title_case.swapcase
88
+ end
89
+
90
+ # @return [String] a +rAnDOm CaSe+ representation of the string
91
+ def random_case
92
+ @words.join(" ")
93
+ .chars
94
+ .map {|c| if rand(2) == 0 then c.downcase else c.upcase end}
95
+ .join
96
+ end
97
+
98
+ SPACE_SEP_REGEXP = /\s+/
99
+ HYPHEN_SEP_REGEXP = /-/
100
+ UNDERSCORE_SEP_REGEXP = /_/
101
+ CASE_SEP_REGEXP = / (?<=[[:lower:]]) (?=[[:upper:]]) # z|A
102
+ | (?<=[[:upper:]]) (?=[[:upper:]] [[:lower:]]) # A|Bc
103
+ /x
104
+ private_constant :SPACE_SEP_REGEXP, :HYPHEN_SEP_REGEXP, :UNDERSCORE_SEP_REGEXP, :CASE_SEP_REGEXP
105
+
106
+ private
107
+
108
+ # @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
124
+ end
125
+ end
126
+ end
127
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ncase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stas Miasnikoŭ
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ description:
56
+ email:
57
+ - miasnikou@yandex.by
58
+ executables:
59
+ - ncase
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - bin/ncase
64
+ - lib/ncase.rb
65
+ - lib/ncase/version.rb
66
+ - lib/ncase/words.rb
67
+ homepage: https://github.com/xmyst/ncase
68
+ licenses:
69
+ - ISC
70
+ metadata:
71
+ source_code_uri: https://github.com/xmyst/ncase
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.0.3
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Enforce a case style.
91
+ test_files: []