strings 0.1.4 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8111a8f94d474b08df2f71900bbd8f39ead7aabc0e3dbfff6917b249bd3322f
4
- data.tar.gz: e86f201edd293da81d6d82210cd4a2cf40ab6098e65edc3a8bb8d0e14849ddb7
3
+ metadata.gz: 4d112bf8f896e770e61346f4f64c2dff39c1d5ec630321d811fadbcf4a96cb0b
4
+ data.tar.gz: 58242b6240dc5400dd1262a4e7692574d60fe2415e6957db8bf7ec0fd52d861c
5
5
  SHA512:
6
- metadata.gz: b16670dfae47bb27c8b7ab2464831fda91baed37d721ae88e5c5a9787a2db5ec888b500922bbd111107cee8d91823e70c7d3111818aed2adf419af524a6313dc
7
- data.tar.gz: 3b6daf8eb6a5ed06ee0d1688b8fd688a3ec21527525db103132cd933f28b25576c55b4b530062f9f88bb0d097f618048eff45dee71a1abe5866e21eb8c74cbd9
6
+ metadata.gz: 68e35d997825c8f3f5349236688db5be5b7c1fc8f5b560f53321e666a4e7dc7dd94c837fe6bf13d97c13d22d34010f0201d3a697956b212c157d835a3549fb15
7
+ data.tar.gz: '087381e615bfa11ddeeacdeca1fbfb866ca4c70977f60df1ad4d02e7fd25b12154e32158e32ada3fe4815ce122288a097112877e85c3ac0cbe4e6bf284b878c0'
@@ -1,5 +1,41 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.2.0] - 2020-08-11
4
+
5
+ ### Changed
6
+ * Change String#wrap to preserve newline character breaks
7
+ * Change gemspec to remove test artefacts and bundler dev dependency
8
+ * Change gemspec to require Ruby >= 2.0.0
9
+
10
+ ## [v0.1.8] - 2019-11-24
11
+
12
+ ### Fixed
13
+ * Fix Ruby 2.7 warnings by Ryan Davis(@zenspider)
14
+
15
+ ## [v0.1.7] - 2019-11-14
16
+
17
+ ### Added
18
+ * Add metadata to gemspec
19
+
20
+ ### Fixed
21
+ * Fix Truncate#truncate to accept length of 1 by Katelyn Schiesser(@slowbro)
22
+
23
+ ## [v0.1.6] - 2019-08-28
24
+
25
+ ### Changed
26
+ * Change Wrap#wrap, Align#align & Pad#pad to handle different line endings
27
+ * Change Pad#pad to pad empty lines
28
+
29
+ ### Fixed
30
+ * Fix Wrap#wrap to handle adjacent ANSI codes
31
+ * Fix Wrap#insert_ansi to handle nested ANSI codes
32
+
33
+ ## [v0.1.5] - 2019-03-29
34
+
35
+ ### Changed
36
+ * Change to update unicode-display_width to the latest version
37
+ * Change to relax development dependencies versions
38
+
3
39
  ## [v0.1.4] - 2018-09-10
4
40
 
5
41
  ### Fixed
@@ -22,8 +58,13 @@
22
58
 
23
59
  ## [v0.1.0] - 2018-01-07
24
60
 
25
- * Inital implementation and release
61
+ * Initial implementation and release
26
62
 
63
+ [v0.2.0]: https://github.com/piotrmurach/strings/compare/v0.1.8...v0.2.0
64
+ [v0.1.8]: https://github.com/piotrmurach/strings/compare/v0.1.7...v0.1.8
65
+ [v0.1.7]: https://github.com/piotrmurach/strings/compare/v0.1.6...v0.1.7
66
+ [v0.1.6]: https://github.com/piotrmurach/strings/compare/v0.1.5...v0.1.6
67
+ [v0.1.5]: https://github.com/piotrmurach/strings/compare/v0.1.4...v0.1.5
27
68
  [v0.1.4]: https://github.com/piotrmurach/strings/compare/v0.1.3...v0.1.4
28
69
  [v0.1.3]: https://github.com/piotrmurach/strings/compare/v0.1.2...v0.1.3
29
70
  [v0.1.2]: https://github.com/piotrmurach/strings/compare/v0.1.1...v0.1.2
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <div align="center">
2
- <img width="225" src="https://cdn.rawgit.com/piotrmurach/strings/master/assets/strings_logo.png" alt="strings logo" />
2
+ <img width="225" src="https://github.com/piotrmurach/strings/blob/master/assets/strings_logo.png" alt="strings logo" />
3
3
  </div>
4
4
 
5
5
  # Strings
@@ -18,7 +18,7 @@
18
18
  [coverage]: https://coveralls.io/github/piotrmurach/strings?branch=master
19
19
  [inchpages]: http://inch-ci.org/github/piotrmurach/strings
20
20
 
21
- > The `Strings` is a set of useful functions such as fold, truncate, wrap, and many more for transforming strings.
21
+ > A set of useful methods for working with strings such as align, truncate, wrap, and many more.
22
22
 
23
23
  ## Installation
24
24
 
@@ -41,8 +41,9 @@ Or install it yourself as:
41
41
  * No monkey-patching String class
42
42
  * Functional API that can be easily wrapped by other objects
43
43
  * Supports multibyte character encodings such as UTF-8, EUC-JP
44
- * Handles languages without whitespaces between words (like Chinese and Japanese)
44
+ * Handles languages without white-spaces between words (like Chinese and Japanese)
45
45
  * Supports ANSI escape codes
46
+ * Flexible by nature, split into [components](#4-components)
46
47
 
47
48
  ## Contents
48
49
 
@@ -56,7 +57,7 @@ Or install it yourself as:
56
57
  * [2.6 truncate](#26-truncate)
57
58
  * [2.7 wrap](#27-wrap)
58
59
  * [3. Extending String class](#3-extending-string-class)
59
- * [4. Utilities](#4-utilities)
60
+ * [4. Components](#4-components)
60
61
 
61
62
  ## 1. Usage
62
63
 
@@ -132,14 +133,14 @@ Strings.align_left(text, 20)
132
133
 
133
134
  ### 2.2 ansi?
134
135
 
135
- To check if a string includes ANSI escape codes use `ansi?` like so:
136
+ To check if a string includes ANSI escape codes use `ansi?` method like so:
136
137
 
137
138
  ```ruby
138
139
  Strings.ansi?("\e[33;44mfoo\e[0m")
139
140
  # => true
140
141
  ```
141
142
 
142
- or
143
+ Or fully qualified name:
143
144
 
144
145
  ```ruby
145
146
  Strings::ANSI.ansi?("\e[33;44mfoo\e[0m")
@@ -148,7 +149,7 @@ Strings::ANSI.ansi?("\e[33;44mfoo\e[0m")
148
149
 
149
150
  ### 2.3 fold
150
151
 
151
- To fold a multiline text into a single line preserving whitespace characters use `fold`:
152
+ To fold a multiline text into a single line preserving white-space characters use `fold`:
152
153
 
153
154
  ```ruby
154
155
  Strings.fold("\tfoo \r\n\n bar")
@@ -277,7 +278,7 @@ text = 'ラドクリフ、マラソン五輪代表に1万m出場にも含み'
277
278
  Strings.truncate(text, 12) # => "ラドクリフ…"
278
279
  ```
279
280
 
280
- **Strings::Truncate** works with ANSI escape codoes:
281
+ **Strings::Truncate** works with ANSI escape codes:
281
282
 
282
283
  ```ruby
283
284
  text = "I try \e[34mall things\e[0m, I achieve what I can"
@@ -287,12 +288,12 @@ Strings.truncate(text, 18)
287
288
 
288
289
  ### 2.7 wrap
289
290
 
290
- To wrap text into lines no longer than `wrap_at` argument length, the `wrap` method will break either on whitespace character or in case of east Asian characters on character boundaries.
291
+ To wrap text into lines no longer than `wrap_at` argument length, the `wrap` method will break either on white-space character or in case of east Asian characters on character boundaries.
291
292
 
292
293
  Given the following text:
293
294
 
294
295
  ```ruby
295
- text "Think not, is my eleventh commandment; and sleep when you can, is my twelfth."
296
+ text = "Think not, is my eleventh commandment; and sleep when you can, is my twelfth."
296
297
  ```
297
298
 
298
299
  Then to wrap the text to given length do:
@@ -338,7 +339,7 @@ Strings::Wrap.wrap(text, wrap_at)
338
339
 
339
340
  ## 3. Extending String class
340
341
 
341
- Though it is highly discouraged to polute core Ruby classes, you can add the required methods to `String` class by using refinements.
342
+ Though it is highly discouraged to pollute core Ruby classes, you can add the required methods to `String` class by using refinements.
342
343
 
343
344
  For example, if you wish to only extend strings with `wrap` method do:
344
345
 
@@ -352,7 +353,7 @@ module MyStringExt
352
353
  end
353
354
  ```
354
355
 
355
- then `wrap` method will be available for any strings where refinement is applied:
356
+ Then `wrap` method will be available for any strings where refinement is applied:
356
357
 
357
358
  ```ruby
358
359
  using MyStringExt
@@ -368,13 +369,16 @@ require 'strings/extensions'
368
369
  using Strings::Extensions
369
370
  ```
370
371
 
371
- ## 4. Utilities
372
+ ## 4. Components
372
373
 
373
- **Strings** aims to be fleaxible and allow you to choose only the utilities that you need. Currently you can choose from:
374
+ **Strings** aims to be flexible and allow you to choose only the components that you need. Currently you can choose from:
374
375
 
375
- | Utility | Description | API docs |
376
+ | Component | Description | API docs |
376
377
  | ------------ | ----------- | -------- |
377
378
  | [strings-ansi](https://github.com/piotrmurach/strings-ansi) | Handle ANSI escape codes in strings. | [docs](http://www.rubydoc.info/gems/strings-ansi) |
379
+ | [strings-case](https://github.com/piotrmurach/strings-case) | Handle case transformations in strings. | [docs](http://www.rubydoc.info/gems/strings-case) |
380
+ | [strings-inflection](https://github.com/piotrmurach/strings-inflection) | Inflects English nouns and verbs. | [docs](http://www.rubydoc.info/gems/strings-inflection) |
381
+ | [strings-numeral](https://github.com/piotrmurach/strings-numeral) | Express numbers as word numerals. | [docs](http://www.rubydoc.info/gems/strings-numeral) |
378
382
 
379
383
  ## Development
380
384
 
@@ -386,7 +390,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
386
390
 
387
391
  Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/strings. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
388
392
 
389
- 1. Fork it ( https://github.com/piotrmurach/verse/fork )
393
+ 1. Fork it ( https://github.com/piotrmurach/strings/fork )
390
394
  2. Create your feature branch (`git checkout -b my-new-feature`)
391
395
  3. Commit your changes (`git commit -am 'Add some feature'`)
392
396
  4. Push to the branch (`git push origin my-new-feature`)
@@ -402,4 +406,4 @@ Everyone interacting in the Strings project’s codebases, issue trackers, chat
402
406
 
403
407
  ## Copyright
404
408
 
405
- Copyright (c) 2017-2018 Piotr Murach. See LICENSE for further details.
409
+ Copyright (c) 2017 Piotr Murach. See LICENSE for further details.
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'strings-ansi'
3
+ require "strings-ansi"
4
4
 
5
- require_relative 'strings/align'
6
- require_relative 'strings/fold'
7
- require_relative 'strings/pad'
8
- require_relative 'strings/truncate'
9
- require_relative 'strings/wrap'
10
- require_relative 'strings/version'
5
+ require_relative "strings/align"
6
+ require_relative "strings/fold"
7
+ require_relative "strings/pad"
8
+ require_relative "strings/truncate"
9
+ require_relative "strings/wrap"
10
+ require_relative "strings/version"
11
11
 
12
12
  module Strings
13
13
  # Align text within the width.
@@ -15,8 +15,8 @@ module Strings
15
15
  # @see Strings::Align#align
16
16
  #
17
17
  # @api public
18
- def align(*args)
19
- Align.align(*args)
18
+ def align(*args, **kws)
19
+ Align.align(*args, **kws)
20
20
  end
21
21
  module_function :align
22
22
 
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'strings-ansi'
4
- require 'unicode/display_width'
3
+ require "strings-ansi"
4
+ require "unicode/display_width"
5
5
 
6
6
  module Strings
7
7
  # Responsible for text alignment
8
8
  module Align
9
- NEWLINE = "\n".freeze
10
-
11
- SPACE = ' '.freeze
9
+ NEWLINE = "\n"
10
+ SPACE = " "
11
+ LINE_BREAK = %r{\r\n|\r|\n}.freeze
12
12
 
13
13
  # Aligns text within the width.
14
14
  #
@@ -23,23 +23,24 @@ module Strings
23
23
  # @example
24
24
  # text = "the madness of men"
25
25
  #
26
- # Strings::Align.align(22, :left)
26
+ # Strings::Align.align(text, 22, direction: :left)
27
27
  # # => "the madness of men "
28
28
  #
29
- # Strings::Align.align(22, :center)
29
+ # Strings::Align.align(text, 22, direction: :center)
30
30
  # # => " the madness of men "
31
31
  #
32
- # Strings::Align(22, :right)
32
+ # Strings::Align(text, 22, direction: :right)
33
33
  # # => " the madness of men"
34
34
  #
35
- # Strings::Align.align(22, :center, fill: '*)
35
+ # Strings::Align.align(text, 22, direction: :center, fill: "*")
36
36
  # # => "***the madness of men***"
37
37
  #
38
38
  # @api public
39
39
  def align(text, width, direction: :left, **options)
40
40
  return text if width.nil?
41
+
41
42
  method = to_alignment(direction)
42
- send(method, text, width, options)
43
+ send(method, text, width, **options)
43
44
  end
44
45
  module_function :align
45
46
 
@@ -62,9 +63,11 @@ module Strings
62
63
  # @return [String]
63
64
  #
64
65
  # @api public
65
- def align_left(text, width, fill: SPACE, separator: NEWLINE)
66
+ def align_left(text, width, fill: SPACE, separator: nil)
66
67
  return if width.nil?
67
- each_line(text, separator) do |line|
68
+ sep = separator || text[LINE_BREAK] || NEWLINE
69
+
70
+ each_line(text, sep) do |line|
68
71
  width_diff = width - display_width(line)
69
72
  if width_diff > 0
70
73
  line + fill * width_diff
@@ -80,9 +83,11 @@ module Strings
80
83
  # @return [String]
81
84
  #
82
85
  # @api public
83
- def align_center(text, width, fill: SPACE, separator: NEWLINE)
86
+ def align_center(text, width, fill: SPACE, separator: nil)
84
87
  return text if width.nil?
85
- each_line(text, separator) do |line|
88
+ sep = separator || text[LINE_BREAK] || NEWLINE
89
+
90
+ each_line(text, sep) do |line|
86
91
  width_diff = width - display_width(line)
87
92
  if width_diff > 0
88
93
  right_count = (width_diff.to_f / 2).ceil
@@ -100,9 +105,11 @@ module Strings
100
105
  # @return [String]
101
106
  #
102
107
  # @api public
103
- def align_right(text, width, fill: SPACE, separator: NEWLINE)
108
+ def align_right(text, width, fill: SPACE, separator: nil)
104
109
  return text if width.nil?
105
- each_line(text, separator) do |line|
110
+ sep = separator || text[LINE_BREAK] || NEWLINE
111
+
112
+ each_line(text, sep) do |line|
106
113
  width_diff = width - display_width(line)
107
114
  if width_diff > 0
108
115
  fill * width_diff + line
@@ -1,20 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../strings'
3
+ require_relative "../strings"
4
4
 
5
5
  module Strings
6
6
  module Extensions
7
7
  refine String do
8
- def align(*args)
9
- Align.align(self, *args)
8
+ def align(*args, **kws)
9
+ Align.align(self, *args, **kws)
10
10
  end
11
11
 
12
- def align_left(*args)
13
- Align.align_left(self, *args)
12
+ def align_left(*args, **kws)
13
+ Align.align_left(self, *args, **kws)
14
14
  end
15
15
 
16
- def align_right(*args)
17
- Align.align_right(self, *args)
16
+ def align_center(*args, **kws)
17
+ Align.align_center(self, *args, **kws)
18
+ end
19
+
20
+ def align_right(*args, **kws)
21
+ Align.align_right(self, *args, **kws)
18
22
  end
19
23
 
20
24
  def ansi?
@@ -25,8 +29,8 @@ module Strings
25
29
  Fold.fold(self, *args)
26
30
  end
27
31
 
28
- def pad(*args)
29
- Pad.pad(self, *args)
32
+ def pad(*args, **kws)
33
+ Pad.pad(self, *args, **kws)
30
34
  end
31
35
 
32
36
  def sanitize
@@ -37,8 +41,8 @@ module Strings
37
41
  Truncate.truncate(self, *args)
38
42
  end
39
43
 
40
- def wrap(*args)
41
- Wrap.wrap(self, *args)
44
+ def wrap(*args, **kws)
45
+ Wrap.wrap(self, *args, **kws)
42
46
  end
43
47
  end
44
48
  end # Extensions
@@ -7,7 +7,7 @@ module Strings
7
7
  # Fold a multiline text into a single line string
8
8
  #
9
9
  # @example
10
- # fold("\tfoo \r\n\n bar") # => "foo bar"
10
+ # fold("\tfoo \r\n\n bar") # => " foo bar"
11
11
  #
12
12
  # @param [String] text
13
13
  #
@@ -20,7 +20,7 @@ module Strings
20
20
  def fold(text, separator = LINE_BREAK)
21
21
  text.gsub(/([ ]+)#{separator}/, "\\1")
22
22
  .gsub(/#{separator}(?<space>[ ]+)/, "\\k<space>")
23
- .gsub(/#{separator}/, ' ')
23
+ .gsub(/#{separator}/, " ")
24
24
  end
25
25
  module_function :fold
26
26
  end # Fold
@@ -1,16 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'strings-ansi'
4
- require 'unicode/display_width'
3
+ require "strings-ansi"
4
+ require "unicode/display_width"
5
5
 
6
- require_relative 'padder'
6
+ require_relative "padder"
7
7
 
8
8
  module Strings
9
9
  # Responsible for text padding
10
10
  module Pad
11
- NEWLINE = "\n".freeze
12
-
13
- SPACE = ' '.freeze
11
+ NEWLINE = "\n"
12
+ SPACE = " "
13
+ LINE_BREAK = %r{\r\n|\r|\n}.freeze
14
14
 
15
15
  # Apply padding to multiline text with ANSI codes
16
16
  #
@@ -31,10 +31,11 @@ module Strings
31
31
  # @return [String]
32
32
  #
33
33
  # @api private
34
- def pad(text, padding, fill: SPACE, separator: NEWLINE)
34
+ def pad(text, padding, fill: SPACE, separator: nil)
35
35
  padding = Strings::Padder.parse(padding)
36
36
  text_copy = text.dup
37
- line_width = max_line_length(text, separator)
37
+ sep = separator || text[LINE_BREAK] || NEWLINE
38
+ line_width = max_line_length(text, sep)
38
39
  output = []
39
40
 
40
41
  filler_line = fill * line_width
@@ -43,7 +44,8 @@ module Strings
43
44
  output << pad_around(filler_line, padding, fill: fill)
44
45
  end
45
46
 
46
- text_copy.split(separator).each do |line|
47
+ text_copy.split(sep).each do |line|
48
+ line = line.empty? ? filler_line : line
47
49
  output << pad_around(line, padding, fill: fill)
48
50
  end
49
51
 
@@ -51,7 +53,7 @@ module Strings
51
53
  output << pad_around(filler_line, padding, fill: fill)
52
54
  end
53
55
 
54
- output.join(separator)
56
+ output.join(sep)
55
57
  end
56
58
  module_function :pad
57
59
 
@@ -77,7 +79,7 @@ module Strings
77
79
  # @api private
78
80
  def max_line_length(text, separator)
79
81
  lines = text.split(separator, -1)
80
- display_width(lines.max_by { |line| display_width(line) } || '')
82
+ display_width(lines.max_by { |line| display_width(line) } || "")
81
83
  end
82
84
  module_function :max_line_length
83
85