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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3b0b1c5b98183dd893256d7d8c198cde2b3db20a
4
- data.tar.gz: 7ae7a02a3dce79af2d4a491357b662c6239b7fc5
2
+ SHA256:
3
+ metadata.gz: ef3d975fc37131548ae27b3de0ec3e702e455b730585ca0c5bbce43cbc8c025c
4
+ data.tar.gz: 76a33cc1ab4c29e44ce88493ade671ccbda0deeabd9daabc78b841adbe51e7f0
5
5
  SHA512:
6
- metadata.gz: ac4ae7d21866bd55951aa50f84c6c8c07dc46f3c2425c7b36407141d852fbc37367d501be6799c39a4a246468fab85a7e573cd4b72293c0795f152180a00baaa
7
- data.tar.gz: 6b8700e6c0477c08f24058c1c67f6e964cb720da9fbd948c0819cc1cb64b7e57532a0711999331e7d1c698f2e18de8cdcc0e053c9fe1fe9f46c0f5271a9c65f7
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
- system "rspec"
6
+ sh "rspec"
7
7
  end
8
8
 
9
9
  desc "Clean up"
10
10
  task "clean" do
11
- system "trash hash-zip-*.gem"
11
+ sh "trash hash-zip-*.gem"
12
12
  end
13
13
 
14
14
  desc "Build gem"
15
15
  task "gem:build" do
16
- system "gem build hash-zip.gemspec"
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
- system "gem", "push", gem_file
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
@@ -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.1.20170628
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: 2017-06-28 00:00:00.000000000 Z
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
- rubyforge_project:
87
- rubygems_version: 2.5.2
88
- signing_key:
86
+ rubygems_version: 3.3.26
87
+ signing_key:
89
88
  specification_version: 4
90
- summary: Hash#zip method
89
+ summary: Hash#zip and Hash#zip_compact methods
91
90
  test_files: []