powerpack 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -5
- data/lib/powerpack/hash/symbolize_keys.rb +6 -2
- data/lib/powerpack/string.rb +1 -0
- data/lib/powerpack/string/blank.rb +14 -3
- data/lib/powerpack/string/format.rb +9 -1
- data/lib/powerpack/string/squish.rb +14 -0
- data/lib/powerpack/string/strip_indent.rb +7 -7
- data/lib/powerpack/string/strip_margin.rb +7 -7
- data/lib/powerpack/version.rb +1 -1
- data/spec/powerpack/string/squish_spec.rb +11 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5ae47f06545fa305812faece61d4a22a32f29cf
|
4
|
+
data.tar.gz: ffc7a033f26e64169194bd31fd0e0d3a97ddb145
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea7099ee760815ad0b5257690a2cc48925c677956f7b51f9a974fe1d19f31b3cd4fcadd9b6ac09b7f57815923d71774ccca763a234785ed34495c48377d8609a
|
7
|
+
data.tar.gz: 298cd5779cd56510d5089f85c23b8cc7b74920cd0be906682c6b214af047c7786d72f4410dc0c017f9931bc2137b17096ebb1222b16d548790b89f2a21997431
|
data/README.md
CHANGED
@@ -22,12 +22,13 @@ Or install it yourself as:
|
|
22
22
|
## Extensions
|
23
23
|
|
24
24
|
* [Hash](http://rdoc.info/github/bbatsov/powerpack/Hash)
|
25
|
-
|
25
|
+
* [#symbolize_keys](http://rdoc.info/github/bbatsov/powerpack/Hash#symbolize_keys-instance_method)
|
26
26
|
* [String](http://rdoc.info/github/bbatsov/powerpack/String)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
* [#blank?](http://rdoc.info/github/bbatsov/powerpack/String#blank?-instance_method)
|
28
|
+
* [#format](http://rdoc.info/github/bbatsov/powerpack/String#format-instance_method)
|
29
|
+
* [#strip_indent](http://rdoc.info/github/bbatsov/powerpack/String#strip_indent-instance_method)
|
30
|
+
* [#strip_margin](http://rdoc.info/github/bbatsov/powerpack/String#strip_margin-instance_method)
|
31
|
+
* [#squish](http://rdoc.info/github/bbatsov/powerpack/String#squish-instance_method)
|
31
32
|
|
32
33
|
## Documentation
|
33
34
|
|
@@ -1,7 +1,11 @@
|
|
1
1
|
unless Hash.method_defined? :symbolize_keys
|
2
2
|
class Hash
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# Converts the keys of the hash to symbols.
|
4
|
+
#
|
5
|
+
# @return [Hash] a copy of the original hash with its keys converted to symbols.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# { 'one' => 1, 'two' => 2 }.symbolize_keys #=> { :one => 1, :two => 2 }
|
5
9
|
def symbolize_keys
|
6
10
|
Hash[map { |(k, v)| [k.to_sym, v] }]
|
7
11
|
end
|
data/lib/powerpack/string.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
1
|
unless String.method_defined? :blank?
|
2
2
|
class String
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# Checks whether a string is blank.
|
4
|
+
#
|
5
|
+
# @return [String] `true` is the string is empty or contains only
|
6
|
+
# whitespace, `false` otherwise
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# ''.blank? #=> true
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# ' '.blank? #=> true
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# ' test'.blank? #=> false
|
5
16
|
def blank?
|
6
|
-
|
17
|
+
empty? || strip.empty?
|
7
18
|
end
|
8
19
|
end
|
9
20
|
end
|
@@ -1,6 +1,14 @@
|
|
1
1
|
unless String.method_defined? :format
|
2
2
|
class String
|
3
|
-
#
|
3
|
+
# A nicer alternative to `Kernel#sprintf` and `String#%`.
|
4
|
+
#
|
5
|
+
# @return [String] the formatted string
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# 'This is %s!'.format('Sparta') #=> 'This is Sparta!'
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# '%d + %d'.format([1, 2]) #=> '1 + 2'
|
4
12
|
def format(*args)
|
5
13
|
super(self, *(args.flatten(1)))
|
6
14
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
unless String.method_defined? :squish
|
2
|
+
class String
|
3
|
+
# Strips leading and trailing whitespace and squashes internal whitespace.
|
4
|
+
#
|
5
|
+
# @return [String] a string with not leading and traling whitespace and no
|
6
|
+
# consecutive whitespace characters inside it
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# ' Peter Parker'.squish #=> 'Peter Parker'
|
10
|
+
def squish
|
11
|
+
strip.gsub(/\s+/, ' ')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -5,14 +5,14 @@ unless String.method_defined? :strip_indent
|
|
5
5
|
#
|
6
6
|
# @example
|
7
7
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
8
|
+
# code = <<-END.strip_indent
|
9
|
+
# def test
|
10
|
+
# some_method
|
11
|
+
# other_method
|
12
|
+
# end
|
13
|
+
# END
|
14
14
|
#
|
15
|
-
#
|
15
|
+
# #=> "def\n some_method\n \nother_method\nend"
|
16
16
|
def strip_indent
|
17
17
|
leading_space = scan(/^[ \t]*(?=\S)/).min
|
18
18
|
indent = leading_space ? leading_space.size : 0
|
@@ -5,14 +5,14 @@ unless String.method_defined? :strip_margin
|
|
5
5
|
#
|
6
6
|
# @example
|
7
7
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
8
|
+
# code = <<-END.strip_margin('|')
|
9
|
+
# |def test
|
10
|
+
# | some_method
|
11
|
+
# | other_method
|
12
|
+
# |end
|
13
|
+
# END
|
14
14
|
#
|
15
|
-
#
|
15
|
+
# #=> "def\n some_method\n \nother_method\nend"
|
16
16
|
def strip_margin(margin_character)
|
17
17
|
margin = '\\' + margin_character
|
18
18
|
gsub(/^\s+#{margin}/, '')
|
data/lib/powerpack/version.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'String#squish' do
|
4
|
+
it 'strips leading and trailing whitespace' do
|
5
|
+
expect(' Peter '.squish).to eq('Peter')
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'compacts internal whitespace' do
|
9
|
+
expect("Peter\r\n \t Parker".squish).to eq('Peter Parker')
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: powerpack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- lib/powerpack/string.rb
|
87
87
|
- lib/powerpack/string/blank.rb
|
88
88
|
- lib/powerpack/string/format.rb
|
89
|
+
- lib/powerpack/string/squish.rb
|
89
90
|
- lib/powerpack/string/strip_indent.rb
|
90
91
|
- lib/powerpack/string/strip_margin.rb
|
91
92
|
- lib/powerpack/version.rb
|
@@ -93,6 +94,7 @@ files:
|
|
93
94
|
- spec/powerpack/hash/symbolize_keys_spec.rb
|
94
95
|
- spec/powerpack/string/blank_spec.rb
|
95
96
|
- spec/powerpack/string/format_spec.rb
|
97
|
+
- spec/powerpack/string/squish_spec.rb
|
96
98
|
- spec/powerpack/string/strip_indent_spec.rb
|
97
99
|
- spec/powerpack/string/strip_margin_spec.rb
|
98
100
|
- spec/spec_helper.rb
|
@@ -124,6 +126,7 @@ test_files:
|
|
124
126
|
- spec/powerpack/hash/symbolize_keys_spec.rb
|
125
127
|
- spec/powerpack/string/blank_spec.rb
|
126
128
|
- spec/powerpack/string/format_spec.rb
|
129
|
+
- spec/powerpack/string/squish_spec.rb
|
127
130
|
- spec/powerpack/string/strip_indent_spec.rb
|
128
131
|
- spec/powerpack/string/strip_margin_spec.rb
|
129
132
|
- spec/spec_helper.rb
|