change_case 0.1.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +64 -0
- data/lib/change_case/string.rb +69 -0
- data/lib/change_case/version.rb +1 -1
- data/lib/change_case.rb +68 -13
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cc32b4cba960d81ff1c275a32d3184fa6bba468fa38dc020eebb86d95ced60d
|
4
|
+
data.tar.gz: a98036290d52e34276bbbc74f64326fff3056e65135311130c27d58a1e88ae56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de7d6787223100cd5d4efe747b72e100258c551e828e0bdb9896608a4c4774bc39324ae82349eeb3caf899e1bb51ff1604ed871a0b3d82383e91705934b730fb
|
7
|
+
data.tar.gz: fe3cbe4baab5e60ddbfdb82ac927d1d08dfd42be08d7ea8173f929e2f03a5d5a3fed3b26f83121fabcd0ec35ca30981ca3455cab42f83949c5a157f466e38d55
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# ChangeCase
|
2
|
+
|
3
|
+
Transform a string between `camelCase`, `PascalCase`, `Capital Case`, `snake_case`, `kebab-case`, `CONSTANT_CASE` and others.
|
4
|
+
|
5
|
+
[](https://github.com/javoeria/change_case/actions/workflows/main.yml)
|
6
|
+
[](https://rubygems.org/gems/change_case)
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Install the gem and add to the application's Gemfile by executing:
|
11
|
+
|
12
|
+
```bash
|
13
|
+
bundle add change_case
|
14
|
+
```
|
15
|
+
|
16
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
17
|
+
|
18
|
+
```bash
|
19
|
+
gem install change_case
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require "change_case"
|
26
|
+
|
27
|
+
ChangeCase.camel("TEST_VALUE") #=> "testValue"
|
28
|
+
# OR
|
29
|
+
"TEST_VALUE".camelcase #=> "testValue"
|
30
|
+
```
|
31
|
+
|
32
|
+
Included case methods:
|
33
|
+
|
34
|
+
| ChangeCase | String | Result |
|
35
|
+
| -------------- | -------------- | ----------- |
|
36
|
+
| `camel` | `camelcase` | `twoWords` |
|
37
|
+
| `capital` | `capitalcase` | `Two Words` |
|
38
|
+
| `constant` | `constantcase` | `TWO_WORDS` |
|
39
|
+
| `dot` | `dotcase` | `two.words` |
|
40
|
+
| `kebab` | `kebabcae` | `two-words` |
|
41
|
+
| `no` | `nocase` | `two words` |
|
42
|
+
| `pascal` | `pascalcase` | `TwoWords` |
|
43
|
+
| `pascal_snake` | `pascalcase` | `Two_Words` |
|
44
|
+
| `path` | `pathcase` | `two/words` |
|
45
|
+
| `sentence` | `sentencecase` | `Two words` |
|
46
|
+
| `snake` | `snakecase` | `two_words` |
|
47
|
+
| `train` | `traincase` | `Two-Words` |
|
48
|
+
| `title` | `titlecase` | `Two Words` |
|
49
|
+
| `swap` | `swapcase` | `tWO wORDS` |
|
50
|
+
| `sponge` | `spongecase` | `TwO wORdS` |
|
51
|
+
|
52
|
+
## Development
|
53
|
+
|
54
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
55
|
+
|
56
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/javoeria/change_case.
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Add ChangeCase methods on the String class by monkey patching.
|
4
|
+
class String
|
5
|
+
# Converts the string to camel case.
|
6
|
+
def camelcase
|
7
|
+
ChangeCase.camel(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Converts the string to capital case.
|
11
|
+
def capitalcase
|
12
|
+
ChangeCase.capital(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Converts the string to constant case.
|
16
|
+
def constantcase
|
17
|
+
ChangeCase.constant(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Converts the string to dot case.
|
21
|
+
def dotcase
|
22
|
+
ChangeCase.dot(self)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Converts the string to kebab/param case.
|
26
|
+
def kebabcase
|
27
|
+
ChangeCase.kebab(self)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Converts the string to space separated lowercase.
|
31
|
+
def nocase
|
32
|
+
ChangeCase.no(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Converts the string to pascal case.
|
36
|
+
def pascalcase(snake: false)
|
37
|
+
snake ? ChangeCase.pascal_snake(self) : ChangeCase.pascal(self)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Converts the string to path case.
|
41
|
+
def pathcase
|
42
|
+
ChangeCase.path(self)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Converts the string to sentence case.
|
46
|
+
def sentencecase
|
47
|
+
ChangeCase.sentence(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Converts the string to snake case.
|
51
|
+
def snakecase
|
52
|
+
ChangeCase.snake(self)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Converts the string to train/header case.
|
56
|
+
def traincase
|
57
|
+
ChangeCase.train(self)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Converts the string to title case.
|
61
|
+
def titlecase
|
62
|
+
ChangeCase.title(self)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Converts the string with random capitalization applied.
|
66
|
+
def spongecase
|
67
|
+
ChangeCase.sponge(self)
|
68
|
+
end
|
69
|
+
end
|
data/lib/change_case/version.rb
CHANGED
data/lib/change_case.rb
CHANGED
@@ -1,53 +1,108 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "change_case/version"
|
4
|
+
require_relative "change_case/string"
|
4
5
|
|
6
|
+
# Module used for converting strings to any case.
|
5
7
|
module ChangeCase
|
8
|
+
# Converts the string to camel case.
|
6
9
|
def self.camel(input)
|
7
|
-
|
10
|
+
split_string(input).map.with_index { |word, i| i.zero? ? word : word.capitalize }.join
|
8
11
|
end
|
9
12
|
|
13
|
+
# Converts the string to capital case.
|
10
14
|
def self.capital(input)
|
11
|
-
|
15
|
+
split_string(input).map(&:capitalize).join(" ")
|
12
16
|
end
|
13
17
|
|
18
|
+
# Converts the string to constant case.
|
14
19
|
def self.constant(input)
|
15
|
-
|
20
|
+
split_string(input).join("_").upcase
|
16
21
|
end
|
17
22
|
|
23
|
+
# Converts the string to dot case.
|
18
24
|
def self.dot(input)
|
19
|
-
|
25
|
+
split_string(input).join(".")
|
20
26
|
end
|
21
27
|
|
28
|
+
# Converts the string to kebab/param case.
|
22
29
|
def self.kebab(input)
|
23
|
-
|
30
|
+
split_string(input).join("-")
|
24
31
|
end
|
25
32
|
|
33
|
+
# Converts the string to space separated lowercase.
|
26
34
|
def self.no(input)
|
27
|
-
|
35
|
+
split_string(input).join(" ")
|
28
36
|
end
|
29
37
|
|
38
|
+
# Converts the string to pascal case.
|
30
39
|
def self.pascal(input)
|
31
|
-
|
40
|
+
split_string(input).map(&:capitalize).join
|
32
41
|
end
|
33
42
|
|
43
|
+
# Converts the string to pascal snake case.
|
44
|
+
def self.pascal_snake(input)
|
45
|
+
split_string(input).map(&:capitalize).join("_")
|
46
|
+
end
|
47
|
+
|
48
|
+
# Converts the string to path case.
|
34
49
|
def self.path(input)
|
35
|
-
|
50
|
+
split_string(input).join("/")
|
36
51
|
end
|
37
52
|
|
53
|
+
# Converts the string to sentence case.
|
38
54
|
def self.sentence(input)
|
39
|
-
|
55
|
+
split_string(input).join(" ").capitalize
|
40
56
|
end
|
41
57
|
|
58
|
+
# Converts the string to snake case.
|
42
59
|
def self.snake(input)
|
43
|
-
|
60
|
+
split_string(input).join("_")
|
44
61
|
end
|
45
62
|
|
63
|
+
# Converts the string to train/header case.
|
46
64
|
def self.train(input)
|
47
|
-
|
65
|
+
split_string(input).map(&:capitalize).join("-")
|
66
|
+
end
|
67
|
+
|
68
|
+
# Converts the string to title case.
|
69
|
+
def self.title(input)
|
70
|
+
small_words = %w[a an and as at because but by en for if in neither nor of on or only over per so some that than the to up upon vs versus via when with without yet]
|
71
|
+
split_string(input).map.with_index { |word, i| small_words.include?(word) && i.positive? ? word : word.capitalize }.join(" ")
|
72
|
+
end
|
73
|
+
|
74
|
+
# Converts the string with uppercase characters to lowercase and vice versa.
|
75
|
+
def self.swap(input)
|
76
|
+
input.swapcase
|
77
|
+
end
|
78
|
+
|
79
|
+
# Converts the string with random capitalization applied.
|
80
|
+
def self.sponge(input)
|
81
|
+
input.chars.map { |char| rand(2).zero? ? char.downcase : char.upcase }.join
|
82
|
+
end
|
83
|
+
|
84
|
+
# Converts the first character in the string to uppercase.
|
85
|
+
def self.upper_first(input)
|
86
|
+
input.empty? ? "" : input[0].upcase.concat(input[1..])
|
87
|
+
end
|
88
|
+
|
89
|
+
# Converts the first character in the string to lowercase.
|
90
|
+
def self.lower_first(input)
|
91
|
+
input.empty? ? "" : input[0].downcase.concat(input[1..])
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns +true+ if the string is uppercase only.
|
95
|
+
def self.upper?(input)
|
96
|
+
input == input.upcase
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns +true+ if the string is lowercase only.
|
100
|
+
def self.lower?(input)
|
101
|
+
input == input.downcase
|
48
102
|
end
|
49
103
|
|
50
|
-
private_class_method def self.
|
51
|
-
input.to_s.strip.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z0-9])([A-Z])/, '\1_\2')
|
104
|
+
private_class_method def self.split_string(input)
|
105
|
+
input.to_s.strip.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z0-9])([A-Z])/, '\1_\2')
|
106
|
+
.downcase.split(/[^a-z0-9']+/).reject(&:empty?)
|
52
107
|
end
|
53
108
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: change_case
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Javier Rosales
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-01-
|
10
|
+
date: 2025-01-16 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
email:
|
13
13
|
- javier.rb_96@hotmail.com
|
@@ -15,12 +15,16 @@ executables: []
|
|
15
15
|
extensions: []
|
16
16
|
extra_rdoc_files: []
|
17
17
|
files:
|
18
|
+
- CHANGELOG.md
|
19
|
+
- README.md
|
18
20
|
- lib/change_case.rb
|
21
|
+
- lib/change_case/string.rb
|
19
22
|
- lib/change_case/version.rb
|
20
23
|
homepage: https://github.com/javoeria/change_case
|
21
24
|
licenses:
|
22
25
|
- MIT
|
23
|
-
metadata:
|
26
|
+
metadata:
|
27
|
+
homepage_uri: https://github.com/javoeria/change_case
|
24
28
|
rdoc_options: []
|
25
29
|
require_paths:
|
26
30
|
- lib
|