array_include_methods 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37098f6edb2efccc49b1957ed3a1fab4ba24de1513c418d6b354c55639f7c21e
4
- data.tar.gz: 43017be01759fa4ad755f7c6eb137dc112579746c04925e7a2d27eacc068bc78
3
+ metadata.gz: cdce8ec8eb451f28c665c7dd6833190699abc55358dd2f488cfe594565ea2d54
4
+ data.tar.gz: bdd5b90776e4a606a161ec8b62fc5e9f1b6d7c3f03c12e77b8e7235907a5a7fb
5
5
  SHA512:
6
- metadata.gz: 443405d84171e9e1bbbb50542d62f7648c4291adc2b35a698b48cc7a9f821e182af43fd43e2a858c42bef31c6e805e4f6d47dfa2b06b20c81b2c480e95ed3eee
7
- data.tar.gz: 751bed4920602a17ac928f24d1d0f9b3ccc0f1f8c5c0d75dc29df580618198cb9b8985059a76d451effbfd1d62366d2e35f839f25e25908c5590fa6b93de505f
6
+ metadata.gz: 96e1cd8df937ded36f613d0fb97d1b1afc98ce44cc13900dfa7599ba0e6289b2119d4df51143145e6d177178fe40e4e72daa0395fad7dfa93e101518fb6740d6
7
+ data.tar.gz: 6d5b550e3acc6c8f2bea70553960a5e1bdf720d87b1603ad58c70bf8d5da53f383b07d10bdc68fd80d51fcdfbf2ae652426b1b3454b0101540c1499309e88e50
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.5.1
4
+
5
+ - `counts` returns a hash of counts of every element in the array, performed in linear time (running time of O(n)).
6
+ - `duplicates` returns a single occurrence of all elements that repeated in an array
7
+ - `array_difference_indices`/`array_difference_indexes` aliases for `array_diff_indices`/`array_diff_indexes`
8
+ - `array_diff`/`array_difference` (return elements of `array_diff_indexes`)
9
+ - `array_intersection` (returns elements of `array_intersection_indexes`)
10
+
3
11
  ## 1.5.0
4
12
 
5
13
  - Add RubyMotion compatibility
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2020 Andy Maleh
1
+ Copyright (c) 2020-2022 Andy Maleh
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # ArrayIncludeMethods 1.5.0 - Ruby Refinement
1
+ # ArrayIncludeMethods 1.5.1 - [Ruby Refinement](https://docs.ruby-lang.org/en/master/syntax/refinements_rdoc.html)
2
2
  [![Gem Version](https://badge.fury.io/rb/array_include_methods.svg)](http://badge.fury.io/rb/array_include_methods)
3
3
  [![Build Status](https://travis-ci.com/AndyObtiva/array_include_methods.svg?branch=master)](https://travis-ci.com/AndyObtiva/array_include_methods)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/AndyObtiva/array_include_methods/badge.svg?branch=master)](https://coveralls.io/github/AndyObtiva/array_include_methods?branch=master)
5
5
 
6
- `Array#include_all?`, `Array#include_any?`, `Array#include_array?`, `Array#array_index`, `Array#array_diff_indices`, and `Array#array_intersection_indices` methods missing from basic Ruby `Array` API.
6
+ `Array#include_all?`, `Array#include_any?`, `Array#include_array?`, `Array#array_index`, `Array#counts`, and `Array#duplicates` methods missing from basic Ruby `Array` API.
7
7
 
8
8
  ## Setup
9
9
 
@@ -12,7 +12,7 @@
12
12
  Include the following in Gemfile:
13
13
 
14
14
  ```ruby
15
- gem 'array_include_methods', '~> 1.5.0'
15
+ gem 'array_include_methods', '~> 1.5.1'
16
16
  ```
17
17
 
18
18
  Run:
@@ -26,7 +26,7 @@ bundle
26
26
  Run:
27
27
 
28
28
  ```
29
- gem install array_include_methods -v1.5.0
29
+ gem install array_include_methods -v1.5.1
30
30
  ```
31
31
 
32
32
  ## Usage
@@ -37,13 +37,13 @@ Add the following line to your application if you are not requiring all gems via
37
37
  require 'array_include_methods'
38
38
  ```
39
39
 
40
- To activate the `ArrayIncludeMethods` Ruby Refinement for the `Array` class, add the following line to every Ruby file that needs it:
40
+ To activate the `ArrayIncludeMethods` [Ruby Refinement](https://docs.ruby-lang.org/en/master/syntax/refinements_rdoc.html) for the `Array` class, add the following line to every Ruby file that needs it:
41
41
 
42
42
  ```ruby
43
43
  using ArrayIncludeMethods
44
44
  ```
45
45
 
46
- Now, you have `#include_all?` and `#include_any?` methods on `Array` objects.
46
+ Now, you have `#include_all?`, `#include_any?`, `#include_array?`, `#array_index`, `#array_diff_indices`, and `#array_intersection_indices` methods on `Array` objects.
47
47
 
48
48
  ## Examples
49
49
 
@@ -128,17 +128,43 @@ Returns indexes from `self` array for which elements do not match elements in `o
128
128
  [1, 2, 3, 4].array_diff_indexes(nil) # returns [0, 1, 2, 3]
129
129
  ```
130
130
 
131
+ ### `Array#counts`
132
+
133
+ Returns a hash of counts of every element in the array,
134
+ performed in linear time (running time of O(n)).
135
+
136
+ ```ruby
137
+ [1, 2, 3, 4].counts # returns {1=>1, 2=>1, 3=>1, 4=>1}
138
+ [1, :a, :a, :b, 'bee', 'see', true, true, nil, nil].counts # returns {1=>1, :a=>2, :b=>1, "bee"=>1, "see"=>1, true=>2, nil=>2}
139
+ [1, :a, :a, :b, 'bee', 'see', true, true, nil].counts # {1=>1, :a=>2, :b=>1, "bee"=>1, "see"=>1, true=>2, nil=>1}
140
+ [1, {a: 1}, :a, :a, :b, 'bee', 'see', true, true, {a: 1}, {a: 1}].counts # {1=>1, {:a=>1}=>3, :a=>2, :b=>1, "bee"=>1, "see"=>1, true=>2}
141
+ [].counts # returns {}
142
+ ```
143
+
144
+ ### `Array#duplicates`
145
+
146
+ Returns a single occurrence of all elements that repeated in an array,
147
+ performed in linear time (running time of O(n)).
148
+
149
+ ```ruby
150
+ [1, 2, 3, 4].duplicates # returns []
151
+ [1, :a, :a, :b, 'bee', 'see', true, true, nil, nil].duplicates # returns [:a, true, nil]
152
+ [1, :a, :a, :b, 'bee', 'see', true, true, nil].duplicates # returns [:a, true]
153
+ [1, {a: 1}, :a, :a, :b, 'bee', 'see', true, true, {a: 1}, {a: 1}].duplicates # returns [:a, true, {a: 1}]
154
+ [].duplicates # returns []
155
+ ```
156
+
131
157
  ## JRuby Compatibility
132
158
 
133
159
  This gem is 100% compatible with JRuby.
134
160
 
135
161
  ## Opal Compatibility
136
162
 
137
- This gem degrades gracefully to monkey-patching in [Opal Ruby](https://opalrb.com) and provides a `using` method shim so consumer code does not have to change if it used gems that rely on the Ruby refinement.
163
+ This gem degrades gracefully to monkey-patching in [Opal Ruby](https://opalrb.com) and provides a `using` method shim so consumer code does not have to change if it used gems that rely on the [Ruby refinement](https://docs.ruby-lang.org/en/master/syntax/refinements_rdoc.html).
138
164
 
139
165
  ## RubyMotion Compatibility
140
166
 
141
- This gem degrades gracefully to monkey-patching in [RubyMotion](http://www.rubymotion.com/) and provides a `using` method shim so consumer code does not have to change if it used gems that rely on the Ruby refinement.
167
+ This gem degrades gracefully to monkey-patching in [RubyMotion](http://www.rubymotion.com/) and provides a `using` method shim so consumer code does not have to change if it used gems that rely on the [Ruby refinement](https://docs.ruby-lang.org/en/master/syntax/refinements_rdoc.html).
142
168
 
143
169
  ## TODO
144
170
 
@@ -160,5 +186,6 @@ This gem degrades gracefully to monkey-patching in [RubyMotion](http://www.rubym
160
186
 
161
187
  ## Copyright
162
188
 
163
- Copyright (c) 2020 Andy Maleh. See LICENSE.txt for
164
- further details.
189
+ [MIT](LICENSE.txt)
190
+
191
+ Copyright (c) 2020-2022 Andy Maleh. See [LICENSE.txt](LICENSE.txt) for further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.0
1
+ 1.5.1
@@ -2,17 +2,17 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: array_include_methods 1.5.0 ruby lib
5
+ # stub: array_include_methods 1.5.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "array_include_methods".freeze
9
- s.version = "1.5.0"
9
+ s.version = "1.5.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Andy Maleh".freeze]
14
- s.date = "2022-06-11"
15
- s.description = "Array#include_all?, Array#include_any?, Array#include_array?, Array#array_index, Array#array_diff_indices, and Array#array_intersection_indices methods missing from basic Ruby Array API. Compatible with Ruby, JRuby, Opal, and RubyMotion.".freeze
14
+ s.date = "2023-02-17"
15
+ s.description = "Array#include_all?, Array#include_any?, Array#include_array?, Array#array_index, Array#counts, and Array#duplicates methods missing from basic Ruby Array API. Compatible with Ruby, JRuby, Opal, and RubyMotion.".freeze
16
16
  s.email = "andy.am@gmail.com".freeze
17
17
  s.extra_rdoc_files = [
18
18
  "CHANGELOG.md",
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.homepage = "http://github.com/AndyObtiva/array_include_methods".freeze
32
32
  s.licenses = ["MIT".freeze]
33
33
  s.rubygems_version = "3.3.3".freeze
34
- s.summary = "Array#include_all? & Array#include_any? methods missing from basic Ruby Array API".freeze
34
+ s.summary = "Array#include_all?, Array#include_any?, and other methods missing from basic Ruby Array API".freeze
35
35
 
36
36
  if s.respond_to? :specification_version then
37
37
  s.specification_version = 4
@@ -82,10 +82,37 @@ module ArrayIncludeMethods
82
82
  end
83
83
  alias array_intersection_indices array_intersection_indexes
84
84
 
85
+ def array_intersection(array)
86
+ array_intersection_indexes(array).map { |index| self[index] }
87
+ end
88
+
85
89
  def array_diff_indexes(array)
86
90
  array_intersection_and_diff_indexes(array)[:diff_indexes]
87
91
  end
88
92
  alias array_diff_indices array_diff_indexes
93
+ alias array_difference_indexes array_diff_indexes
94
+ alias array_difference_indices array_diff_indexes
95
+
96
+ def array_diff(array)
97
+ array_diff_indexes(array).map { |index| self[index] }
98
+ end
99
+ alias array_difference array_diff
100
+
101
+ # Returns a hash of counts of every element in the array,
102
+ # performed in linear time (running time of O(n))
103
+ def counts
104
+ inject({}) do |count_hash, element|
105
+ count_hash[element] ||= 0
106
+ count_hash[element] += 1
107
+ count_hash
108
+ end
109
+ end
110
+
111
+ # Returns a single occurrence of all elements that repeated in an array,
112
+ # performed in linear time (running time of O(n)).
113
+ def duplicates
114
+ counts.select { |element, count| count > 1 }.keys
115
+ end
89
116
 
90
117
  private
91
118
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_include_methods
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-11 00:00:00.000000000 Z
11
+ date: 2023-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -123,8 +123,8 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  description: Array#include_all?, Array#include_any?, Array#include_array?, Array#array_index,
126
- Array#array_diff_indices, and Array#array_intersection_indices methods missing from
127
- basic Ruby Array API. Compatible with Ruby, JRuby, Opal, and RubyMotion.
126
+ Array#counts, and Array#duplicates methods missing from basic Ruby Array API. Compatible
127
+ with Ruby, JRuby, Opal, and RubyMotion.
128
128
  email: andy.am@gmail.com
129
129
  executables: []
130
130
  extensions: []
@@ -162,6 +162,6 @@ requirements: []
162
162
  rubygems_version: 3.3.3
163
163
  signing_key:
164
164
  specification_version: 4
165
- summary: Array#include_all? & Array#include_any? methods missing from basic Ruby Array
166
- API
165
+ summary: Array#include_all?, Array#include_any?, and other methods missing from basic
166
+ Ruby Array API
167
167
  test_files: []