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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 508cc1389e031ef74fd7e700066a4b7642d10db2754acc384670fce2277ea68d
4
- data.tar.gz: c69ddae19a6ddaaeb349111a5cb076f3fb75466584d98953c0c93d47d6247564
3
+ metadata.gz: bf39163ca7677fe7c82b64f86a58ed1960ab37ff099a3d3530a98604d0db8d2f
4
+ data.tar.gz: cac313f36a472d3543f1f61b0d23e428484ebf26dcdf78a8ee8e7001527a17ce
5
5
  SHA512:
6
- metadata.gz: a34b32456b37ec9fb5fcba35b64a28153b4eb76bf89e8afadec977df8f7ad9d1dbf9b4aee90b9df01d915734531355e5ed41ea5970a5cd9e70b51bd488216d06
7
- data.tar.gz: c5b39a60e832cdee4b2a1e02a16b31501bbdf0718c5fd04fef4e1b9243054e713d197203051476dc6387c5d9492d492ed8e2c3279d3d697d9b46e087fb6f41c2
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
@@ -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.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.1.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 [Boolean] oxford Should we place a comma before AND?
16
- # @return [String] The text generated
17
- def joined(oxford: true)
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
- "#{self[0...-1].join(', ')}#{',' if length > 2 && oxford} and #{self[-1]}"
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.1.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"