ellipsized 0.0.1 → 0.1.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: 6aecc397c82b24e3704bb3c017ff811e04034bc08b250b638226e797aa6e8921
4
- data.tar.gz: 4800637798c1d81eb8fe49ee6b298654b3241e64ab4d75e22bd3af245a34abc9
3
+ metadata.gz: 1c05fb67e627a48333959a2e5939faf1ef73fec35c8bc3cb81f106452089797c
4
+ data.tar.gz: 4196eaefe1dbf1ba369d2c35248ae762653e842b8f371529241d9a730caa9773
5
5
  SHA512:
6
- metadata.gz: 10e2415e492615df00a82d62b48b84f9fb2498c8961460228595113f558b0e5f5186d4d02a8a193d6a73120119516c5b215792b023560103b3fb3f64e61ed036
7
- data.tar.gz: 65745ccea3fbbb71d6b40a0eb36c0a17b4cf0dca816e0756c77c611677d86bdf56d5e033ac7ccf634284153466d8b8503888c1908ea9b65fef7d12ae7a13fbc2
6
+ metadata.gz: 5b870d389d1f990a94af3a5f381c486eb144c7984704c97f75fd0ebd8fc66788583de8b58067e509ca9a3a4bc202d56b699125f336aaa04f5cb0254507536d9a
7
+ data.tar.gz: 39e19c22d146df6356ded9ebf56297fb9dadaf681eab7e4a15081c3ff04a1ab8d30a8035ca811a090e6c98faa9a129b72c2a8abb9f1058b4783e671033dc7b08
data/.rubocop.yml CHANGED
@@ -18,4 +18,5 @@ Layout/EndOfLine:
18
18
  EnforcedStyle: lf
19
19
  Style/EvalWithLocation:
20
20
  Enabled: false
21
- require: []
21
+ Metrics/AbcSize:
22
+ Max: 25
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
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.0'
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,47 @@
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: '...')
15
39
  return '' if empty?
16
40
  return self if length <= max
41
+ return '' if max.zero?
42
+ return self[0..max - 1] if gap.length >= max
17
43
 
18
- head = tail = (max - 3) / 2
19
- head += 1 if head + tail + 3 < max
20
- "#{self[0, head]}...#{self[length - tail..]}"
44
+ head = tail = (max - gap.length) / 2
45
+ head += 1 if head + tail + gap.length < max
46
+ head = max if head > max
47
+ "#{self[0, head]}#{gap}#{self[length - tail..]}"
21
48
  end
22
49
  end
@@ -17,4 +17,23 @@ 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_replacement
34
+ assert_equal(
35
+ 'This .. skip ..indow',
36
+ 'This story is very long to fit into a small window'.ellipsized(20, gap: '.. skip ..')
37
+ )
38
+ end
20
39
  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.0
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: []