slim_string 1.0.1 → 1.0.2
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/ChangeLog +5 -0
- data/README.en.rdoc +82 -21
- data/lib/slim_string.rb +2 -5
- data/slim_string.gemspec +1 -1
- data/test/test_slim_string.rb +21 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '043184def22a05113e776e328a7472c6e1a1c352013a0b619c7b2ce6649a5f41'
|
4
|
+
data.tar.gz: 562fbd66e5a1b8564072d3766b478daacaa1cd1718a2b58ab91145d37a2848e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abf6a3ed2ca31405010ec087aa6260badd4f3f1bf4f5bca55a872c575c88bf01985eeff346b2ad785387a00b62688386fb9e2f878a41959e20863b9ded149186
|
7
|
+
data.tar.gz: 92237ddcf40baf8df313cb61fd003cced17a58ef59b14c18ee130a7c180681308037082da7a1ba7f60b0ab889cf7f79f61d386a52efd1fb97b76ee8b36dbb9aa
|
data/ChangeLog
CHANGED
data/README.en.rdoc
CHANGED
@@ -5,56 +5,115 @@
|
|
5
5
|
|
6
6
|
Module {SlimString} contains a single method of the same (but
|
7
7
|
snake-case) name +SlimString#slim_string+, which offers functionality
|
8
|
-
to "slim down" character String, such as truncating extra spaces with many
|
9
|
-
options you can
|
10
|
-
It fully takes into account *
|
8
|
+
to "slim down" character String, such as truncating extra spaces, with many
|
9
|
+
options you can specify.
|
10
|
+
It fully takes into account *UTF8 characters* like CJK Zenkaku spaces
|
11
|
+
(U+3000).
|
11
12
|
|
12
|
-
The full package of this
|
13
|
+
The full package of this module is found in
|
13
14
|
{SlimString Ruby Gems page}[http://rubygems.org/gems/slim_string]
|
14
15
|
(with document created from source annotation with yard) and
|
15
16
|
in {Github}[https://github.com/masasakano/slim_string]
|
16
17
|
|
17
18
|
== Description
|
18
19
|
|
19
|
-
|
20
|
+
The main (and only) method +slim_string+ accepts a String (either as the receiver or first
|
21
|
+
argument) and returns a slimmed-down String.
|
20
22
|
|
21
|
-
|
23
|
+
In default, the returned String is processed as:
|
22
24
|
|
23
|
-
|
24
|
-
2.
|
25
|
+
1. all the "weird" spaces like TAB characters are converted into ASCII white spaces (U+0020),
|
26
|
+
2. all the consecutive white spaces but new-line characters are truncated into a single white space,
|
27
|
+
3. all the consecutive white spaces and new-line characters at the beginning and tail are stripped.
|
25
28
|
|
26
|
-
|
27
|
-
|
28
|
-
3. Include it in +String+ class like and you can use it as an instance method.
|
29
|
+
You can specify more *aggressive* or *tamer* processing, specifying
|
30
|
+
suitable options (see below).
|
29
31
|
|
30
|
-
|
31
|
-
include SlimString
|
32
|
-
end
|
32
|
+
=== How to use it?
|
33
33
|
|
34
|
-
|
34
|
+
First,
|
35
|
+
require 'slim_string'
|
35
36
|
|
36
|
-
|
37
|
+
Then,
|
38
|
+
|
39
|
+
1. Simply call it in a full path (it is extended and so you can call it in this way.
|
40
|
+
SlimString.slim_string("\nabc\n\n", strip: false)
|
41
|
+
# => "\nabc"
|
42
|
+
2. "include" it in your class/module (or top level), and you can use it without the module name.
|
43
|
+
include SlimString
|
44
|
+
slim_string("\nabc\n\n", strip: false)
|
45
|
+
# => "\nabc"
|
46
|
+
3. "include" it in +String+ class like and you can use it as an instance method.
|
47
|
+
class String
|
48
|
+
include SlimString
|
49
|
+
end
|
50
|
+
|
51
|
+
"\nabc\n\n".slim_string(strip: false)
|
52
|
+
# => "\nabc"
|
53
|
+
|
54
|
+
=== Class constant
|
55
|
+
|
56
|
+
+DEF_SLIM_OPTIONS+:: Hash containing the default option values. The processing order in +slim_string+ follows that of the keys of this constant.
|
57
|
+
|
58
|
+
=== Options (keyword options)
|
37
59
|
|
38
60
|
All options (aka, arguments) are Boolean.
|
39
61
|
The given string is processed in this order regardless how you do or
|
40
62
|
do not specify them.
|
41
63
|
|
42
|
-
|
64
|
+
Here, the terms *space* and *blank* follows the Ruby Regexp convention,
|
65
|
+
i.e., *spaces* are non-visible characters including newlines,
|
66
|
+
whereas *blanks* are *spaces* minus new-line characters (\\f\\n\\r\\v; ASCII Formfeed (FF), Linefeed (LF), Carriage Return (CR), Vertical Tab (VT)).
|
67
|
+
|
68
|
+
+delete_newlines+:: (Def: false) Delete all new lines (\\f\\n\\r\\v) (maybe useful for Japanese text, unless more than 1 alphabet word are split across 2 lines).
|
43
69
|
+convert_spaces+:: (Def: false) Convert any sort of spaces (including new lines) into an ASCII space.
|
44
70
|
+convert_blanks+:: (Def: true) Convert all blanks (=spaces except kind of new lines) into an ASCII space.
|
45
71
|
+truncate_spaces+:: (Def: false) Truncate any consecutive spaces into one.
|
46
72
|
+truncate_blanks+:: (Def: true) Truncate any consecutive blanks into (the last) one.
|
47
73
|
+truncate_triple_newlines+:: (Def: false) Truncate 3 or more consecutive newlines into 2.
|
48
|
-
+delete_blanks+:: (Def: false) Simply delete all blanks excluding new lines (n.b., "a top" and "atop" are regarded identical.
|
74
|
+
+delete_blanks+:: (Def: false) Simply delete all blanks excluding new lines (n.b., "a top" and "atop" are regarded identical).
|
49
75
|
+strip+:: (Def: true) Ruby strip; strip spaces including new lines at the head and tail.
|
50
76
|
+trim_blanks+:: (Def: false) strip blanks (but new lines) at the tail.
|
51
77
|
+trim+:: (Def: true) Trim the tail to strip any spaces including new lines at the tail.
|
52
78
|
|
79
|
+
== Examples
|
80
|
+
|
81
|
+
Suppose you include +SlimString+ (see "How to use it?" above for
|
82
|
+
detail).
|
83
|
+
|
84
|
+
In default (see the beginning of Description):
|
85
|
+
|
86
|
+
s = "\nab cd\n ef \t gh \n\n"
|
87
|
+
slim_string(s)
|
88
|
+
# => "ab cd\n ef gh"
|
89
|
+
|
90
|
+
To preserve consecutive white spaces, but otherwise as in the default, do:
|
91
|
+
|
92
|
+
slim_string(s, truncate_blanks: false)
|
93
|
+
# => "ab cd\n ef gh"
|
94
|
+
|
95
|
+
To convert all the newlines into white spaces, but otherwise as in the default, do:
|
96
|
+
|
97
|
+
slim_string(s, convert_spaces: true)
|
98
|
+
# => "ab cd ef gh"
|
99
|
+
|
100
|
+
To cancel all the default options and specify your own (n.b., here, Hash#map
|
101
|
+
is available in Ruby 2.1 (released in 2013) and above), do as follows, for example. In this
|
102
|
+
case, it is identical to Ruby's +String#strip+
|
103
|
+
|
104
|
+
s = " \tA B \n"
|
105
|
+
to_cancel = DEF_SLIM_OPTIONS.map{|k,v| [k, false]}.to_h
|
106
|
+
slim_string(s, **(to_cancel.merge({strip: true})))
|
107
|
+
# => "A B" (== s.strip)
|
108
|
+
|
109
|
+
where +DEF_SLIM_OPTIONS+ is a class constant +SlimString::DEF_SLIM_OPTIONS+
|
110
|
+
|
111
|
+
The test suite under +test/+ directory contains many more examples.
|
112
|
+
|
53
113
|
== Install
|
54
114
|
|
55
|
-
This
|
56
|
-
or above.
|
57
|
-
find in RubyGems.
|
115
|
+
This library requires {Ruby}[http://www.ruby-lang.org] Version 2.0
|
116
|
+
or above.
|
58
117
|
|
59
118
|
== Developer's note
|
60
119
|
|
@@ -79,3 +138,5 @@ Author:: Masa Sakano < info a_t wisebabel dot com >
|
|
79
138
|
Versions:: The versions of this package follow Semantic Versioning (2.0.0) http://semver.org/
|
80
139
|
License:: MIT
|
81
140
|
|
141
|
+
----------
|
142
|
+
|
data/lib/slim_string.rb
CHANGED
@@ -65,11 +65,8 @@ module SlimString
|
|
65
65
|
# String class and use it as an instance method, the first
|
66
66
|
# argument is mandatory.
|
67
67
|
def slim_string(str=nil, **opts)
|
68
|
-
|
69
|
-
|
70
|
-
rescue NoMethodError
|
71
|
-
raise ArgumentError, 'first argument is mandatory and must be String-like.'
|
72
|
-
end
|
68
|
+
ret = (str || self).dup
|
69
|
+
raise ArgumentError, 'first argument is mandatory and must be String-like.' if !ret.respond_to? :gsub!
|
73
70
|
|
74
71
|
DEF_SLIM_OPTIONS.merge(opts).each_pair do |ek, tf|
|
75
72
|
next if !tf
|
data/slim_string.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'date'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = 'slim_string'.sub(/.*/){|c| (c == File.basename(Dir.pwd)) ? c : raise("ERROR: s.name=(#{c}) in gemspec seems wrong!")}
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.2".sub(/.*/){|c| fs = Dir.glob('changelog{,.*}', File::FNM_CASEFOLD); raise('More than one ChangeLog exist!') if fs.size > 1; warn("WARNING: Version(s.version=#{c}) already exists in #{fs[0]} - ok?") if fs.size == 1 && !IO.readlines(fs[0]).grep(/^\(Version: #{Regexp.quote c}\)$/).empty? ; c } # n.b., In macOS, changelog and ChangeLog are identical in default.
|
9
9
|
# s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
10
|
# s.bindir = 'bin'
|
11
11
|
# %w(slim_string).each do |f|
|
data/test/test_slim_string.rb
CHANGED
@@ -69,10 +69,31 @@ class TestSlimStringClass < MiniTest::Test
|
|
69
69
|
ex = " a\nb\f\vc\r\n "+z*3+"d \r\n\r\n"
|
70
70
|
assert_equal ex, slim_string(s, convert_blanks: false, truncate_blanks: false, strip: false, trim_blanks: true, trim: false)
|
71
71
|
|
72
|
+
assert_equal s, slim_string(s, **(DEF_SLIM_OPTIONS.map{|k,v| [k, false]}).to_h)
|
73
|
+
assert_equal s.strip, slim_string(s, **(DEF_SLIM_OPTIONS.map{|k,v| [k, false]}).to_h.merge({strip: true}))
|
74
|
+
|
72
75
|
assert_raises(ArgumentError){ slim_string(s, dummy: true) }
|
73
76
|
|
74
77
|
assert_equal "a\nb\f\vc\r\n d", s.slim_string()
|
75
78
|
assert_equal "a\nb\f\vc\r\n d", SlimString.slim_string(s)
|
79
|
+
assert_raises(ArgumentError){ SlimString.slim_string() }
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_slim_string_in_doc
|
83
|
+
s = " \tA B \n"
|
84
|
+
assert_equal s.strip, slim_string(s, **(DEF_SLIM_OPTIONS.map{|k,v| [k, false]}).to_h.merge({strip: true}))
|
85
|
+
assert_equal "A B", slim_string(s, **(DEF_SLIM_OPTIONS.map{|k,v| [k, false]}).to_h.merge({strip: true}))
|
86
|
+
|
87
|
+
to_cancel = DEF_SLIM_OPTIONS.map{|k,v| [k, false]}.to_h
|
88
|
+
assert_equal s.strip, slim_string(s, **(to_cancel.merge({strip: true})))
|
89
|
+
|
90
|
+
s = "\nab cd\n ef \t gh \n\n"
|
91
|
+
ex = "ab cd\n ef gh"
|
92
|
+
assert_equal ex, slim_string(s)
|
93
|
+
ex = "ab cd ef gh"
|
94
|
+
assert_equal ex, slim_string(s, convert_spaces: true)
|
95
|
+
ex = "ab cd\n ef gh"
|
96
|
+
assert_equal ex, slim_string(s, truncate_blanks: false)
|
76
97
|
end
|
77
98
|
end # class TestUnitSlimString < MiniTest::Test
|
78
99
|
|