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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +10 -8
- data/joined.gemspec +1 -1
- data/lib/joined.rb +15 -8
- data/test/test_joined.rb +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf39163ca7677fe7c82b64f86a58ed1960ab37ff099a3d3530a98604d0db8d2f
|
4
|
+
data.tar.gz: cac313f36a472d3543f1f61b0d23e428484ebf26dcdf78a8ee8e7001527a17ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17ac3bfd0677cde3c1caf8e49b52c24addeaa3512053e6aa60b21a36b9c574b67df9834bdd5068159c78186bc3c3ab40b87d02cb7d9e6ff1074492bf3bbdf047
|
7
|
+
data.tar.gz: 978bbf113d2e52aa15dfc935c219dc2cca47fdc0cea9f84671f6083bc472b193c8085ea4251a0f58a79027c3348d26e23734ff725cfce799c0cdb712f77779ae
|
data/Gemfile.lock
CHANGED
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
|
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
|
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
|
-
|
44
|
-
|
45
|
-
|
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.
|
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
|
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
|
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
|
-
#
|
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
|