joined 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: 7dd36033f9acabd1327c4bbd212c9fd6c76fe3f525de7d0f7f64431ac3c97205
4
- data.tar.gz: 9de8199c4b2b5b1d88b82f6864e839b7aba336500ea8638af2768d2e57480513
3
+ metadata.gz: bf39163ca7677fe7c82b64f86a58ed1960ab37ff099a3d3530a98604d0db8d2f
4
+ data.tar.gz: cac313f36a472d3543f1f61b0d23e428484ebf26dcdf78a8ee8e7001527a17ce
5
5
  SHA512:
6
- metadata.gz: e98eca82e38a6a82284a17b33556d84f06aec3c7954b6e4d2a59a514352c76512933628be44e171e3e47be4870c4303e10f528daeb2dce0e197c23bf3d4fe6d3
7
- data.tar.gz: 38298fde467ae58616c81b8daea4a9f31989a768010965613c0acb51c28f12737fd789b8388d4ee90fb75d22dba5e35c04a83c09570c3d5d2c55a3e858a3bc77
6
+ metadata.gz: 17ac3bfd0677cde3c1caf8e49b52c24addeaa3512053e6aa60b21a36b9c574b67df9834bdd5068159c78186bc3c3ab40b87d02cb7d9e6ff1074492bf3bbdf047
7
+ data.tar.gz: 978bbf113d2e52aa15dfc935c219dc2cca47fdc0cea9f84671f6083bc472b193c8085ea4251a0f58a79027c3348d26e23734ff725cfce799c0cdb712f77779ae
data/Gemfile.lock CHANGED
@@ -29,7 +29,7 @@ GEM
29
29
  rake (13.2.1)
30
30
  regexp_parser (2.10.0)
31
31
  rexml (3.4.1)
32
- rubocop (1.75.5)
32
+ rubocop (1.75.6)
33
33
  json (~> 2.3)
34
34
  language_server-protocol (~> 3.17.0.2)
35
35
  lint_roller (~> 1.1.0)
data/README.md CHANGED
@@ -32,17 +32,19 @@ That's it.
32
32
  The `joined` method supports the following parameters:
33
33
 
34
34
  * `words_connector` (String) (defaults to: ', ') -
35
- the sign or word used to join all but the last element
36
- in arrays with three or more elements.
35
+ the string used to join all but the last element of the list.
37
36
  * `last_word_connector` (String) (defaults to: ', and ') -
38
- the sign or word used to join the last element in arrays
39
- with three or more element.
37
+ the string used to join the last element of the list.
40
38
  * `oxford` (Boolean) (defaults to: true) -
41
39
  should we place a comma before the `last_word_connector`?
42
- If false, it will remove a leading comma from the `last_word_connector`,
43
- however, it does not add a comma if one is not already specified in the `last_word_connector`.
44
-
45
- See the [Yard docs](https://rubydoc.info/github/yegor256/joined/master/frames) for full gem documentation.
40
+ If false, it will remove a leading comma from the `last_word_connector`.
41
+ If true, it will preserve the leading comma specified
42
+ in the `last_word_connector`, but it will not insert one
43
+ if not already present.
44
+
45
+ See the
46
+ [Yard docs](https://rubydoc.info/github/yegor256/joined/master/frames)
47
+ for full gem documentation.
46
48
 
47
49
  ## How to contribute
48
50
 
data/joined.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 = 'joined'
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 .joined() method to Array'
15
15
  s.description =
data/lib/joined.rb CHANGED
@@ -13,23 +13,30 @@ class Array
13
13
  # and placing "AND" between the last two items.
14
14
  #
15
15
  # @param [String] words_connector
16
- # The sign or word used to join all but the last element
17
- # in arrays with three or more elements.
16
+ # The string used to join all but the last element of the list
18
17
  # @param [String] last_word_connector
19
- # The sign or word used to join the last element in arrays
20
- # with three or more element.
18
+ # The string used to join the last element of the list.
21
19
  # @param [Boolean] oxford
22
20
  # Should we place a comma before the :last_word_connector?
23
- # If false, it will remove a leading comma from the :last_word_connector,
24
- # however it does not add a comma if one is not already specified in the :last_word_connector.
21
+ # If false, it will remove a leading comma from the :last_word_connector.
22
+ # If true, it will preserve the leading comma specified
23
+ # in the :last_word_connector, but it will not insert one
24
+ # if not already present.
25
+ # @param [Boolean] comma_before
26
+ # Should we move comma before the quotes symbol
27
+ # If false, it will do nothing
28
+ # If true, it will move all commas before the quotes
25
29
  # @return [String] The text generated (with items joined)
26
- def joined(oxford: true, words_connector: ', ', last_word_connector: ', and ')
30
+ def joined(oxford: true, words_connector: ', ', last_word_connector: ', and ', comma_before: false)
27
31
  return '' if empty?
28
32
  return first if length == 1
29
33
 
30
34
  final_connector = (last_word_connector || '').dup
31
35
  final_connector.sub!(/^,/, '') unless oxford && length > 2
32
36
 
33
- "#{self[0...-1].join(words_connector)}#{final_connector}#{self[-1]}"
37
+ result = "#{self[0...-1].join(words_connector)}#{final_connector}#{self[-1]}"
38
+ return result.gsub(/"([^"]+)"\s*,/, '"\1,"') if comma_before
39
+
40
+ result
34
41
  end
35
42
  end
data/test/test_joined.rb CHANGED
@@ -64,4 +64,8 @@ class Testjoined < Minitest::Test
64
64
 
65
65
  assert_equal 'unknown keyword: :passing', exception.message
66
66
  end
67
+
68
+ def test_with_comma_before
69
+ assert_equal '"one," "two," and "three"', %w[one two three].map { |f| "\"#{f}\"" }.joined(comma_before: true)
70
+ end
67
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joined
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