ellipsized 0.2.0 → 0.3.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: abc2bd33ae6083981ccec4e019ae59fb4b8f572201137b2cc2b0e34dbea907b7
4
- data.tar.gz: 447f61c07ba3e232ee181c8ed4bd1ecfba632085e409230d816b5e524ceec940
3
+ metadata.gz: 8e4185354881717b62aaf4170c011968eb50b5a7dd4fcc0947cb0a7192677c69
4
+ data.tar.gz: d36e583d3c9c31f57b00766e6441be34bcb93a4073fa3f38bba82d4170d25f94
5
5
  SHA512:
6
- metadata.gz: 13a5933003e880a53970c9931d29be03d3ac985cc994261683e0876ad28a14900b8eb2423d000d68b7b601a6fa5a351c351903562b1b9430cb36b2ba005c21c3
7
- data.tar.gz: 7d870cd017c7b628a23546bb62e724eaa357ec145ce5e4dd701158bf4984a76174448afc586868c045732edcc94df98e00e276ce70824ea757b2116cc5e31b83
6
+ metadata.gz: adf0564a1ba5e3cbd27e4627a61e82e32f1d6f065c4b6a51de22c37b8942b1da8b0d0e8106334e327f49b4d1f39073038cc899f798b7e24deb381ad984ffc0f6
7
+ data.tar.gz: 2832a6bfa7f841f49fd83e4ca7a2150809cf7ffa800a914214f6dbebed5da9411e11433a955832f510039a4aaf1e85fb575f549a82c562e7634f658db21378ca
@@ -16,4 +16,4 @@ jobs:
16
16
  runs-on: ubuntu-24.04
17
17
  steps:
18
18
  - uses: actions/checkout@v4
19
- - uses: crate-ci/typos@v1.32.0
19
+ - uses: crate-ci/typos@v1.33.1
data/.rubocop.yml CHANGED
@@ -19,10 +19,12 @@ Layout/EndOfLine:
19
19
  Style/EvalWithLocation:
20
20
  Enabled: false
21
21
  Metrics/AbcSize:
22
- Max: 30
22
+ Max: 50
23
+ Metrics/ClassLength:
24
+ Max: 112
23
25
  Metrics/CyclomaticComplexity:
24
- Max: 10
26
+ Max: 22
25
27
  Metrics/PerceivedComplexity:
26
- Max: 10
28
+ Max: 18
27
29
  Metrics/MethodLength:
28
- Max: 30
30
+ Max: 38
data/Gemfile.lock CHANGED
@@ -29,7 +29,7 @@ GEM
29
29
  rake (13.3.0)
30
30
  regexp_parser (2.10.0)
31
31
  rexml (3.4.1)
32
- rubocop (1.75.8)
32
+ rubocop (1.76.0)
33
33
  json (~> 2.3)
34
34
  language_server-protocol (~> 3.17.0.2)
35
35
  lint_roller (~> 1.1.0)
@@ -37,10 +37,10 @@ GEM
37
37
  parser (>= 3.3.0.2)
38
38
  rainbow (>= 2.2.2, < 4.0)
39
39
  regexp_parser (>= 2.9.3, < 3.0)
40
- rubocop-ast (>= 1.44.0, < 2.0)
40
+ rubocop-ast (>= 1.45.0, < 2.0)
41
41
  ruby-progressbar (~> 1.7)
42
42
  unicode-display_width (>= 2.4.0, < 4.0)
43
- rubocop-ast (1.44.1)
43
+ rubocop-ast (1.45.0)
44
44
  parser (>= 3.3.7.2)
45
45
  prism (~> 1.4)
46
46
  rubocop-minitest (0.38.1)
data/ellipsized.gemspec CHANGED
@@ -9,7 +9,7 @@ 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.2.0'
12
+ s.version = '0.3.0'
13
13
  s.license = 'MIT'
14
14
  s.summary = 'A simple Ruby gem that adds a .ellipsized() method to String'
15
15
  s.description =
data/lib/ellipsized.rb CHANGED
@@ -14,10 +14,26 @@ class String
14
14
  # maximum length. The original string is returned if it is already shorter than
15
15
  # or equal to the maximum length.
16
16
  #
17
- # @param [Integer] max The maximum length of the resulting string
18
- # @param [String] gap The string to use as a gap (default: '...')
19
- # @param [Symbol] align The alignment can be :left, :center, or :right
20
- # (default: :center)
17
+ # @param [Array] args The mix of possible arguments passed to the method:
18
+ # [Integer] max The maximum length of the resulting string (default: 64)
19
+ # [String] gap The string to use as a gap (default: '...')
20
+ # [Symbol] align The alignment can be :left, :center, or :right
21
+ # (default: :center)
22
+ # Since the three arguments have different types, we can distinguish them by
23
+ # type. Based on that, the following permutations can be defined, 3! = 6:
24
+ # s.ellipsized(10, '...', :right)
25
+ # s.ellipsized(10, :right, '...')
26
+ # s.ellipsized('...', 10, :right)
27
+ # s.ellipsized('...', :right, 10)
28
+ # s.ellipsized(:right, '...', 10)
29
+ # s.ellipsized(:right, 10, '...')
30
+ # Keep in mind, any argument may be omitted:
31
+ # s.ellipsized(10, '...', :right)
32
+ # s.ellipsized(10, :right)
33
+ # s.ellipsized('...')
34
+ # s.ellipsized(10)
35
+ # s.ellipsized(:right)
36
+ # All of the above are valid use cases.
21
37
  # @return [String] The truncated string with gap in the middle if necessary
22
38
  #
23
39
  # @example Basic usage with default parameters
@@ -41,13 +57,36 @@ class String
41
57
  # "Short".ellipsized # => "Short"
42
58
  # "xyz".ellipsized(0) # => ""
43
59
  # "xyz".ellipsized(2, '...') # => "xy"
44
- def ellipsized(max = 64, gap = '...', align = :center)
45
- validate_arguments(max, gap, align)
60
+ def ellipsized(*args)
61
+ raise "Unsupported number of arguments: #{args.length}" if args.length > 3
62
+
63
+ max = gap = align = nil
64
+ args.each do |arg|
65
+ raise "Unsupported argument type: #{arg}" unless [
66
+ Integer,
67
+ String,
68
+ Symbol
69
+ ].include?(arg.class)
70
+
71
+ case arg
72
+ when Integer
73
+ max = arg
74
+ raise "Max length (#{max}) is negative" if max.negative?
75
+ return '' if max.zero?
76
+ when String
77
+ gap = arg
78
+ when Symbol
79
+ align = arg
80
+ raise "Unsupported align: #{align}" unless
81
+ %i[left center right].include?(align) # rubocop:disable Performance/CollectionLiteralInLoop
82
+ end
83
+ end
84
+ max ||= 64
85
+ gap ||= '...'
86
+ align ||= :center
87
+
46
88
  return '' if empty?
47
89
  return self if length <= max
48
- return '' if max.zero?
49
-
50
- gap = gap.to_s
51
90
  return self[0..max - 1] if gap.length >= max
52
91
 
53
92
  case align
@@ -62,13 +101,4 @@ class String
62
101
  "#{self[0, max - gap.length]}#{gap}"
63
102
  end
64
103
  end
65
-
66
- private
67
-
68
- def validate_arguments(max, gap, align)
69
- raise "Max length must be an Integer, while #{max.class.name} provided" unless max.is_a?(Integer)
70
- raise "Max length (#{max}) is negative" if max.negative?
71
- raise "The gap doesn't implement to_s()" unless gap.respond_to?(:to_s)
72
- raise "Unsupported align: #{align}" unless %i[left center right].include?(align)
73
- end
74
104
  end
@@ -120,4 +120,18 @@ class TestEllipsized < Minitest::Test
120
120
  'This story is very long to fit into a small window'.ellipsized(20, '.. skip ..', :right)
121
121
  )
122
122
  end
123
+
124
+ def test_arguments_permutations
125
+ assert_equal('app...na', 'apple and banana'.ellipsized(8))
126
+ assert_equal('app...na', 'apple and banana'.ellipsized(8, '...'))
127
+ assert_equal('app...na', 'apple and banana'.ellipsized('...', 8))
128
+ assert_equal('app...na', 'apple and banana'.ellipsized(8, :center))
129
+ assert_equal('app...na', 'apple and banana'.ellipsized(:center, 8))
130
+ assert_equal('app...na', 'apple and banana'.ellipsized(8, '...', :center))
131
+ assert_equal('app...na', 'apple and banana'.ellipsized(8, :center, '...'))
132
+ assert_equal('app...na', 'apple and banana'.ellipsized('...', 8, :center))
133
+ assert_equal('app...na', 'apple and banana'.ellipsized('...', :center, 8))
134
+ assert_equal('app...na', 'apple and banana'.ellipsized(:center, 8, '...'))
135
+ assert_equal('app...na', 'apple and banana'.ellipsized(:center, '...', 8))
136
+ end
123
137
  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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko