Module: Rudash::Default
- Included in:
- R_
- Defined in:
- lib/rudash/at.rb,
lib/rudash/eq.rb,
lib/rudash/get.rb,
lib/rudash/map.rb,
lib/rudash/nil.rb,
lib/rudash/nth.rb,
lib/rudash/set.rb,
lib/rudash/each.rb,
lib/rudash/find.rb,
lib/rudash/flip.rb,
lib/rudash/flow.rb,
lib/rudash/hash.rb,
lib/rudash/head.rb,
lib/rudash/join.rb,
lib/rudash/keys.rb,
lib/rudash/last.rb,
lib/rudash/pick.rb,
lib/rudash/size.rb,
lib/rudash/some.rb,
lib/rudash/tail.rb,
lib/rudash/take.rb,
lib/rudash/uniq.rb,
lib/rudash/array.rb,
lib/rudash/chain.rb,
lib/rudash/curry.rb,
lib/rudash/empty.rb,
lib/rudash/equal.rb,
lib/rudash/every.rb,
lib/rudash/range.rb,
lib/rudash/slice.rb,
lib/rudash/union.rb,
lib/rudash/unset.rb,
lib/rudash/concat.rb,
lib/rudash/filter.rb,
lib/rudash/negate.rb,
lib/rudash/number.rb,
lib/rudash/reduce.rb,
lib/rudash/reject.rb,
lib/rudash/remove.rb,
lib/rudash/string.rb,
lib/rudash/update.rb,
lib/rudash/compact.rb,
lib/rudash/initial.rb,
lib/rudash/reverse.rb,
lib/rudash/without.rb,
lib/rudash/group_by.rb,
lib/rudash/identity.rb,
lib/rudash/ends_with.rb,
lib/rudash/find_last.rb,
lib/rudash/capitalize.rb,
lib/rudash/difference.rb,
lib/rudash/drop_right.rb,
lib/rudash/each_right.rb,
lib/rudash/flow_right.rb,
lib/rudash/intersection.rb,
lib/rudash/reduce_right.rb
Instance Method Summary collapse
- #array?(value) ⇒ Boolean
- #at(object, paths) ⇒ Object
- #capitalize(value) ⇒ Object
- #chain(value) ⇒ Object
- #compact(array) ⇒ Object
- #concat(head, *values) ⇒ Object
- #curry(a_lambda) ⇒ Object
- #difference(*values) ⇒ Object
- #drop_right(array, *rest_args) ⇒ Object
- #each(collection, *rest_args) ⇒ Object
- #each_right(collection, *rest_args) ⇒ Object
- #empty?(value) ⇒ Boolean
- #ends_with?(str, *rest_args) ⇒ Boolean
- #eq?(object, other) ⇒ Boolean
- #equal?(value, other) ⇒ Boolean
- #every?(array, filter) ⇒ Boolean
- #filter(collection, *rest_args) ⇒ Object
- #find(collection, *rest_args) ⇒ Object
- #find_last(collection, *rest_args) ⇒ Object
- #first(*args) ⇒ Object
- #flip(a_lambda) ⇒ Object
- #flow(*funs) ⇒ Object
- #flow_right(*funs) ⇒ Object
- #for_each(*args) ⇒ Object
- #for_each_right(*args) ⇒ Object
- #get(hash, path, *_rest_args) ⇒ Object
- #group_by(collection, *rest_args) ⇒ Object
- #hash?(value) ⇒ Boolean
- #head(array) ⇒ Object
- #identity(first_arg, *_rest_args) ⇒ Object
- #initial(array) ⇒ Object
- #intersection(*values) ⇒ Object
- #join(array, separator = ',') ⇒ Object
- #keys(value) ⇒ Object
- #last(array) ⇒ Object
- #map(collection, *rest_args) ⇒ Object
- #negate(a_lambda) ⇒ Object
- #nil?(object) ⇒ Boolean
- #nth(array, *rest_args) ⇒ Object
- #number?(value) ⇒ Boolean
- #pick(hash, paths) ⇒ Object
- #range(*args) ⇒ Object
- #reduce(collection, *rest_args) ⇒ Object
- #reduce_right(collection, *rest_args) ⇒ Object
- #reject(collection, *rest_args) ⇒ Object
- #remove(array, *rest_args) ⇒ Object
- #reverse(value) ⇒ Object
- #set(object, path, value) ⇒ Object
- #size(something) ⇒ Object
- #slice(array, *rest_args) ⇒ Object
- #some?(array, filter) ⇒ Boolean
- #string?(object) ⇒ Boolean
- #tail(array) ⇒ Object
- #take(array, *rest_args) ⇒ Object
- #union(*values) ⇒ Object
- #uniq(value) ⇒ Object
- #unset(object, path) ⇒ Object
- #update(object, path, *rest_args) ⇒ Object
- #without(array, *values) ⇒ Object
Instance Method Details
#array?(value) ⇒ Boolean
3 4 5 |
# File 'lib/rudash/array.rb', line 3 def array?(value) value.is_a?(Array) end |
#at(object, paths) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/rudash/at.rb', line 3 def at(object, paths) get_mapper = ->(path) { self.get(object, path) } self.map(paths, get_mapper) end |
#capitalize(value) ⇒ Object
3 4 5 |
# File 'lib/rudash/capitalize.rb', line 3 def capitalize(value) value.is_a?(String) ? value.capitalize : value.to_s end |
#chain(value) ⇒ Object
3 4 5 |
# File 'lib/rudash/chain.rb', line 3 def chain(value) Rudash::ChainUtils::ChainWrapper.new(value, self) end |
#compact(array) ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/compact.rb', line 3 def compact(array) return [] unless array.is_a?(Array) array.compact end |
#concat(head, *values) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/rudash/concat.rb', line 3 def concat(head, *values) head_arr = head.is_a?(Array) ? head : [head] return head_arr if values.size.zero? head_arr + self.concat(*values) end |
#curry(a_lambda) ⇒ Object
3 4 5 |
# File 'lib/rudash/curry.rb', line 3 def curry(a_lambda) a_lambda.is_a?(Proc) ? a_lambda.curry : (raise 'Expected a Lambda') end |
#difference(*values) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/rudash/difference.rb', line 3 def difference(*values) diff_reducer = ->(acc, current) { return [] unless acc.is_a?(Array) return acc unless current.is_a?(Array) acc - current } self.reduce(values, diff_reducer) end |
#drop_right(array, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/rudash/drop_right.rb', line 3 def drop_right(array, *rest_args) return [] unless self.array?(array) n = self.head(rest_args) || 1 return array if n <= 0 self.take(array, self.size(array) - n) end |
#each(collection, *rest_args) ⇒ Object
3 4 5 6 |
# File 'lib/rudash/each.rb', line 3 def each(collection, *rest_args) self.map(collection, *rest_args) collection end |
#each_right(collection, *rest_args) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/rudash/each_right.rb', line 3 def each_right(collection, *rest_args) reversed_collection = Rudash::Utils.force_reverse(collection) self.each(reversed_collection, *rest_args) collection end |
#empty?(value) ⇒ Boolean
3 4 5 6 7 8 9 10 |
# File 'lib/rudash/empty.rb', line 3 def empty?(value) case value when Hash, Array value.empty? else true end end |
#ends_with?(str, *rest_args) ⇒ Boolean
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/rudash/ends_with.rb', line 3 def ends_with?(str, *rest_args) case str when String suffix = self.head(rest_args) return false if suffix.nil? str.end_with?(suffix.to_s) else false end end |
#eq?(object, other) ⇒ Boolean
3 4 5 |
# File 'lib/rudash/eq.rb', line 3 def eq?(object, other) object.equal?(other) end |
#equal?(value, other) ⇒ Boolean
3 4 5 |
# File 'lib/rudash/equal.rb', line 3 def equal?(value, other) value == other end |
#every?(array, filter) ⇒ Boolean
3 4 5 6 |
# File 'lib/rudash/every.rb', line 3 def every?(array, filter) filtered_arr = self.filter(array, filter) filtered_arr.length == array.length end |
#filter(collection, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rudash/filter.rb', line 3 def filter(collection, *rest_args) predicate_fn = self.head(rest_args) || self.method(:identity) if predicate_fn.is_a?(Hash) slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.call(predicate_fn) return self.filter(collection, slice_matcher) end return [] unless Rudash::Utils.function?(predicate_fn) if collection.is_a?(Array) return collection.select.with_index do |x, idx| Rudash::DynamicArgsCount.call(predicate_fn, x, idx) end elsif collection.is_a?(Hash) return collection.select do |k, v| Rudash::DynamicArgsCount.call(predicate_fn, v, k) end.values else return [] end end |
#find(collection, *rest_args) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/rudash/find.rb', line 3 def find(collection, *rest_args) iteratee_fn = self.head(rest_args) filtered_arr = self.filter(collection, iteratee_fn) filtered_arr[0] end |
#find_last(collection, *rest_args) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/rudash/find_last.rb', line 3 def find_last(collection, *rest_args) iteratee_fn = self.head(rest_args) filtered_arr = self.filter(collection, iteratee_fn) filtered_arr[filtered_arr.length - 1] end |
#first(*args) ⇒ Object
9 10 11 |
# File 'lib/rudash/head.rb', line 9 def first(*args) self.head(*args) end |
#flip(a_lambda) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/rudash/flip.rb', line 3 def flip(a_lambda) raise 'Expected a lambda/Method' unless Rudash::Utils.function?(a_lambda) ->(*args) { reveresed_args = args.reverse a_lambda.call(*reveresed_args) } end |
#flow(*funs) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/rudash/flow.rb', line 3 def flow(*funs) flatten_funs = funs.flatten ->(*args) { self.reduce(flatten_funs, ->(acc, fn) { Rudash::DynamicArgsCount.call(fn, *self.concat(acc)) }, args) } end |
#flow_right(*funs) ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/flow_right.rb', line 3 def flow_right(*funs) flatten_funs = funs.flatten.reverse self.flow(flatten_funs) end |
#for_each(*args) ⇒ Object
8 9 10 |
# File 'lib/rudash/each.rb', line 8 def for_each(*args) self.each(*args) end |
#for_each_right(*args) ⇒ Object
10 11 12 |
# File 'lib/rudash/each_right.rb', line 10 def for_each_right(*args) self.each_right(*args) end |
#get(hash, path, *_rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rudash/get.rb', line 3 def get(hash, path, *_rest_args) return nil if !path.is_a?(String) && !path.is_a?(Array) return nil if !hash.is_a?(Array) && !hash.is_a?(Hash) resolved_path = Rudash::PathResolver.resolve(path) get_reducer = ->(acc, current) { return nil if acc.nil? if acc.is_a?(Array) && Rudash::Utils.match_number?(current) acc[current.to_i] elsif acc.is_a?(Array) && !Rudash::Utils.match_number?(current) nil elsif acc.is_a?(Hash) acc[current.to_sym] || acc[current] else return nil end } self.reduce(resolved_path, get_reducer, hash) end |
#group_by(collection, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rudash/group_by.rb', line 3 def group_by(collection, *rest_args) iteratee = self.head(rest_args) || self.method(:identity) reducer = ->(acc, current) { key = Rudash::DynamicArgsCount.call(iteratee, current) if acc[key] acc[key] << current else acc[key] = [current] end acc } self.reduce(collection, reducer, {}) end |
#hash?(value) ⇒ Boolean
3 4 5 |
# File 'lib/rudash/hash.rb', line 3 def hash?(value) value.is_a?(Hash) end |
#head(array) ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/head.rb', line 3 def head(array) return nil unless array.is_a?(Array) array.first end |
#identity(first_arg, *_rest_args) ⇒ Object
3 4 5 |
# File 'lib/rudash/identity.rb', line 3 def identity(first_arg, *_rest_args) first_arg end |
#initial(array) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/rudash/initial.rb', line 3 def initial(array) return [] unless array.is_a?(Array) *initial, _last = array initial end |
#intersection(*values) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/rudash/intersection.rb', line 3 def intersection(*values) intersection_reducer = ->(acc, current) { return [] if !current.is_a?(Array) || !acc.is_a?(Array) acc & current } self.reduce(values, intersection_reducer, values[0]) end |
#join(array, separator = ',') ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/join.rb', line 3 def join(array, separator = ',') return '' unless array.is_a?(Array) array.join(separator.to_s) end |
#keys(value) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/rudash/keys.rb', line 3 def keys(value) case value when Hash value.map { |key, _value| key.to_s } when Array value.map.with_index { |_value, index| index.to_s } else [] end end |
#last(array) ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/last.rb', line 3 def last(array) return nil unless array.is_a?(Array) array.last end |
#map(collection, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rudash/map.rb', line 3 def map(collection, *rest_args) iteratee_fn = self.head(rest_args) || self.method(:identity) col = collection.is_a?(String) ? collection.split('') : collection return self.map(collection, -> { nil }) unless Rudash::Utils.function?(iteratee_fn) if col.is_a?(Array) return col.map.with_index do |value, index| Rudash::DynamicArgsCount.call(iteratee_fn, value, index) end elsif col.is_a?(Hash) return col.map do |k, v| Rudash::DynamicArgsCount.call(iteratee_fn, v, k) end else return [] end end |
#negate(a_lambda) ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/negate.rb', line 3 def negate(a_lambda) raise 'Expected a lambda/Method' unless Rudash::Utils.function?(a_lambda) ->(*args) { !a_lambda.call(*args) } end |
#nil?(object) ⇒ Boolean
3 4 5 |
# File 'lib/rudash/nil.rb', line 3 def nil?(object) object.nil? end |
#nth(array, *rest_args) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/rudash/nth.rb', line 3 def nth(array, *rest_args) n = self.head(rest_args) || 0 return self.nth(array, 0) unless self.number?(n) return nil unless self.array?(array) array[n] end |
#number?(value) ⇒ Boolean
3 4 5 |
# File 'lib/rudash/number.rb', line 3 def number?(value) value.is_a?(Numeric) end |
#pick(hash, paths) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/rudash/pick.rb', line 3 def pick(hash, paths) return self.pick(hash, [paths]) unless paths.is_a?(Array) return {} unless hash.is_a?(Hash) picked_hash = {} eacher = ->(path) { value = self.get(hash, path) self.set(picked_hash, path, value) unless value.nil? } self.each(paths, eacher) picked_hash end |
#range(*args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rudash/range.rb', line 3 def range(*args) start_step = 0 step_jump = 1 end_step = 0 case args.size when 0 return [] when 1 end_step = args[0] when 2 start_step, end_step = args else start_step, end_step, step_jump = args step_jump_configured = true end # step_jump direction (+/-) should be defined by start/end values norm_step_jump = (end_step > start_step ? step_jump.abs : -step_jump.abs) # illegal behaviors return [] if norm_step_jump != step_jump && step_jump_configured # End illegal behavior iterator = start_step result = [] # calculate loop count boundaries = [start_step, end_step] max = boundaries.max min = boundaries.min i = (norm_step_jump.zero? ? (max - min) : ((max - min).to_f / norm_step_jump)).abs # end loop calculation while i > 0 result << iterator iterator += norm_step_jump i -= 1 end result end |
#reduce(collection, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rudash/reduce.rb', line 3 def reduce(collection, *rest_args) reducer = rest_args[0] initial_state = rest_args[1] col = collection.is_a?(String) ? collection.split('') : collection return self.reduce(collection, -> { nil }) unless Rudash::Utils.function?(reducer) case rest_args.size when 1 return col.reduce do |acc, current| if col.is_a?(Hash) Rudash::DynamicArgsCount.call(reducer, acc, current[1], current[0]) else Rudash::DynamicArgsCount.call(reducer, acc, current) end end when 2 return col.reduce(initial_state) do |acc, current| if col.is_a?(Hash) Rudash::DynamicArgsCount.call(reducer, acc, current[1], current[0]) else Rudash::DynamicArgsCount.call(reducer, acc, current) end end else return nil end end |
#reduce_right(collection, *rest_args) ⇒ Object
3 4 5 6 |
# File 'lib/rudash/reduce_right.rb', line 3 def reduce_right(collection, *rest_args) reversed_collection = Rudash::Utils.force_reverse(collection) self.reduce(reversed_collection, *rest_args) end |
#reject(collection, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rudash/reject.rb', line 3 def reject(collection, *rest_args) filter = self.head(rest_args) || self.method(:identity) if filter.is_a?(Hash) slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.call(filter) return self.filter(collection, self.negate(slice_matcher)) elsif Rudash::Utils.function?(filter) return self.filter(collection, self.negate(filter)) else return [] end end |
#remove(array, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/rudash/remove.rb', line 3 def remove(array, *rest_args) return [] unless array.is_a?(Array) predicate_fn = self.head(rest_args) removed_items = self.filter(array, predicate_fn) array.replace(array - removed_items) removed_items end |
#reverse(value) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/rudash/reverse.rb', line 3 def reverse(value) case value when Array, String value.reverse else value end end |
#set(object, path, value) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rudash/set.rb', line 3 def set(object, path, value) return object if !object.is_a?(Hash) && !object.is_a?(Array) resolved_path = Rudash::PathResolver.resolve(path) Rudash::NestedPathCreator.create_path_if_not_exist(object, resolved_path) *initial_path, last = resolved_path last_key = Rudash::Utils.match_number?(last) ? last.to_i : last.to_sym if initial_path.size.zero? object[last_key] = value return object end last_parent = self.get(object, initial_path) last_parent[last_key] = value object end |
#size(something) ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/size.rb', line 3 def size(something) return 0 if self.nil?(something) something.size end |
#slice(array, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/rudash/slice.rb', line 3 def slice(array, *rest_args) return self.slice(array.split(''), *rest_args) if array.is_a?(String) return [] unless array.is_a?(Array) start_point = rest_args[0] || 0 end_point = rest_args[1] || array.size return [] unless end_point.is_a?(Numeric) array.slice(start_point, end_point - start_point) || [] end |
#some?(array, filter) ⇒ Boolean
3 4 5 6 |
# File 'lib/rudash/some.rb', line 3 def some?(array, filter) filtered_arr = self.filter(array, filter) !filtered_arr.empty? end |
#string?(object) ⇒ Boolean
3 4 5 |
# File 'lib/rudash/string.rb', line 3 def string?(object) object.is_a?(String) end |
#tail(array) ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/tail.rb', line 3 def tail(array) return [] unless array.is_a?(Array) array[1..-1] || [] end |
#take(array, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/rudash/take.rb', line 3 def take(array, *rest_args) return [] unless self.array?(array) count = self.head(rest_args) || 1 begin return array.take(count) rescue ArgumentError return [] end end |
#union(*values) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/rudash/union.rb', line 3 def union(*values) union_reducer = ->(acc, current) { return acc if !current.is_a?(Array) || !acc.is_a?(Array) acc | current } is_array = ->(value) { value.is_a?(Array) } arr_values = self.filter(values, is_array) head = self.head(arr_values) self.reduce(arr_values, union_reducer, head) || [] end |
#uniq(value) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/rudash/uniq.rb', line 3 def uniq(value) case value when String self.uniq(value.split('')) when Array value.uniq else [] end end |
#unset(object, path) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rudash/unset.rb', line 3 def unset(object, path) return object if !object.is_a?(Hash) && !object.is_a?(Array) *initial_path, last = Rudash::PathResolver.resolve(path) last_parent = self.get(object, initial_path) case last_parent when Array return false unless Rudash::Utils.match_number?(last) last_key = last.to_i if last_key > 0 && last_key < last_parent.length last_parent.delete_at(last_key) true else false end when Hash last_key = Rudash::Utils.match_number?(last) ? last.to_i : last.to_sym if last_parent.key?(last_key) last_parent.delete(last_key) true else false end else false end end |
#update(object, path, *rest_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rudash/update.rb', line 3 def update(object, path, *rest_args) updater_fn = self.head(rest_args) || self.method(:identity) return object unless Rudash::Utils.function?(updater_fn) current_value = self.get(object, path) self.set( object, path, Rudash::DynamicArgsCount.call(updater_fn, current_value) ) object end |
#without(array, *values) ⇒ Object
3 4 5 6 7 |
# File 'lib/rudash/without.rb', line 3 def without(array, *values) return [] unless array.is_a?(Array) self.difference(array, values) end |