hesh 0.0.1 → 0.0.2

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: 2f5f07e54c97be55a6c8da62fc4f23c1390586a4830498f104d1ff5eb47d72aa
4
- data.tar.gz: 823b42dfcdaf654f21dd8add2e6fb97b45e5e0d1eea72a5e09eb4fa4e69d1836
3
+ metadata.gz: 51993aff5492c29105b73ec622942f5f827da21e1c8f2c8bbebf0c4c52707878
4
+ data.tar.gz: 3a7796f89cc9806cb576f0120c67ec86560587ea12d6b55040ab63538c35bbcb
5
5
  SHA512:
6
- metadata.gz: d9509dd0909fdfa13744facf7918eb611c96ccec274d76c0c74364a86581cf248e3812177fbf79ba564f2fb0e34e149da4f84d841896800241847db37af3ca5b
7
- data.tar.gz: 784df8f49a27f83405dfeff657ffb8e1b07e50f08b2e1906a621bc892ab71b9b271f7c632ade98a6d9a7d82f5f73e3c0d8a0591418cd41303c496294e9550387
6
+ metadata.gz: b096afaddb9863fbb9cb08feeb37c920a1f00e41b3931672a3cadb1e26a19278e1ee1c9653063afc59e31a3bc845eea881c6be9c49b86a87442cac5fce814149
7
+ data.tar.gz: bd4d3db10f5aff1754fa2821b096973e7c9ee07cfde296cf1d1a9a697076e8be6e934fe9d21edc559820ca0dd11c91c1d1c45c15dc240b2262dc63eb6c832eb6
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hesh (0.0.1)
4
+ hesh (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -23,9 +23,28 @@ Hesh.count_from(%w(a a a b b b b c c))
23
23
  # Inverts the relation of a hash
24
24
  Hesh.invert_relation({ 1 => %w(a b), 2 => %w(b c), 3 => %w(a c) })
25
25
  => { "a" => [1, 3], "b" => [1, 2], "c" => [2, 3] }
26
+
27
+ # Merges and sums together values
28
+ Hesh.merge_sum({ a: 1 }, { a: 2 }, { a: 3 })
29
+ => { a: 6 }
30
+
31
+ # Merges and joins together arrays
32
+ Hesh.merge_join({ a: [1,2,3] }, { a: [4] })
33
+ => { a: [1, 2, 3, 4] }
34
+
35
+ # Merges with a function
36
+ Hesh.merge_with({ a: [1,2,3] }, { a: [1,4] }) { |k, o, n| o - n }
37
+ => { a: [2, 3] }
38
+
39
+ # Merges deep!
40
+ a = { a: { b: { c: 1, d: 2 } } }
41
+ b = { a: { b: { c: 4, d: 3 }, x: 4 } }
42
+
43
+ Hesh.merge_deep(a, b) { |k,o,n| o + n }
44
+ => { a: { b: { c: 5, d: 5 }, x: 4 } }
26
45
  ```
27
46
 
28
- More examples to come later
47
+ More examples to come later, the rspec tests have the most fun in them.
29
48
 
30
49
  ## Installation
31
50
 
data/lib/hesh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Hesh
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/hesh.rb CHANGED
@@ -5,6 +5,9 @@ class Hesh
5
5
  # you happen to give it!
6
6
  IDENTITY_FN = -> v { v }
7
7
 
8
+ # We prefer the new and shiny here, yes we do.
9
+ PREFER_NEW_FN = -> k, o, n { n }
10
+
8
11
  def initialize
9
12
  raise 'Why no no no, hesh is not to be made! Not yet!'
10
13
  end
@@ -63,5 +66,60 @@ class Hesh
63
66
  vs.each { |v| h[v] << k }
64
67
  }
65
68
  end
69
+
70
+ # Merges a series of hashes and sums their values
71
+ #
72
+ # @param *hashes [Array[Hash]]
73
+ # Collection of hashes
74
+ #
75
+ # @return [Hash[Any, Integer]]
76
+ # Summed hash
77
+ def merge_sum(*hashes)
78
+ hashes.reduce(Hash.new(0), &merges { |k, o, n| o + n })
79
+ end
80
+
81
+ # Merges a series of hashes and joins their values
82
+ #
83
+ # @param *hashes [Array[Hash]]
84
+ # Collection of hashes
85
+ #
86
+ # @return [Hash[Any, Array]]
87
+ # Joined hash
88
+ def merge_join(*hashes)
89
+ hashes.reduce(Hesh.of_array, &merges { |k, o, n| o + n })
90
+ end
91
+
92
+ # Merges a series of hashes with a function
93
+ #
94
+ # @param *hashes [Array[Hash]]
95
+ # Collection of hashes
96
+ #
97
+ # @return [Hash[Any, Any]]
98
+ # Merged hash
99
+ def merge_with(*hashes, &fn)
100
+ hashes.reduce({}, &merges(&fn))
101
+ end
102
+
103
+ # Merges a series of hashes infintely deep with a function
104
+ #
105
+ # @param *hashes [Array[Hash]]
106
+ # Collection of hashes
107
+ #
108
+ # @param &fn [Proc]
109
+ # Function for joining values, defaults to preferring newest value
110
+ #
111
+ # @return [Hash[Any, Any]]
112
+ # Joined hash
113
+ def merge_deep(*hashes, &fn)
114
+ fn ||= PREFER_NEW_FN
115
+
116
+ hashes.reduce({}, &merges { |k, o, n|
117
+ o.is_a?(Hash) ? merge_deep(o, n, &fn) : fn[k, o, n]
118
+ })
119
+ end
120
+
121
+ private def merges(&fn)
122
+ -> a, b { a.merge(b, &fn) }
123
+ end
66
124
  end
67
125
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hesh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Weaver