hesh 0.0.1 → 0.0.2
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/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +20 -1
- data/lib/hesh/version.rb +1 -1
- data/lib/hesh.rb +58 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51993aff5492c29105b73ec622942f5f827da21e1c8f2c8bbebf0c4c52707878
|
4
|
+
data.tar.gz: 3a7796f89cc9806cb576f0120c67ec86560587ea12d6b55040ab63538c35bbcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b096afaddb9863fbb9cb08feeb37c920a1f00e41b3931672a3cadb1e26a19278e1ee1c9653063afc59e31a3bc845eea881c6be9c49b86a87442cac5fce814149
|
7
|
+
data.tar.gz: bd4d3db10f5aff1754fa2821b096973e7c9ee07cfde296cf1d1a9a697076e8be6e934fe9d21edc559820ca0dd11c91c1d1c45c15dc240b2262dc63eb6c832eb6
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
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
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
|