rudash 2.8.0 → 2.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rudash/at.rb +11 -10
  3. data/lib/rudash/capitalize.rb +9 -7
  4. data/lib/rudash/compact.rb +7 -5
  5. data/lib/rudash/concat.rb +10 -8
  6. data/lib/rudash/curry.rb +9 -7
  7. data/lib/rudash/difference.rb +14 -12
  8. data/lib/rudash/each.rb +12 -10
  9. data/lib/rudash/each_right.rb +14 -12
  10. data/lib/rudash/eq.rb +5 -3
  11. data/lib/rudash/every.rb +8 -6
  12. data/lib/rudash/filter.rb +25 -25
  13. data/lib/rudash/find.rb +10 -10
  14. data/lib/rudash/find_last.rb +10 -9
  15. data/lib/rudash/flip.rb +9 -7
  16. data/lib/rudash/get.rb +26 -25
  17. data/lib/rudash/head.rb +10 -8
  18. data/lib/rudash/identity.rb +5 -3
  19. data/lib/rudash/initial.rb +7 -5
  20. data/lib/rudash/intersection.rb +13 -11
  21. data/lib/rudash/is_array.rb +5 -3
  22. data/lib/rudash/is_empty.rb +9 -7
  23. data/lib/rudash/is_equal.rb +5 -3
  24. data/lib/rudash/is_hash.rb +5 -3
  25. data/lib/rudash/is_nil.rb +5 -3
  26. data/lib/rudash/is_number.rb +5 -3
  27. data/lib/rudash/is_proc.rb +5 -3
  28. data/lib/rudash/is_string.rb +9 -7
  29. data/lib/rudash/join.rb +7 -5
  30. data/lib/rudash/keys.rb +11 -9
  31. data/lib/rudash/last.rb +6 -4
  32. data/lib/rudash/map.rb +23 -22
  33. data/lib/rudash/negate.rb +11 -9
  34. data/lib/rudash/pick.rb +20 -20
  35. data/lib/rudash/reduce.rb +34 -32
  36. data/lib/rudash/reduce_right.rb +8 -6
  37. data/lib/rudash/remove.rb +12 -12
  38. data/lib/rudash/reverse.rb +9 -7
  39. data/lib/rudash/set.rb +22 -20
  40. data/lib/rudash/size.rb +8 -6
  41. data/lib/rudash/slice.rb +13 -11
  42. data/lib/rudash/some.rb +8 -6
  43. data/lib/rudash/tail.rb +6 -4
  44. data/lib/rudash/union.rb +18 -18
  45. data/lib/rudash/uniq.rb +11 -9
  46. data/lib/rudash/update.rb +10 -11
  47. data/lib/rudash/without.rb +8 -6
  48. data/lib/rudash.rb +46 -46
  49. data/lib/utils/index.rb +14 -12
  50. data/lib/utils/nested_path_creator.rb +22 -20
  51. data/lib/utils/path_resolver.rb +17 -15
  52. data/lib/utils/subset_deep_match.rb +42 -40
  53. metadata +1 -1
data/lib/rudash/keys.rb CHANGED
@@ -1,12 +1,14 @@
1
- module Keys
2
- def keys(value)
3
- case value
4
- when Hash
5
- value.map { |key, value| "#{key}" }
6
- when Array
7
- value.map.with_index { |value, index| "#{index}" }
8
- else
9
- []
1
+ module Rudash
2
+ module Keys
3
+ def keys(value)
4
+ case value
5
+ when Hash
6
+ value.map { |key, value| "#{key}" }
7
+ when Array
8
+ value.map.with_index { |value, index| "#{index}" }
9
+ else
10
+ []
11
+ end
10
12
  end
11
13
  end
12
14
  end
data/lib/rudash/last.rb CHANGED
@@ -1,6 +1,8 @@
1
- module Last
2
- def last(array)
3
- return nil if !array.is_a?(Array)
4
- array.last
1
+ module Rudash
2
+ module Last
3
+ def last(array)
4
+ return nil if !array.is_a?(Array)
5
+ array.last
6
+ end
5
7
  end
6
8
  end
data/lib/rudash/map.rb CHANGED
@@ -1,29 +1,30 @@
1
1
  require_relative 'head.rb'
2
2
  require_relative 'identity.rb'
3
3
 
4
- module Map
5
- extend Head
6
- extend Identity
7
-
8
- def map(collection, *rest_args)
9
- mapper_proc = self.head(rest_args) || self.method(:identity)
10
-
11
- col = collection.is_a?(String) ? collection.split('') : collection
12
-
13
- if col.is_a?(Array)
14
- begin
15
- return col.map.with_index { |value, index| mapper_proc[value, index] }
16
- rescue ArgumentError => e
17
- return col.map { |value| mapper_proc[value] }
18
- end
19
- elsif col.is_a?(Hash)
20
- begin
21
- return col.map { |k,v| mapper_proc[v, k] }
22
- rescue ArgumentError => e
23
- return col.map { |k,v| mapper_proc[v] }
4
+ module Rudash
5
+ module Map
6
+ extend Rudash
7
+
8
+ def map(collection, *rest_args)
9
+ mapper_proc = self.head(rest_args) || self.method(:identity)
10
+
11
+ col = collection.is_a?(String) ? collection.split('') : collection
12
+
13
+ if col.is_a?(Array)
14
+ begin
15
+ return col.map.with_index { |value, index| mapper_proc[value, index] }
16
+ rescue ArgumentError => e
17
+ return col.map { |value| mapper_proc[value] }
18
+ end
19
+ elsif col.is_a?(Hash)
20
+ begin
21
+ return col.map { |k,v| mapper_proc[v, k] }
22
+ rescue ArgumentError => e
23
+ return col.map { |k,v| mapper_proc[v] }
24
+ end
25
+ else
26
+ return []
24
27
  end
25
- else
26
- return []
27
28
  end
28
29
  end
29
30
  end
data/lib/rudash/negate.rb CHANGED
@@ -1,12 +1,14 @@
1
- module Negate
2
- def negate(a_proc)
3
- case a_proc
4
- when Proc
5
- negate_proc = -> (*args) {
6
- !a_proc[*args]
7
- }
8
- else
9
- raise 'Expected a Proc'
1
+ module Rudash
2
+ module Negate
3
+ def negate(a_proc)
4
+ case a_proc
5
+ when Proc
6
+ negate_proc = -> (*args) {
7
+ !a_proc[*args]
8
+ }
9
+ else
10
+ raise 'Expected a Proc'
11
+ end
10
12
  end
11
13
  end
12
14
  end
data/lib/rudash/pick.rb CHANGED
@@ -2,25 +2,25 @@ require_relative 'each.rb'
2
2
  require_relative 'get.rb'
3
3
  require_relative 'set.rb'
4
4
 
5
- module Pick
6
- extend Each
7
- extend Get
8
- extend Set
9
-
10
- def pick(hash, paths)
11
- return self.pick(hash, [paths]) if !paths.is_a?(Array)
12
- return {} if !hash.is_a?(Hash)
13
-
14
- picked_hash = {}
15
-
16
- eacher = -> (path) {
17
- value = self.get(hash, path)
18
- if !value.nil?
19
- self.set(picked_hash, path, value)
20
- end
21
- }
22
-
23
- self.each(paths, eacher)
24
- picked_hash
5
+ module Rudash
6
+ module Pick
7
+ extend Rudash
8
+
9
+ def pick(hash, paths)
10
+ return self.pick(hash, [paths]) if !paths.is_a?(Array)
11
+ return {} if !hash.is_a?(Hash)
12
+
13
+ picked_hash = {}
14
+
15
+ eacher = -> (path) {
16
+ value = self.get(hash, path)
17
+ if !value.nil?
18
+ self.set(picked_hash, path, value)
19
+ end
20
+ }
21
+
22
+ self.each(paths, eacher)
23
+ picked_hash
24
+ end
25
25
  end
26
26
  end
data/lib/rudash/reduce.rb CHANGED
@@ -1,37 +1,39 @@
1
- module Reduce
2
- def reduce(collection, *rest_args)
3
- reducer = rest_args[0]
4
- initial_state = rest_args[1]
5
-
6
- col = collection.is_a?(String) ? collection.split('') : collection
7
-
8
- case rest_args.size
9
- when 1
10
- return col.reduce { |acc, current|
11
- if col.is_a?(Hash)
12
- begin
13
- reducer[acc, current[1], current[0]]
14
- rescue ArgumentError => e
15
- reducer[acc, current[1]]
1
+ module Rudash
2
+ module Reduce
3
+ def reduce(collection, *rest_args)
4
+ reducer = rest_args[0]
5
+ initial_state = rest_args[1]
6
+
7
+ col = collection.is_a?(String) ? collection.split('') : collection
8
+
9
+ case rest_args.size
10
+ when 1
11
+ return col.reduce { |acc, current|
12
+ if col.is_a?(Hash)
13
+ begin
14
+ reducer[acc, current[1], current[0]]
15
+ rescue ArgumentError => e
16
+ reducer[acc, current[1]]
17
+ end
18
+ else
19
+ reducer[acc, current]
16
20
  end
17
- else
18
- reducer[acc, current]
19
- end
20
- }
21
- when 2
22
- return col.reduce(initial_state) { |acc, current|
23
- if col.is_a?(Hash)
24
- begin
25
- reducer[acc, current[1], current[0]]
26
- rescue ArgumentError => e
27
- reducer[acc, current[1]]
21
+ }
22
+ when 2
23
+ return col.reduce(initial_state) { |acc, current|
24
+ if col.is_a?(Hash)
25
+ begin
26
+ reducer[acc, current[1], current[0]]
27
+ rescue ArgumentError => e
28
+ reducer[acc, current[1]]
29
+ end
30
+ else
31
+ reducer[acc, current]
28
32
  end
29
- else
30
- reducer[acc, current]
31
- end
32
- }
33
- else
34
- return nil
33
+ }
34
+ else
35
+ return nil
36
+ end
35
37
  end
36
38
  end
37
39
  end
@@ -1,11 +1,13 @@
1
1
  require_relative 'reduce.rb'
2
2
  require_relative '../utils/index.rb'
3
3
 
4
- module ReduceRight
5
- extend Reduce
6
-
7
- def reduce_right(collection, *rest_args)
8
- reversed_collection = Utils.force_reverse(collection)
9
- self.reduce(reversed_collection, *rest_args)
4
+ module Rudash
5
+ module ReduceRight
6
+ extend Rudash
7
+
8
+ def reduce_right(collection, *rest_args)
9
+ reversed_collection = Rudash::Utils.force_reverse(collection)
10
+ self.reduce(reversed_collection, *rest_args)
11
+ end
10
12
  end
11
13
  end
data/lib/rudash/remove.rb CHANGED
@@ -1,17 +1,17 @@
1
1
  require_relative 'identity.rb'
2
2
  require_relative 'filter.rb'
3
3
 
4
- module Remove
5
- extend Identity
6
- extend Negate
7
- extend Filter
8
-
9
- def remove(array, *rest_args)
10
- predicate = self.head(rest_args) || self.method(:identity)
11
- return [] if !array.is_a?(Array)
12
- removed_items = self.filter(array, predicate)
13
-
14
- array.replace(array - removed_items)
15
- removed_items
4
+ module Rudash
5
+ module Remove
6
+ extend Rudash
7
+
8
+ def remove(array, *rest_args)
9
+ predicate = self.head(rest_args) || self.method(:identity)
10
+ return [] if !array.is_a?(Array)
11
+ removed_items = self.filter(array, predicate)
12
+
13
+ array.replace(array - removed_items)
14
+ removed_items
15
+ end
16
16
  end
17
17
  end
@@ -1,10 +1,12 @@
1
- module Reverse
2
- def reverse(value)
3
- case value
4
- when Array, String
5
- value.reverse
6
- else
7
- value
1
+ module Rudash
2
+ module Reverse
3
+ def reverse(value)
4
+ case value
5
+ when Array, String
6
+ value.reverse
7
+ else
8
+ value
9
+ end
8
10
  end
9
11
  end
10
12
  end
data/lib/rudash/set.rb CHANGED
@@ -3,26 +3,28 @@ require_relative '../utils/nested_path_creator.rb'
3
3
  require_relative '../utils/index.rb'
4
4
  require_relative 'get.rb'
5
5
 
6
- module Set
7
- extend Get
8
-
9
- def set(object, path, value)
10
- return object if !object.is_a?(Hash) && !object.is_a?(Array)
11
-
12
- resolved_path = PathResolver.resolve(path)
13
- NestedPathCreator.create_path_if_not_exist(object, resolved_path)
14
-
15
- *initial_path, last = resolved_path
16
-
17
- last_key = Utils.match_number?(last) ? last.to_i : last.to_sym
18
-
19
- if initial_path.size == 0
20
- object[last_key] = value
21
- return object
6
+ module Rudash
7
+ module Set
8
+ extend Rudash
9
+
10
+ def set(object, path, value)
11
+ return object if !object.is_a?(Hash) && !object.is_a?(Array)
12
+
13
+ resolved_path = Rudash::PathResolver.resolve(path)
14
+ Rudash::NestedPathCreator.create_path_if_not_exist(object, resolved_path)
15
+
16
+ *initial_path, last = resolved_path
17
+
18
+ last_key = Rudash::Utils.match_number?(last) ? last.to_i : last.to_sym
19
+
20
+ if initial_path.size == 0
21
+ object[last_key] = value
22
+ return object
23
+ end
24
+
25
+ last_parent = self.get(object, initial_path)
26
+ last_parent[last_key] = value
27
+ object
22
28
  end
23
-
24
- last_parent = self.get(object, initial_path)
25
- last_parent[last_key] = value
26
- object
27
29
  end
28
30
  end
data/lib/rudash/size.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require_relative 'is_nil.rb'
2
2
 
3
- module Size
4
- extend IsNil
5
-
6
- def size(something)
7
- return 0 if self.is_nil?(something)
8
- something.size
3
+ module Rudash
4
+ module Size
5
+ extend Rudash
6
+
7
+ def size(something)
8
+ return 0 if self.is_nil?(something)
9
+ something.size
10
+ end
9
11
  end
10
12
  end
data/lib/rudash/slice.rb CHANGED
@@ -1,13 +1,15 @@
1
- module Slice
2
- def slice(array, *rest_args)
3
- return self.slice(array.split(''), *rest_args) if array.is_a?(String)
4
- return [] if !array.is_a?(Array)
5
-
6
- start_point = rest_args[0] || 0
7
- end_point = rest_args[1] || array.size
8
-
9
- return [] if !end_point.is_a?(Numeric)
10
-
11
- array.slice(start_point, end_point - start_point) || []
1
+ module Rudash
2
+ module Slice
3
+ def slice(array, *rest_args)
4
+ return self.slice(array.split(''), *rest_args) if array.is_a?(String)
5
+ return [] if !array.is_a?(Array)
6
+
7
+ start_point = rest_args[0] || 0
8
+ end_point = rest_args[1] || array.size
9
+
10
+ return [] if !end_point.is_a?(Numeric)
11
+
12
+ array.slice(start_point, end_point - start_point) || []
13
+ end
12
14
  end
13
15
  end
data/lib/rudash/some.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require_relative 'filter.rb'
2
2
 
3
- module Some
4
- extend Filter
5
-
6
- def some?(array, filter)
7
- filtered_arr = self.filter(array, filter)
8
- filtered_arr.length != 0
3
+ module Rudash
4
+ module Some
5
+ extend Rudash
6
+
7
+ def some?(array, filter)
8
+ filtered_arr = self.filter(array, filter)
9
+ filtered_arr.length != 0
10
+ end
9
11
  end
10
12
  end
data/lib/rudash/tail.rb CHANGED
@@ -1,6 +1,8 @@
1
- module Tail
2
- def tail(array)
3
- return [] if !array.is_a?(Array)
4
- array[1..-1] || []
1
+ module Rudash
2
+ module Tail
3
+ def tail(array)
4
+ return [] if !array.is_a?(Array)
5
+ array[1..-1] || []
6
+ end
5
7
  end
6
8
  end
data/lib/rudash/union.rb CHANGED
@@ -2,23 +2,23 @@ require_relative 'reduce.rb'
2
2
  require_relative 'head.rb'
3
3
  require_relative 'filter.rb'
4
4
 
5
- module Union
6
- extend Reduce
7
- extend Head
8
- extend Filter
9
-
10
- def union(*values)
11
-
12
- union_reducer = -> (acc, current) {
13
- return acc if !current.is_a?(Array) || !acc.is_a?(Array)
14
- acc | current
15
- }
16
-
17
- is_array = -> (value) { value.is_a?(Array) }
18
-
19
- arr_values = self.filter(values, is_array)
20
- head = self.head(arr_values)
21
-
22
- self.reduce(arr_values, union_reducer, head) || []
5
+ module Rudash
6
+ module Union
7
+ extend Rudash
8
+
9
+ def union(*values)
10
+
11
+ union_reducer = -> (acc, current) {
12
+ return acc if !current.is_a?(Array) || !acc.is_a?(Array)
13
+ acc | current
14
+ }
15
+
16
+ is_array = -> (value) { value.is_a?(Array) }
17
+
18
+ arr_values = self.filter(values, is_array)
19
+ head = self.head(arr_values)
20
+
21
+ self.reduce(arr_values, union_reducer, head) || []
22
+ end
23
23
  end
24
24
  end
data/lib/rudash/uniq.rb CHANGED
@@ -1,12 +1,14 @@
1
- module Uniq
2
- def uniq(value)
3
- case value
4
- when String
5
- self.uniq(value.split(''))
6
- when Array
7
- value.uniq
8
- else
9
- []
1
+ module Rudash
2
+ module Uniq
3
+ def uniq(value)
4
+ case value
5
+ when String
6
+ self.uniq(value.split(''))
7
+ when Array
8
+ value.uniq
9
+ else
10
+ []
11
+ end
10
12
  end
11
13
  end
12
14
  end
data/lib/rudash/update.rb CHANGED
@@ -3,16 +3,15 @@ require_relative 'get.rb'
3
3
  require_relative 'set.rb'
4
4
  require_relative 'head.rb'
5
5
 
6
- module Update
7
- extend Identity
8
- extend Get
9
- extend Set
10
- extend Head
11
-
12
- def update(object, path, *rest_args)
13
- updater = self.head(rest_args) || self.method(:identity)
14
- current_value = self.get(object, path)
15
- self.set(object, path, updater.(current_value))
16
- object
6
+ module Rudash
7
+ module Update
8
+ extend Rudash
9
+
10
+ def update(object, path, *rest_args)
11
+ updater = self.head(rest_args) || self.method(:identity)
12
+ current_value = self.get(object, path)
13
+ self.set(object, path, updater.(current_value))
14
+ object
15
+ end
17
16
  end
18
17
  end
@@ -1,10 +1,12 @@
1
1
  require_relative 'difference.rb'
2
2
 
3
- module Without
4
- extend Difference
5
-
6
- def without(array, *values)
7
- return [] if !array.is_a?(Array)
8
- self.difference(array, values)
3
+ module Rudash
4
+ module Without
5
+ extend Rudash
6
+
7
+ def without(array, *values)
8
+ return [] if !array.is_a?(Array)
9
+ self.difference(array, values)
10
+ end
9
11
  end
10
12
  end
data/lib/rudash.rb CHANGED
@@ -46,50 +46,50 @@ require_relative './rudash/remove.rb'
46
46
  require_relative './rudash/union.rb'
47
47
 
48
48
  class R_
49
- extend Map
50
- extend IsNil
51
- extend Filter
52
- extend Some
53
- extend Every
54
- extend Find
55
- extend Concat
56
- extend FindLast
57
- extend Reduce
58
- extend ReduceRight
59
- extend Compact
60
- extend Head
61
- extend Last
62
- extend Tail
63
- extend Size
64
- extend Flip
65
- extend Identity
66
- extend Get
67
- extend IsArray
68
- extend IsEqual
69
- extend IsHash
70
- extend IsEmpty
71
- extend IsProc
72
- extend Reverse
73
- extend Curry
74
- extend IsNumber
75
- extend Keys
76
- extend Each
77
- extend Uniq
78
- extend Difference
79
- extend IsString
80
- extend Eq
81
- extend EachRight
82
- extend At
83
- extend Negate
84
- extend Capitalize
85
- extend Without
86
- extend Intersection
87
- extend Join
88
- extend Initial
89
- extend Set
90
- extend Pick
91
- extend Update
92
- extend Slice
93
- extend Remove
94
- extend Union
49
+ extend Rudash::Map
50
+ extend Rudash::IsNil
51
+ extend Rudash::Filter
52
+ extend Rudash::Some
53
+ extend Rudash::Every
54
+ extend Rudash::Find
55
+ extend Rudash::Concat
56
+ extend Rudash::FindLast
57
+ extend Rudash::Reduce
58
+ extend Rudash::ReduceRight
59
+ extend Rudash::Compact
60
+ extend Rudash::Head
61
+ extend Rudash::Last
62
+ extend Rudash::Tail
63
+ extend Rudash::Size
64
+ extend Rudash::Flip
65
+ extend Rudash::Identity
66
+ extend Rudash::Get
67
+ extend Rudash::IsArray
68
+ extend Rudash::IsEqual
69
+ extend Rudash::IsHash
70
+ extend Rudash::IsEmpty
71
+ extend Rudash::IsProc
72
+ extend Rudash::Reverse
73
+ extend Rudash::Curry
74
+ extend Rudash::IsNumber
75
+ extend Rudash::Keys
76
+ extend Rudash::Each
77
+ extend Rudash::Uniq
78
+ extend Rudash::Difference
79
+ extend Rudash::IsString
80
+ extend Rudash::Eq
81
+ extend Rudash::EachRight
82
+ extend Rudash::At
83
+ extend Rudash::Negate
84
+ extend Rudash::Capitalize
85
+ extend Rudash::Without
86
+ extend Rudash::Intersection
87
+ extend Rudash::Join
88
+ extend Rudash::Initial
89
+ extend Rudash::Set
90
+ extend Rudash::Pick
91
+ extend Rudash::Update
92
+ extend Rudash::Slice
93
+ extend Rudash::Remove
94
+ extend Rudash::Union
95
95
  end