rudash 2.9.1 → 2.10.0

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
  SHA256:
3
- metadata.gz: 82271921675a8d420c4e1dd198c0e07f6438a323040ac28f6cb07042e776ad3c
4
- data.tar.gz: 66901b07176af3319fce0a930752d03e1d761652cccc47a7f1d7be300af45676
3
+ metadata.gz: 9093255b12cd834ff23fa463f4c7f2c4b0916a589c973c680cd4488149c9e6db
4
+ data.tar.gz: 268185d416326b2e38ff02bb895e09625277a2a06c012b5af0938450886bf190
5
5
  SHA512:
6
- metadata.gz: 4087dbb48f61d30663fe2f25b18866bd2b1dcd8c1878692243330fe0cdcf6708dabdddd7ab07d892f221d7084d51d78f68076a92e5450d8ba98ae7067e477050
7
- data.tar.gz: d8c6597e919c2e3d9a5e3e148adbc5a2b1bcbef14cac0113f5a391e05a545d1dbd188f0b15176ee4ead8acd42e084ca03964bf3983b283925ec96b4601ac91ea
6
+ metadata.gz: 8d5f5e26667dccc7712a9127de99eb7a5a2fef6dc345e3e91972b3708c976b5e89f426f0766df0de4fd4ca1e94d4c4aa184e09b6610d588cdd724d3ea3caa6b3
7
+ data.tar.gz: be2ffa703e19ffabf205a959e622b4db8bf457fbc9a35f7630cab5eb0af8179fd3b733c7e08e062886aff775dec77e08e81191da8fc58d7a84039beac7f503e5
@@ -1,12 +1,7 @@
1
1
  module Rudash
2
2
  module Capitalize
3
3
  def capitalize(value)
4
- case value
5
- when String
6
- value.capitalize
7
- else
8
- value.to_s
9
- end
4
+ value.is_a?(String) ? value.capitalize : value.to_s
10
5
  end
11
6
  end
12
7
  end
data/lib/rudash/curry.rb CHANGED
@@ -1,12 +1,7 @@
1
1
  module Rudash
2
2
  module Curry
3
3
  def curry(a_proc)
4
- case a_proc
5
- when Proc
6
- a_proc.curry
7
- else
8
- raise 'Expected a Proc'
9
- end
4
+ a_proc.is_a?(Proc) ? a_proc.curry : (raise 'Expected a Proc')
10
5
  end
11
6
  end
12
7
  end
data/lib/rudash/filter.rb CHANGED
@@ -1,38 +1,27 @@
1
1
  require_relative '../utils/index.rb'
2
2
  require_relative '../utils/subset_deep_match.rb'
3
+ require_relative '../utils/dynamic_args_count.rb'
3
4
 
4
5
  module Rudash
5
6
  module Filter
6
7
  def filter(collection, *rest_args)
7
- filter = self.head(rest_args) || self.method(:identity)
8
+ predicate_fn = self.head(rest_args) || self.method(:identity)
8
9
 
9
- if filter.is_a?(Hash)
10
- slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.(filter)
10
+ if predicate_fn.is_a?(Hash)
11
+ slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.(predicate_fn)
11
12
  return self.filter(collection, slice_matcher)
12
13
  end
13
14
 
14
- return [] if !Rudash::Utils.is_function?(filter)
15
+ return [] if !Rudash::Utils.is_function?(predicate_fn)
15
16
 
16
17
  if collection.is_a?(Array)
17
- begin
18
- return collection.select.with_index { |x, idx| filter.(x, idx) }
19
- rescue ArgumentError => e
20
- begin
21
- return collection.select { |x| filter.(x) }
22
- rescue ArgumentError => e
23
- return collection.select { filter.() }
24
- end
25
- end
18
+ return collection.select.with_index { |x, idx|
19
+ Rudash::DynamicArgsCount.call(predicate_fn, x, idx)
20
+ }
26
21
  elsif collection.is_a?(Hash)
27
- begin
28
- return collection.select { |k, v| filter.(v, k) }.values
29
- rescue ArgumentError => e
30
- begin
31
- return collection.select { |k, v| filter.(v) }.values
32
- rescue ArgumentError => e
33
- return collection.select { filter.() }.values
34
- end
35
- end
22
+ return collection.select { |k, v|
23
+ Rudash::DynamicArgsCount.call(predicate_fn, v, k)
24
+ }.values
36
25
  else
37
26
  return []
38
27
  end
data/lib/rudash/find.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Rudash
2
2
  module Find
3
3
  def find(collection, *rest_args)
4
- filter_proc = self.head(rest_args) || self.method(:identity)
5
- filtered_arr = self.filter(collection, filter_proc)
4
+ iteratee_fn = self.head(rest_args)
5
+ filtered_arr = self.filter(collection, iteratee_fn)
6
6
 
7
7
  filtered_arr[0]
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module Rudash
2
2
  module FindLast
3
3
  def find_last(collection, *rest_args)
4
- filter_proc = self.head(rest_args) || self.method(:identity)
5
- filtered_arr = self.filter(collection, filter_proc)
4
+ iteratee_fn = self.head(rest_args)
5
+ filtered_arr = self.filter(collection, iteratee_fn)
6
6
 
7
7
  filtered_arr[filtered_arr.length - 1]
8
8
  end
@@ -1,12 +1,7 @@
1
1
  module Rudash
2
2
  module IsString
3
3
  def is_string?(object)
4
- case object
5
- when String
6
- true
7
- else
8
- false
9
- end
4
+ object.is_a?(String)
10
5
  end
11
6
  end
12
7
  end
data/lib/rudash/map.rb CHANGED
@@ -1,33 +1,22 @@
1
1
  require_relative '../utils/index.rb'
2
+ require_relative '../utils/dynamic_args_count.rb'
2
3
 
3
4
  module Rudash
4
5
  module Map
5
6
  def map(collection, *rest_args)
6
- mapper_proc = self.head(rest_args) || self.method(:identity)
7
+ iteratee_fn = self.head(rest_args) || self.method(:identity)
7
8
  col = collection.is_a?(String) ? collection.split('') : collection
8
9
 
9
- return self.map(collection, -> () { nil }) if !Rudash::Utils.is_function?(mapper_proc)
10
+ return self.map(collection, -> () { nil }) if !Rudash::Utils.is_function?(iteratee_fn)
10
11
 
11
12
  if col.is_a?(Array)
12
- begin
13
- return col.map.with_index { |value, index| mapper_proc.(value, index) }
14
- rescue ArgumentError => e
15
- begin
16
- return col.map { |value| mapper_proc.(value) }
17
- rescue ArgumentError => e
18
- return col.map { mapper_proc.() }
19
- end
20
- end
13
+ return col.map.with_index { |value, index|
14
+ Rudash::DynamicArgsCount.call(iteratee_fn, value, index)
15
+ }
21
16
  elsif col.is_a?(Hash)
22
- begin
23
- return col.map { |k,v| mapper_proc.(v, k) }
24
- rescue ArgumentError => e
25
- begin
26
- return col.map { |k,v| mapper_proc.(v) }
27
- rescue ArgumentError => e
28
- return col.map { mapper_proc.() }
29
- end
30
- end
17
+ return col.map { |k,v|
18
+ Rudash::DynamicArgsCount.call(iteratee_fn, v, k)
19
+ }
31
20
  else
32
21
  return []
33
22
  end
@@ -0,0 +1,46 @@
1
+ module Rudash
2
+ module Range
3
+ def range(*args)
4
+ start_step = 0
5
+ step_jump = 1
6
+ end_step = 0
7
+
8
+ case args.size
9
+ when 0
10
+ return []
11
+ when 1
12
+ end_step = args[0]
13
+ when 2
14
+ start_step, end_step = args
15
+ else
16
+ start_step, end_step, step_jump = args
17
+ step_jump_configured = true
18
+ end
19
+
20
+ # step_jump direction (+/-) should be defined by start/end values
21
+ norm_step_jump = (end_step > start_step ? step_jump.abs : -step_jump.abs)
22
+
23
+ # illegal behaviors
24
+ return [] if (norm_step_jump != step_jump && step_jump_configured)
25
+ # End illegal behavior
26
+
27
+ iterator = start_step
28
+ result = []
29
+
30
+ # calculate loop count
31
+ boundaries = [start_step, end_step]
32
+ max = boundaries.max
33
+ min = boundaries.min
34
+ i = (norm_step_jump == 0 ? (max - min) : ((max - min).to_f / norm_step_jump)).abs
35
+ # end loop calculation
36
+
37
+ while i > 0
38
+ result << iterator
39
+ iterator += norm_step_jump
40
+ i-=1
41
+ end
42
+
43
+ result
44
+ end
45
+ end
46
+ end
data/lib/rudash/reduce.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative '../utils/index.rb'
2
+ require_relative '../utils/dynamic_args_count.rb'
2
3
 
3
4
  module Rudash
4
5
  module Reduce
@@ -7,31 +8,23 @@ module Rudash
7
8
  initial_state = rest_args[1]
8
9
  col = collection.is_a?(String) ? collection.split('') : collection
9
10
 
10
- return self.reduce(collection, -> (acc, current) { nil }) if !Rudash::Utils.is_function?(reducer)
11
+ return self.reduce(collection, -> () { nil }) if !Rudash::Utils.is_function?(reducer)
11
12
 
12
13
  case rest_args.size
13
14
  when 1
14
15
  return col.reduce { |acc, current|
15
16
  if col.is_a?(Hash)
16
- begin
17
- reducer.(acc, current[1], current[0])
18
- rescue ArgumentError => e
19
- reducer.(acc, current[1])
20
- end
17
+ Rudash::DynamicArgsCount.call(reducer, acc, current[1], current[0])
21
18
  else
22
- reducer.(acc, current)
19
+ Rudash::DynamicArgsCount.call(reducer, acc, current)
23
20
  end
24
21
  }
25
22
  when 2
26
23
  return col.reduce(initial_state) { |acc, current|
27
24
  if col.is_a?(Hash)
28
- begin
29
- reducer.(acc, current[1], current[0])
30
- rescue ArgumentError => e
31
- reducer.(acc, current[1])
32
- end
25
+ Rudash::DynamicArgsCount.call(reducer, acc, current[1], current[0])
33
26
  else
34
- reducer.(acc, current)
27
+ Rudash::DynamicArgsCount.call(reducer, acc, current)
35
28
  end
36
29
  }
37
30
  else
data/lib/rudash/remove.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  module Rudash
2
2
  module Remove
3
3
  def remove(array, *rest_args)
4
- predicate = self.head(rest_args) || self.method(:identity)
4
+ predicate_fn = self.head(rest_args)
5
5
  return [] if !array.is_a?(Array)
6
- removed_items = self.filter(array, predicate)
6
+ removed_items = self.filter(array, predicate_fn)
7
7
 
8
8
  array.replace(array - removed_items)
9
9
  removed_items
data/lib/rudash/update.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  require_relative '../utils/index.rb'
2
+ require_relative '../utils/dynamic_args_count.rb'
2
3
 
3
4
  module Rudash
4
5
  module Update
5
6
  def update(object, path, *rest_args)
6
- updater = self.head(rest_args) || self.method(:identity)
7
- return object if !Rudash::Utils.is_function?(updater)
7
+ updater_fn = self.head(rest_args) || self.method(:identity)
8
+ return object if !Rudash::Utils.is_function?(updater_fn)
8
9
 
9
10
  current_value = self.get(object, path)
10
- self.set(object, path, updater.(current_value))
11
+ self.set(object, path, Rudash::DynamicArgsCount.call(updater_fn, current_value))
11
12
  object
12
13
  end
13
14
  end
data/lib/rudash.rb CHANGED
@@ -44,6 +44,7 @@ require_relative './rudash/slice.rb'
44
44
  require_relative './rudash/remove.rb'
45
45
  require_relative './rudash/union.rb'
46
46
  require_relative './rudash/reject.rb'
47
+ require_relative './rudash/range.rb'
47
48
 
48
49
  # This is the exposed Gem class that contains all Rudash methods.
49
50
  # New methods can use already implemented methods in the library by refering to "self"
@@ -95,4 +96,5 @@ class R_
95
96
  extend Rudash::Remove
96
97
  extend Rudash::Union
97
98
  extend Rudash::Reject
99
+ extend Rudash::Range
98
100
  end
@@ -0,0 +1,19 @@
1
+ # This module was written in order to give the predicate functions for R_.filter, R_.map, R_.each, etc..
2
+ # to be called with any dynamic arguments as it can. What differante JavaScript from Ruby is that once you configured a function
3
+ # with a specific set of arguments then you must call it with that set or else you will get an exception.
4
+ # That is so useful for predicate functions that developer define out of the library scope. We send all the arguments
5
+ # to the developer defined Proc and if it's failed because of ArgumentError we call it recursively with less argument until success.
6
+
7
+ module Rudash
8
+ module DynamicArgsCount
9
+ def self.call(func, *args)
10
+ begin
11
+ return func.(*args)
12
+ rescue ArgumentError => e
13
+ raise 'Argument Error' if args.size == 0
14
+ *initial, last = args
15
+ return self.call(func, *initial)
16
+ end
17
+ end
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rudash
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.1
4
+ version: 2.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Islam Attrash
@@ -49,6 +49,7 @@ files:
49
49
  - lib/rudash/map.rb
50
50
  - lib/rudash/negate.rb
51
51
  - lib/rudash/pick.rb
52
+ - lib/rudash/range.rb
52
53
  - lib/rudash/reduce.rb
53
54
  - lib/rudash/reduce_right.rb
54
55
  - lib/rudash/reject.rb
@@ -63,6 +64,7 @@ files:
63
64
  - lib/rudash/uniq.rb
64
65
  - lib/rudash/update.rb
65
66
  - lib/rudash/without.rb
67
+ - lib/utils/dynamic_args_count.rb
66
68
  - lib/utils/index.rb
67
69
  - lib/utils/nested_path_creator.rb
68
70
  - lib/utils/path_resolver.rb