rekey 0.0.1 → 0.0.3

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
  SHA1:
3
- metadata.gz: aef7b449f66795bace280c82a74dc2ec9b6fb0e9
4
- data.tar.gz: 00ce3968fcfa316c3f9f49279e26b46c0bd30d8c
3
+ metadata.gz: fa0f9ac28de18ed312d7886726801ae4eeabc6ed
4
+ data.tar.gz: dfa6edfbb2dfd2313044f416e56bdeae376c3997
5
5
  SHA512:
6
- metadata.gz: 23b83545770104e3f968e1dfcf7ae3a503a87cce2b9452773bd4edec3b0da3ea3d380de358aea09aaa625e3fbfafbf53a858cb2bc1851f10d176886641af4c51
7
- data.tar.gz: b78c2be0c10cfc6f56594b08987208433a7b2b2f1b255296bc996f7cc1bfea7614934095a08455a14b4dcb6d6c86479e4b7a9c035455b3579a483870129c9f03
6
+ metadata.gz: fe5cb87711746ab6717dd2e90566110841a8520dd611c88fa56b7f93734dfd36e8416f67b94fc4539a30cff793e6e583b4ece0f5529c466cd3f4327ab2be4fb1
7
+ data.tar.gz: 1c1f02d4cef3e52f5d40fdad3ea2dac4299100af0971e0a361e73662e536ae7a79efc94b7d26ac9d1b75d5a42022f956263bd751b715e3a365f3acfb5e3cfe10
data/lib/rekey.rb CHANGED
@@ -1,6 +1,3 @@
1
- require 'awesome_print'
2
- require 'byebug'
3
-
4
1
  module Rekey
5
2
  class << self
6
3
 
@@ -12,7 +9,7 @@ module Rekey
12
9
  end
13
10
  else
14
11
  unless key_handle or value_handle
15
- raise ArgumentError.new 'expected key_handle or value_handle'
12
+ raise ArgumentError.new 'expected 1 or 2 args, got 0'
16
13
  end
17
14
  end
18
15
 
@@ -28,14 +25,8 @@ module Rekey
28
25
  proc {|v| v}
29
26
  end
30
27
 
28
+ # return type will be determined later
31
29
  res = nil
32
- # determine return type. generally a Hash, except when input
33
- # is an Array and key_handle is null
34
- if block
35
- res = {}
36
- else
37
- res = (key_handle or enumerable.respond_to?(:keys)) ? {} : []
38
- end
39
30
 
40
31
  enumerable.each do |*args|
41
32
  key = key_fn.call *args
@@ -45,13 +36,19 @@ module Rekey
45
36
 
46
37
  if block
47
38
  if block.arity <= 0
48
- # function pointer
39
+ # function pointer, eg. &:to_i
49
40
  new_key = block.call value
50
41
  elsif block.arity == 1
51
42
  # standard block
52
43
  new_key = block.call value
53
44
  else
54
- new_key, new_value = block.call key, value
45
+ data = block.call key, value
46
+ if data.is_a? Array
47
+ new_key, new_value = data
48
+ else
49
+ # only returned a new key value
50
+ new_key = data
51
+ end
55
52
  end
56
53
  else
57
54
  new_key = pull value, key_handle if key_handle
@@ -59,7 +56,21 @@ module Rekey
59
56
  end
60
57
 
61
58
  # collect results
59
+
60
+ unless res
61
+ # determine return type based on the first
62
+ # computed key value
63
+ res = new_key ? {} : []
64
+ end
65
+
62
66
  if res.is_a? Array
67
+ unless new_key.nil?
68
+ # safeguard against stupidity
69
+ raise ArgumentError.new(
70
+ "not expecting a key value, got: #{new_key}"
71
+ )
72
+ end
73
+
63
74
  res.push new_value
64
75
  else
65
76
  res[new_key] = new_value
@@ -73,15 +84,28 @@ module Rekey
73
84
  private
74
85
 
75
86
  def pull v, handle
76
- # byebug
77
- if v.respond_to? handle
87
+ if ([Symbol, String].include? handle.class) and v.respond_to? handle
78
88
  if v.method(handle).arity <= 0
79
89
  v.send handle
80
90
  else
81
91
  v.send handle, v
82
92
  end
83
- else
93
+ elsif v.is_a? Array
94
+ if handle.is_a? String and handle.include? '..'
95
+ # properly format ranges
96
+ ends = handle.split('.').reject(&:empty?).map(&:to_i)
97
+ handle = Range.new(
98
+ ends.first,
99
+ ends.last,
100
+ handle.include?('...') # exclude_end
101
+ )
102
+ end
84
103
 
104
+ v[handle]
105
+ elsif v.is_a? Hash
106
+ v[handle]
107
+ else
108
+ raise ArgumentError.new "invalid handle: #{handle}, for value #{v}"
85
109
  end
86
110
  end
87
111
 
data/lib/rekey/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rekey
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -0,0 +1,76 @@
1
+ require 'minitest/autorun'
2
+ require 'rekey'
3
+
4
+
5
+ class RekeyArrayBlockTest < Minitest::Test
6
+
7
+ def setup
8
+ @input = [*1..3]
9
+ end
10
+
11
+ def test_both
12
+ assert_equal({
13
+ 1 => 1,
14
+ 2 => 2,
15
+ 3 => 3,
16
+ },
17
+ @input.rekey {|k, v| [v, v] }
18
+ )
19
+
20
+ assert_equal({
21
+ '1' => 1.0,
22
+ '2' => 2.0,
23
+ '3' => 3.0,
24
+ },
25
+ @input.rekey {|k, v| [v.to_s, v.to_f] }
26
+ )
27
+ end
28
+
29
+ def test_keys
30
+ assert_equal({
31
+ 1 => 1,
32
+ 2 => 2,
33
+ 3 => 3,
34
+ },
35
+ @input.rekey {|v| v }
36
+ )
37
+
38
+ assert_equal({
39
+ 2 => 1,
40
+ 4 => 2,
41
+ 6 => 3,
42
+ },
43
+ @input.rekey {|v| v * 2 }
44
+ )
45
+
46
+ assert_equal({
47
+ '1' => 1,
48
+ '2' => 2,
49
+ '3' => 3,
50
+ },
51
+ @input.rekey {|v| v.to_s }
52
+ )
53
+ end
54
+
55
+ def test_nil_key
56
+ assert_equal(
57
+ @input,
58
+ @input.rekey {|k, v| [nil, v.to_i] }
59
+ )
60
+
61
+ assert_equal(
62
+ @input,
63
+ # k is nil because input is array
64
+ @input.rekey {|k, v| [k, v.to_i] }
65
+ )
66
+
67
+ assert_equal([
68
+ '1',
69
+ '2',
70
+ '3',
71
+ ],
72
+ @input.rekey {|k, v| [nil, v.to_s] }
73
+ )
74
+ end
75
+
76
+ end
@@ -0,0 +1,183 @@
1
+ require 'minitest/autorun'
2
+ require 'rekey'
3
+
4
+
5
+ class RekeyArrayHandlersTest < Minitest::Test
6
+
7
+ def setup
8
+ @input = [*1..3]
9
+ end
10
+
11
+ def test_both
12
+ assert_equal({
13
+ 1 => 1,
14
+ 2 => 2,
15
+ 3 => 3,
16
+ },
17
+ @input.rekey(:to_i, :to_i)
18
+ )
19
+
20
+ assert_equal({
21
+ '1' => 1.0,
22
+ '2' => 2.0,
23
+ '3' => 3.0,
24
+ },
25
+ @input.rekey(:to_s, :to_f)
26
+ )
27
+
28
+ assert_equal({
29
+ '1' => 1.0,
30
+ '2' => 2.0,
31
+ '3' => 3.0,
32
+ },
33
+ @input.rekey('to_s', 'to_f')
34
+ )
35
+ end
36
+
37
+ def test_keys
38
+ assert_equal({
39
+ '1' => 1,
40
+ '2' => 2,
41
+ '3' => 3,
42
+ },
43
+ @input.rekey(:to_s)
44
+ )
45
+
46
+ assert_equal({
47
+ '1' => 1,
48
+ '2' => 2,
49
+ '3' => 3,
50
+ },
51
+ @input.rekey('to_s')
52
+ )
53
+
54
+ assert_equal({
55
+ 1.0 => 1,
56
+ 2.0 => 2,
57
+ 3.0 => 3,
58
+ },
59
+ @input.rekey(:to_f)
60
+ )
61
+
62
+ assert_equal({
63
+ 1.0 => 1,
64
+ 2.0 => 2,
65
+ 3.0 => 3,
66
+ },
67
+ @input.rekey(&:to_f)
68
+ )
69
+ end
70
+
71
+ def test_values
72
+ assert_equal(
73
+ @input,
74
+ @input.rekey(nil, :to_i)
75
+ )
76
+
77
+ assert_equal(
78
+ @input.map(&:to_f),
79
+ @input.rekey(nil, :to_f)
80
+ )
81
+
82
+ assert_equal(
83
+ @input.map(&:to_s),
84
+ @input.rekey(nil, 'to_s')
85
+ )
86
+ end
87
+
88
+ def test_array_indices
89
+ array_data = [
90
+ [ 1, 2, 3 ],
91
+ [ 4, 5, 6 ],
92
+ [ 7, 8, 9 ],
93
+ ]
94
+
95
+ assert_equal(
96
+ array_data,
97
+ array_data.rekey(nil, :to_a)
98
+ )
99
+
100
+ assert_equal(
101
+ [ 1, 4, 7 ],
102
+ array_data.rekey(nil, 0)
103
+ )
104
+
105
+ assert_equal({
106
+ 1 => 3,
107
+ 4 => 6,
108
+ 7 => 9,
109
+ },
110
+ array_data.rekey(0, 2)
111
+ )
112
+
113
+ array_data = [
114
+ [ 1 ],
115
+ [ 2, 1 ],
116
+ [ 3, 2, 1 ],
117
+ ]
118
+ assert_equal({
119
+ 1 => 1,
120
+ 2 => 2,
121
+ 3 => 3,
122
+ },
123
+ array_data.rekey(0, :count)
124
+ )
125
+ end
126
+
127
+ def test_array_hash
128
+ data = [
129
+ { a: 1 },
130
+ { b: 2 },
131
+ { c: 3 },
132
+ ]
133
+
134
+ assert_equal(
135
+ [ [:a], [:b], [:c] ],
136
+ data.rekey(nil, :keys)
137
+ )
138
+
139
+ assert_equal(
140
+ [ :a, :b, :c ],
141
+ data.rekey(nil, :keys).rekey(nil, :first)
142
+ )
143
+
144
+ assert_equal({
145
+ { a: 1 }.hash => data.first,
146
+ },
147
+ data.take(1).rekey(:hash)
148
+ )
149
+
150
+ assert_equal({
151
+ { a: 1 }.hash => [:a, 1],
152
+ },
153
+ data.take(1).rekey(:hash, :first)
154
+ )
155
+ end
156
+
157
+
158
+ def test_hash_indices
159
+ data = [
160
+ { i: 1, v: 'a' },
161
+ { i: 2, v: 'b' },
162
+ { i: 3, v: 'c' },
163
+ ]
164
+
165
+ assert_equal({
166
+ 1 => data[0],
167
+ 2 => data[1],
168
+ 3 => data[2],
169
+ },
170
+ data.rekey(:i)
171
+ )
172
+
173
+ assert_equal({
174
+ 1 => 'a',
175
+ 2 => 'b',
176
+ 3 => 'c',
177
+ },
178
+ data.rekey(:i, :v)
179
+ )
180
+ end
181
+
182
+
183
+ end
@@ -0,0 +1,100 @@
1
+ require 'minitest/autorun'
2
+ require 'rekey'
3
+
4
+
5
+ class RekeyHashBlockTest < Minitest::Test
6
+ def setup
7
+ @input = {
8
+ a: 1,
9
+ b: 2,
10
+ c: 3,
11
+ }
12
+ end
13
+
14
+ def test_both
15
+ assert_equal(
16
+ @input,
17
+ @input.rekey {|k, v| [k, v] }
18
+ )
19
+
20
+ assert_equal({
21
+ 1 => 1,
22
+ 2 => 2,
23
+ 3 => 3,
24
+ },
25
+ @input.rekey {|k, v| [v, v] }
26
+ )
27
+
28
+ assert_equal({
29
+ '1' => 1.0,
30
+ '2' => 2.0,
31
+ '3' => 3.0,
32
+ },
33
+ @input.rekey {|k, v| [v.to_s, v.to_f] }
34
+ )
35
+ end
36
+
37
+ def test_arity1_keys
38
+ assert_equal({
39
+ 1 => 1,
40
+ 2 => 2,
41
+ 3 => 3,
42
+ },
43
+ @input.rekey {|v| v }
44
+ )
45
+
46
+ assert_equal({
47
+ '1' => 1,
48
+ '2' => 2,
49
+ '3' => 3,
50
+ },
51
+ @input.rekey {|v| v.to_s }
52
+ )
53
+
54
+ # it can use Arrays as Hash keys...if you really want to
55
+ assert_equal({
56
+ [1] => 1,
57
+ [2] => 2,
58
+ [3] => 3,
59
+ },
60
+ @input.rekey {|v| [v] }
61
+ )
62
+ end
63
+
64
+ def test_arity2_keys
65
+ assert_equal({
66
+ 1 => 1,
67
+ 2 => 2,
68
+ 3 => 3,
69
+ },
70
+ @input.rekey {|k, v| v }
71
+ )
72
+
73
+ assert_equal({
74
+ a: 1,
75
+ b: 2,
76
+ c: 3,
77
+ },
78
+ @input.rekey {|k, v| k }
79
+ )
80
+
81
+ assert_equal({
82
+ 'aa' => 1,
83
+ 'bb' => 2,
84
+ 'cc' => 3,
85
+ },
86
+ @input.rekey {|k, v| k.to_s * 2 }
87
+ )
88
+ end
89
+
90
+ def test_nil_key
91
+ assert_equal([
92
+ 1,
93
+ 2,
94
+ 3,
95
+ ],
96
+ @input.rekey {|k, v| [nil, v] }
97
+ )
98
+ end
99
+
100
+ end
@@ -0,0 +1,131 @@
1
+ require 'minitest/autorun'
2
+ require 'rekey'
3
+
4
+
5
+ class RekeyHashHandlersTest < Minitest::Test
6
+ def setup
7
+ @input = {
8
+ a: 1,
9
+ b: 2,
10
+ c: 3,
11
+ }
12
+ end
13
+
14
+ def test_both
15
+ assert_equal({
16
+ 1 => 1,
17
+ 2 => 2,
18
+ 3 => 3,
19
+ },
20
+ @input.rekey(:to_i, :to_i)
21
+ )
22
+
23
+ assert_equal({
24
+ '1' => 1.0,
25
+ '2' => 2.0,
26
+ '3' => 3.0,
27
+ },
28
+ @input.rekey(:to_s, :to_i)
29
+ )
30
+
31
+ assert_equal({
32
+ '1' => 1.0,
33
+ '2' => 2.0,
34
+ '3' => 3.0,
35
+ },
36
+ @input.rekey('to_s', 'to_i')
37
+ )
38
+
39
+ end
40
+
41
+ def test_keys
42
+ assert_equal({
43
+ '1' => 1,
44
+ '2' => 2,
45
+ '3' => 3,
46
+ },
47
+ @input.rekey(:to_s)
48
+ )
49
+
50
+ assert_equal({
51
+ 1.0 => 1,
52
+ 2.0 => 2,
53
+ 3.0 => 3,
54
+ },
55
+ @input.rekey(:to_f)
56
+ )
57
+ end
58
+
59
+ def test_values
60
+ assert_equal(
61
+ @input,
62
+ @input.rekey(nil, :to_i)
63
+ )
64
+
65
+ assert_equal({
66
+ a: '1',
67
+ b: '2',
68
+ c: '3',
69
+ },
70
+ @input.rekey(nil, :to_s)
71
+ )
72
+
73
+ assert_equal({
74
+ a: '1',
75
+ b: '2',
76
+ c: '3',
77
+ },
78
+ @input.rekey(nil, 'to_s')
79
+ )
80
+ end
81
+
82
+ def test_hash_array
83
+ data = {
84
+ a: [ 1, 2, 3 ],
85
+ b: [ 4, 5, 6 ],
86
+ c: [ 7, 8, 9 ],
87
+ }
88
+
89
+ assert_equal({
90
+ a: 1,
91
+ b: 4,
92
+ c: 7,
93
+ },
94
+ data.rekey(nil, 0)
95
+ )
96
+
97
+ assert_equal({
98
+ 1 => 2,
99
+ 4 => 5,
100
+ 7 => 8,
101
+ },
102
+ data.rekey(0, 1)
103
+ )
104
+ end
105
+
106
+ def test_hash_hash
107
+ data = {
108
+ a: { i: 1, v: 2 },
109
+ b: { i: 2, v: 4 },
110
+ c: { i: 3, v: 6 },
111
+ }
112
+
113
+ assert_equal({
114
+ a: 1,
115
+ b: 2,
116
+ c: 3,
117
+ },
118
+ data.rekey(nil, :i)
119
+ )
120
+
121
+ assert_equal({
122
+ 1 => 2,
123
+ 2 => 4,
124
+ 3 => 6,
125
+ },
126
+ data.rekey(:i, :v)
127
+ )
128
+
129
+ end
130
+
131
+ end
@@ -0,0 +1,44 @@
1
+ require 'minitest/autorun'
2
+ require 'rekey'
3
+
4
+
5
+ class RekeyRangeTest < Minitest::Test
6
+
7
+ def setup
8
+ @input = [
9
+ [ 1, 2, 3 ],
10
+ [ 4, 5, 6 ],
11
+ ]
12
+ end
13
+
14
+ def test_stuff
15
+ assert_equal([
16
+ [ 1, 2 ],
17
+ [ 4, 5 ],
18
+ ],
19
+ @input.rekey(nil, '0..1')
20
+ )
21
+
22
+ assert_equal([
23
+ [ 2 ],
24
+ [ 5 ],
25
+ ],
26
+ @input.rekey(nil, '1...2')
27
+ )
28
+
29
+ assert_equal({
30
+ [ 2 ] => @input[0],
31
+ [ 5 ] => @input[1],
32
+ },
33
+ @input.rekey('1...2')
34
+ )
35
+
36
+ assert_equal({
37
+ 3 => [ 1, 2 ],
38
+ 6 => [ 4, 5 ],
39
+ },
40
+ @input.rekey(2, '0..1')
41
+ )
42
+ end
43
+
44
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rekey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-28 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2016-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: |2
14
28
  For turning Arrays into Hashes, with keys computed from the Array
15
29
  contents. Or for rekeying Hashes.
@@ -23,6 +37,11 @@ extra_rdoc_files: []
23
37
  files:
24
38
  - lib/rekey.rb
25
39
  - lib/rekey/version.rb
40
+ - test/test_array_block.rb
41
+ - test/test_array_handlers.rb
42
+ - test/test_hash_block.rb
43
+ - test/test_hash_handlers.rb
44
+ - test/test_ranges.rb
26
45
  homepage: https://github.com/d1hotpep/rekey
27
46
  licenses:
28
47
  - MIT
@@ -47,4 +66,9 @@ rubygems_version: 2.5.1
47
66
  signing_key:
48
67
  specification_version: 4
49
68
  summary: Rekey
50
- test_files: []
69
+ test_files:
70
+ - test/test_array_block.rb
71
+ - test/test_array_handlers.rb
72
+ - test/test_hash_block.rb
73
+ - test/test_hash_handlers.rb
74
+ - test/test_ranges.rb