joined 0.1.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/.github/workflows/typos.yml +19 -0
- data/.gitignore +3 -2
- data/.rubocop.yml +1 -0
- data/Gemfile.lock +3 -3
- data/README.md +19 -0
- data/REUSE.toml +8 -7
- data/joined.gemspec +1 -1
- data/lib/joined.rb +23 -4
- data/test/test_joined.rb +43 -0
- metadata +2 -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
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2025 Yegor Bugayenko
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
---
|
4
|
+
# yamllint disable rule:line-length
|
5
|
+
name: typos
|
6
|
+
'on':
|
7
|
+
push:
|
8
|
+
branches:
|
9
|
+
- master
|
10
|
+
pull_request:
|
11
|
+
branches:
|
12
|
+
- master
|
13
|
+
jobs:
|
14
|
+
typos:
|
15
|
+
timeout-minutes: 15
|
16
|
+
runs-on: ubuntu-24.04
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v4
|
19
|
+
- uses: crate-ci/typos@v1.32.0
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -10,8 +10,8 @@ GEM
|
|
10
10
|
ast (2.4.3)
|
11
11
|
builder (3.3.0)
|
12
12
|
docile (1.4.1)
|
13
|
-
json (2.
|
14
|
-
language_server-protocol (3.17.0.
|
13
|
+
json (2.12.0)
|
14
|
+
language_server-protocol (3.17.0.5)
|
15
15
|
lint_roller (1.1.0)
|
16
16
|
minitest (5.25.5)
|
17
17
|
minitest-reporters (1.7.1)
|
@@ -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.
|
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
@@ -27,6 +27,25 @@ orange, banana, and pear
|
|
27
27
|
|
28
28
|
That's it.
|
29
29
|
|
30
|
+
## Options
|
31
|
+
|
32
|
+
The `joined` method supports the following parameters:
|
33
|
+
|
34
|
+
* `words_connector` (String) (defaults to: ', ') -
|
35
|
+
the string used to join all but the last element of the list.
|
36
|
+
* `last_word_connector` (String) (defaults to: ', and ') -
|
37
|
+
the string used to join the last element of the list.
|
38
|
+
* `oxford` (Boolean) (defaults to: true) -
|
39
|
+
should we place a comma before the `last_word_connector`?
|
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.
|
48
|
+
|
30
49
|
## How to contribute
|
31
50
|
|
32
51
|
Read
|
data/REUSE.toml
CHANGED
@@ -4,9 +4,17 @@
|
|
4
4
|
version = 1
|
5
5
|
[[annotations]]
|
6
6
|
path = [
|
7
|
+
".DS_Store",
|
8
|
+
".gitattributes",
|
9
|
+
".gitignore",
|
10
|
+
".pdd",
|
7
11
|
"**.json",
|
8
12
|
"**.md",
|
13
|
+
"**.png",
|
9
14
|
"**.txt",
|
15
|
+
"**/.DS_Store",
|
16
|
+
"**/.gitignore",
|
17
|
+
"**/.pdd",
|
10
18
|
"**/*.csv",
|
11
19
|
"**/*.jpg",
|
12
20
|
"**/*.json",
|
@@ -16,15 +24,8 @@ path = [
|
|
16
24
|
"**/*.svg",
|
17
25
|
"**/*.txt",
|
18
26
|
"**/*.vm",
|
19
|
-
"**/.DS_Store",
|
20
|
-
"**/.gitignore",
|
21
|
-
"**/.pdd",
|
22
27
|
"**/CNAME",
|
23
28
|
"**/Gemfile.lock",
|
24
|
-
".DS_Store",
|
25
|
-
".gitattributes",
|
26
|
-
".gitignore",
|
27
|
-
".pdd",
|
28
29
|
"Gemfile.lock",
|
29
30
|
"README.md",
|
30
31
|
"renovate.json",
|
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
@@ -12,12 +12,31 @@ class Array
|
|
12
12
|
# Join strings into a single line, splitting them with comma
|
13
13
|
# and placing "AND" between the last two items.
|
14
14
|
#
|
15
|
-
# @param [
|
16
|
-
#
|
17
|
-
|
15
|
+
# @param [String] words_connector
|
16
|
+
# The string used to join all but the last element of the list
|
17
|
+
# @param [String] last_word_connector
|
18
|
+
# The string used to join the last element of the list.
|
19
|
+
# @param [Boolean] oxford
|
20
|
+
# Should we place a comma before 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
|
29
|
+
# @return [String] The text generated (with items joined)
|
30
|
+
def joined(oxford: true, words_connector: ', ', last_word_connector: ', and ', comma_before: false)
|
18
31
|
return '' if empty?
|
19
32
|
return first if length == 1
|
20
33
|
|
21
|
-
|
34
|
+
final_connector = (last_word_connector || '').dup
|
35
|
+
final_connector.sub!(/^,/, '') unless oxford && length > 2
|
36
|
+
|
37
|
+
result = "#{self[0...-1].join(words_connector)}#{final_connector}#{self[-1]}"
|
38
|
+
return result.gsub(/"([^"]+)"\s*,/, '"\1,"') if comma_before
|
39
|
+
|
40
|
+
result
|
22
41
|
end
|
23
42
|
end
|
data/test/test_joined.rb
CHANGED
@@ -25,4 +25,47 @@ class Testjoined < Minitest::Test
|
|
25
25
|
assert_equal('apple, banana and orange', %w[apple banana orange].joined(oxford: false))
|
26
26
|
assert_equal('apple, banana, orange and pear', %w[apple banana orange pear].joined(oxford: false))
|
27
27
|
end
|
28
|
+
|
29
|
+
def test_with_words_connector
|
30
|
+
assert_equal('one two, and three', %w[one two three].joined(words_connector: ' '))
|
31
|
+
assert_equal('one & two, and three', %w[one two three].joined(words_connector: ' & '))
|
32
|
+
assert_equal('onetwo, and three', %w[one two three].joined(words_connector: nil))
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_with_last_word_connector
|
36
|
+
assert_equal 'one, two, and also three', %w[one two three].joined(last_word_connector: ', and also ')
|
37
|
+
assert_equal 'one, two and also three', %w[one two three].joined(last_word_connector: ' and also ')
|
38
|
+
assert_equal 'one, twothree', %w[one two three].joined(last_word_connector: nil)
|
39
|
+
assert_equal 'one, two three', %w[one two three].joined(last_word_connector: ' ')
|
40
|
+
assert_equal 'one, two and three', %w[one two three].joined(last_word_connector: ' and ')
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_with_last_word_connector_but_without_oxford_comma
|
44
|
+
assert_equal 'one, two and also three', %w[one two three].joined(oxford: false, last_word_connector: ', and also ')
|
45
|
+
assert_equal 'one, two and also three', %w[one two three].joined(oxford: false, last_word_connector: ' and also ')
|
46
|
+
assert_equal 'one, twothree', %w[one two three].joined(oxford: false, last_word_connector: nil)
|
47
|
+
assert_equal 'one, two three', %w[one two three].joined(oxford: false, last_word_connector: ' ')
|
48
|
+
assert_equal 'one, two and three', %w[one two three].joined(oxford: false, last_word_connector: ' and ')
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_two_elements
|
52
|
+
assert_equal 'one and two', %w[one two].joined
|
53
|
+
assert_equal 'one two', %w[one two].joined(last_word_connector: ' ')
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_one_element
|
57
|
+
assert_equal 'one', ['one'].joined
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_with_invalid_options
|
61
|
+
exception = assert_raises ArgumentError do
|
62
|
+
%w[one two].joined(passing: 'invalid option')
|
63
|
+
end
|
64
|
+
|
65
|
+
assert_equal 'unknown keyword: :passing', exception.message
|
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
|
28
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- ".github/workflows/pdd.yml"
|
30
30
|
- ".github/workflows/rake.yml"
|
31
31
|
- ".github/workflows/reuse.yml"
|
32
|
+
- ".github/workflows/typos.yml"
|
32
33
|
- ".github/workflows/xcop.yml"
|
33
34
|
- ".github/workflows/yamllint.yml"
|
34
35
|
- ".gitignore"
|