keycase 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7da8baf95ed5db98cd67e6d9ad5ceb98911bd1549a3508eb00522523a520aa9f
4
+ data.tar.gz: 8adc12f0490f1e3fbbc65e8c47746c183b028f4a8cc326f3fbadec4988e3e32f
5
+ SHA512:
6
+ metadata.gz: 02dc22181e2875b26713d9a7227a4168d8ebd9d03e44cbdc7a6fd525a3f3f7b9322b7e4d4738bddad3245d2574b8ad3329fd57c0d45f3c0075fadc084dec27e5
7
+ data.tar.gz: 84b5aed31fc5628cbf16aa92799de771768fa8bcef8585d789d4e6c00967a833913204fd033dd58fd13dd03bc20752d37aaf5fc86b1db970f28397e065551126
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # bundler
14
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 naoigcat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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
+ THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Keycase
2
+
3
+ This gem converts the case of strings, symbols, and keys of hash recursively.
4
+ The convertible cases are camelCase, PascalCase, snake_case, etc.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "keycase"
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install keycase
21
+
22
+ ## Usage
23
+
24
+ ```sh
25
+ $ irb --context-mode=1
26
+ ```
27
+
28
+ ```rb
29
+ > require "bundler/setup"
30
+ > require "keycase"
31
+ > using Keycase::CamelCase
32
+ > hash = {
33
+ :symbol_key => "symbol value",
34
+ "text_key" => "text value",
35
+ :camelKey => "camel value",
36
+ :PascalKey => "pascal value",
37
+ :nested_hash => {
38
+ :nested_symbol_key => "nested symbol value",
39
+ "nested_text_key" => "nested text value",
40
+ :nestedCamelKey => "nested camel value",
41
+ :NestedPascalKey => "nested pascal value",
42
+ },
43
+ :nested_array => [
44
+ { :array_nested_hash_1 => "nested value 1" },
45
+ { :array_nested_hash_2 => "nested value 2" },
46
+ ],
47
+ }
48
+ > hash.with_camel_case_keys
49
+ => {
50
+ :symbolKey => "symbol value",
51
+ "textKey" => "text value",
52
+ :camelKey => "camel value",
53
+ :pascalKey => "pascal value",
54
+ :nestedHash => {
55
+ :nestedSymbolKey => "nested symbol value",
56
+ "nestedTextKey" => "nested text value",
57
+ :nestedCamelKey => "nested camel value",
58
+ :nestedPascalKey => "nested pascal value",
59
+ },
60
+ :nestedArray => [
61
+ { :arrayNestedHash1 => "nested value 1" },
62
+ { :arrayNestedHash2 => "nested value 2" },
63
+ ],
64
+ }
65
+ ```
66
+
67
+ ## Development
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.
70
+
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
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/naoigcat/ruby-keycase.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative "lib/keycase/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "keycase"
5
+ spec.version = Keycase::VERSION
6
+ spec.authors = ["naoigcat"]
7
+ spec.email = ["17925623+naoigcat@users.noreply.github.com"]
8
+
9
+ spec.summary = "Converts the case of strings, symbols, and keys of hash."
10
+ spec.description = <<~DESCRIPTION
11
+ This gem converts the case of strings, symbols, and keys of hash recursively.
12
+ The convertible cases are camelCase, PascalCase, snake_case, etc.
13
+ DESCRIPTION
14
+ spec.homepage = "https://github.com/naoigcat/ruby-keycase"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.metadata["homepage_uri"] = spec.homepage
28
+ spec.metadata["source_code_uri"] = spec.homepage
29
+ end
@@ -0,0 +1,8 @@
1
+ require "keycase/version"
2
+ require "keycase/camel_case"
3
+ require "keycase/kebab_case"
4
+ require "keycase/pascal_case"
5
+ require "keycase/snake_case"
6
+
7
+ module Keycase
8
+ end
@@ -0,0 +1,49 @@
1
+ module Keycase
2
+ module CamelCase
3
+ refine Object do
4
+ def to_camel_case
5
+ self
6
+ end
7
+
8
+ def with_camel_case_keys
9
+ self
10
+ end
11
+ end
12
+
13
+ refine String do
14
+ def to_camel_case
15
+ gsub(/(?<=[0-9a-z])(?=[A-Z])/) do |_|
16
+ "_"
17
+ end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched| # rubocop:disable Style/SymbolProc
18
+ matched.capitalize
19
+ end.sub(/^(?:\W|_)*([A-Z]+(?=[A-Z][0-9A-Za-z]|\d|$)|[A-Z][a-z])/) do |_|
20
+ Regexp.last_match(1).downcase
21
+ end.gsub(/(?:\b|\W|_)*([0-9A-Z])/) do |_|
22
+ Regexp.last_match(1)
23
+ end.gsub(/(?:\W|_)*$/, "")
24
+ end
25
+ end
26
+
27
+ refine Symbol do
28
+ def to_camel_case
29
+ to_s.to_camel_case.to_sym
30
+ end
31
+ end
32
+
33
+ refine Array do
34
+ def with_camel_case_keys
35
+ map do |value| # rubocop:disable Style/SymbolProc
36
+ value.with_camel_case_keys
37
+ end
38
+ end
39
+ end
40
+
41
+ refine Hash do
42
+ def with_camel_case_keys
43
+ each_with_object({}) do |(key, value), memo|
44
+ memo[key.to_camel_case] = value.with_camel_case_keys
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,45 @@
1
+ module Keycase
2
+ module KebabCase
3
+ refine Object do
4
+ def to_kebab_case
5
+ self
6
+ end
7
+
8
+ def with_kebab_case_keys
9
+ self
10
+ end
11
+ end
12
+
13
+ refine String do
14
+ def to_kebab_case
15
+ gsub(/(?<=[0-9a-z])(?=[A-Z])/) do |_|
16
+ "-"
17
+ end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched|
18
+ "-#{matched.downcase}"
19
+ end.gsub(/(?:\W|_)+/, "-").gsub(/^(?:\W|_)*|(?:\W|_)*$/, "").downcase
20
+ end
21
+ end
22
+
23
+ refine Symbol do
24
+ def to_kebab_case
25
+ to_s.to_kebab_case.to_sym
26
+ end
27
+ end
28
+
29
+ refine Array do
30
+ def with_kebab_case_keys
31
+ map do |value| # rubocop:disable Style/SymbolProc
32
+ value.with_kebab_case_keys
33
+ end
34
+ end
35
+ end
36
+
37
+ refine Hash do
38
+ def with_kebab_case_keys
39
+ each_with_object({}) do |(key, value), memo|
40
+ memo[key.to_kebab_case] = value.with_kebab_case_keys
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,49 @@
1
+ module Keycase
2
+ module PascalCase
3
+ refine Object do
4
+ def to_pascal_case
5
+ self
6
+ end
7
+
8
+ def with_pascal_case_keys
9
+ self
10
+ end
11
+ end
12
+
13
+ refine String do
14
+ def to_pascal_case
15
+ gsub(/(?<=[0-9a-z])(?=[A-Z])/) do |_|
16
+ "_"
17
+ end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched| # rubocop:disable Style/SymbolProc
18
+ matched.capitalize
19
+ end.sub(/^(?:\W|_)*([A-Z]+(?=[A-Z][0-9A-Za-z]|\d|$)|[A-Z][a-z])/) do |_|
20
+ Regexp.last_match(1).capitalize
21
+ end.gsub(/(?:\b|\W|_)*([0-9A-Z])/) do |_|
22
+ Regexp.last_match(1)
23
+ end.gsub(/(?:\W|_)*$/, "")
24
+ end
25
+ end
26
+
27
+ refine Symbol do
28
+ def to_pascal_case
29
+ to_s.to_pascal_case.to_sym
30
+ end
31
+ end
32
+
33
+ refine Array do
34
+ def with_pascal_case_keys
35
+ map do |value| # rubocop:disable Style/SymbolProc
36
+ value.with_pascal_case_keys
37
+ end
38
+ end
39
+ end
40
+
41
+ refine Hash do
42
+ def with_pascal_case_keys
43
+ each_with_object({}) do |(key, value), memo|
44
+ memo[key.to_pascal_case] = value.with_pascal_case_keys
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,45 @@
1
+ module Keycase
2
+ module SnakeCase
3
+ refine Object do
4
+ def to_snake_case
5
+ self
6
+ end
7
+
8
+ def with_snake_case_keys
9
+ self
10
+ end
11
+ end
12
+
13
+ refine String do
14
+ def to_snake_case
15
+ gsub(/(?<=[0-9a-z])(?=[A-Z])/) do |_|
16
+ "_"
17
+ end.gsub(/(?<=\b|\W|_)[0-9A-Za-z]+(?=\b|\W|_)/) do |matched|
18
+ "_#{matched.downcase}"
19
+ end.gsub(/(?:\W|_)+/, "_").gsub(/^(?:\W|_)*|(?:\W|_)*$/, "").downcase
20
+ end
21
+ end
22
+
23
+ refine Symbol do
24
+ def to_snake_case
25
+ to_s.to_snake_case.to_sym
26
+ end
27
+ end
28
+
29
+ refine Array do
30
+ def with_snake_case_keys
31
+ map do |value| # rubocop:disable Style/SymbolProc
32
+ value.with_snake_case_keys
33
+ end
34
+ end
35
+ end
36
+
37
+ refine Hash do
38
+ def with_snake_case_keys
39
+ each_with_object({}) do |(key, value), memo|
40
+ memo[key.to_snake_case] = value.with_snake_case_keys
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Keycase
2
+ VERSION = "1.0.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keycase
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - naoigcat
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ This gem converts the case of strings, symbols, and keys of hash recursively.
15
+ The convertible cases are camelCase, PascalCase, snake_case, etc.
16
+ email:
17
+ - 17925623+naoigcat@users.noreply.github.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - ".travis.yml"
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - keycase.gemspec
32
+ - lib/keycase.rb
33
+ - lib/keycase/camel_case.rb
34
+ - lib/keycase/kebab_case.rb
35
+ - lib/keycase/pascal_case.rb
36
+ - lib/keycase/snake_case.rb
37
+ - lib/keycase/version.rb
38
+ homepage: https://github.com/naoigcat/ruby-keycase
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://github.com/naoigcat/ruby-keycase
43
+ source_code_uri: https://github.com/naoigcat/ruby-keycase
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.0.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.1.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Converts the case of strings, symbols, and keys of hash.
63
+ test_files: []