strings-case 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.
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strings
4
+ module Case
5
+ module Extensions
6
+ refine String do
7
+ def camelcase(*args)
8
+ Strings::Case.camelcase(self, *args)
9
+ end
10
+ alias lower_camelcase camelcase
11
+
12
+ def constcase(*args)
13
+ Strings::Case.constcase(self, *args)
14
+ end
15
+ alias constantcase constcase
16
+
17
+ def headercase(*args)
18
+ Strings::Case.headercase(self, *args)
19
+ end
20
+
21
+ def kebabcase(*args)
22
+ Strings::Case.kebabcase(self, *args)
23
+ end
24
+ alias dashcase kebabcase
25
+
26
+ def pascalcase(*args)
27
+ Strings::Case.pascalcase(self, *args)
28
+ end
29
+ alias upper_camelcase pascalcase
30
+
31
+ def pathcase(*args)
32
+ Strings::Case.pathcase(self, *args)
33
+ end
34
+
35
+ def sentencecase(*args)
36
+ Strings::Case.sentencecase(self, *args)
37
+ end
38
+
39
+ def snakecase(*args)
40
+ Strings::Case.snakecase(self, *args)
41
+ end
42
+ alias underscore snakecase
43
+
44
+ def titlecase(*args)
45
+ Strings::Case.titlecase(self, *args)
46
+ end
47
+ end
48
+ end # Extensions
49
+ end # Case
50
+ end # Strings
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strings
4
+ module Case
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ENV["COVERAGE"] || ENV["TRAVIS"]
4
+ require "simplecov"
5
+ require "coveralls"
6
+
7
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
8
+ SimpleCov::Formatter::HTMLFormatter,
9
+ Coveralls::SimpleCov::Formatter
10
+ ])
11
+
12
+ SimpleCov.start do
13
+ command_name "spec"
14
+ add_filter "spec"
15
+ end
16
+ end
17
+
18
+ require "bundler/setup"
19
+ require "strings/case"
20
+
21
+ module Helpers
22
+ def modern_ruby?
23
+ Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.4")
24
+ end
25
+ end
26
+
27
+ RSpec.configure do |config|
28
+ config.extend Helpers
29
+
30
+ # Enable flags like --only-failures and --next-failure
31
+ config.example_status_persistence_file_path = ".rspec_status"
32
+
33
+ # Disable RSpec exposing methods globally on `Module` and `main`
34
+ config.disable_monkey_patching!
35
+
36
+ config.expect_with :rspec do |c|
37
+ c.syntax = :expect
38
+ end
39
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::Case, "#camelcase" do
4
+ {
5
+ nil => nil,
6
+ "" => "",
7
+ "f" => "f",
8
+ "1234" => "1234",
9
+ "FOO" => "foo",
10
+ "FooBar" => "fooBar",
11
+ "fooBar" => "fooBar",
12
+ "foo bar" => "fooBar",
13
+ "Foo - Bar" => "fooBar",
14
+ "foo & bar" => "fooBar",
15
+ "FooFooBar" => "fooFooBar",
16
+ "Foo2Foo2Bar" => "foo2Foo2Bar",
17
+ "foo-bar-baz" => "fooBarBaz",
18
+ "foo_bar_1_2" => "fooBar12",
19
+ "_foo_bar_baz_" => "fooBarBaz",
20
+ "--foo-bar--" => "fooBar",
21
+ "FOO_BAR_baz" => "fooBarBaz",
22
+ "__FOO_BAR__" => "fooBar",
23
+ "Foo w1th apo's and punc]t" => "fooW1thAposAndPunct",
24
+ "getHTTPResponse" => "getHttpResponse",
25
+ "currencyISOCode" => "currencyIsoCode",
26
+ "get2HTTPResponse" => "get2HttpResponse",
27
+ "HTTPResponseCode" => "httpResponseCode",
28
+ "HTTPResponseCodeXY" => "httpResponseCodeXy",
29
+ "supports IPv6 on iOS?" => "supportsIpv6OnIos"
30
+ }.each do |actual, expected|
31
+ it "applies camelcase to #{actual.inspect} -> #{expected.inspect}" do
32
+ expect(Strings::Case.camelcase(actual)).to eq(expected)
33
+ end
34
+ end
35
+
36
+ it "supports unicode", if: modern_ruby? do
37
+ expect(Strings::Case.camelcase("ЗдравствуйтеПривет")).to eq("здравствуйтеПривет")
38
+ end
39
+
40
+ it "allows to preserve acronyms" do
41
+ camelized = Strings::Case.lower_camelcase("HTTP response code", acronyms: ["HTTP"])
42
+
43
+ expect(camelized).to eq("HTTPResponseCode")
44
+ end
45
+
46
+ it "changes a separator to :" do
47
+ camelized = Strings::Case.camelcase("HTTP response code", separator: ":")
48
+
49
+ expect(camelized).to eq("http:Response:Code")
50
+ end
51
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::Case, "#constcase" do
4
+ {
5
+ nil => nil,
6
+ "" => "",
7
+ "1234" => "1234",
8
+ "FOO" => "FOO",
9
+ "FooBar" => "FOO_BAR",
10
+ "fooBar" => "FOO_BAR",
11
+ "foo bar" => "FOO_BAR",
12
+ "Foo - Bar" => "FOO_BAR",
13
+ "foo & bar" => "FOO_BAR",
14
+ "FooFooBar" => "FOO_FOO_BAR",
15
+ "Foo2Foo2Bar" => "FOO2_FOO2_BAR",
16
+ "foo-bar-baz" => "FOO_BAR_BAZ",
17
+ "foo_bar_1_2" => "FOO_BAR_1_2",
18
+ "_foo_bar_baz_" => "_FOO_BAR_BAZ_",
19
+ "--foo-bar--" => "FOO_BAR",
20
+ "FOO_BAR_baz" => "FOO_BAR_BAZ",
21
+ "__FOO_BAR__" => "_FOO_BAR_",
22
+ "Foo w1th apo's and punc]t" => "FOO_W1TH_APOS_AND_PUNCT",
23
+ "getHTTPResponse" => "GET_HTTP_RESPONSE",
24
+ "currencyISOCode" => "CURRENCY_ISO_CODE",
25
+ "get2HTTPResponse" => "GET2_HTTP_RESPONSE",
26
+ "HTTPResponseCode" => "HTTP_RESPONSE_CODE",
27
+ "HTTPResponseCodeXY" => "HTTP_RESPONSE_CODE_XY",
28
+ "supports IPv6 on iOS?" => "SUPPORTS_IPV6_ON_IOS",
29
+ }.each do |actual, expected|
30
+ it "applies constcase to #{actual.inspect} -> #{expected.inspect}" do
31
+ expect(Strings::Case.constcase(actual)).to eq(expected)
32
+ end
33
+ end
34
+
35
+ it "supports unicode", if: modern_ruby? do
36
+ expect(Strings::Case.constantcase("ЗдравствуйтеПривет")).to eq("ЗДРАВСТВУЙТЕ_ПРИВЕТ")
37
+ end
38
+
39
+ it "changes a separator to :" do
40
+ constant = Strings::Case.constcase("HTTP response code", separator: ":")
41
+
42
+ expect(constant).to eq("HTTP:RESPONSE:CODE")
43
+ end
44
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strings/case/extensions"
4
+
5
+ using Strings::Case::Extensions
6
+
7
+ RSpec.describe Strings::Case::Extensions do
8
+ it "camelcase a string" do
9
+ expect("foo bar baz".camelcase).to eq("fooBarBaz")
10
+ end
11
+
12
+ it "camelcase a string with acronyms" do
13
+ expect("HTTP Response Code".lower_camelcase(acronyms: ["HTTP"])).to eq("HTTPResponseCode")
14
+ end
15
+
16
+ it "constcase a string" do
17
+ expect("foo bar baz".constcase).to eq("FOO_BAR_BAZ")
18
+ end
19
+
20
+ it "headercases a string" do
21
+ expect("foo bar baz".headercase).to eq("Foo-Bar-Baz")
22
+ end
23
+
24
+ it "headercases a string with acronyms" do
25
+ expect("HTTP Response Code".headercase(acronyms: ["HTTP"])).to eq("HTTP-Response-Code")
26
+ end
27
+
28
+ it "kebabcases a string" do
29
+ expect("foo bar baz".kebabcase).to eq("foo-bar-baz")
30
+ end
31
+
32
+ it "kebabcases a string with acronyms" do
33
+ expect("HTTP Response Code".dashcase(acronyms: ["HTTP"])).to eq("HTTP-response-code")
34
+ end
35
+
36
+ it "pascalcases a string" do
37
+ expect("foo bar baz".pascalcase).to eq("FooBarBaz")
38
+ end
39
+
40
+ it "pascalcases a string with acronyms" do
41
+ expect("HTTP Response Code".pascalcase(acronyms: ["HTTP"])).to eq("HTTPResponseCode")
42
+ end
43
+
44
+ it "pathcases a string" do
45
+ expect("foo bar baz".pathcase).to eq("foo/bar/baz")
46
+ end
47
+
48
+ it "pathcases a string with acronyms" do
49
+ expect("HTTP Response Code".pathcase(acronyms: ["HTTP"])).to eq("HTTP/response/code")
50
+ end
51
+
52
+ it "sentencecases a string" do
53
+ expect("foo bar baz".sentencecase).to eq("Foo bar baz")
54
+ end
55
+
56
+ it "sentencecases a string with acronyms" do
57
+ expect("HTTP Response Code".sentencecase(acronyms: ["HTTP"])).to eq("HTTP response code")
58
+ end
59
+
60
+ it "snakecases a string" do
61
+ expect("foo bar baz".snakecase).to eq("foo_bar_baz")
62
+ end
63
+
64
+ it "snakecases a string with acronyms" do
65
+ expect("HTTP Response Code".underscore(acronyms: ["HTTP"])).to eq("HTTP_response_code")
66
+ end
67
+
68
+ it "titlecases a string" do
69
+ expect("foo bar baz".titlecase).to eq("Foo Bar Baz")
70
+ end
71
+
72
+ it "titlecase a string with acronyms" do
73
+ expect("HTTP Response Code".titlecase(acronyms: ["HTTP"])).to eq("HTTP Response Code")
74
+ end
75
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::Case, "#headercase" do
4
+ {
5
+ nil => nil,
6
+ "" => "",
7
+ "1234" => "1234",
8
+ "FOO" => "Foo",
9
+ "FooBar" => "Foo-Bar",
10
+ "fooBar" => "Foo-Bar",
11
+ "foo bar" => "Foo-Bar",
12
+ "Foo - Bar" => "Foo-Bar",
13
+ "foo & bar" => "Foo-Bar",
14
+ "FooFooBar" => "Foo-Foo-Bar",
15
+ "Foo2Foo2Bar" => "Foo2-Foo2-Bar",
16
+ "foo-bar-baz" => "Foo-Bar-Baz",
17
+ "foo_bar_1_2" => "Foo-Bar-1-2",
18
+ "_foo_bar_baz_" => "Foo-Bar-Baz",
19
+ "--foo-bar--" => "-Foo-Bar-",
20
+ "FOO_BAR_baz" => "Foo-Bar-Baz",
21
+ "__FOO_BAR__" => "Foo-Bar",
22
+ "Foo w1th apo's and punc]t" => "Foo-W1th-Apos-And-Punct",
23
+ "getHTTPResponse" => "Get-Http-Response",
24
+ "currencyISOCode" => "Currency-Iso-Code",
25
+ "get2HTTPResponse" => "Get2-Http-Response",
26
+ "HTTPResponseCode" => "Http-Response-Code",
27
+ "HTTPResponseCodeXY" => "Http-Response-Code-Xy",
28
+ "supports IPv6 on iOS?" => "Supports-Ipv6-On-Ios",
29
+ }.each do |actual, expected|
30
+ it "applies headercase to #{actual.inspect} -> #{expected.inspect}" do
31
+ expect(Strings::Case.headercase(actual)).to eq(expected)
32
+ end
33
+ end
34
+
35
+ it "supports unicode", if: modern_ruby? do
36
+ expect(Strings::Case.headercase("ЗдравствуйтеПривет")).to eq("Здравствуйте-Привет")
37
+ end
38
+
39
+ it "allows to preserve acronyms" do
40
+ headered = Strings::Case.headercase("HTTP response code", acronyms: ["HTTP"])
41
+
42
+ expect(headered).to eq("HTTP-Response-Code")
43
+ end
44
+
45
+ it "changes a separator to :" do
46
+ headered = Strings::Case.headercase("HTTP response code", separator: ":")
47
+
48
+ expect(headered).to eq("Http:Response:Code")
49
+ end
50
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::Case, "#kebabcase" do
4
+ {
5
+ "" => "",
6
+ "1234" => "1234",
7
+ "FOO" => "foo",
8
+ "FooBar" => "foo-bar",
9
+ "fooBar" => "foo-bar",
10
+ "foo bar" => "foo-bar",
11
+ "Foo - Bar" => "foo-bar",
12
+ "foo & bar" => "foo-bar",
13
+ "FooFooBar" => "foo-foo-bar",
14
+ "Foo2Foo2Bar" => "foo2-foo2-bar",
15
+ "foo-bar-baz" => "foo-bar-baz",
16
+ "_foo_bar_baz_" => "foo-bar-baz",
17
+ "--foo-bar--" => "-foo-bar-",
18
+ "FOO_BAR_baz" => "foo-bar-baz",
19
+ "__FOO_BAR__" => "foo-bar",
20
+ "Foo w1th apo's and punc]t" => "foo-w1th-apos-and-punct",
21
+ "getHTTPResponse" => "get-http-response",
22
+ "currencyISOCode" => "currency-iso-code",
23
+ "get2HTTPResponse" => "get2-http-response",
24
+ "HTTPResponseCode" => "http-response-code",
25
+ "HTTPResponseCodeXY" => "http-response-code-xy",
26
+ "supports IPv6 on iOS?" => "supports-ipv6-on-ios"
27
+ }.each do |actual, expected|
28
+ it "applies kebabcase from #{actual.inspect} to #{expected.inspect}" do
29
+ expect(Strings::Case.kebabcase(actual)).to eq(expected)
30
+ end
31
+ end
32
+
33
+ it "supports unicode", if: modern_ruby? do
34
+ expect(Strings::Case.kebabcase("ЗдравствуйтеПривет")).to eq("здравствуйте-привет")
35
+ end
36
+
37
+ it "allows to preserve acronyms" do
38
+ dashed = Strings::Case.dashcase("HTTP response code", acronyms: ["HTTP"])
39
+
40
+ expect(dashed).to eq("HTTP-response-code")
41
+ end
42
+
43
+ it "changes a separator to :" do
44
+ dashed = Strings::Case.kebabcase("HTTP response code", separator: ":")
45
+
46
+ expect(dashed).to eq("http:response:code")
47
+ end
48
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::Case, "#pascalcase" do
4
+ {
5
+ nil => nil,
6
+ "" => "",
7
+ "f" => "F",
8
+ "1234" => "1234",
9
+ "FOO" => "Foo",
10
+ "FooBar" => "FooBar",
11
+ "fooBar" => "FooBar",
12
+ "foo bar" => "FooBar",
13
+ "Foo - Bar" => "FooBar",
14
+ "foo & bar" => "FooBar",
15
+ "FooFooBar" => "FooFooBar",
16
+ "Foo2Foo2Bar" => "Foo2Foo2Bar",
17
+ "foo-bar-baz" => "FooBarBaz",
18
+ "foo_bar_1_2" => "FooBar12",
19
+ "_foo_bar_baz_" => "FooBarBaz",
20
+ "--foo-bar--" => "FooBar",
21
+ "FOO_BAR_baz" => "FooBarBaz",
22
+ "__FOO_BAR__" => "FooBar",
23
+ "Foo w1th apo's and punc]t" => "FooW1thAposAndPunct",
24
+ "getHTTPResponse" => "GetHttpResponse",
25
+ "currencyISOCode" => "CurrencyIsoCode",
26
+ "get2HTTPResponse" => "Get2HttpResponse",
27
+ "HTTPResponseCode" => "HttpResponseCode",
28
+ "HTTPResponseCodeXY" => "HttpResponseCodeXy",
29
+ "supports IPv6 on iOS?" => "SupportsIpv6OnIos"
30
+ }.each do |actual, expected|
31
+ it "applies pascalcase to #{actual.inspect} -> #{expected.inspect}" do
32
+ expect(Strings::Case.pascalcase(actual)).to eq(expected)
33
+ end
34
+ end
35
+
36
+ it "supports unicode", if: modern_ruby? do
37
+ expect(Strings::Case.pascalcase("ЗдравствуйтеПривет")).to eq("ЗдравствуйтеПривет")
38
+ end
39
+
40
+ it "allows to preserve acronyms" do
41
+ camelized = Strings::Case.upper_camelcase("HTTP response code", acronyms: ["HTTP"])
42
+
43
+ expect(camelized).to eq("HTTPResponseCode")
44
+ end
45
+
46
+ it "changes a separator to :" do
47
+ camelized = Strings::Case.pascalcase("HTTP response code", separator: ":")
48
+
49
+ expect(camelized).to eq("Http:Response:Code")
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::Case, "#pathcase" do
4
+ {
5
+ nil => nil,
6
+ "" => "",
7
+ "f" => "f",
8
+ "1234" => "1234",
9
+ "FOO" => "foo",
10
+ "FooBar" => "foo/bar",
11
+ "fooBar" => "foo/bar",
12
+ "foo bar" => "foo/bar",
13
+ "Foo - Bar" => "foo/bar",
14
+ "foo & bar" => "foo/bar",
15
+ "FooFooBar" => "foo/foo/bar",
16
+ "Foo2Foo2Bar" => "foo2/foo2/bar",
17
+ "foo-bar-baz" => "foo/bar/baz",
18
+ "foo_bar_1_2" => "foo/bar/1/2",
19
+ "_foo_bar_baz_" => "foo/bar/baz",
20
+ "--foo-bar--" => "foo/bar",
21
+ "FOO_BAR_baz" => "foo/bar/baz",
22
+ "__FOO_BAR__" => "foo/bar",
23
+ "Foo w1th apo's and punc]t" => "foo/w1th/apos/and/punct",
24
+ "getHTTPResponse" => "get/http/response",
25
+ "currencyISOCode" => "currency/iso/code",
26
+ "get2HTTPResponse" => "get2/http/response",
27
+ "HTTPResponseCode" => "http/response/code",
28
+ "HTTPResponseCodeXY" => "http/response/code/xy",
29
+ "supports IPv6 on iOS?" => "supports/ipv6/on/ios"
30
+ }.each do |actual, expected|
31
+ it "applies pathcase to #{actual.inspect} -> #{expected.inspect}" do
32
+ expect(Strings::Case.pathcase(actual)).to eq(expected)
33
+ end
34
+ end
35
+
36
+ it "supports unicode", if: modern_ruby? do
37
+ expect(Strings::Case.pathcase("ЗдравствуйтеПривет")).to eq("здравствуйте/привет")
38
+ end
39
+
40
+ it "allows to preserve acronyms" do
41
+ pathed = Strings::Case.pathcase("HTTP response code", acronyms: ["HTTP"])
42
+
43
+ expect(pathed).to eq("HTTP/response/code")
44
+ end
45
+
46
+ it "changes file path separator" do
47
+ pathed = Strings::Case.pathcase("HTTP response code", separator: "\\")
48
+
49
+ expect(pathed).to eq("http\\response\\code")
50
+ end
51
+ end