hmap 1.0.0 → 1.0.1

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: b91ede0c8bd571c77da15568b4418141005fd68b
4
- data.tar.gz: 5d3be03169ec696f3a4c5787a54eed3a7a82e4f1
2
+ SHA256:
3
+ metadata.gz: 6ad321ffc1bab03aa6a94f4e528c10b0c2cdc53a803a422f6327d12ab1abe9d4
4
+ data.tar.gz: 8faaec21ec0526bd3ea5fc3189045ec1cf2adcac7740352691e9b8a0a1cc6923
5
5
  SHA512:
6
- metadata.gz: 4490aad3efaa67f76f58fd9af4a61e5cf26c919140ecef9b11ac637b37d4b763b191efc84c218d7e6e3e7e0441deaff432201014756754e64384d16e6daadf7c
7
- data.tar.gz: 2b443f56320051e2f824eb147de39ed6ecb8b9559041cee6692ed6986ad57b13078503c40b3206e409f4a682c3779977d4d13f110b24d3073380967f05a474fb
6
+ metadata.gz: e6eec1dd98f07ca4d3f4da63bc1c8a31bf95d0edddc0265b8f259cdd84d05ad904c7b5c2bd2df78c44e8d3957492019f31dba6faa038eeaf6b3e8f4836770b14
7
+ data.tar.gz: 800788420878ef4950b798bdee053380deeb981638594299ef26bf35b9bbb901fc731c73e734b3292f25f6bdef5ffc9c1f8947ead4ae949817a8f71d23e488af
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ### 1.0.1 (2024-07-12)
2
+ - fix changelog
3
+ - rebuild
4
+ - codecov
5
+ - readme
6
+ - upgrades
7
+ - update libs
8
+
9
+ ### 1.0.0, 2018-12-03
10
+ - launch
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hmap (1.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (11.1.3)
10
+ diff-lcs (1.5.1)
11
+ docile (1.4.0)
12
+ rspec (3.13.0)
13
+ rspec-core (~> 3.13.0)
14
+ rspec-expectations (~> 3.13.0)
15
+ rspec-mocks (~> 3.13.0)
16
+ rspec-core (3.13.0)
17
+ rspec-support (~> 3.13.0)
18
+ rspec-expectations (3.13.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.13.0)
21
+ rspec-mocks (3.13.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.13.0)
24
+ rspec-support (3.13.1)
25
+ simplecov (0.22.0)
26
+ docile (~> 1.1)
27
+ simplecov-html (~> 0.11)
28
+ simplecov_json_formatter (~> 0.1)
29
+ simplecov-html (0.12.3)
30
+ simplecov_json_formatter (0.1.4)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ byebug (>= 11)
37
+ hmap!
38
+ rspec (>= 3.10)
39
+ simplecov (>= 0.22)
40
+
41
+ BUNDLED WITH
42
+ 2.5.15
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Daniel Pepper
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ Hmap
2
+ ======
3
+
4
+ Improved Hash mapping functions
5
+
6
+ ### Install
7
+ ```gem install hmap```
8
+
9
+
10
+ ### Usage
11
+ #### Hash.map
12
+ Create a Hash using an Enumerable by mapping it's items into corresponding hash values
13
+ ```ruby
14
+ Hash.map [:a, :bb, :ccc] {|v| v.length }
15
+ => { a: 1, bb: 2, ccc: 3 }
16
+ ```
17
+
18
+ #### hmap
19
+ Map an Enumerable into a Hash, like Hash[obj.map ... ]
20
+ ```ruby
21
+ Hash.hmap [ 1, 2, 3 ] { |v| [ v, v * 2 ] }
22
+ => { 1 => 2, 2 => 4, 3 => 6 }
23
+
24
+ {
25
+ 'a' => 1,
26
+ 'b' => 2,
27
+ 'c' => 3,
28
+ }.hmap {|k, v| [ k * v, v ** 2 ] }
29
+ => { 'a' => 1, 'bb' => 4, 'ccc' => 9 }
30
+ ```
31
+
32
+ #### vmap
33
+ Transform the values of a Hash
34
+ ```ruby
35
+ {
36
+ a: 1,
37
+ b: 2,
38
+ c: 3,
39
+ }.vmap {|v| v * 2 }
40
+ => { a: 2, b: 4, c: 6 }
41
+
42
+ {
43
+ a: 1,
44
+ b: 2,
45
+ c: 3,
46
+ }.vmap &:to_s
47
+ => { a: '2', b: '4', c: '6' }
48
+ ```
49
+
50
+ #### kmap
51
+ Transform the keys of a Hash
52
+ ```ruby
53
+ {
54
+ 'a' => 1,
55
+ 'b' => 2,
56
+ 'c' => 3,
57
+ }.kmap {|k, v| k * v }
58
+ => { 'a' => 1, 'bb' => 2, 'ccc' => 3 }
59
+
60
+ {
61
+ 'a' => 1,
62
+ 'b' => 2,
63
+ 'c' => 3,
64
+ }.kmap &:to_sym
65
+ => { a: 1, b: 2, c: 3 }
66
+ ```
67
+
68
+
69
+ ##
70
+ Thanks to [J. Pablo Fernández](https://github.com/pupeno/hmap)
71
+
72
+ ----
73
+ ![Gem](https://img.shields.io/gem/dt/hmap?style=plastic)
74
+ [![codecov](https://codecov.io/gh/dpep/rb_hmap/branch/main/graph/badge.svg)](https://codecov.io/gh/dpep/rb_hmap)
data/hmap.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.authors = ["Daniel Pepper"]
3
+ s.description = "Improved Hash mapping functions"
4
+ s.files = `git ls-files * ":!:spec"`.split("\n")
5
+ s.homepage = "https://github.com/dpep/rb_hmap"
6
+ s.license = "MIT"
7
+ s.name = File.basename(__FILE__, ".gemspec")
8
+ s.summary = "Hmap"
9
+ s.version = "1.0.1"
10
+
11
+ s.required_ruby_version = ">= 3"
12
+
13
+ s.add_development_dependency "byebug", ">= 11"
14
+ s.add_development_dependency "rspec", ">= 3.10"
15
+ s.add_development_dependency "simplecov", ">= 0.22"
16
+ end
data/lib/hmap/hmap.rb CHANGED
@@ -1,65 +1,60 @@
1
- class Hash
2
-
3
- class << self
4
-
5
- # create Hash from given keys and mapped values
6
- def map(obj, &block)
7
- res = obj.map do |*args|
8
- block.call(*args)
9
- end
10
-
11
- Hash[obj.zip res]
1
+ module Hmap
2
+ # create Hash from given keys and mapped values
3
+ def map(obj, &block)
4
+ res = obj.map do |*args|
5
+ block.call(*args)
12
6
  end
13
7
 
14
-
15
- # map an Enumerable into a Hash, like Hash[obj.map ... ]
16
- def hmap(obj, &block)
17
- Hash[
18
- obj.map do |*args|
19
- block.call(*args)
20
- end.compact
21
- ]
22
- end
23
-
24
- end # class << self
25
-
26
-
27
- # map the block's results back to a hash
28
- def hmap(&block)
29
- clone.hmap!(&block)
8
+ Hash[obj.zip res]
30
9
  end
31
10
 
32
- def hmap!(&block)
33
- data = Hash.hmap(self, &block)
34
- clear
35
- merge! data
11
+ # map an Enumerable into a Hash, like Hash[obj.map ... ]
12
+ def hmap(obj, &block)
13
+ Hash[
14
+ obj.map do |*args|
15
+ block.call(*args)
16
+ end.compact
17
+ ]
36
18
  end
37
19
 
38
-
39
- # map keys, but preserve associated values
40
- # ie. http://apidock.com/rails/v4.2.7/Hash/transform_keys
41
- def kmap(&block)
42
- clone.kmap!(&block)
20
+ def self.extended(base)
21
+ base.include(InstanceMethods)
43
22
  end
44
23
 
45
- def kmap!(&block)
46
- hmap! do |k, v|
47
- [ block.arity <= 1 ? block.call(k) : block.call(k, v), v ]
24
+ module InstanceMethods
25
+ # map the block's results back to a hash
26
+ def hmap(&block)
27
+ clone.hmap!(&block)
48
28
  end
49
- end
50
29
 
30
+ def hmap!(&block)
31
+ data = Hash.hmap(self, &block)
32
+ clear
33
+ merge! data
34
+ end
51
35
 
52
- # map values, but preserve associated keys
53
- # ie. http://apidock.com/rails/v4.2.7/Hash/transform_values
54
- def vmap(&block)
55
- clone.vmap!(&block)
56
- end
36
+ # map keys, but preserve associated values
37
+ # ie. http://apidock.com/rails/v4.2.7/Hash/transform_keys
38
+ def kmap(&block)
39
+ clone.kmap!(&block)
40
+ end
57
41
 
58
- def vmap!(&block)
59
- each do |k, v|
60
- self[k] = block.arity <= 1 ? block.call(v) : block.call(k, v)
42
+ def kmap!(&block)
43
+ hmap! do |k, v|
44
+ [ block.arity <= 1 ? block.call(k) : block.call(k, v), v ]
45
+ end
61
46
  end
62
- end
63
47
 
48
+ # map values, but preserve associated keys
49
+ # ie. http://apidock.com/rails/v4.2.7/Hash/transform_values
50
+ def vmap(&block)
51
+ clone.vmap!(&block)
52
+ end
64
53
 
54
+ def vmap!(&block)
55
+ each do |k, v|
56
+ self[k] = block.arity <= 1 ? block.call(v) : block.call(k, v)
57
+ end
58
+ end
59
+ end
65
60
  end
@@ -0,0 +1,3 @@
1
+ module Hmap
2
+ VERSION = Gem.loaded_specs["hmap"].version.to_s
3
+ end
data/lib/hmap.rb CHANGED
@@ -1,6 +1,4 @@
1
1
  require_relative 'hmap/hmap'
2
+ require_relative 'hmap/version'
2
3
 
3
-
4
- module Hmap
5
- VERSION = '1.0.0'
6
- end
4
+ Hash.extend(Hmap)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-03 00:00:00.000000000 Z
11
+ date: 2024-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -16,56 +16,62 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '11'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '11'
27
27
  - !ruby/object:Gem::Dependency
28
- name: minitest
28
+ name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3.10'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3.10'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: simplecov
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '0.22'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '0.22'
55
55
  description: Improved Hash mapping functions
56
- email:
56
+ email:
57
57
  executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
+ - CHANGELOG.md
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE.txt
65
+ - README.md
66
+ - hmap.gemspec
61
67
  - lib/hmap.rb
62
68
  - lib/hmap/hmap.rb
63
- - test/test_hmap.rb
69
+ - lib/hmap/version.rb
64
70
  homepage: https://github.com/dpep/rb_hmap
65
71
  licenses:
66
72
  - MIT
67
73
  metadata: {}
68
- post_install_message:
74
+ post_install_message:
69
75
  rdoc_options: []
70
76
  require_paths:
71
77
  - lib
@@ -73,17 +79,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
79
  requirements:
74
80
  - - ">="
75
81
  - !ruby/object:Gem::Version
76
- version: '0'
82
+ version: '3'
77
83
  required_rubygems_version: !ruby/object:Gem::Requirement
78
84
  requirements:
79
85
  - - ">="
80
86
  - !ruby/object:Gem::Version
81
87
  version: '0'
82
88
  requirements: []
83
- rubyforge_project:
84
- rubygems_version: 2.6.11
85
- signing_key:
89
+ rubygems_version: 3.5.15
90
+ signing_key:
86
91
  specification_version: 4
87
92
  summary: Hmap
88
- test_files:
89
- - test/test_hmap.rb
93
+ test_files: []
data/test/test_hmap.rb DELETED
@@ -1,279 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'hmap'
3
-
4
-
5
- class HmapTest < Minitest::Test
6
-
7
- def test_Hash_map
8
- data = {
9
- c: 2,
10
- b: 3,
11
- a: 1,
12
- }
13
-
14
- assert_equal(
15
- data,
16
- Hash.map(data.keys) {|x| data[x]}
17
- )
18
-
19
- assert_equal(
20
- {},
21
- Hash.map([]) {|x| nil}
22
- )
23
-
24
- assert_equal(
25
- {
26
- 1 => nil,
27
- 2 => nil,
28
- 3 => nil,
29
- },
30
- Hash.map([1,2,3]) {|x| nil}
31
- )
32
-
33
- assert_equal(
34
- {
35
- 1 => false,
36
- 2 => false,
37
- 3 => true,
38
- },
39
- Hash.map([1,2,3]) {|x| x > 2 }
40
- )
41
-
42
- assert_equal(
43
- { 1 => [ 2, 3 ] },
44
- Hash.map([1]) {|x| [ 2, 3 ] }
45
- )
46
- end
47
-
48
-
49
- def test_Hash_hmap
50
- data = {
51
- c: 2,
52
- b: 3,
53
- a: 1,
54
- }
55
-
56
- assert_equal(
57
- data,
58
- Hash.hmap(data) {|k, v| [k, v]}
59
- )
60
-
61
- assert_equal(
62
- { a: 2, b: 6, c: 4 },
63
- Hash.hmap(data) {|k, v| [k, v * 2]}
64
- )
65
-
66
- assert_equal(
67
- { 1 => 2, 2 => 4 },
68
- Hash.hmap([1, 2]) {|x| [ x, x * 2 ] }
69
- )
70
-
71
- assert_equal(
72
- data,
73
- Hash.hmap(data.keys) {|x| [x, data[x]]}
74
- )
75
-
76
- assert_equal(
77
- { nil => 3 },
78
- Hash.hmap([1,2,3]) {|x| [nil, x]}
79
- )
80
-
81
- assert_equal(
82
- {},
83
- Hash.hmap([]) {|x| nil}
84
- )
85
-
86
- assert_equal(
87
- {},
88
- Hash.hmap([1, 2, 3]) {|x| nil}
89
- )
90
- end
91
-
92
-
93
- def test_hmap
94
- data = {
95
- c: 2,
96
- b: 3,
97
- a: 1,
98
- }
99
-
100
- assert_equal(
101
- {
102
- c: 4,
103
- b: 6,
104
- a: 2,
105
- },
106
- data.hmap {|k, v| [k, v * 2]}
107
- )
108
-
109
- assert_equal(
110
- {
111
- cc: 4,
112
- bb: 6,
113
- aa: 2,
114
- },
115
- data.hmap {|k, v| [(k.to_s * 2).to_sym, v * 2]}
116
- )
117
-
118
- assert_equal(
119
- {},
120
- {}.hmap {|k, v| [k, v * 2]}
121
- )
122
-
123
- assert_equal(
124
- {},
125
- {a: 1}.hmap {|k, v| nil}
126
- )
127
-
128
-
129
- assert_equal(
130
- {
131
- c: 4,
132
- b: 6,
133
- a: 2,
134
- },
135
- data.hmap! {|k, v| [k, v * 2]}
136
- )
137
- assert_equal(
138
- {
139
- c: 4,
140
- b: 6,
141
- a: 2,
142
- },
143
- data
144
- )
145
- end
146
-
147
-
148
- def test_kmap
149
- data = {
150
- 'a' => 1,
151
- 'b' => 2,
152
- 'c' => 3,
153
- }
154
-
155
- assert_equal(
156
- data,
157
- data.kmap(&:to_s)
158
- )
159
-
160
- assert_equal(
161
- {
162
- 'aa' => 1,
163
- 'bb' => 2,
164
- 'cc' => 3,
165
- },
166
- data.kmap {|k| k * 2}
167
- )
168
-
169
- assert_equal(
170
- {
171
- 'aa' => 1,
172
- 'bb' => 2,
173
- 'cc' => 3,
174
- },
175
- data.kmap {|k, v| k * 2}
176
- )
177
-
178
- assert_equal(
179
- {
180
- 'aa' => 1,
181
- 'bb' => 2,
182
- 'cc' => 3,
183
- },
184
- data.kmap! {|k, v| k * 2}
185
- )
186
- assert_equal(
187
- {
188
- 'aa' => 1,
189
- 'bb' => 2,
190
- 'cc' => 3,
191
- },
192
- data
193
- )
194
- end
195
-
196
-
197
- def test_vmap
198
- data = {
199
- a: 1,
200
- b: 2,
201
- c: 3,
202
- }
203
-
204
- assert_equal(
205
- {
206
- a: 1.0,
207
- b: 2.0,
208
- c: 3.0,
209
- },
210
- data.vmap(&:to_f)
211
- )
212
-
213
- assert_equal(
214
- {
215
- a: 2,
216
- b: 4,
217
- c: 6,
218
- },
219
- data.vmap {|v| v * 2}
220
- )
221
-
222
- assert_equal(
223
- {
224
- a: 2,
225
- b: 4,
226
- c: 6,
227
- },
228
- data.vmap {|k, v| v * 2}
229
- )
230
-
231
- assert_equal(
232
- {
233
- a: :a,
234
- b: :b,
235
- c: :c,
236
- },
237
- data.vmap {|k, v| k}
238
- )
239
-
240
-
241
- assert_equal(
242
- {
243
- a: 2,
244
- b: 4,
245
- c: 6,
246
- },
247
- data.vmap! {|v| v * 2}
248
- )
249
- assert_equal(
250
- {
251
- a: 2,
252
- b: 4,
253
- c: 6,
254
- },
255
- data
256
- )
257
- end
258
-
259
-
260
- def test_default_value
261
- data = Hash.new 123
262
-
263
- assert_equal(
264
- 123,
265
- data.kmap {|v| nil }[:abc]
266
- )
267
-
268
- assert_equal(
269
- 123,
270
- data.vmap {|v| nil }[:abc]
271
- )
272
-
273
- assert_equal(
274
- 123,
275
- data.hmap {|v| nil }[:abc]
276
- )
277
- end
278
-
279
- end