string_foundation 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +75 -0
- data/Gemfile +38 -0
- data/Gemfile.lock +58 -0
- data/LICENSE.txt +21 -0
- data/README.md +324 -0
- data/Rakefile +13 -0
- data/bin/console +15 -0
- data/bin/setup +11 -0
- data/circle.yml +14 -0
- data/coverage/assets/0.10.0/application.css +799 -0
- data/coverage/assets/0.10.0/application.js +1707 -0
- data/coverage/assets/0.10.0/colorbox/border.png +0 -0
- data/coverage/assets/0.10.0/colorbox/controls.png +0 -0
- data/coverage/assets/0.10.0/colorbox/loading.gif +0 -0
- data/coverage/assets/0.10.0/colorbox/loading_background.png +0 -0
- data/coverage/assets/0.10.0/favicon_green.png +0 -0
- data/coverage/assets/0.10.0/favicon_red.png +0 -0
- data/coverage/assets/0.10.0/favicon_yellow.png +0 -0
- data/coverage/assets/0.10.0/loading.gif +0 -0
- data/coverage/assets/0.10.0/magnify.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/index.html +12508 -0
- data/lib/string_foundation/case.rb +102 -0
- data/lib/string_foundation/convert.rb +51 -0
- data/lib/string_foundation/convertible.rb +40 -0
- data/lib/string_foundation/like.rb +24 -0
- data/lib/string_foundation/version.rb +7 -0
- data/lib/string_foundation/with.rb +26 -0
- data/lib/string_foundation.rb +6 -0
- data/pkg/string_foundation-1.0.0.gem +0 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/string_foundation/case_spec.rb +901 -0
- data/spec/string_foundation/convert_spec.rb +234 -0
- data/spec/string_foundation/convertaile_spec.rb +286 -0
- data/spec/string_foundation/like_spec.rb +218 -0
- data/spec/string_foundation/version_spec.rb +14 -0
- data/spec/string_foundation/with_spec.rb +99 -0
- data/spec/support/enhanced_matchers_extension.rb +11 -0
- data/string_foundation.gemspec +22 -0
- data/string_foundation.png +0 -0
- metadata +106 -0
@@ -0,0 +1,102 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# LIB - STRING FOUNDATION - CASE
|
3
|
+
# ==============================================================================
|
4
|
+
# frozen_string_literal: true
|
5
|
+
class String
|
6
|
+
|
7
|
+
# Convert to lowerCamelCase.
|
8
|
+
def to_lcamel
|
9
|
+
ucamel = self.to_ucamel
|
10
|
+
ucamel.make_head_lower
|
11
|
+
end
|
12
|
+
|
13
|
+
# Convert to UpperCamelCase.
|
14
|
+
def to_ucamel
|
15
|
+
self.split_camel.map do |cw|
|
16
|
+
cw.split(/\.|_|-|\s/).map do |w|
|
17
|
+
w.capitalize
|
18
|
+
end.join
|
19
|
+
end.join
|
20
|
+
end
|
21
|
+
|
22
|
+
# Convert to lower_snake_case.
|
23
|
+
def to_lsnake
|
24
|
+
usnake = self.to_usnake
|
25
|
+
usnake.downcase
|
26
|
+
end
|
27
|
+
|
28
|
+
# Convert to Upper_Snake_Case.
|
29
|
+
def to_usnake
|
30
|
+
self.split_camel.map do |cw|
|
31
|
+
cw.split(/\.|_|-|\s/).map do |w|
|
32
|
+
w.capitalize
|
33
|
+
end.join('_')
|
34
|
+
end.join('_')
|
35
|
+
end
|
36
|
+
|
37
|
+
# Convert to lower-kebab-case.
|
38
|
+
def to_lkebab
|
39
|
+
ukebab = self.to_ukebab
|
40
|
+
ukebab.downcase
|
41
|
+
end
|
42
|
+
|
43
|
+
# Convert to Upper-Kebab-Case.
|
44
|
+
def to_ukebab
|
45
|
+
self.split_camel.map do |cw|
|
46
|
+
cw.split(/\.|_|-|\s/).map do |w|
|
47
|
+
w.capitalize
|
48
|
+
end.join('-')
|
49
|
+
end.join('-')
|
50
|
+
end
|
51
|
+
|
52
|
+
# Convert to lower space case.
|
53
|
+
def to_lspace
|
54
|
+
uspace = self.to_uspace
|
55
|
+
uspace.downcase
|
56
|
+
end
|
57
|
+
|
58
|
+
# Convert to Upper Space Case.
|
59
|
+
def to_uspace
|
60
|
+
self.split_camel.map do |cw|
|
61
|
+
cw.split(/\.|_|-|\s/).map do |w|
|
62
|
+
w.capitalize
|
63
|
+
end.join(' ')
|
64
|
+
end.join(' ')
|
65
|
+
end
|
66
|
+
|
67
|
+
# Convert to lower.dot.case.
|
68
|
+
def to_ldot
|
69
|
+
udot = self.to_udot
|
70
|
+
udot.downcase
|
71
|
+
end
|
72
|
+
|
73
|
+
# Convert to Upper.Dot.Case.
|
74
|
+
def to_udot
|
75
|
+
self.split_camel.map do |cw|
|
76
|
+
cw.split(/\.|_|-|\s/).map do |w|
|
77
|
+
w.capitalize
|
78
|
+
end.join('.')
|
79
|
+
end.join('.')
|
80
|
+
end
|
81
|
+
|
82
|
+
# Split string according to camel case.
|
83
|
+
def split_camel
|
84
|
+
self.split /(?=[A-Z])/
|
85
|
+
end
|
86
|
+
|
87
|
+
# Whether a character is uppder case or not.
|
88
|
+
def is_upper?
|
89
|
+
/[[:upper:]]/.match(self) ? true : false
|
90
|
+
end
|
91
|
+
|
92
|
+
# Whether a character is lower case or not.
|
93
|
+
def is_lower?
|
94
|
+
/[[:lower:]]/.match(self) ? true : false
|
95
|
+
end
|
96
|
+
|
97
|
+
# Make first character lower case.
|
98
|
+
def make_head_lower
|
99
|
+
self[0].downcase + self[1..-1]
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# LIB - STRING FOUNDATION - CONVERT
|
3
|
+
# ==============================================================================
|
4
|
+
# frozen_string_literal: true
|
5
|
+
require_relative 'convertible'
|
6
|
+
require_relative 'like'
|
7
|
+
require_relative 'with'
|
8
|
+
class String
|
9
|
+
|
10
|
+
# Convert to TrueClass or FalseClass.
|
11
|
+
def to_bool
|
12
|
+
unless self.to_bool?
|
13
|
+
raise TypeError.new("#{self} cannot be converted to TrueClass or FalseClass")
|
14
|
+
end
|
15
|
+
|
16
|
+
(self == 'true')
|
17
|
+
end
|
18
|
+
|
19
|
+
# Convert a booly string to TrueClass or FalseClass.
|
20
|
+
def to_booly
|
21
|
+
unless self.to_booly?
|
22
|
+
raise TypeError.new("#{self} cannot be converted to TrueClass or FalseClass")
|
23
|
+
end
|
24
|
+
|
25
|
+
return true if self == 'true' || (self.to_f? && self.to_f > 0)
|
26
|
+
false
|
27
|
+
end
|
28
|
+
|
29
|
+
# Convert to a pretty value.
|
30
|
+
def to_pretty
|
31
|
+
return self.without_leading_zeros.to_i if self.like_i?
|
32
|
+
return self.without_leading_zeros.to_f if self.like_f?
|
33
|
+
return self.to_bool if self.to_bool?
|
34
|
+
|
35
|
+
(self.length > 0) ? self : nil
|
36
|
+
end
|
37
|
+
|
38
|
+
# Convert from newline character to specific characters.
|
39
|
+
def nl_to(char)
|
40
|
+
char = '' if char.nil?
|
41
|
+
self.gsub(/(\r\n|\n)/, char)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Convert from newline character to a HTML tag "<br>".
|
45
|
+
def nl_to_br
|
46
|
+
self.nl_to('<br>')
|
47
|
+
end
|
48
|
+
|
49
|
+
alias_method :nl2, :nl_to
|
50
|
+
alias_method :nl2br, :nl_to_br
|
51
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# LIB - STRING FOUNDATION - CONVERTIBLE
|
3
|
+
# ==============================================================================
|
4
|
+
# frozen_string_literal: true
|
5
|
+
require_relative 'with'
|
6
|
+
class String
|
7
|
+
|
8
|
+
# Whether or not to be possible to covert String to Integer.
|
9
|
+
def to_i?
|
10
|
+
Integer(Float(self.without_leading_zeros))
|
11
|
+
true
|
12
|
+
rescue ArgumentError
|
13
|
+
false
|
14
|
+
end
|
15
|
+
|
16
|
+
# Whether or not to be possible to covert String to Float.
|
17
|
+
def to_f?
|
18
|
+
Float(self.without_leading_zeros)
|
19
|
+
true
|
20
|
+
rescue ArgumentError
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
# Whether or not to be possible to covert String to Boolean.
|
25
|
+
def to_bool?
|
26
|
+
return true if %w(true false).include?(self)
|
27
|
+
false
|
28
|
+
end
|
29
|
+
|
30
|
+
# Whether or not to be possible to covert String to something which behaves
|
31
|
+
# like boolean types.
|
32
|
+
def to_booly?
|
33
|
+
return true if self.length == 0
|
34
|
+
return true if %w(true false).include?(self)
|
35
|
+
return true if self.to_f?
|
36
|
+
|
37
|
+
false
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# LIB - STRING FOUNDATION - LIKE
|
3
|
+
# ==============================================================================
|
4
|
+
# frozen_string_literal: true
|
5
|
+
require_relative 'convertible'
|
6
|
+
class String
|
7
|
+
|
8
|
+
# Check whether a string is an integral number.
|
9
|
+
def like_i?
|
10
|
+
return false unless self.to_i?
|
11
|
+
|
12
|
+
num = self.without_leading_zeros
|
13
|
+
(num.to_i == num.to_i) && !num.include?('.')
|
14
|
+
end
|
15
|
+
|
16
|
+
# Check whether a string is a floating point number.
|
17
|
+
def like_f?
|
18
|
+
return false unless self.to_f?
|
19
|
+
|
20
|
+
num = self.without_leading_zeros
|
21
|
+
(num.to_i != num.to_f) || num.include?('.')
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# LIB - STRING FOUNDATION - VERSION
|
3
|
+
# ==============================================================================
|
4
|
+
# frozen_string_literal: true
|
5
|
+
module StringFoundation
|
6
|
+
VERSION = '1.0.0'
|
7
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# LIB - STRING FOUNDATION - WITH
|
3
|
+
# ==============================================================================
|
4
|
+
# frozen_string_literal: true
|
5
|
+
class String
|
6
|
+
|
7
|
+
# Remove leading zeros.
|
8
|
+
def without_leading_zeros
|
9
|
+
return self if self == '0'
|
10
|
+
|
11
|
+
is_positive = self.start_with?('0')
|
12
|
+
is_negative = self.start_with?('-0')
|
13
|
+
if is_positive || is_negative
|
14
|
+
sig = self[0, self.length].gsub(/(^0+)|(^-0+)/, '')
|
15
|
+
|
16
|
+
sig = '0' + sig if sig.start_with?('.') || sig.length == 0
|
17
|
+
sig = '-' + sig if is_negative && sig != '0'
|
18
|
+
|
19
|
+
sig
|
20
|
+
else
|
21
|
+
self
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
alias_method :without_zero_pad, :without_leading_zeros
|
26
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# LIB - STRING FOUNDATION - STRING FOUNDATION
|
3
|
+
# ==============================================================================
|
4
|
+
# frozen_string_literal: true
|
5
|
+
# Require methods.
|
6
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'string_foundation','*.rb')).each { |f| require f }
|
Binary file
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# SPEC - SPEC HELPER
|
3
|
+
# ==============================================================================
|
4
|
+
# frozen_string_literal: true
|
5
|
+
# Configure Simple Cov to get coverage and Codecov.
|
6
|
+
# !! These should be written at the beginning of this file to work. !!
|
7
|
+
require 'simplecov'
|
8
|
+
require 'simplecov-console'
|
9
|
+
require 'codecov'
|
10
|
+
if ENV['CI']
|
11
|
+
SimpleCov.formatter = SimpleCov::Formatter::Codecov
|
12
|
+
else
|
13
|
+
SimpleCov.formatters = [
|
14
|
+
SimpleCov::Formatter::HTMLFormatter,
|
15
|
+
SimpleCov::Formatter::Console,
|
16
|
+
]
|
17
|
+
end
|
18
|
+
SimpleCov.start
|
19
|
+
|
20
|
+
# Require core files.
|
21
|
+
require 'bundler/setup'
|
22
|
+
require 'string_foundation'
|
23
|
+
require 'support/enhanced_matchers_extension'
|
24
|
+
|
25
|
+
# Require components.
|
26
|
+
require 'random_token'
|
27
|
+
|
28
|
+
# ------------------------------------------------------------------------------
|
29
|
+
# RSpec Settings
|
30
|
+
# ------------------------------------------------------------------------------
|
31
|
+
RSpec.configure do |config|
|
32
|
+
config.include EnhancedMatchersExtension
|
33
|
+
|
34
|
+
# Enable flags like --only-failures and --next-failure .
|
35
|
+
config.example_status_persistence_file_path = '.rspec_status'
|
36
|
+
|
37
|
+
config.expect_with :rspec do |c|
|
38
|
+
c.syntax = :expect
|
39
|
+
end
|
40
|
+
end
|