ellipsized 0.0.1 → 0.1.1

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: 6aecc397c82b24e3704bb3c017ff811e04034bc08b250b638226e797aa6e8921
4
- data.tar.gz: 4800637798c1d81eb8fe49ee6b298654b3241e64ab4d75e22bd3af245a34abc9
3
+ metadata.gz: f3e00b5825661c9c4fd7a4dc9d98f1ba2e2bee79287ee92dc500577a822b640d
4
+ data.tar.gz: a36bd9c135be5d2aeb1831e6673059c4d1d3ad0e55de4ae560200316a49209c1
5
5
  SHA512:
6
- metadata.gz: 10e2415e492615df00a82d62b48b84f9fb2498c8961460228595113f558b0e5f5186d4d02a8a193d6a73120119516c5b215792b023560103b3fb3f64e61ed036
7
- data.tar.gz: 65745ccea3fbbb71d6b40a0eb36c0a17b4cf0dca816e0756c77c611677d86bdf56d5e033ac7ccf634284153466d8b8503888c1908ea9b65fef7d12ae7a13fbc2
6
+ metadata.gz: 63dd527b450d3bda6eed773830ddcf1296a5dbaddc2f7ffccf2d51a85e06b3eb3d3800de614be24aca7e5c8b2c1c4a3251c99bbbecd9e425e7ad469adbfe202b
7
+ data.tar.gz: df4702c055f20d2020cd053a10d9765149828d29c9fd966dd4c5252ad7c183faab75b0f786577c54996fdcc53ba735c544faf3d57c43753d5d242c55dca46590
data/.rubocop.yml CHANGED
@@ -18,4 +18,11 @@ Layout/EndOfLine:
18
18
  EnforcedStyle: lf
19
19
  Style/EvalWithLocation:
20
20
  Enabled: false
21
- require: []
21
+ Metrics/AbcSize:
22
+ Max: 30
23
+ Metrics/CyclomaticComplexity:
24
+ Max: 10
25
+ Metrics/PerceivedComplexity:
26
+ Max: 10
27
+ Metrics/MethodLength:
28
+ Max: 30
data/README.md CHANGED
@@ -11,8 +11,8 @@
11
11
  [![Hits-of-Code](https://hitsofcode.com/github/yegor256/ellipsized)](https://hitsofcode.com/view/github/yegor256/ellipsized)
12
12
  [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/ellipsized/blob/master/LICENSE.txt)
13
13
 
14
- It makes a string fit into required length, replacing
15
- part of it, in the middle, with ellipsis:
14
+ It makes a string fit into a required length by replacing
15
+ part of it in the middle with an [ellipsis]:
16
16
 
17
17
  ```ruby
18
18
  require 'ellipsized'
@@ -25,6 +25,18 @@ Prints:
25
25
  Hello, ...world!
26
26
  ```
27
27
 
28
+ You can also specify what to fill the gap with:
29
+
30
+ ```ruby
31
+ puts 'How are you doing?'.ellipsized(14, gap: '.. skip ..')
32
+ ```
33
+
34
+ Prints:
35
+
36
+ ```text
37
+ Ho.. skip ..g?
38
+ ```
39
+
28
40
  That's it.
29
41
 
30
42
  ## How to contribute
@@ -34,7 +46,7 @@ Read
34
46
  Make sure your build is green before you contribute
35
47
  your pull request. You will need to have
36
48
  [Ruby](https://www.ruby-lang.org/en/) 3.0+ and
37
- [Bundler](https://bundler.io/) installed. Then:
49
+ [Bundler](https://bundler.io/) installed. Then run:
38
50
 
39
51
  ```bash
40
52
  bundle update
@@ -42,3 +54,5 @@ bundle exec rake
42
54
  ```
43
55
 
44
56
  If it's clean and you don't see any error messages, submit your pull request.
57
+
58
+ [ellipsis]: https://en.wikipedia.org/wiki/Ellipsis
data/ellipsized.gemspec CHANGED
@@ -9,15 +9,15 @@ Gem::Specification.new do |s|
9
9
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
10
10
  s.required_ruby_version = '>=3.2'
11
11
  s.name = 'ellipsized'
12
- s.version = '0.0.1'
12
+ s.version = '0.1.1'
13
13
  s.license = 'MIT'
14
- s.summary = 'A simple Ruby gem that adds a .ellipsized() method to Array'
14
+ s.summary = 'A simple Ruby gem that adds a .ellipsized() method to String'
15
15
  s.description =
16
- 'It makes a string fit into required length, replacing ' \
17
- 'part of it, in the middle, with ellipsis.'
16
+ 'It makes a string fit into a required length by replacing ' \
17
+ 'part of it in the middle with an ellipsis.'
18
18
  s.authors = ['Yegor Bugayenko']
19
19
  s.email = 'yegor256@gmail.com'
20
- s.homepage = 'http://github.com/yegor256/ellipsized'
20
+ s.homepage = 'https://github.com/yegor256/ellipsized'
21
21
  s.files = `git ls-files`.split($RS)
22
22
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
23
  s.rdoc_options = ['--charset=UTF-8']
data/lib/ellipsized.rb CHANGED
@@ -3,20 +3,52 @@
3
3
  # SPDX-FileCopyrightText: Copyright (c) 2025 Yegor Bugayenko
4
4
  # SPDX-License-Identifier: MIT
5
5
 
6
- # Replaces part of the text with ellipsis.
6
+ # Replaces part of the text with a gap.
7
7
  #
8
8
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
9
9
  # Copyright:: Copyright (c) 2025 Yegor Bugayenko
10
10
  # License:: MIT
11
11
  class String
12
- # @param [Integer] max The maximum length of the string, expected
13
- # @return [String] The text with ellipsis inside (if necessary)
14
- def ellipsized(max = 64)
12
+ # Truncates a string to specified maximum length, inserting a gap in the middle
13
+ # if necessary. The resulting string will never be longer than the specified
14
+ # maximum length. The original string is returned if it is already shorter than
15
+ # or equal to the maximum length.
16
+ #
17
+ # @param [Integer] max The maximum length of the resulting string
18
+ # @param [String] gap The string to use as gap (default: '...')
19
+ # @return [String] The truncated string with gap in the middle if necessary
20
+ #
21
+ # @example Basic usage with default parameters
22
+ # "Hello, world!".ellipsized
23
+ # # => "Hello, world!"
24
+ #
25
+ # @example Truncate a long string
26
+ # "This is a very long string that needs to be truncated".ellipsized(20)
27
+ # # => "This is...truncated"
28
+ #
29
+ # @example Custom gap
30
+ # "Another very long string example".ellipsized(15, gap: "***")
31
+ # # => "Anoth***example"
32
+ #
33
+ # @example Edge cases
34
+ # "".ellipsized # => ""
35
+ # "Short".ellipsized # => "Short"
36
+ # "xyz".ellipsized(0) # => ""
37
+ # "xyz".ellipsized(2, gap: "...") # => "xy"
38
+ def ellipsized(max = 64, gap: '...')
39
+ raise "Max length must be an Integer, while #{max.class.name} provided" unless max.is_a?(Integer)
40
+ raise "Max length (#{max}) is negative" if max.negative?
15
41
  return '' if empty?
16
42
  return self if length <= max
43
+ return '' if max.zero?
44
+ raise "The gap doesn't implement to_s()" unless gap.respond_to?(:to_s)
17
45
 
18
- head = tail = (max - 3) / 2
19
- head += 1 if head + tail + 3 < max
20
- "#{self[0, head]}...#{self[length - tail..]}"
46
+ gap = gap.to_s
47
+ return self[0..max - 1] if gap.length >= max
48
+
49
+ head = tail = (max - gap.length) / 2
50
+ head += 1 if head + tail + gap.length < max
51
+ head = max if head > max
52
+ "#{self[0, head]}#{gap}#{self[length - tail..]}"
21
53
  end
22
54
  end
data/test/test__helper.rb CHANGED
@@ -15,8 +15,8 @@ unless SimpleCov.running || ENV['PICKS']
15
15
  SimpleCov::Formatter::CoberturaFormatter
16
16
  ]
17
17
  )
18
- SimpleCov.minimum_coverage 95
19
- SimpleCov.minimum_coverage_by_file 90
18
+ SimpleCov.minimum_coverage 100
19
+ SimpleCov.minimum_coverage_by_file 100
20
20
  SimpleCov.start do
21
21
  add_filter 'test/'
22
22
  add_filter 'vendor/'
@@ -17,4 +17,33 @@ class TestEllipsized < Minitest::Test
17
17
  assert_equal('app...na', 'apple and banana'.ellipsized(8))
18
18
  assert_equal('пр...г!', 'привет, друг!'.ellipsized(7))
19
19
  end
20
+
21
+ def test_with_very_small_window
22
+ assert_equal('', 'The Godfather'.ellipsized(0))
23
+ assert_equal('T', 'The Godfather'.ellipsized(1))
24
+ assert_equal('Th', 'The Godfather'.ellipsized(2))
25
+ assert_equal('The', 'The Godfather'.ellipsized(3))
26
+ assert_equal('T...', 'The Godfather'.ellipsized(4))
27
+ assert_equal('T...r', 'The Godfather'.ellipsized(5))
28
+ assert_equal('Th...r', 'The Godfather'.ellipsized(6))
29
+ assert_equal('Th...er', 'The Godfather'.ellipsized(7))
30
+ assert_equal('The...er', 'The Godfather'.ellipsized(8))
31
+ end
32
+
33
+ def test_with_empty_gap
34
+ assert_equal('', 'Encapsulation'.ellipsized(0, gap: ''))
35
+ assert_equal('E', 'Encapsulation'.ellipsized(1, gap: ''))
36
+ assert_equal('En', 'Encapsulation'.ellipsized(2, gap: ''))
37
+ assert_equal('Enn', 'Encapsulation'.ellipsized(3, gap: ''))
38
+ assert_equal('Enon', 'Encapsulation'.ellipsized(4, gap: ''))
39
+ assert_equal('Encon', 'Encapsulation'.ellipsized(5, gap: ''))
40
+ assert_equal('Encion', 'Encapsulation'.ellipsized(6, gap: ''))
41
+ end
42
+
43
+ def test_with_replacement
44
+ assert_equal(
45
+ 'This .. skip ..indow',
46
+ 'This story is very long to fit into a small window'.ellipsized(20, gap: '.. skip ..')
47
+ )
48
+ end
20
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ellipsized
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -9,8 +9,8 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
- description: It makes a string fit into required length, replacing part of it, in
13
- the middle, with ellipsis.
12
+ description: It makes a string fit into a required length by replacing part of it
13
+ in the middle with an ellipsis.
14
14
  email: yegor256@gmail.com
15
15
  executables: []
16
16
  extensions: []
@@ -47,7 +47,7 @@ files:
47
47
  - renovate.json
48
48
  - test/test__helper.rb
49
49
  - test/test_ellipsized.rb
50
- homepage: http://github.com/yegor256/ellipsized
50
+ homepage: https://github.com/yegor256/ellipsized
51
51
  licenses:
52
52
  - MIT
53
53
  metadata:
@@ -69,5 +69,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  requirements: []
70
70
  rubygems_version: 3.6.7
71
71
  specification_version: 4
72
- summary: A simple Ruby gem that adds a .ellipsized() method to Array
72
+ summary: A simple Ruby gem that adds a .ellipsized() method to String
73
73
  test_files: []