joined 0.0.1 → 0.2.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: db52755ef87faa3e9cad4394811ca4305108bceeb3b3456852c49a7f13af87cb
4
- data.tar.gz: 789ca4420f0a0d461306c95478efda9665a3617ea8ba41d47beb147c6b5868e4
3
+ metadata.gz: 7dd36033f9acabd1327c4bbd212c9fd6c76fe3f525de7d0f7f64431ac3c97205
4
+ data.tar.gz: 9de8199c4b2b5b1d88b82f6864e839b7aba336500ea8638af2768d2e57480513
5
5
  SHA512:
6
- metadata.gz: 3c2049dde1dc54630b03c71b2456034cc19cd3cc819ff11f88ceaa29f0015e77951ed9ceea4a5be7353f342f1583ba00d5896f13e2fb0b1d8c17af59c2b75533
7
- data.tar.gz: 8caec01188aa49976f89ea3afbfbdcae7a7945d695711b44e1a7b3126c37a5a810898784cf6f4e421346afeb281e723752dafab01d4977d584b9281216102594
6
+ metadata.gz: e98eca82e38a6a82284a17b33556d84f06aec3c7954b6e4d2a59a514352c76512933628be44e171e3e47be4870c4303e10f528daeb2dce0e197c23bf3d4fe6d3
7
+ data.tar.gz: 38298fde467ae58616c81b8daea4a9f31989a768010965613c0acb51c28f12737fd789b8388d4ee90fb75d22dba5e35c04a83c09570c3d5d2c55a3e858a3bc77
@@ -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
@@ -1,8 +1,9 @@
1
- *.gem
2
- .DS_Store
3
1
  .bundle/
2
+ .DS_Store
4
3
  .idea/
5
4
  .yardoc/
5
+ *.gem
6
+ .ruby-*
6
7
  coverage/
7
8
  doc/
8
9
  node_modules/
data/.rubocop.yml CHANGED
@@ -18,3 +18,4 @@ Layout/EndOfLine:
18
18
  EnforcedStyle: lf
19
19
  Style/EvalWithLocation:
20
20
  Enabled: false
21
+ require: []
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.11.3)
14
- language_server-protocol (3.17.0.4)
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.4)
32
+ rubocop (1.75.5)
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
@@ -11,7 +11,7 @@
11
11
  [![Hits-of-Code](https://hitsofcode.com/github/yegor256/joined)](https://hitsofcode.com/view/github/yegor256/joined)
12
12
  [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/joined/blob/master/LICENSE.txt)
13
13
 
14
- Maybe you know [`to_sentence`][to_sentence] from Rails.
14
+ Do you know [`to_sentence`][to_sentence] from Rails?
15
15
  This gem does exactly the same, but without Rails.
16
16
 
17
17
  ```ruby
@@ -27,6 +27,23 @@ 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 sign or word used to join all but the last element
36
+ in arrays with three or more elements.
37
+ * `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.
40
+ * `oxford` (Boolean) (defaults to: true) -
41
+ 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.
46
+
30
47
  ## How to contribute
31
48
 
32
49
  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.0.1'
12
+ s.version = '0.2.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
@@ -9,10 +9,27 @@
9
9
  # Copyright:: Copyright (c) 2025 Yegor Bugayenko
10
10
  # License:: MIT
11
11
  class Array
12
- def joined
12
+ # Join strings into a single line, splitting them with comma
13
+ # and placing "AND" between the last two items.
14
+ #
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.
18
+ # @param [String] last_word_connector
19
+ # The sign or word used to join the last element in arrays
20
+ # with three or more element.
21
+ # @param [Boolean] oxford
22
+ # 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.
25
+ # @return [String] The text generated (with items joined)
26
+ def joined(oxford: true, words_connector: ', ', last_word_connector: ', and ')
13
27
  return '' if empty?
14
28
  return first if length == 1
15
29
 
16
- "#{self[0...-1].join(', ')}#{',' if length > 2} and #{self[-1]}"
30
+ final_connector = (last_word_connector || '').dup
31
+ final_connector.sub!(/^,/, '') unless oxford && length > 2
32
+
33
+ "#{self[0...-1].join(words_connector)}#{final_connector}#{self[-1]}"
17
34
  end
18
35
  end
data/test/test_joined.rb CHANGED
@@ -17,4 +17,51 @@ class Testjoined < Minitest::Test
17
17
  assert_equal('apple, banana, and orange', %w[apple banana orange].joined)
18
18
  assert_equal('apple, banana, orange, and pear', %w[apple banana orange pear].joined)
19
19
  end
20
+
21
+ def test_without_oxford_comma
22
+ assert_equal('', [].joined(oxford: false))
23
+ assert_equal('apple', ['apple'].joined(oxford: false))
24
+ assert_equal('apple and banana', %w[apple banana].joined(oxford: false))
25
+ assert_equal('apple, banana and orange', %w[apple banana orange].joined(oxford: false))
26
+ assert_equal('apple, banana, orange and pear', %w[apple banana orange pear].joined(oxford: false))
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
20
67
  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.0.1
4
+ version: 0.2.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"