array_include_methods 1.0.4 → 1.4.0

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: 7ddeb62919efd84e6bfe45c278769d00ffc9d824a662d59c6f66a2d07af634ff
4
- data.tar.gz: bb74486e281c925702761a5a83ae47804f326ebbb05b09b6275ad0dee6999634
3
+ metadata.gz: 201b9e696a4644180df82e669767d5a83cd626dc9863ba7451d22707c53b20ba
4
+ data.tar.gz: b3920beaa33a19dedf9430eca869400cf763d0ddc0a28005550a7ea569c953e6
5
5
  SHA512:
6
- metadata.gz: ead0318fcb6aff83e48151fb1925c708af07430198c23366ce1415d7b4e1e58266484f4e76352c36480d1a59e6cc2469f255e0c3419e5a1ca485bf22aa45276d
7
- data.tar.gz: fa6628a52ceb617d01cf40b5a88f6ce8c45f88ce117e605ade63f077d08df49501938fb92c234d2156babada06e76af79183d7260cfc838b3386ec2e210d7848
6
+ metadata.gz: '00929ce43674a96779a5a08c43357fcd0e4708782586a732f9040f6981c70858a80971f101a61a22b1d6de3e0e7a36d8d585d318f66370f040535443ea3ac04a'
7
+ data.tar.gz: d71713c34a7adad5d900342273666c99388b0c5e2b8cff0f3a45071c07809664bc904fd0d68576d322361b0e29dd118ae22076d7272fc7cb57379b72a43ae270
data/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.4.0
4
+
5
+ - `array_diff_indexes(other_array)` (alias: `array_diff_indices`)
6
+ - `array_intersection_indexes(other_array)` (alias: `array_intersection_indices`)
7
+
8
+ ## 1.3.0
9
+
10
+ - [API Breaking] Separate between operations `include_any?(*array)` and `include_any?(array)` to avoid confusion (remove support for the latter as it is not necessary)
11
+ - [API Breaking] Separate between operations `include_all?(*array)` and `include_all?(array)` to avoid confusion (rename the latter to `include_array?(array)`)
12
+ - `Array#include_all?(*other_array, same_sort: true)` accepts `same_sort` option (default: `false`)
13
+
14
+ ## 1.2.0
15
+
16
+ - Add `Array#array_index(array)` method to determine start array index of other array
17
+
18
+ ## 1.1.0
19
+
20
+ - Perform contiguous-element `include_all?([...])` check against an array argument (not splatted)
21
+ - Perform sorted `include_all?([...])` check against an array argument (not splatted)
22
+ - Perform non-repetition `include_all?([...])` check against an array argument (not splatted)
23
+
3
24
  ## 1.0.4
4
25
 
5
26
  - Support splat args (e.g. `include_any?(1, 2, 3)` instead of `include_any?([1, 2, 3])`)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # ArrayIncludeMethods 1.0.4 - Ruby Refinement
1
+ # ArrayIncludeMethods 1.4.0 - Ruby Refinement
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)
@@ -12,7 +12,7 @@
12
12
  Include the following in Gemfile:
13
13
 
14
14
  ```ruby
15
- gem 'array_include_methods', '~> 1.0.4'
15
+ gem 'array_include_methods', '~> 1.4.0'
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.0.4
29
+ gem install array_include_methods -v1.4.0
30
30
  ```
31
31
 
32
32
  ## Usage
@@ -47,31 +47,90 @@ Now, you have `#include_all?` and `#include_any?` methods on `Array` objects.
47
47
 
48
48
  ## Examples
49
49
 
50
- ### `Array#include_all?(other_array)`
50
+ ### `Array#include_any?(*other_array)`
51
51
 
52
52
  ```ruby
53
- [1, 2, 3, 4].include_all?([2, 4]) # returns true
53
+ [1, 2, 3, 4].include_any?(2, 4, 5) # returns true
54
+ [1, 2, 3, 4].include_any?(6, 7) # returns false
55
+ [1, 2, 3, 4].include_any?() # returns true
56
+ [1, 2, 3, 4].include_any?(nil) # returns false
57
+ ```
58
+
59
+ ### `Array#include_all?(*other_array)`
60
+
61
+ ```ruby
62
+ [1, 2, 3, 4].include_all?(2, 3) # returns true
54
63
  [1, 2, 3, 4].include_all?(2, 4) # returns true
55
- [1, 2, 3, 4].include_all?([2, 4, 5]) # returns false
64
+ [1, 2, 3, 4].include_all?(4, 2) # returns true
65
+ [1, 2, 3, 4].include_all?(4, 2, same_sort: true) # returns false
66
+ [1, 2, 3, 4].include_all?(2, 4, 4) # returns true
56
67
  [1, 2, 3, 4].include_all?(2, 4, 5) # returns false
57
- [1, 2, 3, 4].include_all?([]) # returns true
68
+ [1, 2, 3, 4].include_all?() # returns true
58
69
  [1, 2, 3, 4].include_all?(nil) # returns false
59
70
  ```
60
71
 
61
- ### `Array#include_any?(other_array)`
72
+ ### `Array#include_array?(other_array)`
62
73
 
63
74
  ```ruby
64
- [1, 2, 3, 4].include_any?([2, 4, 5]) # returns true
65
- [1, 2, 3, 4].include_any?(2, 4, 5) # returns true
66
- [1, 2, 3, 4].include_any?([6, 7]) # returns false
67
- [1, 2, 3, 4].include_any?(6, 7) # returns false
68
- [1, 2, 3, 4].include_any?([]) # returns true
69
- [1, 2, 3, 4].include_any?(nil) # returns false
75
+ [1, 2, 3, 4].include_array?([2, 3]) # returns true
76
+ [1, 2, 3, 4].include_array?([2, 4]) # returns false
77
+ [1, 2, 3, 4].include_array?([4, 2]) # returns false
78
+ [1, 2, 3, 4].include_array?([2, 4, 4]) # returns false
79
+ [1, 2, 3, 4].include_array?([2, 4, 5]) # returns false
80
+ [1, 2, 3, 4].include_array?([]) # returns true
81
+ [1, 2, 3, 4].include_array?([nil]) # returns false
82
+ ```
83
+
84
+ ### `Array#array_index(other_array)`
85
+
86
+ Returns first array index of `other_array` in `first_array` assuming `first_array.include_all?(other_array)` returns true
87
+
88
+ ```ruby
89
+ [1, 2, 3, 4].array_index([2, 3, 4]) # returns 1
90
+ [1, 2, 3, 4].array_index([2, 3]) # returns 1
91
+ [1, 2, 3, 4].array_index([3, 4]) # returns 2
92
+ [1, 2, 3, 4].array_index([2, 4]) # returns -1
93
+ [1, 2, 3, 4].array_index([4, 2]) # returns -1
94
+ [1, 2, 3, 4].array_index([2, 4, 5]) # returns -1
95
+ [1, 2, 3, 4].array_index([]) # returns -1
96
+ [1, 2, 3, 4].array_index(nil) # returns -1
97
+ ```
98
+
99
+ ### `Array#array_intersection_indexes(other_array)`
100
+ (alias: `Array#array_intersection_indices(other_array)`)
101
+
102
+ Returns indexes from `self` array for which elements match elements in `other_array` assuming same sort
103
+
104
+ ```ruby
105
+ [1, 2, 3, 4].array_intersection_indexes([2, 3, 4]) # returns [1, 2, 3]
106
+ [1, 2, 3, 4].array_intersection_indexes([2, 3]) # returns [1, 2]
107
+ [1, 2, 3, 4].array_intersection_indexes([3, 4]) # returns [2, 3]
108
+ [1, 2, 3, 4].array_intersection_indexes([2, 4]) # returns [1, 3]
109
+ [1, 2, 3, 4].array_intersection_indexes([4, 2]) # returns [3]
110
+ [1, 2, 3, 4].array_intersection_indexes([2, 4, 5]) # returns [1, 3]
111
+ [1, 2, 3, 4].array_intersection_indexes([]) # returns []
112
+ [1, 2, 3, 4].array_intersection_indexes(nil) # returns []
113
+ ```
114
+
115
+ ### `Array#array_diff_indexes(other_array)`
116
+ (alias: `Array#array_diff_indices(other_array)`)
117
+
118
+ Returns indexes from `self` array for which elements do not match elements in `other_array` assuming same sort
119
+
120
+ ```ruby
121
+ [1, 2, 3, 4].array_diff_indexes([2, 3, 4]) # returns [0]
122
+ [1, 2, 3, 4].array_diff_indexes([2, 3]) # returns [0, 3]
123
+ [1, 2, 3, 4].array_diff_indexes([3, 4]) # returns [0, 1]
124
+ [1, 2, 3, 4].array_diff_indexes([2, 4]) # returns [0, 2]
125
+ [1, 2, 3, 4].array_diff_indexes([4, 2]) # returns [0, 1, 2]
126
+ [1, 2, 3, 4].array_diff_indexes([2, 4, 5]) # returns [0, 2]
127
+ [1, 2, 3, 4].array_diff_indexes([]) # returns [0, 1, 2, 3]
128
+ [1, 2, 3, 4].array_diff_indexes(nil) # returns [0, 1, 2, 3]
70
129
  ```
71
130
 
72
131
  ## Opal Compatibility
73
132
 
74
- This gem degrades gracefully to monkey-patching in Opal Ruby and provides a `using` method shim so consumer code does not have to change if it used gems that rely on the Ruby refinement
133
+ 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
75
134
 
76
135
  ## TODO
77
136
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.4
1
+ 1.4.0
@@ -2,16 +2,16 @@
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.0.4 ruby lib
5
+ # stub: array_include_methods 1.4.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "array_include_methods".freeze
9
- s.version = "1.0.4"
9
+ s.version = "1.4.0"
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 = "2020-11-15"
14
+ s.date = "2021-09-26"
15
15
  s.description = "Array#include_all? & Array#include_any? methods missing from basic Ruby Array API".freeze
16
16
  s.email = "andy.am@gmail.com".freeze
17
17
  s.extra_rdoc_files = [
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
29
29
  ]
30
30
  s.homepage = "http://github.com/AndyObtiva/array_include_methods".freeze
31
31
  s.licenses = ["MIT".freeze]
32
- s.rubygems_version = "3.1.4".freeze
32
+ s.rubygems_version = "3.2.22".freeze
33
33
  s.summary = "Array#include_all? & Array#include_any? methods missing from basic Ruby Array API".freeze
34
34
 
35
35
  if s.respond_to? :specification_version then
@@ -43,6 +43,8 @@ Gem::Specification.new do |s|
43
43
  s.add_development_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
44
44
  s.add_development_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
45
45
  s.add_development_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
46
+ s.add_development_dependency(%q<puts_debuggerer>.freeze, ["> 0"])
47
+ s.add_development_dependency(%q<rake-tui>.freeze, ["> 0"])
46
48
  else
47
49
  s.add_dependency(%q<rspec>.freeze, ["~> 3.5.0"])
48
50
  s.add_dependency(%q<rdoc>.freeze, ["~> 3.12"])
@@ -50,6 +52,8 @@ Gem::Specification.new do |s|
50
52
  s.add_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
51
53
  s.add_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
52
54
  s.add_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
55
+ s.add_dependency(%q<puts_debuggerer>.freeze, ["> 0"])
56
+ s.add_dependency(%q<rake-tui>.freeze, ["> 0"])
53
57
  end
54
58
  end
55
59
 
@@ -8,36 +8,94 @@ module ArrayIncludeMethods
8
8
  # Returns `true` if all of the given `array` elements are present in `self`,
9
9
  # otherwise returns `false`
10
10
  # Always returns `true` if the given `array` is empty
11
+ # `same_sort` option indicates that array must have the same sort as `self`. By default, it is `false`
12
+ def include_all?(*array, same_sort: false)
13
+ return false if array.size > self.size
14
+ self_copy = self.dup
15
+ array_copy = array.dup
16
+ book_keeping_array_copy = array.dup
17
+ self_element_index = last_element_index = -1
18
+ array_copy.each do |element|
19
+ if self_copy.include?(element)
20
+ last_element_index = self_element_index
21
+ self_element_index = self_copy.index(element)
22
+ return false if same_sort && self_element_index < last_element_index
23
+ self_copy.delete_at(self_element_index)
24
+ book_keeping_array_copy.delete(element)
25
+ else
26
+ return false
27
+ end
28
+ end
29
+ book_keeping_array_copy.empty?
30
+ end
31
+
32
+ # Returns `true` if the given `array` is present in `self` (in the same element order without repetition)
33
+ # Always returns `true` if the given `array` is empty
11
34
  # Always returns `false` if the given `array` is nil
12
- def include_all?(*array)
13
- array = array[0] if array.size == 1 && array[0].is_a?(Array)
35
+ def include_array?(array)
14
36
  return false if array.nil?
15
- array_include_other_array_same_class_elements = lambda do |a1, a2|
16
- begin
17
- (a1 & a2).uniq.sort == a2.uniq.sort
18
- rescue ArgumentError => e
19
- a2.uniq.reduce(true) { |result, element| result && a1.include?(element) }
37
+ if array.size > self.size
38
+ false
39
+ else
40
+ size_diff = self.size - array.size
41
+ (size_diff + 1).times.any? do |start_index|
42
+ self[start_index, array.size] == array
20
43
  end
21
44
  end
22
- self_grouped_by = self.group_by(&:class)
23
- array_grouped_by = array.group_by(&:class)
24
- return false unless array_include_other_array_same_class_elements.call(self_grouped_by.keys.map(&:to_s), array_grouped_by.keys.map(&:to_s))
25
- array_grouped_by.reduce(true) do |result, pair|
26
- array_class = pair.first
27
- array_elements = pair.last
28
- self_grouped_by[array_class]
29
- result && array_include_other_array_same_class_elements.call(self_grouped_by[array_class], array_elements)
30
- end
31
45
  end
32
46
 
33
47
  # Returns `true` if any of the given `array` elements are present in `self`,
34
48
  # otherwise returns `false`
35
49
  # Always returns `true` if the given `array` is empty
36
- # Always returns `false` if the given `array` is nil
37
50
  def include_any?(*array)
38
- array = array[0] if array.size == 1 && array[0].is_a?(Array)
39
51
  !array.nil? && (array.empty? || !(self & array).empty?)
40
52
  end
53
+
54
+ def array_index(array)
55
+ result_array_index = -1
56
+ return result_array_index if array.nil?
57
+ if array.size <= self.size
58
+ size_diff = self.size - array.size
59
+ current_array_index = nil
60
+ result = (size_diff + 1).times.any? do |start_index|
61
+ current_array_index = start_index
62
+ self[start_index, array.size] == array
63
+ end
64
+ result_array_index = current_array_index if result
65
+ end
66
+ result_array_index
67
+ end
68
+
69
+ def array_intersection_indexes(array)
70
+ array_intersection_and_diff_indexes(array)[:intersection_indexes]
71
+ end
72
+ alias array_intersection_indices array_intersection_indexes
73
+
74
+ def array_diff_indexes(array)
75
+ array_intersection_and_diff_indexes(array)[:diff_indexes]
76
+ end
77
+ alias array_diff_indices array_diff_indexes
78
+
79
+ private
80
+
81
+ def array_intersection_and_diff_indexes(array)
82
+ return {intersection_indexes: [], diff_indexes: self.size.times.to_a} if array.nil?
83
+ intersection_indexes = []
84
+ diff_indexes = []
85
+ array_current_index = 0
86
+ each_with_index do |element, index|
87
+ if element == array[array_current_index]
88
+ intersection_indexes << index
89
+ array_current_index += 1
90
+ else
91
+ diff_indexes << index
92
+ end
93
+ end
94
+ {
95
+ intersection_indexes: intersection_indexes,
96
+ diff_indexes: diff_indexes
97
+ }
98
+ end
41
99
  end
42
100
  end
43
101
  if RUBY_PLATFORM == 'opal'
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.0.4
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-15 00:00:00.000000000 Z
11
+ date: 2021-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -94,6 +94,34 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: 0.7.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: puts_debuggerer
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake-tui
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">"
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">"
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  description: Array#include_all? & Array#include_any? methods missing from basic Ruby
98
126
  Array API
99
127
  email: andy.am@gmail.com
@@ -129,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
157
  - !ruby/object:Gem::Version
130
158
  version: '0'
131
159
  requirements: []
132
- rubygems_version: 3.1.4
160
+ rubygems_version: 3.2.22
133
161
  signing_key:
134
162
  specification_version: 4
135
163
  summary: Array#include_all? & Array#include_any? methods missing from basic Ruby Array