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 +4 -4
- data/.rubocop.yml +2 -1
- data/README.md +15 -3
- data/ellipsized.gemspec +5 -5
- data/lib/ellipsized.rb +34 -7
- data/test/test_ellipsized.rb +19 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c05fb67e627a48333959a2e5939faf1ef73fec35c8bc3cb81f106452089797c
|
4
|
+
data.tar.gz: 4196eaefe1dbf1ba369d2c35248ae762653e842b8f371529241d9a730caa9773
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b870d389d1f990a94af3a5f381c486eb144c7984704c97f75fd0ebd8fc66788583de8b58067e509ca9a3a4bc202d56b699125f336aaa04f5cb0254507536d9a
|
7
|
+
data.tar.gz: 39e19c22d146df6356ded9ebf56297fb9dadaf681eab7e4a15081c3ff04a1ab8d30a8035ca811a090e6c98faa9a129b72c2a8abb9f1058b4783e671033dc7b08
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -11,8 +11,8 @@
|
|
11
11
|
[](https://hitsofcode.com/view/github/yegor256/ellipsized)
|
12
12
|
[](https://github.com/yegor256/ellipsized/blob/master/LICENSE.txt)
|
13
13
|
|
14
|
-
It makes a string fit into required length
|
15
|
-
part of it
|
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
|
12
|
+
s.version = '0.1.0'
|
13
13
|
s.license = 'MIT'
|
14
|
-
s.summary = 'A simple Ruby gem that adds a .ellipsized() method to
|
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
|
17
|
-
'part of it
|
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 = '
|
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
|
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
|
-
#
|
13
|
-
#
|
14
|
-
|
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 -
|
19
|
-
head += 1 if head + tail +
|
20
|
-
|
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
|
data/test/test_ellipsized.rb
CHANGED
@@ -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
|
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
|
13
|
-
the middle
|
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:
|
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
|
72
|
+
summary: A simple Ruby gem that adds a .ellipsized() method to String
|
73
73
|
test_files: []
|