module Rudash::Default
Public Instance Methods
array?(value)
click to toggle source
# File lib/rudash/array.rb, line 3 def array?(value) value.is_a?(Array) end
at(object, paths)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/capitalize.rb, line 3 def capitalize(value) value.is_a?(String) ? value.capitalize : value.to_s end
chain(value)
click to toggle source
# File lib/rudash/chain.rb, line 3 def chain(value) Rudash::ChainUtils::ChainWrapper.new(value, self) end
compact(array)
click to toggle source
# File lib/rudash/compact.rb, line 3 def compact(array) return [] unless array.is_a?(Array) array.compact end
concat(head, *values)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/each.rb, line 3 def each(collection, *rest_args) self.map(collection, *rest_args) collection end
each_right(collection, *rest_args)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/eq.rb, line 3 def eq?(object, other) object.equal?(other) end
equal?(value, other)
click to toggle source
# File lib/rudash/equal.rb, line 3 def equal?(value, other) value == other end
every?(array, filter)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/head.rb, line 9 def first(*args) self.head(*args) end
flip(a_lambda)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/each.rb, line 8 def for_each(*args) self.each(*args) end
for_each_right(*args)
click to toggle source
# File lib/rudash/each_right.rb, line 10 def for_each_right(*args) self.each_right(*args) end
get(hash, path, *_rest_args)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/hash.rb, line 3 def hash?(value) value.is_a?(Hash) end
head(array)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/identity.rb, line 3 def identity(first_arg, *_rest_args) first_arg end
initial(array)
click to toggle source
# File lib/rudash/initial.rb, line 3 def initial(array) return [] unless array.is_a?(Array) *initial, _last = array initial end
intersection(*values)
click to toggle source
# 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 = ',')
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/last.rb, line 3 def last(array) return nil unless array.is_a?(Array) array.last end
map(collection, *rest_args)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/nil.rb, line 3 def nil?(object) object.nil? end
nth(array, *rest_args)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/number.rb, line 3 def number?(value) value.is_a?(Numeric) end
pick(hash, paths)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/size.rb, line 3 def size(something) return 0 if self.nil?(something) something.size end
slice(array, *rest_args)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/some.rb, line 3 def some?(array, filter) filtered_arr = self.filter(array, filter) !filtered_arr.empty? end
string?(object)
click to toggle source
# File lib/rudash/string.rb, line 3 def string?(object) object.is_a?(String) end
tail(array)
click to toggle source
# File lib/rudash/tail.rb, line 3 def tail(array) return [] unless array.is_a?(Array) array[1..-1] || [] end
take(array, *rest_args)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/rudash/without.rb, line 3 def without(array, *values) return [] unless array.is_a?(Array) self.difference(array, values) end