array_include_methods 1.0.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -0
- data/README.md +43 -18
- data/VERSION +1 -0
- data/array_include_methods.gemspec +57 -0
- data/lib/array_include_methods.rb +47 -4
- metadata +54 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 925807376e5d7c387227dc33369fa80dcefd7abc7f4164f5ed939e8de9a0fa66
|
4
|
+
data.tar.gz: 5a316e7e928da5eb1028e626fbcdec588e21fc66d683afec48278da7bdcc5899
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e50f5e30e11b7e2c19234fe207c436b87ee37ff690212af30eb9460eab789ba08dc06b545d55aa2d41d80ccaf2a97f37c6ee2dfeaac31f7d53be55b1ccb7b26
|
7
|
+
data.tar.gz: 9de14d6773e0f55527f6857fb72c75be710615cc02eb61fcf297ca00d3ee2db3d9b9a2e6fea05ebde468497d79a66a38996bd2f1877b0f9813b798b94b93d1dc
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## 1.2.0
|
4
|
+
|
5
|
+
- Add `Array#array_index(array)` method to determine start array index of other array
|
6
|
+
|
7
|
+
## 1.1.0
|
8
|
+
|
9
|
+
- Perform contiguous-element `include_all?([...])` check against an array argument (not splatted)
|
10
|
+
- Perform sorted `include_all?([...])` check against an array argument (not splatted)
|
11
|
+
- Perform non-repetition `include_all?([...])` check against an array argument (not splatted)
|
12
|
+
|
13
|
+
## 1.0.4
|
14
|
+
|
15
|
+
- Support splat args (e.g. `include_any?(1, 2, 3)` instead of `include_any?([1, 2, 3])`)
|
16
|
+
|
17
|
+
## 1.0.3
|
18
|
+
|
19
|
+
- Opal compatibility through monkey-patching and providing a `using` method shim so that existing gems that used the refinement in Ruby work
|
20
|
+
|
21
|
+
## 1.0.2
|
22
|
+
|
23
|
+
- Handled case of two arrays with elements of unsortable object types (e.g. Hash)
|
24
|
+
|
25
|
+
## 1.0.1
|
26
|
+
|
27
|
+
- Handled case of two arrays with different ordering of common elements
|
28
|
+
|
29
|
+
## 1.0.0
|
30
|
+
|
31
|
+
- Initial implementation of Array#include_all? & Array#include_any?
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
# ArrayIncludeMethods 1.0
|
1
|
+
# ArrayIncludeMethods 1.2.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
|
+
[![Build Status](https://travis-ci.com/AndyObtiva/array_include_methods.svg?branch=master)](https://travis-ci.com/AndyObtiva/array_include_methods)
|
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)
|
3
5
|
|
4
6
|
`Array#include_all?` & `Array#include_any?` methods missing from basic Ruby `Array` API.
|
5
7
|
|
@@ -10,7 +12,7 @@
|
|
10
12
|
Include the following in Gemfile:
|
11
13
|
|
12
14
|
```ruby
|
13
|
-
gem 'array_include_methods', '~> 1.0
|
15
|
+
gem 'array_include_methods', '~> 1.2.0'
|
14
16
|
```
|
15
17
|
|
16
18
|
Run:
|
@@ -24,7 +26,7 @@ bundle
|
|
24
26
|
Run:
|
25
27
|
|
26
28
|
```
|
27
|
-
gem install array_include_methods -v1.0
|
29
|
+
gem install array_include_methods -v1.2.0
|
28
30
|
```
|
29
31
|
|
30
32
|
## Usage
|
@@ -45,37 +47,60 @@ Now, you have `#include_all?` and `#include_any?` methods on `Array` objects.
|
|
45
47
|
|
46
48
|
## Examples
|
47
49
|
|
48
|
-
### `Array#
|
50
|
+
### `Array#include_any?(*other_array)`
|
49
51
|
|
50
52
|
```ruby
|
51
|
-
[1, 2, 3, 4].
|
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
|
+
[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
|
58
|
+
[1, 2, 3, 4].include_any?(nil) # returns false
|
59
|
+
```
|
60
|
+
|
61
|
+
### `Array#include_all?(*other_array)`
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
[1, 2, 3, 4].include_all?(2, 3) # returns true
|
65
|
+
[1, 2, 3, 4].include_all?([2, 3]) # returns true
|
66
|
+
[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
|
+
[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)
|
70
|
+
[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
|
+
[1, 2, 3, 4].include_all?(2, 4, 5) # returns false
|
52
73
|
[1, 2, 3, 4].include_all?([2, 4, 5]) # returns false
|
53
74
|
[1, 2, 3, 4].include_all?([]) # returns true
|
54
75
|
[1, 2, 3, 4].include_all?(nil) # returns false
|
55
76
|
```
|
56
77
|
|
57
|
-
### `Array#
|
78
|
+
### `Array#array_index(other_array)`
|
79
|
+
|
80
|
+
Returns first array index of `other_array` in `first_array` assuming `first_array.include_all?(other_array)` returns true
|
58
81
|
|
59
82
|
```ruby
|
60
|
-
[1, 2, 3, 4].
|
61
|
-
[1, 2, 3, 4].
|
62
|
-
[1, 2, 3, 4].
|
63
|
-
[1, 2, 3, 4].
|
83
|
+
[1, 2, 3, 4].array_index([2, 3, 4]) # returns 1
|
84
|
+
[1, 2, 3, 4].array_index([2, 3]) # returns 1
|
85
|
+
[1, 2, 3, 4].array_index([3, 4]) # returns 2
|
86
|
+
[1, 2, 3, 4].array_index([2, 4]) # returns -1
|
87
|
+
[1, 2, 3, 4].array_index([4, 2]) # returns -1
|
88
|
+
[1, 2, 3, 4].array_index([2, 4, 5]) # returns -1
|
89
|
+
[1, 2, 3, 4].array_index([]) # returns -1
|
90
|
+
[1, 2, 3, 4].array_index(nil) # returns -1
|
64
91
|
```
|
65
92
|
|
66
|
-
##
|
67
|
-
|
68
|
-
### 1.0.2
|
93
|
+
## Opal Compatibility
|
69
94
|
|
70
|
-
-
|
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
|
71
96
|
|
72
|
-
|
97
|
+
## TODO
|
73
98
|
|
74
|
-
|
99
|
+
[TODO.md](TODO.md)
|
75
100
|
|
76
|
-
|
101
|
+
## Change Log
|
77
102
|
|
78
|
-
|
103
|
+
[CHANGELOG.md](CHANGELOG.md)
|
79
104
|
|
80
105
|
## Contributing to array_include_methods
|
81
106
|
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.2.0
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: array_include_methods 1.2.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "array_include_methods".freeze
|
9
|
+
s.version = "1.2.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
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
|
16
|
+
s.email = "andy.am@gmail.com".freeze
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"CHANGELOG.md",
|
19
|
+
"LICENSE.txt",
|
20
|
+
"README.md"
|
21
|
+
]
|
22
|
+
s.files = [
|
23
|
+
"CHANGELOG.md",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.md",
|
26
|
+
"VERSION",
|
27
|
+
"array_include_methods.gemspec",
|
28
|
+
"lib/array_include_methods.rb"
|
29
|
+
]
|
30
|
+
s.homepage = "http://github.com/AndyObtiva/array_include_methods".freeze
|
31
|
+
s.licenses = ["MIT".freeze]
|
32
|
+
s.rubygems_version = "3.2.22".freeze
|
33
|
+
s.summary = "Array#include_all? & Array#include_any? methods missing from basic Ruby Array API".freeze
|
34
|
+
|
35
|
+
if s.respond_to? :specification_version then
|
36
|
+
s.specification_version = 4
|
37
|
+
end
|
38
|
+
|
39
|
+
if s.respond_to? :add_runtime_dependency then
|
40
|
+
s.add_development_dependency(%q<rspec>.freeze, ["~> 3.5.0"])
|
41
|
+
s.add_development_dependency(%q<rdoc>.freeze, ["~> 3.12"])
|
42
|
+
s.add_development_dependency(%q<jeweler>.freeze, ["~> 2.3.9"])
|
43
|
+
s.add_development_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
|
44
|
+
s.add_development_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
|
45
|
+
s.add_development_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
|
46
|
+
s.add_development_dependency(%q<rake-tui>.freeze, ["> 0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<rspec>.freeze, ["~> 3.5.0"])
|
49
|
+
s.add_dependency(%q<rdoc>.freeze, ["~> 3.12"])
|
50
|
+
s.add_dependency(%q<jeweler>.freeze, ["~> 2.3.9"])
|
51
|
+
s.add_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
|
52
|
+
s.add_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
|
53
|
+
s.add_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
|
54
|
+
s.add_dependency(%q<rake-tui>.freeze, ["> 0"])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
@@ -1,16 +1,37 @@
|
|
1
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
|
2
7
|
refine Array do
|
3
8
|
# Returns `true` if all of the given `array` elements are present in `self`,
|
4
9
|
# otherwise returns `false`
|
5
10
|
# Always returns `true` if the given `array` is empty
|
6
11
|
# Always returns `false` if the given `array` is nil
|
7
|
-
def include_all?(array)
|
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
|
8
18
|
return false if array.nil?
|
9
19
|
array_include_other_array_same_class_elements = lambda do |a1, a2|
|
10
20
|
begin
|
11
|
-
|
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
|
12
33
|
rescue ArgumentError => e
|
13
|
-
a2.uniq.
|
34
|
+
a2.uniq.all? { |element| a1.include?(element) }
|
14
35
|
end
|
15
36
|
end
|
16
37
|
self_grouped_by = self.group_by(&:class)
|
@@ -28,8 +49,30 @@ module ArrayIncludeMethods
|
|
28
49
|
# otherwise returns `false`
|
29
50
|
# Always returns `true` if the given `array` is empty
|
30
51
|
# Always returns `false` if the given `array` is nil
|
31
|
-
def include_any?(array)
|
52
|
+
def include_any?(*array)
|
53
|
+
array = array[0] if array.size == 1 && array[0].is_a?(Array)
|
32
54
|
!array.nil? && (array.empty? || !(self & array).empty?)
|
33
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
|
34
77
|
end
|
35
78
|
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.0
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Maleh
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -52,18 +52,60 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.3.9
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.8.23
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.8.23
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: simplecov
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - "
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.16.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.16.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov-lcov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.7.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.7.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake-tui
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">"
|
60
102
|
- !ruby/object:Gem::Version
|
61
103
|
version: '0'
|
62
104
|
type: :development
|
63
105
|
prerelease: false
|
64
106
|
version_requirements: !ruby/object:Gem::Requirement
|
65
107
|
requirements:
|
66
|
-
- - "
|
108
|
+
- - ">"
|
67
109
|
- !ruby/object:Gem::Version
|
68
110
|
version: '0'
|
69
111
|
description: Array#include_all? & Array#include_any? methods missing from basic Ruby
|
@@ -72,17 +114,21 @@ email: andy.am@gmail.com
|
|
72
114
|
executables: []
|
73
115
|
extensions: []
|
74
116
|
extra_rdoc_files:
|
117
|
+
- CHANGELOG.md
|
75
118
|
- LICENSE.txt
|
76
119
|
- README.md
|
77
120
|
files:
|
121
|
+
- CHANGELOG.md
|
78
122
|
- LICENSE.txt
|
79
123
|
- README.md
|
124
|
+
- VERSION
|
125
|
+
- array_include_methods.gemspec
|
80
126
|
- lib/array_include_methods.rb
|
81
127
|
homepage: http://github.com/AndyObtiva/array_include_methods
|
82
128
|
licenses:
|
83
129
|
- MIT
|
84
130
|
metadata: {}
|
85
|
-
post_install_message:
|
131
|
+
post_install_message:
|
86
132
|
rdoc_options: []
|
87
133
|
require_paths:
|
88
134
|
- lib
|
@@ -97,8 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
143
|
- !ruby/object:Gem::Version
|
98
144
|
version: '0'
|
99
145
|
requirements: []
|
100
|
-
rubygems_version: 3.
|
101
|
-
signing_key:
|
146
|
+
rubygems_version: 3.2.22
|
147
|
+
signing_key:
|
102
148
|
specification_version: 4
|
103
149
|
summary: Array#include_all? & Array#include_any? methods missing from basic Ruby Array
|
104
150
|
API
|