hash-zip 0.1.20170628 → 0.2.20230803
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +15 -0
- data/Rakefile +4 -4
- data/lib/hash-zip.rb +26 -0
- data/spec/hash_zip_spec.rb +23 -0
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ef3d975fc37131548ae27b3de0ec3e702e455b730585ca0c5bbce43cbc8c025c
|
4
|
+
data.tar.gz: 76a33cc1ab4c29e44ce88493ade671ccbda0deeabd9daabc78b841adbe51e7f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 841022114898d68f3fa50872eb4ed1b705fb4ff1f0d742e8a490d990cd6c79c1b8b6a0a66d5a96a154fa7445e6c2985a1c8c4953ac9de093f1590004d5fd944f
|
7
|
+
data.tar.gz: f7d302d27c7e3f2408ac7e75e4ce72a084ba53860dceb8716c4fa968c0a6234d58dc094369f195fa2e83f89140dd57c97a74dd922e7ee89b4675bf44a63f080d
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
Implements a much needed `Hash#zip` method.
|
2
2
|
|
3
|
+
To install run `gem install hash-zip`, and then `require "hash-zip"` in your code.
|
4
|
+
|
3
5
|
## Usage
|
4
6
|
|
5
7
|
You can zip multiple Hashes. Keys in result will be keys in any of the inputs, and values will be Arrays with missing values padded by `nil`s.
|
@@ -26,4 +28,17 @@ end
|
|
26
28
|
|
27
29
|
It works with more than one argument (`a.zip(b,c)`) as well as with zero arguments (`a.zip()`).
|
28
30
|
|
31
|
+
You can also use the `zip_compact` variant which discards nil values:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
a = {x: 1, y: 2}
|
35
|
+
b = {y: 3, z: 4}
|
36
|
+
a.zip_compact(b) do |key, value_a, value_b|
|
37
|
+
p [key, value_a, value_b]
|
38
|
+
# Yields [:x, 1]
|
39
|
+
# [:y, 2, 3]
|
40
|
+
# [:z, 4]
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
29
44
|
If arguments are not `Hash`es, but accept `to_h`, they will be converted automatically before zipping.
|
data/Rakefile
CHANGED
@@ -3,21 +3,21 @@ task "test" => "spec"
|
|
3
3
|
|
4
4
|
desc "Run tests"
|
5
5
|
task "spec" do
|
6
|
-
|
6
|
+
sh "rspec"
|
7
7
|
end
|
8
8
|
|
9
9
|
desc "Clean up"
|
10
10
|
task "clean" do
|
11
|
-
|
11
|
+
sh "trash hash-zip-*.gem"
|
12
12
|
end
|
13
13
|
|
14
14
|
desc "Build gem"
|
15
15
|
task "gem:build" do
|
16
|
-
|
16
|
+
sh "gem build hash-zip.gemspec"
|
17
17
|
end
|
18
18
|
|
19
19
|
desc "Upload gem"
|
20
20
|
task "gem:push" => "gem:build" do
|
21
21
|
gem_file = Dir["hash-zip-*.gem"][-1] or raise "No gem found"
|
22
|
-
|
22
|
+
sh "gem", "push", gem_file
|
23
23
|
end
|
data/lib/hash-zip.rb
CHANGED
@@ -23,4 +23,30 @@ class Hash
|
|
23
23
|
result
|
24
24
|
end
|
25
25
|
end
|
26
|
+
|
27
|
+
def zip_compact(*args)
|
28
|
+
sources = [self]
|
29
|
+
args.each do |arg|
|
30
|
+
if arg.is_a?(Hash)
|
31
|
+
sources << arg
|
32
|
+
else
|
33
|
+
sources << arg.to_h
|
34
|
+
end
|
35
|
+
end
|
36
|
+
output_keys = sources.flat_map(&:keys).uniq
|
37
|
+
if block_given?
|
38
|
+
output_keys.each do |key|
|
39
|
+
values = sources.map{|source| source[key]}.compact
|
40
|
+
yield(key, *values)
|
41
|
+
end
|
42
|
+
nil
|
43
|
+
else
|
44
|
+
result = {}
|
45
|
+
output_keys.each do |key|
|
46
|
+
values = sources.map{|source| source[key]}.compact
|
47
|
+
result[key] = values
|
48
|
+
end
|
49
|
+
result
|
50
|
+
end
|
51
|
+
end
|
26
52
|
end
|
data/spec/hash_zip_spec.rb
CHANGED
@@ -58,4 +58,27 @@ describe "Hash#zip" do
|
|
58
58
|
x: [1,4], y: [2,nil], z: [3,5], w: [nil,6]
|
59
59
|
})
|
60
60
|
end
|
61
|
+
|
62
|
+
it "allows ignoring nils" do
|
63
|
+
a = {x: 1, y: 2}
|
64
|
+
b = {x: 3, z: 4}
|
65
|
+
expect(a.zip_compact(b)).to eq({
|
66
|
+
x: [1,3], y: [2], z: [4]
|
67
|
+
})
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns nil, yields for each compacted item when passed a block" do
|
71
|
+
a = {x: 1, y: 2, z: 3}
|
72
|
+
b = {x: 4, z: 5, w: 6}
|
73
|
+
calls = []
|
74
|
+
expect(a.zip_compact(b){|*args|
|
75
|
+
calls << args
|
76
|
+
}).to eq(nil)
|
77
|
+
expect(calls).to eq([
|
78
|
+
[:x, 1, 4],
|
79
|
+
[:y, 2],
|
80
|
+
[:z, 3, 5],
|
81
|
+
[:w, 6],
|
82
|
+
])
|
83
|
+
end
|
61
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-zip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.20230803
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomasz Wegrzanowski
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -68,7 +68,7 @@ homepage: https://github.com/taw/hash-zip
|
|
68
68
|
licenses:
|
69
69
|
- MIT
|
70
70
|
metadata: {}
|
71
|
-
post_install_message:
|
71
|
+
post_install_message:
|
72
72
|
rdoc_options: []
|
73
73
|
require_paths:
|
74
74
|
- lib
|
@@ -83,9 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
83
|
- !ruby/object:Gem::Version
|
84
84
|
version: '0'
|
85
85
|
requirements: []
|
86
|
-
|
87
|
-
|
88
|
-
signing_key:
|
86
|
+
rubygems_version: 3.3.26
|
87
|
+
signing_key:
|
89
88
|
specification_version: 4
|
90
|
-
summary: Hash#zip
|
89
|
+
summary: Hash#zip and Hash#zip_compact methods
|
91
90
|
test_files: []
|