rudash 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rudash/compact.rb +12 -0
- data/lib/rudash/concat.rb +16 -0
- data/lib/rudash/curry.rb +13 -0
- data/lib/rudash/difference.rb +18 -0
- data/lib/rudash/each.rb +16 -0
- data/lib/rudash/every.rb +13 -0
- data/lib/rudash/filter.rb +48 -0
- data/lib/rudash/find.rb +18 -0
- data/lib/rudash/find_last.rb +14 -0
- data/lib/rudash/flip.rb +11 -0
- data/lib/rudash/get.rb +42 -0
- data/lib/rudash/head.rb +12 -0
- data/lib/rudash/identity.rb +5 -0
- data/lib/rudash/is_array.rb +5 -0
- data/lib/rudash/is_empty.rb +12 -0
- data/lib/rudash/is_equal.rb +5 -0
- data/lib/rudash/is_hash.rb +5 -0
- data/lib/rudash/is_nil.rb +5 -0
- data/lib/rudash/is_number.rb +5 -0
- data/lib/rudash/is_proc.rb +5 -0
- data/lib/rudash/keys.rb +14 -0
- data/lib/rudash/last.rb +8 -0
- data/lib/rudash/map.rb +22 -0
- data/lib/rudash/reduce.rb +35 -0
- data/lib/rudash/reduce_right.rb +19 -0
- data/lib/rudash/reverse.rb +12 -0
- data/lib/rudash/size.rb +13 -0
- data/lib/rudash/some.rb +12 -0
- data/lib/rudash/tail.rb +8 -0
- data/lib/rudash/uniq.rb +14 -0
- data/lib/rudash.rb +60 -3
- metadata +34 -4
- data/lib/map.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 064bfa431a92255617323d2a88029201fd4e88e38d73de7e6f5ad6c701edc47b
|
4
|
+
data.tar.gz: f7b8aec8cd3c9c036bcf5a4466e5655b25f7e242f6b173f48bb036960bebff03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 076d48427362914d7342e53951a33efc05dbada37b3831a3a58248f35a946462c1682abcd0215c0f93c3ca4ffbfa357a459014f5b12a3faf64aea7bd58534608
|
7
|
+
data.tar.gz: 164842487b1cb948775efaff8ab87bea1463010f86ad2a7d99d727184f0532ac391c5ac2f8bb8c04737636a1825fc3ec9eedba4227638d8c7c380cff50316341
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative 'is_nil.rb'
|
2
|
+
|
3
|
+
module Concat
|
4
|
+
extend IsNil
|
5
|
+
|
6
|
+
def concat
|
7
|
+
concat_proc = -> (head, *values) {
|
8
|
+
head_arr = head.is_a?(Array) ? head : [head]
|
9
|
+
if values.size == 0
|
10
|
+
return head_arr
|
11
|
+
else
|
12
|
+
return head_arr + self.concat[*values]
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
data/lib/rudash/curry.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative 'reduce.rb'
|
2
|
+
|
3
|
+
module Difference
|
4
|
+
extend Reduce
|
5
|
+
|
6
|
+
def difference
|
7
|
+
difference_proc = -> (*values) {
|
8
|
+
diff_reducer = -> (acc, current) {
|
9
|
+
return [] if !acc.is_a?(Array)
|
10
|
+
return acc if !current.is_a?(Array)
|
11
|
+
|
12
|
+
acc - current
|
13
|
+
}
|
14
|
+
|
15
|
+
self.reduce[values, diff_reducer]
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
data/lib/rudash/each.rb
ADDED
data/lib/rudash/every.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require_relative 'is_nil.rb'
|
2
|
+
require_relative 'identity.rb'
|
3
|
+
require_relative 'head.rb'
|
4
|
+
|
5
|
+
module Filter
|
6
|
+
extend IsNil
|
7
|
+
extend Identity
|
8
|
+
extend Head
|
9
|
+
|
10
|
+
def filter
|
11
|
+
filter_proc = -> (collection, *rest_args) {
|
12
|
+
filter = self.head[rest_args] || self.identity
|
13
|
+
|
14
|
+
if collection.is_a?(Array) and filter.is_a?(Hash)
|
15
|
+
filtered_arr = []
|
16
|
+
collection.each do |v|
|
17
|
+
match = true
|
18
|
+
filter.each do |key, value|
|
19
|
+
if (value != v[key])
|
20
|
+
match = false
|
21
|
+
break
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
if (match == true)
|
26
|
+
filtered_arr << v
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
return filtered_arr
|
31
|
+
elsif collection.is_a?(Array)
|
32
|
+
if filter.arity == 1
|
33
|
+
return collection.select { |x| filter[x] }
|
34
|
+
else
|
35
|
+
return collection.select.with_index { |x, idx| filter[x, idx] }
|
36
|
+
end
|
37
|
+
elsif collection.is_a?(Hash)
|
38
|
+
if filter.arity == 1
|
39
|
+
return collection.select { |k, v| filter[v] }
|
40
|
+
else
|
41
|
+
return collection.select { |k, v| filter[v, k] }
|
42
|
+
end
|
43
|
+
else
|
44
|
+
return []
|
45
|
+
end
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
data/lib/rudash/find.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative 'filter.rb'
|
2
|
+
require_relative 'head.rb'
|
3
|
+
require_relative 'identity.rb'
|
4
|
+
|
5
|
+
module Find
|
6
|
+
extend Filter
|
7
|
+
extend Head
|
8
|
+
extend Identity
|
9
|
+
|
10
|
+
def find
|
11
|
+
find_proc = -> (collection, *rest_args) {
|
12
|
+
filter_proc = self.head[rest_args] || self.identity
|
13
|
+
filtered_arr = self.filter[collection, filter_proc]
|
14
|
+
|
15
|
+
filtered_arr[0]
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require_relative 'filter.rb'
|
2
|
+
|
3
|
+
module FindLast
|
4
|
+
extend Filter
|
5
|
+
|
6
|
+
def find_last
|
7
|
+
find_last_proc = -> (collection, *rest_args) {
|
8
|
+
filter_proc = self.head[rest_args] || self.identity
|
9
|
+
filtered_arr = self.filter[collection, filter_proc]
|
10
|
+
|
11
|
+
filtered_arr[filtered_arr.length - 1]
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
data/lib/rudash/flip.rb
ADDED
data/lib/rudash/get.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'reduce.rb'
|
2
|
+
|
3
|
+
module Get
|
4
|
+
extend Reduce
|
5
|
+
|
6
|
+
def get
|
7
|
+
get_proc = -> (hash, path, *rest_args) {
|
8
|
+
return nil if !path.is_a?(String) and !path.is_a?(Array)
|
9
|
+
return nil if !hash.is_a?(Array) and !hash.is_a?(Hash)
|
10
|
+
|
11
|
+
transform_path = -> (path) {
|
12
|
+
normalized_path = path
|
13
|
+
|
14
|
+
if normalized_path.is_a?(Array)
|
15
|
+
normalized_path = normalized_path.join('.')
|
16
|
+
end
|
17
|
+
|
18
|
+
normalized_path = normalized_path.gsub /[\[\]]/, "."
|
19
|
+
normalized_path = normalized_path.gsub /\.\./, "."
|
20
|
+
splitted_hash = normalized_path.split('.')
|
21
|
+
}
|
22
|
+
|
23
|
+
splitted_hash = transform_path[path]
|
24
|
+
|
25
|
+
get_reducer = -> (acc, current) {
|
26
|
+
return nil if acc.nil?
|
27
|
+
|
28
|
+
if acc.is_a?(Array) and current.match(/^(\d)+$/)
|
29
|
+
acc[current.to_i]
|
30
|
+
elsif acc.is_a?(Array) and !current.match(/^(\d)+$/)
|
31
|
+
nil
|
32
|
+
elsif acc.is_a?(Hash)
|
33
|
+
acc[current.to_sym]
|
34
|
+
else
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
}
|
38
|
+
|
39
|
+
self.reduce[splitted_hash, get_reducer, hash]
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
data/lib/rudash/head.rb
ADDED
data/lib/rudash/keys.rb
ADDED
data/lib/rudash/last.rb
ADDED
data/lib/rudash/map.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Map
|
2
|
+
def map
|
3
|
+
map_proc = -> (collection, *rest_args) {
|
4
|
+
mapper_proc = self.head[rest_args] || self.identity
|
5
|
+
|
6
|
+
col = collection.is_a?(String) ? collection.split('') : collection
|
7
|
+
|
8
|
+
if col.is_a?(Array)
|
9
|
+
return mapper_proc.arity == 1 ?
|
10
|
+
col.map { |value| mapper_proc[value] } :
|
11
|
+
col.map.with_index { |value, index| mapper_proc[value, index] }
|
12
|
+
|
13
|
+
elsif col.is_a?(Hash)
|
14
|
+
return mapper_proc.arity == 1 ?
|
15
|
+
col.map { |k,v| mapper_proc[v] } :
|
16
|
+
col.map { |k,v| mapper_proc[v, k] }
|
17
|
+
else
|
18
|
+
return []
|
19
|
+
end
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Reduce
|
2
|
+
def reduce
|
3
|
+
reduce_proc = -> (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
|
+
reducer.arity == 2 ?
|
14
|
+
reducer[acc, current[1]] :
|
15
|
+
reducer[acc, current[1], current[0]]
|
16
|
+
else
|
17
|
+
reducer[acc, current]
|
18
|
+
end
|
19
|
+
}
|
20
|
+
when 2
|
21
|
+
return col.reduce(initial_state) { |acc, current|
|
22
|
+
if col.is_a?(Hash)
|
23
|
+
reducer.arity == 2 ?
|
24
|
+
reducer[acc, current[1]] :
|
25
|
+
reducer[acc, current[1], current[0]]
|
26
|
+
else
|
27
|
+
reducer[acc, current]
|
28
|
+
end
|
29
|
+
}
|
30
|
+
else
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative 'reduce.rb'
|
2
|
+
|
3
|
+
module ReduceRight
|
4
|
+
extend Reduce
|
5
|
+
|
6
|
+
def reduce_right
|
7
|
+
reduce_right_proc = -> (collection, *rest_args) {
|
8
|
+
reversed_collection =
|
9
|
+
case collection
|
10
|
+
when Array then collection.reverse
|
11
|
+
when Hash then collection.reverse_each.to_h
|
12
|
+
when String then collection.reverse
|
13
|
+
else []
|
14
|
+
end
|
15
|
+
|
16
|
+
self.reduce[reversed_collection, *rest_args]
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
data/lib/rudash/size.rb
ADDED
data/lib/rudash/some.rb
ADDED
data/lib/rudash/tail.rb
ADDED
data/lib/rudash/uniq.rb
ADDED
data/lib/rudash.rb
CHANGED
@@ -1,6 +1,63 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require_relative './rudash/map.rb'
|
2
|
+
require_relative './rudash/is_nil.rb'
|
3
|
+
require_relative './rudash/filter.rb'
|
4
|
+
require_relative './rudash/some.rb'
|
5
|
+
require_relative './rudash/every.rb'
|
6
|
+
require_relative './rudash/find.rb'
|
7
|
+
require_relative './rudash/concat.rb'
|
8
|
+
require_relative './rudash/find_last.rb'
|
9
|
+
require_relative './rudash/reduce.rb'
|
10
|
+
require_relative './rudash/reduce_right.rb'
|
11
|
+
require_relative './rudash/compact.rb'
|
12
|
+
require_relative './rudash/head.rb'
|
13
|
+
require_relative './rudash/last.rb'
|
14
|
+
require_relative './rudash/tail.rb'
|
15
|
+
require_relative './rudash/size.rb'
|
16
|
+
require_relative './rudash/flip.rb'
|
17
|
+
require_relative './rudash/identity.rb'
|
18
|
+
require_relative './rudash/get.rb'
|
19
|
+
require_relative './rudash/is_array.rb'
|
20
|
+
require_relative './rudash/is_equal.rb'
|
21
|
+
require_relative './rudash/is_hash.rb'
|
22
|
+
require_relative './rudash/is_empty.rb'
|
23
|
+
require_relative './rudash/is_proc.rb'
|
24
|
+
require_relative './rudash/reverse.rb'
|
25
|
+
require_relative './rudash/curry.rb'
|
26
|
+
require_relative './rudash/is_number.rb'
|
27
|
+
require_relative './rudash/keys.rb'
|
28
|
+
require_relative './rudash/each.rb'
|
29
|
+
require_relative './rudash/uniq.rb'
|
30
|
+
require_relative './rudash/difference.rb'
|
3
31
|
|
4
|
-
class
|
32
|
+
class R_
|
5
33
|
extend Map
|
34
|
+
extend IsNil
|
35
|
+
extend Filter
|
36
|
+
extend Some
|
37
|
+
extend Every
|
38
|
+
extend Find
|
39
|
+
extend Concat
|
40
|
+
extend FindLast
|
41
|
+
extend Reduce
|
42
|
+
extend ReduceRight
|
43
|
+
extend Compact
|
44
|
+
extend Head
|
45
|
+
extend Last
|
46
|
+
extend Tail
|
47
|
+
extend Size
|
48
|
+
extend Flip
|
49
|
+
extend Identity
|
50
|
+
extend Get
|
51
|
+
extend IsArray
|
52
|
+
extend IsEqual
|
53
|
+
extend IsHash
|
54
|
+
extend IsEmpty
|
55
|
+
extend IsProc
|
56
|
+
extend Reverse
|
57
|
+
extend Curry
|
58
|
+
extend IsNumber
|
59
|
+
extend Keys
|
60
|
+
extend Each
|
61
|
+
extend Uniq
|
62
|
+
extend Difference
|
6
63
|
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: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Islam Attrash
|
@@ -16,11 +16,41 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
-
- lib/map.rb
|
20
19
|
- lib/rudash.rb
|
21
|
-
|
20
|
+
- lib/rudash/compact.rb
|
21
|
+
- lib/rudash/concat.rb
|
22
|
+
- lib/rudash/curry.rb
|
23
|
+
- lib/rudash/difference.rb
|
24
|
+
- lib/rudash/each.rb
|
25
|
+
- lib/rudash/every.rb
|
26
|
+
- lib/rudash/filter.rb
|
27
|
+
- lib/rudash/find.rb
|
28
|
+
- lib/rudash/find_last.rb
|
29
|
+
- lib/rudash/flip.rb
|
30
|
+
- lib/rudash/get.rb
|
31
|
+
- lib/rudash/head.rb
|
32
|
+
- lib/rudash/identity.rb
|
33
|
+
- lib/rudash/is_array.rb
|
34
|
+
- lib/rudash/is_empty.rb
|
35
|
+
- lib/rudash/is_equal.rb
|
36
|
+
- lib/rudash/is_hash.rb
|
37
|
+
- lib/rudash/is_nil.rb
|
38
|
+
- lib/rudash/is_number.rb
|
39
|
+
- lib/rudash/is_proc.rb
|
40
|
+
- lib/rudash/keys.rb
|
41
|
+
- lib/rudash/last.rb
|
42
|
+
- lib/rudash/map.rb
|
43
|
+
- lib/rudash/reduce.rb
|
44
|
+
- lib/rudash/reduce_right.rb
|
45
|
+
- lib/rudash/reverse.rb
|
46
|
+
- lib/rudash/size.rb
|
47
|
+
- lib/rudash/some.rb
|
48
|
+
- lib/rudash/tail.rb
|
49
|
+
- lib/rudash/uniq.rb
|
50
|
+
homepage: https://github.com/Attrash-Islam/rudash
|
22
51
|
licenses: []
|
23
|
-
metadata:
|
52
|
+
metadata:
|
53
|
+
source_code_uri: https://github.com/Attrash-Islam/rudash
|
24
54
|
post_install_message:
|
25
55
|
rdoc_options: []
|
26
56
|
require_paths:
|