array_include_methods 1.0.3 → 1.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 +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +44 -13
- data/VERSION +1 -1
- data/array_include_methods.gemspec +8 -4
- data/lib/array_include_methods.rb +47 -18
- metadata +31 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f893cf42569d8afb770fc426df2ad336d81f32af27a9c3793960288ffba9da90
|
4
|
+
data.tar.gz: 2f68fec3b44267052eca66fdecf5b9507e8c80774210a6e93a1754ef9e44ff05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84634ac0899e45f2d48f28de1c3ee849b4c78c1691bcbf748e955595ee95c3dd4d54e12b2c7c07d01815f23cda0cfcc18e8959beb716559366c584b5542b6568
|
7
|
+
data.tar.gz: 93928af74b96981a77b1163b92cd4f2106b98cfcf96e0b9d3aeb8b463f426c2cf3fee6caa8e6d95e2be6052837bc785c6a73f886975b057bac9b52424c57e186
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## 1.3.0
|
4
|
+
|
5
|
+
- [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)
|
6
|
+
- [API Breaking] Separate between operations `include_all?(*array)` and `include_all?(array)` to avoid confusion (rename the latter to `include_array?(array)`)
|
7
|
+
- `Array#include_all?(*other_array, same_sort: true)` accepts `same_sort` option (default: `false`)
|
8
|
+
|
9
|
+
## 1.2.0
|
10
|
+
|
11
|
+
- Add `Array#array_index(array)` method to determine start array index of other array
|
12
|
+
|
13
|
+
## 1.1.0
|
14
|
+
|
15
|
+
- Perform contiguous-element `include_all?([...])` check against an array argument (not splatted)
|
16
|
+
- Perform sorted `include_all?([...])` check against an array argument (not splatted)
|
17
|
+
- Perform non-repetition `include_all?([...])` check against an array argument (not splatted)
|
18
|
+
|
19
|
+
## 1.0.4
|
20
|
+
|
21
|
+
- Support splat args (e.g. `include_any?(1, 2, 3)` instead of `include_any?([1, 2, 3])`)
|
22
|
+
|
3
23
|
## 1.0.3
|
4
24
|
|
5
25
|
- Opal compatibility through monkey-patching and providing a `using` method shim so that existing gems that used the refinement in Ruby work
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# ArrayIncludeMethods 1.0
|
1
|
+
# ArrayIncludeMethods 1.3.0 - Ruby Refinement
|
2
2
|
[](http://badge.fury.io/rb/array_include_methods)
|
3
3
|
[](https://travis-ci.com/AndyObtiva/array_include_methods)
|
4
4
|
[](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
|
15
|
+
gem 'array_include_methods', '~> 1.3.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
|
29
|
+
gem install array_include_methods -v1.3.0
|
30
30
|
```
|
31
31
|
|
32
32
|
## Usage
|
@@ -47,27 +47,58 @@ Now, you have `#include_all?` and `#include_any?` methods on `Array` objects.
|
|
47
47
|
|
48
48
|
## Examples
|
49
49
|
|
50
|
-
### `Array#
|
50
|
+
### `Array#include_any?(*other_array)`
|
51
51
|
|
52
52
|
```ruby
|
53
|
-
[1, 2, 3, 4].
|
54
|
-
[1, 2, 3, 4].
|
55
|
-
[1, 2, 3, 4].
|
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
|
63
|
+
[1, 2, 3, 4].include_all?(2, 4) # returns true
|
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
|
67
|
+
[1, 2, 3, 4].include_all?(2, 4, 5) # returns false
|
68
|
+
[1, 2, 3, 4].include_all?() # returns true
|
56
69
|
[1, 2, 3, 4].include_all?(nil) # returns false
|
57
70
|
```
|
58
71
|
|
59
|
-
### `Array#
|
72
|
+
### `Array#include_array?(other_array)`
|
60
73
|
|
61
74
|
```ruby
|
62
|
-
[1, 2, 3, 4].
|
63
|
-
[1, 2, 3, 4].
|
64
|
-
[1, 2, 3, 4].
|
65
|
-
[1, 2, 3, 4].
|
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
|
66
97
|
```
|
67
98
|
|
68
99
|
## Opal Compatibility
|
69
100
|
|
70
|
-
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
|
101
|
+
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
|
71
102
|
|
72
103
|
## TODO
|
73
104
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.3.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
|
5
|
+
# stub: array_include_methods 1.3.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
|
9
|
+
s.version = "1.3.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 = "
|
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.
|
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,34 +8,63 @@ 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
|
35
|
+
def include_array?(array)
|
13
36
|
return false if array.nil?
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
19
43
|
end
|
20
44
|
end
|
21
|
-
self_grouped_by = self.group_by(&:class)
|
22
|
-
array_grouped_by = array.group_by(&:class)
|
23
|
-
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))
|
24
|
-
array_grouped_by.reduce(true) do |result, pair|
|
25
|
-
array_class = pair.first
|
26
|
-
array_elements = pair.last
|
27
|
-
self_grouped_by[array_class]
|
28
|
-
result && array_include_other_array_same_class_elements.call(self_grouped_by[array_class], array_elements)
|
29
|
-
end
|
30
45
|
end
|
31
46
|
|
32
47
|
# Returns `true` if any of the given `array` elements are present in `self`,
|
33
48
|
# otherwise returns `false`
|
34
49
|
# Always returns `true` if the given `array` is empty
|
35
|
-
|
36
|
-
def include_any?(array)
|
50
|
+
def include_any?(*array)
|
37
51
|
!array.nil? && (array.empty? || !(self & array).empty?)
|
38
|
-
end
|
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
|
39
68
|
end
|
40
69
|
end
|
41
70
|
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
|
+
version: 1.3.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:
|
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.
|
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
|