array_include_methods 1.2.0 → 1.5.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: 925807376e5d7c387227dc33369fa80dcefd7abc7f4164f5ed939e8de9a0fa66
4
- data.tar.gz: 5a316e7e928da5eb1028e626fbcdec588e21fc66d683afec48278da7bdcc5899
3
+ metadata.gz: 37098f6edb2efccc49b1957ed3a1fab4ba24de1513c418d6b354c55639f7c21e
4
+ data.tar.gz: 43017be01759fa4ad755f7c6eb137dc112579746c04925e7a2d27eacc068bc78
5
5
  SHA512:
6
- metadata.gz: 8e50f5e30e11b7e2c19234fe207c436b87ee37ff690212af30eb9460eab789ba08dc06b545d55aa2d41d80ccaf2a97f37c6ee2dfeaac31f7d53be55b1ccb7b26
7
- data.tar.gz: 9de14d6773e0f55527f6857fb72c75be710615cc02eb61fcf297ca00d3ee2db3d9b9a2e6fea05ebde468497d79a66a38996bd2f1877b0f9813b798b94b93d1dc
6
+ metadata.gz: 443405d84171e9e1bbbb50542d62f7648c4291adc2b35a698b48cc7a9f821e182af43fd43e2a858c42bef31c6e805e4f6d47dfa2b06b20c81b2c480e95ed3eee
7
+ data.tar.gz: 751bed4920602a17ac928f24d1d0f9b3ccc0f1f8c5c0d75dc29df580618198cb9b8985059a76d451effbfd1d62366d2e35f839f25e25908c5590fa6b93de505f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.5.0
4
+
5
+ - Add RubyMotion compatibility
6
+ - Ensure Opal compatibility happens through RUBY_ENGINE instead of RUBY_PLATFORM
7
+
8
+ ## 1.4.0
9
+
10
+ - `array_diff_indexes(other_array)` (alias: `array_diff_indices`)
11
+ - `array_intersection_indexes(other_array)` (alias: `array_intersection_indices`)
12
+
13
+ ## 1.3.0
14
+
15
+ - [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)
16
+ - [API Breaking] Separate between operations `include_all?(*array)` and `include_all?(array)` to avoid confusion (rename the latter to `include_array?(array)`)
17
+ - `Array#include_all?(*other_array, same_sort: true)` accepts `same_sort` option (default: `false`)
18
+
3
19
  ## 1.2.0
4
20
 
5
21
  - Add `Array#array_index(array)` method to determine start array index of other array
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # ArrayIncludeMethods 1.2.0 - Ruby Refinement
1
+ # ArrayIncludeMethods 1.5.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)
5
5
 
6
- `Array#include_all?` & `Array#include_any?` methods missing from basic Ruby `Array` API.
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.
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.2.0'
15
+ gem 'array_include_methods', '~> 1.5.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.2.0
29
+ gem install array_include_methods -v1.5.0
30
30
  ```
31
31
 
32
32
  ## Usage
@@ -51,10 +51,8 @@ Now, you have `#include_all?` and `#include_any?` methods on `Array` objects.
51
51
 
52
52
  ```ruby
53
53
  [1, 2, 3, 4].include_any?(2, 4, 5) # returns true
54
- [1, 2, 3, 4].include_any?([2, 4, 5]) # returns true
55
54
  [1, 2, 3, 4].include_any?(6, 7) # returns false
56
- [1, 2, 3, 4].include_any?([6, 7]) # returns false
57
- [1, 2, 3, 4].include_any?([]) # returns true
55
+ [1, 2, 3, 4].include_any?() # returns true
58
56
  [1, 2, 3, 4].include_any?(nil) # returns false
59
57
  ```
60
58
 
@@ -62,19 +60,27 @@ Now, you have `#include_all?` and `#include_any?` methods on `Array` objects.
62
60
 
63
61
  ```ruby
64
62
  [1, 2, 3, 4].include_all?(2, 3) # returns true
65
- [1, 2, 3, 4].include_all?([2, 3]) # returns true
66
63
  [1, 2, 3, 4].include_all?(2, 4) # returns true
67
- [1, 2, 3, 4].include_all?([2, 4]) # returns false (checks true array containment)
68
64
  [1, 2, 3, 4].include_all?(4, 2) # returns true
69
- [1, 2, 3, 4].include_all?([4, 2]) # returns false (checks true array containment)
65
+ [1, 2, 3, 4].include_all?(4, 2, same_sort: true) # returns false
70
66
  [1, 2, 3, 4].include_all?(2, 4, 4) # returns true
71
- [1, 2, 3, 4].include_all?([2, 4, 4]) # returns false (checks true array containment)
72
67
  [1, 2, 3, 4].include_all?(2, 4, 5) # returns false
73
- [1, 2, 3, 4].include_all?([2, 4, 5]) # returns false
74
- [1, 2, 3, 4].include_all?([]) # returns true
68
+ [1, 2, 3, 4].include_all?() # returns true
75
69
  [1, 2, 3, 4].include_all?(nil) # returns false
76
70
  ```
77
71
 
72
+ ### `Array#include_array?(other_array)`
73
+
74
+ ```ruby
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
+
78
84
  ### `Array#array_index(other_array)`
79
85
 
80
86
  Returns first array index of `other_array` in `first_array` assuming `first_array.include_all?(other_array)` returns true
@@ -90,9 +96,49 @@ Returns first array index of `other_array` in `first_array` assuming `first_arra
90
96
  [1, 2, 3, 4].array_index(nil) # returns -1
91
97
  ```
92
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]
129
+ ```
130
+
131
+ ## JRuby Compatibility
132
+
133
+ This gem is 100% compatible with JRuby.
134
+
93
135
  ## Opal Compatibility
94
136
 
95
- 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
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.
138
+
139
+ ## RubyMotion Compatibility
140
+
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.
96
142
 
97
143
  ## TODO
98
144
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.5.0
@@ -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.2.0 ruby lib
5
+ # stub: array_include_methods 1.5.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "array_include_methods".freeze
9
- s.version = "1.2.0"
9
+ s.version = "1.5.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 = "2021-09-25"
15
- s.description = "Array#include_all? & Array#include_any? methods missing from basic Ruby Array API".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
16
16
  s.email = "andy.am@gmail.com".freeze
17
17
  s.extra_rdoc_files = [
18
18
  "CHANGELOG.md",
@@ -25,11 +25,12 @@ Gem::Specification.new do |s|
25
25
  "README.md",
26
26
  "VERSION",
27
27
  "array_include_methods.gemspec",
28
- "lib/array_include_methods.rb"
28
+ "lib/array_include_methods.rb",
29
+ "lib/array_include_methods/array.rb"
29
30
  ]
30
31
  s.homepage = "http://github.com/AndyObtiva/array_include_methods".freeze
31
32
  s.licenses = ["MIT".freeze]
32
- s.rubygems_version = "3.2.22".freeze
33
+ s.rubygems_version = "3.3.3".freeze
33
34
  s.summary = "Array#include_all? & Array#include_any? methods missing from basic Ruby Array API".freeze
34
35
 
35
36
  if s.respond_to? :specification_version then
@@ -43,6 +44,7 @@ Gem::Specification.new do |s|
43
44
  s.add_development_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
44
45
  s.add_development_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
45
46
  s.add_development_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
47
+ s.add_development_dependency(%q<puts_debuggerer>.freeze, ["> 0"])
46
48
  s.add_development_dependency(%q<rake-tui>.freeze, ["> 0"])
47
49
  else
48
50
  s.add_dependency(%q<rspec>.freeze, ["~> 3.5.0"])
@@ -51,6 +53,7 @@ Gem::Specification.new do |s|
51
53
  s.add_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
52
54
  s.add_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
53
55
  s.add_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
56
+ s.add_dependency(%q<puts_debuggerer>.freeze, ["> 0"])
54
57
  s.add_dependency(%q<rake-tui>.freeze, ["> 0"])
55
58
  end
56
59
  end
@@ -0,0 +1,111 @@
1
+ if ['opal', 'rubymotion'].include?(RUBY_ENGINE)
2
+ # Create a shim using method that does nothing since we monkey-patch in Opal earlier in `refine` method
3
+ def self.using(refinement)
4
+ # NO OP
5
+ end
6
+
7
+ module ArrayIncludeMethods
8
+ def self.refine(class_or_module, &refinement)
9
+ class_or_module.class_eval(&refinement)
10
+ end
11
+ end
12
+ end
13
+
14
+ module ArrayIncludeMethods
15
+ refine Array do
16
+ # Returns `true` if all of the given `array` elements are present in `self`,
17
+ # otherwise returns `false`
18
+ # Always returns `true` if the given `array` is empty
19
+ # `same_sort` kwarg indicates that array must have the same sort as `self`. By default, it is `false`
20
+ # e.g. [1,2,3].include_all?(2,3, same_sort: true) => true
21
+ def include_all?(*array)
22
+ options = array.last.is_a?(Hash) && array.last.has_key?(:same_sort) ? array.pop : {same_sort: false}
23
+ same_sort = options[:same_sort]
24
+ return false if array.size > self.size
25
+ self_copy = self.dup
26
+ array_copy = array.dup
27
+ book_keeping_array_copy = array.dup
28
+ self_element_index = last_element_index = -1
29
+ array_copy.each do |element|
30
+ if self_copy.include?(element)
31
+ last_element_index = self_element_index
32
+ self_element_index = self_copy.index(element)
33
+ return false if same_sort && self_element_index < last_element_index
34
+ self_copy.delete_at(self_element_index)
35
+ book_keeping_array_copy.delete(element)
36
+ else
37
+ return false
38
+ end
39
+ end
40
+ book_keeping_array_copy.empty?
41
+ end
42
+
43
+ # Returns `true` if the given `array` is present in `self` (in the same element order without repetition)
44
+ # Always returns `true` if the given `array` is empty
45
+ # Always returns `false` if the given `array` is nil
46
+ def include_array?(array)
47
+ return false if array.nil?
48
+ if array.size > self.size
49
+ false
50
+ else
51
+ size_diff = self.size - array.size
52
+ (size_diff + 1).times.any? do |start_index|
53
+ self[start_index, array.size] == array
54
+ end
55
+ end
56
+ end
57
+
58
+ # Returns `true` if any of the given `array` elements are present in `self`,
59
+ # otherwise returns `false`
60
+ # Always returns `true` if the given `array` is empty
61
+ def include_any?(*array)
62
+ !array.nil? && (array.empty? || !(self & array).empty?)
63
+ end
64
+
65
+ def array_index(array)
66
+ result_array_index = -1
67
+ return result_array_index if array.nil?
68
+ if array.size <= self.size
69
+ size_diff = self.size - array.size
70
+ current_array_index = nil
71
+ result = (size_diff + 1).times.any? do |start_index|
72
+ current_array_index = start_index
73
+ self[start_index, array.size] == array
74
+ end
75
+ result_array_index = current_array_index if result
76
+ end
77
+ result_array_index
78
+ end
79
+
80
+ def array_intersection_indexes(array)
81
+ array_intersection_and_diff_indexes(array)[:intersection_indexes]
82
+ end
83
+ alias array_intersection_indices array_intersection_indexes
84
+
85
+ def array_diff_indexes(array)
86
+ array_intersection_and_diff_indexes(array)[:diff_indexes]
87
+ end
88
+ alias array_diff_indices array_diff_indexes
89
+
90
+ private
91
+
92
+ def array_intersection_and_diff_indexes(array)
93
+ return {intersection_indexes: [], diff_indexes: self.size.times.to_a} if array.nil?
94
+ intersection_indexes = []
95
+ diff_indexes = []
96
+ array_current_index = 0
97
+ each_with_index do |element, index|
98
+ if element == array[array_current_index]
99
+ intersection_indexes << index
100
+ array_current_index += 1
101
+ else
102
+ diff_indexes << index
103
+ end
104
+ end
105
+ {
106
+ intersection_indexes: intersection_indexes,
107
+ diff_indexes: diff_indexes
108
+ }
109
+ end
110
+ end
111
+ end
@@ -1,78 +1,7 @@
1
- module ArrayIncludeMethods
2
- if RUBY_PLATFORM == 'opal'
3
- def self.refine(class_or_module, &refinement)
4
- class_or_module.class_eval(&refinement)
5
- end
6
- end
7
- refine Array do
8
- # Returns `true` if all of the given `array` elements are present in `self`,
9
- # otherwise returns `false`
10
- # Always returns `true` if the given `array` is empty
11
- # Always returns `false` if the given `array` is nil
12
- def include_all?(*array)
13
- array_argument = false
14
- if array.size == 1 && array[0].is_a?(Array)
15
- array_argument = true
16
- array = array[0]
17
- end
18
- return false if array.nil?
19
- array_include_other_array_same_class_elements = lambda do |a1, a2|
20
- begin
21
- if array_argument
22
- if a2.size > a1.size
23
- false
24
- else
25
- size_diff = a1.size - a2.size
26
- (size_diff + 1).times.any? do |start_index|
27
- a1[start_index, a2.size] == a2
28
- end
29
- end
30
- else
31
- (a1 & a2).uniq.sort == a2.uniq.sort
32
- end
33
- rescue ArgumentError => e
34
- a2.uniq.all? { |element| a1.include?(element) }
35
- end
36
- end
37
- self_grouped_by = self.group_by(&:class)
38
- array_grouped_by = array.group_by(&:class)
39
- 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))
40
- array_grouped_by.reduce(true) do |result, pair|
41
- array_class = pair.first
42
- array_elements = pair.last
43
- self_grouped_by[array_class]
44
- result && array_include_other_array_same_class_elements.call(self_grouped_by[array_class], array_elements)
45
- end
46
- end
47
-
48
- # Returns `true` if any of the given `array` elements are present in `self`,
49
- # otherwise returns `false`
50
- # Always returns `true` if the given `array` is empty
51
- # Always returns `false` if the given `array` is nil
52
- def include_any?(*array)
53
- array = array[0] if array.size == 1 && array[0].is_a?(Array)
54
- !array.nil? && (array.empty? || !(self & array).empty?)
55
- end
56
-
57
- def array_index(array)
58
- result_array_index = -1
59
- return result_array_index if array.nil?
60
- if array.size <= self.size
61
- size_diff = self.size - array.size
62
- current_array_index = nil
63
- result = (size_diff + 1).times.any? do |start_index|
64
- current_array_index = start_index
65
- self[start_index, array.size] == array
66
- end
67
- result_array_index = current_array_index if result
68
- end
69
- result_array_index
70
- end
71
- end
72
- end
73
- if RUBY_PLATFORM == 'opal'
74
- # Create a shim using method that does nothing since we monkey-patch in Opal earlier in `refine` method
75
- def self.using(refinement)
76
- # NO OP
1
+ begin
2
+ Motion::Project::App.setup do |app|
3
+ app.files += [File.join(__dir__, 'array_include_methods', 'array.rb')]
77
4
  end
5
+ rescue
6
+ require_relative 'array_include_methods/array'
78
7
  end
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.2.0
4
+ version: 1.5.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: 2021-09-25 00:00:00.000000000 Z
11
+ date: 2022-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -94,6 +94,20 @@ 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'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rake-tui
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -108,8 +122,9 @@ dependencies:
108
122
  - - ">"
109
123
  - !ruby/object:Gem::Version
110
124
  version: '0'
111
- description: Array#include_all? & Array#include_any? methods missing from basic Ruby
112
- Array API
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.
113
128
  email: andy.am@gmail.com
114
129
  executables: []
115
130
  extensions: []
@@ -124,6 +139,7 @@ files:
124
139
  - VERSION
125
140
  - array_include_methods.gemspec
126
141
  - lib/array_include_methods.rb
142
+ - lib/array_include_methods/array.rb
127
143
  homepage: http://github.com/AndyObtiva/array_include_methods
128
144
  licenses:
129
145
  - MIT
@@ -143,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
159
  - !ruby/object:Gem::Version
144
160
  version: '0'
145
161
  requirements: []
146
- rubygems_version: 3.2.22
162
+ rubygems_version: 3.3.3
147
163
  signing_key:
148
164
  specification_version: 4
149
165
  summary: Array#include_all? & Array#include_any? methods missing from basic Ruby Array