deepmap 0.1.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/deepmap/deepmap.rb +21 -18
- data/readme.md +9 -16
- metadata +24 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8d0c139648afeaa87a8ea6bb16067cb7f1f92164da6027384b8bcd7ceac10eff
|
4
|
+
data.tar.gz: 74f443fb5271e3ceab1ff0937bc8144828961600ba72ac74268a39aef7563b33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0bc67590a7a5787783cfbf57be228d288cad7b0605a5fe4c2c9adc8ae1a3899ea4895b864c67d09e7203d01af248cc04e32645e42334b0a4b2d52a46f46bf3e6
|
7
|
+
data.tar.gz: d82348cf15853ec822742224830e407e52256fef6ae35229a2135a1763a713f74b2c33703fbd4133bf07604ed4a6aa170806b418129dc7b46fa5a32c984f21aa
|
data/lib/deepmap/deepmap.rb
CHANGED
@@ -3,20 +3,21 @@
|
|
3
3
|
module DeepMap
|
4
4
|
def deep_map
|
5
5
|
return self if !block_given?
|
6
|
-
recurse {|x| yield x}
|
6
|
+
recurse { |x| yield x }
|
7
7
|
end
|
8
8
|
|
9
9
|
def key_map
|
10
10
|
return self if !block_given?
|
11
|
-
recurse("key") {|x| yield x}
|
11
|
+
recurse("key") { |x| yield x }
|
12
12
|
end
|
13
13
|
|
14
14
|
def val_map
|
15
15
|
return self if !block_given?
|
16
|
-
recurse("val") {|x| yield x}
|
16
|
+
recurse("val") { |x| yield x }
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
|
+
|
20
21
|
# Main recursive method. With the block passed in, keep recursing through the
|
21
22
|
# object, applying the block to either each key or value (or both) depending
|
22
23
|
# on the first caller of this method. This is the value stored in 't' type. If
|
@@ -24,30 +25,33 @@ module DeepMap
|
|
24
25
|
# the key is an array, then map the recursive method over each item in the
|
25
26
|
# array. If it is niether a hash or an array, then apply the block to that
|
26
27
|
# item, and return the result.
|
27
|
-
def recurse(t="both", h=self)
|
28
|
-
if h.is_a?(Hash)
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
[ yield(k), recurse(t, v) {|x| yield x } ]
|
34
|
-
when "val"
|
35
|
-
[ k, recurse(t, v) {|x| yield x} ]
|
36
|
-
end
|
37
|
-
}]
|
29
|
+
def recurse(t = "both", h = self)
|
30
|
+
if h.is_a?(Hash)
|
31
|
+
hash_collect(t, h) { |x| yield x }
|
38
32
|
|
39
33
|
elsif h.is_a?(Array)
|
40
|
-
|
41
|
-
h.map {|v| recurse(t, v) {|x| yield(x) } }
|
34
|
+
h.map { |v| recurse(t, v) { |x| yield x } }
|
42
35
|
|
43
36
|
else # apply to value
|
44
|
-
|
45
37
|
t == "key" ? h : yield(h)
|
46
38
|
|
47
39
|
end
|
48
40
|
end
|
49
|
-
end
|
50
41
|
|
42
|
+
def hash_collect(t, h)
|
43
|
+
Hash[h.collect { |k, v|
|
44
|
+
case t # type
|
45
|
+
when "both", "key"
|
46
|
+
[yield(k), recurse(t, v) { |x| yield x }]
|
47
|
+
|
48
|
+
when "val"
|
49
|
+
[k, recurse(t, v) { |x| yield x }]
|
50
|
+
|
51
|
+
end
|
52
|
+
}]
|
53
|
+
end
|
54
|
+
end
|
51
55
|
|
52
56
|
# Adding deep_map, key_map, val_map methods to Hash and Array classes.
|
53
57
|
|
@@ -58,4 +62,3 @@ end
|
|
58
62
|
class Array
|
59
63
|
include DeepMap
|
60
64
|
end
|
61
|
-
|
data/readme.md
CHANGED
@@ -1,12 +1,8 @@
|
|
1
|
-
deepmap
|
2
|
-
=======
|
3
|
-
|
1
|
+
# deepmap
|
4
2
|
|
5
3
|
[![Gem Version](https://badge.fury.io/rb/deepmap.svg)](https://badge.fury.io/rb/deepmap)
|
6
|
-
[![Build Status](https://travis-ci.org/jeremywrnr/deepmap.svg?branch=master)](https://travis-ci.org/jeremywrnr/deepmap)
|
7
4
|
[![MIT](https://img.shields.io/npm/l/alt.svg?style=flat)](http://jeremywrnr.com/mit-license)
|
8
5
|
|
9
|
-
|
10
6
|
## setup
|
11
7
|
|
12
8
|
[sudo] gem install deepmap
|
@@ -15,16 +11,15 @@ deepmap
|
|
15
11
|
|
16
12
|
Ruby gem that adds three methods to the Hash and Array classes. Overview:
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
* `#deep_map` - apply block to each key and value in object
|
15
|
+
* `#key_map` - apply block to each key in object
|
16
|
+
* `#val_map` - apply block to each value in object
|
21
17
|
|
22
18
|
These may be useful when you want to apply a function to each value (or key or
|
23
19
|
pair) of a complex nested object (such as the result of parsing a YAML or JSON
|
24
20
|
file), which can have hashes or arrays as subfields. The function currently
|
25
21
|
needs to be passed in as a block (see usage).
|
26
22
|
|
27
|
-
|
28
23
|
## usage
|
29
24
|
|
30
25
|
```ruby
|
@@ -46,7 +41,7 @@ irb(main):005:0> test.val_map {|i| i.to_i * 2 }
|
|
46
41
|
|
47
42
|
Once you `require 'deepmap'`, you can call any of the three provided functions
|
48
43
|
on any (existing or new!) hash or array, as demonstrated above. You can also
|
49
|
-
use the (
|
44
|
+
use the (`&:method`) shortcut to call a method on each object concisely:
|
50
45
|
|
51
46
|
```ruby
|
52
47
|
irb(main):002:0> test = { 'a' => 'b', 'c' => ['d', 'e'], 'f' => { 'g' => ['h', 'i', { 'j' => 'k' }] } }
|
@@ -62,7 +57,6 @@ irb(main):005:0> test.val_map(&:upcase)
|
|
62
57
|
=> {"a"=>"B", "c"=>["D", "E"], "f"=>{"g"=>["H", "I", {"j"=>"K"}]}}
|
63
58
|
```
|
64
59
|
|
65
|
-
|
66
60
|
## development / testing
|
67
61
|
|
68
62
|
First, clone this repo:
|
@@ -78,17 +72,16 @@ try running `gem install bundler`. Once that is done:
|
|
78
72
|
Now, we should be able to build the gem locally. This will build the local
|
79
73
|
deepmap gem and link it in your path, so you can playing around with `deepmap`.
|
80
74
|
|
81
|
-
rake build
|
75
|
+
bundle exec rake build
|
82
76
|
|
83
77
|
This uses `rspec` and `rake` to run a suite of unit tests. To run the suite:
|
84
78
|
|
85
|
-
rake
|
86
|
-
|
79
|
+
bundle exec rake
|
87
80
|
|
88
81
|
## todo
|
89
82
|
|
90
|
-
|
83
|
+
* collison checking/warning/confirmation workflow
|
84
|
+
* support key-val iteration in mapping over vals/keys. This would allow support
|
91
85
|
for mapping conditionally based on a key/value, even if you are iterating
|
92
86
|
over all values/keys (`key_map {|k, v| k > 0 ? k : v }`).
|
93
87
|
|
94
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deepmap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Warner
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -16,28 +16,42 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.8.0
|
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:
|
26
|
+
version: 3.8.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 12.3.3
|
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:
|
40
|
+
version: 12.3.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rufo
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.15.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.15.1
|
41
55
|
description: Map functions over nested hash/arrays objects (e.g. JSON).
|
42
56
|
email: jeremywrnr@gmail.com
|
43
57
|
executables: []
|
@@ -50,7 +64,7 @@ homepage: http://github.com/jeremywrnr/deepmap
|
|
50
64
|
licenses:
|
51
65
|
- MIT
|
52
66
|
metadata: {}
|
53
|
-
post_install_message:
|
67
|
+
post_install_message:
|
54
68
|
rdoc_options: []
|
55
69
|
require_paths:
|
56
70
|
- lib/deepmap
|
@@ -65,10 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
79
|
- !ruby/object:Gem::Version
|
66
80
|
version: '0'
|
67
81
|
requirements: []
|
68
|
-
|
69
|
-
|
70
|
-
signing_key:
|
82
|
+
rubygems_version: 3.3.7
|
83
|
+
signing_key:
|
71
84
|
specification_version: 4
|
72
85
|
summary: Nested hash/array function mapping.
|
73
86
|
test_files: []
|
74
|
-
has_rdoc:
|