keycase 1.0.0 → 1.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 +4 -4
- data/{LICENSE.txt → LICENSE.md} +6 -6
- data/README.md +38 -11
- data/keycase.gemspec +9 -6
- data/lib/keycase/camel_case.rb +27 -9
- data/lib/keycase/kebab_case.rb +26 -8
- data/lib/keycase/pascal_case.rb +27 -9
- data/lib/keycase/recursive_transform/engine.rb +81 -0
- data/lib/keycase/recursive_transform/errors.rb +12 -0
- data/lib/keycase/snake_case.rb +26 -8
- data/lib/keycase/version.rb +3 -1
- data/lib/keycase.rb +4 -0
- metadata +12 -19
- data/.gitignore +0 -14
- data/.rspec +0 -3
- data/.travis.yml +0 -6
- data/Gemfile +0 -6
- data/Rakefile +0 -6
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9269ac3f6277ae7eec6635c9bb3a5187522ba0d2d7fb25544d7f1e4aa9fb76f6
|
|
4
|
+
data.tar.gz: a4ba9ae8a5305896cb5f10775a19f77e7b7ea93d7699c54f628d4e93bc5d84ad
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c3c1c1d9292ddcbf6105612cf83c6015701729dcd4735237426726bbd38ba73c69f637600d24d420957c386499818997a4d89402e6eadc4a32fcd089e30e47b5
|
|
7
|
+
data.tar.gz: bf20fa1ce9b135f12a080c01bb2fcf55906cd66027273b31c29346a818c070d56471695d2a1b68c0fdd21cea5ca56f4d172410c3c0283d245386d43dac3d18a6
|
data/{LICENSE.txt → LICENSE.md}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# MIT License
|
|
2
2
|
|
|
3
|
-
Copyright
|
|
3
|
+
Copyright © 2020 naoigcat
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
9
9
|
copies of the Software, and to permit persons to whom the Software is
|
|
10
10
|
furnished to do so, subject to the following conditions:
|
|
11
11
|
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
14
|
|
|
15
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Keycase
|
|
2
2
|
|
|
3
3
|
This gem converts the case of strings, symbols, and keys of hash recursively.
|
|
4
4
|
The convertible cases are camelCase, PascalCase, snake_case, etc.
|
|
5
5
|
|
|
6
|
-
##
|
|
6
|
+
## Installation
|
|
7
7
|
|
|
8
8
|
Add this line to your application's Gemfile:
|
|
9
9
|
|
|
@@ -13,16 +13,16 @@ gem "keycase"
|
|
|
13
13
|
|
|
14
14
|
And then execute:
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
$ bundle install
|
|
17
17
|
|
|
18
18
|
Or install it yourself as:
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
$ gem install keycase
|
|
21
21
|
|
|
22
|
-
##
|
|
22
|
+
## Usage
|
|
23
23
|
|
|
24
24
|
```sh
|
|
25
|
-
|
|
25
|
+
irb --context-mode=1
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
```rb
|
|
@@ -64,16 +64,43 @@ $ irb --context-mode=1
|
|
|
64
64
|
}
|
|
65
65
|
```
|
|
66
66
|
|
|
67
|
-
##
|
|
67
|
+
## Development
|
|
68
68
|
|
|
69
|
-
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.
|
|
69
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests with the local Ruby version defined in `mise.toml` (currently Ruby 3.4.9). You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
70
70
|
|
|
71
71
|
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
Tasks are defined in `mise.toml`. They run **RuboCop, and RSpec inside Docker** (via Docker Compose), so Docker must be available. Use these tasks for syntax checks and tests across supported Ruby versions.
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
Run **RuboCop** on every supported Ruby image (2.3 through 4.0):
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
```sh
|
|
78
|
+
mise run -j 1 rubocop
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Run **RSpec** the same way:
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
mise run -j 1 rspec
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
These commands execute the version-specific tasks in order (`rubocop23` … `rubocop40`, `rspec23` … `rspec40`). To run against a **single** Ruby version, use the matching task name, for example:
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
mise run rubocop34
|
|
91
|
+
mise run rspec34
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
To list all tasks and descriptions:
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
mise tasks
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Contributing
|
|
101
|
+
|
|
102
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/naoigcat/ruby-keycase>.
|
|
103
|
+
|
|
104
|
+
## License
|
|
78
105
|
|
|
79
106
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/keycase.gemspec
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative "lib/keycase/version"
|
|
2
4
|
|
|
3
5
|
Gem::Specification.new do |spec|
|
|
@@ -7,23 +9,24 @@ Gem::Specification.new do |spec|
|
|
|
7
9
|
spec.email = ["17925623+naoigcat@users.noreply.github.com"]
|
|
8
10
|
|
|
9
11
|
spec.summary = "Converts the case of strings, symbols, and keys of hash."
|
|
10
|
-
spec.description =
|
|
12
|
+
spec.description = <<-DESCRIPTION
|
|
11
13
|
This gem converts the case of strings, symbols, and keys of hash recursively.
|
|
12
14
|
The convertible cases are camelCase, PascalCase, snake_case, etc.
|
|
13
15
|
DESCRIPTION
|
|
14
16
|
spec.homepage = "https://github.com/naoigcat/ruby-keycase"
|
|
15
17
|
spec.license = "MIT"
|
|
16
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.
|
|
18
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
17
19
|
|
|
18
20
|
# Specify which files should be added to the gem when it is released.
|
|
19
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
22
|
+
spec.require_paths = ["lib"]
|
|
20
23
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
21
|
-
`git ls-files -z`.split("\x0").
|
|
24
|
+
`git ls-files -z`.split("\x0").select do |f|
|
|
25
|
+
f.match(%r{^lib/|^LICENSE\.md$|^README\.md$|^keycase\.gemspec$})
|
|
26
|
+
end
|
|
22
27
|
end
|
|
23
|
-
spec.bindir = "exe"
|
|
24
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
25
|
-
spec.require_paths = ["lib"]
|
|
26
28
|
|
|
27
29
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
28
30
|
spec.metadata["source_code_uri"] = spec.homepage
|
|
31
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
29
32
|
end
|
data/lib/keycase/camel_case.rb
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
require_relative "recursive_transform/engine"
|
|
6
|
+
|
|
1
7
|
module Keycase
|
|
2
8
|
module CamelCase
|
|
3
9
|
refine Object do
|
|
@@ -5,16 +11,18 @@ module Keycase
|
|
|
5
11
|
self
|
|
6
12
|
end
|
|
7
13
|
|
|
8
|
-
def with_camel_case_keys
|
|
14
|
+
def with_camel_case_keys(_options = {})
|
|
9
15
|
self
|
|
10
16
|
end
|
|
11
17
|
end
|
|
12
18
|
|
|
13
19
|
refine String do
|
|
14
20
|
def to_camel_case
|
|
15
|
-
gsub(/(?<=[
|
|
21
|
+
gsub(/(?<=[A-Z])(?=[A-Z][a-z])/) do |_|
|
|
22
|
+
"_"
|
|
23
|
+
end.gsub(/(?<=[0-9a-z])(?=[A-Z])/) do |_|
|
|
16
24
|
"_"
|
|
17
|
-
end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched|
|
|
25
|
+
end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched|
|
|
18
26
|
matched.capitalize
|
|
19
27
|
end.sub(/^(?:\W|_)*([A-Z]+(?=[A-Z][0-9A-Za-z]|\d|$)|[A-Z][a-z])/) do |_|
|
|
20
28
|
Regexp.last_match(1).downcase
|
|
@@ -31,17 +39,27 @@ module Keycase
|
|
|
31
39
|
end
|
|
32
40
|
|
|
33
41
|
refine Array do
|
|
34
|
-
def with_camel_case_keys
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
def with_camel_case_keys(options = {})
|
|
43
|
+
Keycase::RecursiveTransform::Engine.transform_array(
|
|
44
|
+
self,
|
|
45
|
+
::Set.new,
|
|
46
|
+
0,
|
|
47
|
+
options[:max_depth]
|
|
48
|
+
) do |key|
|
|
49
|
+
key.to_camel_case
|
|
37
50
|
end
|
|
38
51
|
end
|
|
39
52
|
end
|
|
40
53
|
|
|
41
54
|
refine Hash do
|
|
42
|
-
def with_camel_case_keys
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
def with_camel_case_keys(options = {})
|
|
56
|
+
Keycase::RecursiveTransform::Engine.transform_hash(
|
|
57
|
+
self,
|
|
58
|
+
::Set.new,
|
|
59
|
+
0,
|
|
60
|
+
options[:max_depth]
|
|
61
|
+
) do |key|
|
|
62
|
+
key.to_camel_case
|
|
45
63
|
end
|
|
46
64
|
end
|
|
47
65
|
end
|
data/lib/keycase/kebab_case.rb
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
require_relative "recursive_transform/engine"
|
|
6
|
+
|
|
1
7
|
module Keycase
|
|
2
8
|
module KebabCase
|
|
3
9
|
refine Object do
|
|
@@ -5,14 +11,16 @@ module Keycase
|
|
|
5
11
|
self
|
|
6
12
|
end
|
|
7
13
|
|
|
8
|
-
def with_kebab_case_keys
|
|
14
|
+
def with_kebab_case_keys(_options = {})
|
|
9
15
|
self
|
|
10
16
|
end
|
|
11
17
|
end
|
|
12
18
|
|
|
13
19
|
refine String do
|
|
14
20
|
def to_kebab_case
|
|
15
|
-
gsub(/(?<=[
|
|
21
|
+
gsub(/(?<=[A-Z])(?=[A-Z][a-z])/) do |_|
|
|
22
|
+
"-"
|
|
23
|
+
end.gsub(/(?<=[0-9a-z])(?=[A-Z])/) do |_|
|
|
16
24
|
"-"
|
|
17
25
|
end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched|
|
|
18
26
|
"-#{matched.downcase}"
|
|
@@ -27,17 +35,27 @@ module Keycase
|
|
|
27
35
|
end
|
|
28
36
|
|
|
29
37
|
refine Array do
|
|
30
|
-
def with_kebab_case_keys
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
def with_kebab_case_keys(options = {})
|
|
39
|
+
Keycase::RecursiveTransform::Engine.transform_array(
|
|
40
|
+
self,
|
|
41
|
+
::Set.new,
|
|
42
|
+
0,
|
|
43
|
+
options[:max_depth]
|
|
44
|
+
) do |key|
|
|
45
|
+
key.to_kebab_case
|
|
33
46
|
end
|
|
34
47
|
end
|
|
35
48
|
end
|
|
36
49
|
|
|
37
50
|
refine Hash do
|
|
38
|
-
def with_kebab_case_keys
|
|
39
|
-
|
|
40
|
-
|
|
51
|
+
def with_kebab_case_keys(options = {})
|
|
52
|
+
Keycase::RecursiveTransform::Engine.transform_hash(
|
|
53
|
+
self,
|
|
54
|
+
::Set.new,
|
|
55
|
+
0,
|
|
56
|
+
options[:max_depth]
|
|
57
|
+
) do |key|
|
|
58
|
+
key.to_kebab_case
|
|
41
59
|
end
|
|
42
60
|
end
|
|
43
61
|
end
|
data/lib/keycase/pascal_case.rb
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
require_relative "recursive_transform/engine"
|
|
6
|
+
|
|
1
7
|
module Keycase
|
|
2
8
|
module PascalCase
|
|
3
9
|
refine Object do
|
|
@@ -5,16 +11,18 @@ module Keycase
|
|
|
5
11
|
self
|
|
6
12
|
end
|
|
7
13
|
|
|
8
|
-
def with_pascal_case_keys
|
|
14
|
+
def with_pascal_case_keys(_options = {})
|
|
9
15
|
self
|
|
10
16
|
end
|
|
11
17
|
end
|
|
12
18
|
|
|
13
19
|
refine String do
|
|
14
20
|
def to_pascal_case
|
|
15
|
-
gsub(/(?<=[
|
|
21
|
+
gsub(/(?<=[A-Z])(?=[A-Z][a-z])/) do |_|
|
|
22
|
+
"_"
|
|
23
|
+
end.gsub(/(?<=[0-9a-z])(?=[A-Z])/) do |_|
|
|
16
24
|
"_"
|
|
17
|
-
end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched|
|
|
25
|
+
end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched|
|
|
18
26
|
matched.capitalize
|
|
19
27
|
end.sub(/^(?:\W|_)*([A-Z]+(?=[A-Z][0-9A-Za-z]|\d|$)|[A-Z][a-z])/) do |_|
|
|
20
28
|
Regexp.last_match(1).capitalize
|
|
@@ -31,17 +39,27 @@ module Keycase
|
|
|
31
39
|
end
|
|
32
40
|
|
|
33
41
|
refine Array do
|
|
34
|
-
def with_pascal_case_keys
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
def with_pascal_case_keys(options = {})
|
|
43
|
+
Keycase::RecursiveTransform::Engine.transform_array(
|
|
44
|
+
self,
|
|
45
|
+
::Set.new,
|
|
46
|
+
0,
|
|
47
|
+
options[:max_depth]
|
|
48
|
+
) do |key|
|
|
49
|
+
key.to_pascal_case
|
|
37
50
|
end
|
|
38
51
|
end
|
|
39
52
|
end
|
|
40
53
|
|
|
41
54
|
refine Hash do
|
|
42
|
-
def with_pascal_case_keys
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
def with_pascal_case_keys(options = {})
|
|
56
|
+
Keycase::RecursiveTransform::Engine.transform_hash(
|
|
57
|
+
self,
|
|
58
|
+
::Set.new,
|
|
59
|
+
0,
|
|
60
|
+
options[:max_depth]
|
|
61
|
+
) do |key|
|
|
62
|
+
key.to_pascal_case
|
|
45
63
|
end
|
|
46
64
|
end
|
|
47
65
|
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "errors"
|
|
4
|
+
|
|
5
|
+
module Keycase
|
|
6
|
+
module RecursiveTransform
|
|
7
|
+
module Engine
|
|
8
|
+
class << self
|
|
9
|
+
def transform_hash(hash, visiting, depth, max_depth, &key_converter)
|
|
10
|
+
check_depth!(depth, max_depth)
|
|
11
|
+
|
|
12
|
+
oid = hash.object_id
|
|
13
|
+
raise CircularStructureError, "Keycase detected a circular reference in a Hash" if visiting.include?(oid)
|
|
14
|
+
|
|
15
|
+
visiting.add(oid)
|
|
16
|
+
begin
|
|
17
|
+
hash.each_with_object({}) do |(key, value), memo|
|
|
18
|
+
new_key = key_converter.call(key)
|
|
19
|
+
if memo.key?(new_key)
|
|
20
|
+
message = "Keycase detected a key collision: #{key.inspect} converted to " \
|
|
21
|
+
"#{new_key.inspect}, which already exists in the transformed hash"
|
|
22
|
+
raise KeyCollisionError, message
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
memo[new_key] = transform_value(
|
|
26
|
+
value,
|
|
27
|
+
visiting,
|
|
28
|
+
depth + 1,
|
|
29
|
+
max_depth,
|
|
30
|
+
&key_converter
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
ensure
|
|
34
|
+
visiting.delete(oid)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def transform_array(array, visiting, depth, max_depth, &key_converter)
|
|
39
|
+
check_depth!(depth, max_depth)
|
|
40
|
+
|
|
41
|
+
oid = array.object_id
|
|
42
|
+
raise CircularStructureError, "Keycase detected a circular reference in an Array" if visiting.include?(oid)
|
|
43
|
+
|
|
44
|
+
visiting.add(oid)
|
|
45
|
+
begin
|
|
46
|
+
array.map do |element|
|
|
47
|
+
transform_value(
|
|
48
|
+
element,
|
|
49
|
+
visiting,
|
|
50
|
+
depth + 1,
|
|
51
|
+
max_depth,
|
|
52
|
+
&key_converter
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
ensure
|
|
56
|
+
visiting.delete(oid)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def transform_value(value, visiting, depth, max_depth, &key_converter)
|
|
61
|
+
case value
|
|
62
|
+
when Hash
|
|
63
|
+
transform_hash(value, visiting, depth, max_depth, &key_converter)
|
|
64
|
+
when Array
|
|
65
|
+
transform_array(value, visiting, depth, max_depth, &key_converter)
|
|
66
|
+
else
|
|
67
|
+
value
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def check_depth!(depth, max_depth)
|
|
74
|
+
return if max_depth.nil?
|
|
75
|
+
|
|
76
|
+
raise StructureTooDeepError, "Keycase nesting exceeds max_depth (#{max_depth})" if depth > max_depth
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Keycase
|
|
4
|
+
# Signals that recursive key conversion cannot finish because the input graph loops.
|
|
5
|
+
class CircularStructureError < StandardError; end
|
|
6
|
+
|
|
7
|
+
# Signals that conversion would overwrite data by mapping multiple source keys to one key.
|
|
8
|
+
class KeyCollisionError < StandardError; end
|
|
9
|
+
|
|
10
|
+
# Signals that the caller's depth limit rejected input that is too deeply nested.
|
|
11
|
+
class StructureTooDeepError < StandardError; end
|
|
12
|
+
end
|
data/lib/keycase/snake_case.rb
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
require_relative "recursive_transform/engine"
|
|
6
|
+
|
|
1
7
|
module Keycase
|
|
2
8
|
module SnakeCase
|
|
3
9
|
refine Object do
|
|
@@ -5,14 +11,16 @@ module Keycase
|
|
|
5
11
|
self
|
|
6
12
|
end
|
|
7
13
|
|
|
8
|
-
def with_snake_case_keys
|
|
14
|
+
def with_snake_case_keys(_options = {})
|
|
9
15
|
self
|
|
10
16
|
end
|
|
11
17
|
end
|
|
12
18
|
|
|
13
19
|
refine String do
|
|
14
20
|
def to_snake_case
|
|
15
|
-
gsub(/(?<=[
|
|
21
|
+
gsub(/(?<=[A-Z])(?=[A-Z][a-z])/) do |_|
|
|
22
|
+
"_"
|
|
23
|
+
end.gsub(/(?<=[0-9a-z])(?=[A-Z])/) do |_|
|
|
16
24
|
"_"
|
|
17
25
|
end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched|
|
|
18
26
|
"_#{matched.downcase}"
|
|
@@ -27,17 +35,27 @@ module Keycase
|
|
|
27
35
|
end
|
|
28
36
|
|
|
29
37
|
refine Array do
|
|
30
|
-
def with_snake_case_keys
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
def with_snake_case_keys(options = {})
|
|
39
|
+
Keycase::RecursiveTransform::Engine.transform_array(
|
|
40
|
+
self,
|
|
41
|
+
::Set.new,
|
|
42
|
+
0,
|
|
43
|
+
options[:max_depth]
|
|
44
|
+
) do |key|
|
|
45
|
+
key.to_snake_case
|
|
33
46
|
end
|
|
34
47
|
end
|
|
35
48
|
end
|
|
36
49
|
|
|
37
50
|
refine Hash do
|
|
38
|
-
def with_snake_case_keys
|
|
39
|
-
|
|
40
|
-
|
|
51
|
+
def with_snake_case_keys(options = {})
|
|
52
|
+
Keycase::RecursiveTransform::Engine.transform_hash(
|
|
53
|
+
self,
|
|
54
|
+
::Set.new,
|
|
55
|
+
0,
|
|
56
|
+
options[:max_depth]
|
|
57
|
+
) do |key|
|
|
58
|
+
key.to_snake_case
|
|
41
59
|
end
|
|
42
60
|
end
|
|
43
61
|
end
|
data/lib/keycase/version.rb
CHANGED
data/lib/keycase.rb
CHANGED
metadata
CHANGED
|
@@ -1,38 +1,32 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: keycase
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- naoigcat
|
|
8
|
-
|
|
9
|
-
bindir: exe
|
|
8
|
+
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies: []
|
|
13
|
-
description: |
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
description: |2
|
|
13
|
+
This gem converts the case of strings, symbols, and keys of hash recursively.
|
|
14
|
+
The convertible cases are camelCase, PascalCase, snake_case, etc.
|
|
16
15
|
email:
|
|
17
16
|
- 17925623+naoigcat@users.noreply.github.com
|
|
18
17
|
executables: []
|
|
19
18
|
extensions: []
|
|
20
19
|
extra_rdoc_files: []
|
|
21
20
|
files:
|
|
22
|
-
-
|
|
23
|
-
- ".rspec"
|
|
24
|
-
- ".travis.yml"
|
|
25
|
-
- Gemfile
|
|
26
|
-
- LICENSE.txt
|
|
21
|
+
- LICENSE.md
|
|
27
22
|
- README.md
|
|
28
|
-
- Rakefile
|
|
29
|
-
- bin/console
|
|
30
|
-
- bin/setup
|
|
31
23
|
- keycase.gemspec
|
|
32
24
|
- lib/keycase.rb
|
|
33
25
|
- lib/keycase/camel_case.rb
|
|
34
26
|
- lib/keycase/kebab_case.rb
|
|
35
27
|
- lib/keycase/pascal_case.rb
|
|
28
|
+
- lib/keycase/recursive_transform/engine.rb
|
|
29
|
+
- lib/keycase/recursive_transform/errors.rb
|
|
36
30
|
- lib/keycase/snake_case.rb
|
|
37
31
|
- lib/keycase/version.rb
|
|
38
32
|
homepage: https://github.com/naoigcat/ruby-keycase
|
|
@@ -41,7 +35,7 @@ licenses:
|
|
|
41
35
|
metadata:
|
|
42
36
|
homepage_uri: https://github.com/naoigcat/ruby-keycase
|
|
43
37
|
source_code_uri: https://github.com/naoigcat/ruby-keycase
|
|
44
|
-
|
|
38
|
+
rubygems_mfa_required: 'true'
|
|
45
39
|
rdoc_options: []
|
|
46
40
|
require_paths:
|
|
47
41
|
- lib
|
|
@@ -49,15 +43,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
49
43
|
requirements:
|
|
50
44
|
- - ">="
|
|
51
45
|
- !ruby/object:Gem::Version
|
|
52
|
-
version: 2.
|
|
46
|
+
version: 2.3.0
|
|
53
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
48
|
requirements:
|
|
55
49
|
- - ">="
|
|
56
50
|
- !ruby/object:Gem::Version
|
|
57
51
|
version: '0'
|
|
58
52
|
requirements: []
|
|
59
|
-
rubygems_version: 3.
|
|
60
|
-
signing_key:
|
|
53
|
+
rubygems_version: 3.6.9
|
|
61
54
|
specification_version: 4
|
|
62
55
|
summary: Converts the case of strings, symbols, and keys of hash.
|
|
63
56
|
test_files: []
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/bin/console
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
require "bundler/setup"
|
|
4
|
-
require "keycase"
|
|
5
|
-
|
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
-
|
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
-
# require "pry"
|
|
11
|
-
# Pry.start
|
|
12
|
-
|
|
13
|
-
require "irb"
|
|
14
|
-
IRB.start(__FILE__)
|