deep_sort 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -8
- data/lib/deep_sort.rb +22 -2
- data/lib/deep_sort/{array_ext.rb → array.rb} +7 -7
- data/lib/deep_sort/extend.rb +7 -0
- data/lib/deep_sort/{hash_ext.rb → hash.rb} +7 -7
- data/lib/deep_sort/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d902dc8fc0a4ecbb6e53e31306256e8b582c8c62
|
4
|
+
data.tar.gz: 27e1a75e36c82b6a15bae09fb1bbeb6d6e52ecda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce2fa3b09a814bd5a834cfd3ba1455c342945b9832f8f0a87021f048d69cb6cfc04cbba8afec1cc080c290bf4c91ea80cca552ba0c8fe9fbabce80aae01bc68d
|
7
|
+
data.tar.gz: 5e87d45d71dc79fd48b781b5d235204350fbc3017a7c3a5581d7bfdcd63d4b24320dc4f94333b8dfae06b95986b11a1c364bcb8e56d41ce2ac1526ad94d08e4f
|
data/README.md
CHANGED
@@ -21,12 +21,14 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
$ gem install deep_sort
|
23
23
|
|
24
|
-
##
|
24
|
+
## Methods
|
25
25
|
|
26
|
-
* `
|
27
|
-
* `
|
28
|
-
* `
|
29
|
-
* `
|
26
|
+
* `DeepSort.deep_sort(obj, options = {})`
|
27
|
+
* `require 'deep_sort/extend'`
|
28
|
+
* `Array#deep_sort(options = {})`
|
29
|
+
* `Array#deep_sort!(options = {})`
|
30
|
+
* `Hash#deep_sort(options = {})`
|
31
|
+
* `Hash#deep_sort!(options = {})`
|
30
32
|
|
31
33
|
## Usage
|
32
34
|
|
@@ -50,8 +52,7 @@ hash = {"firstName"=>"John",
|
|
50
52
|
"number"=>[5, 4, 9, 7],
|
51
53
|
"tags"=>Set["staff", "engineer", {"joined_at"=>"2016", "emp_no"=>10}, "writer"]}
|
52
54
|
|
53
|
-
|
54
|
-
pp hash.deep_sort
|
55
|
+
pp DeepSort.deep_sort(hash)
|
55
56
|
#=> {"address"=>
|
56
57
|
# {"city"=>"New York",
|
57
58
|
# "postalCode"=>"10021",
|
@@ -71,7 +72,7 @@ pp hash.deep_sort
|
|
71
72
|
### `sort_enum: true`
|
72
73
|
|
73
74
|
```ruby
|
74
|
-
pp
|
75
|
+
pp DeepSort.deep_sort(sort_enum: true)
|
75
76
|
#=> {"address"=>
|
76
77
|
# {"city"=>"New York",
|
77
78
|
# "postalCode"=>"10021",
|
data/lib/deep_sort.rb
CHANGED
@@ -1,3 +1,23 @@
|
|
1
1
|
require 'deep_sort/version'
|
2
|
-
require 'deep_sort/
|
3
|
-
require 'deep_sort/
|
2
|
+
require 'deep_sort/array'
|
3
|
+
require 'deep_sort/hash'
|
4
|
+
|
5
|
+
module DeepSort
|
6
|
+
def deep_sort(obj, options = {})
|
7
|
+
case obj
|
8
|
+
when Array
|
9
|
+
DeepSort::Array.deep_sort(obj, options)
|
10
|
+
when Hash
|
11
|
+
DeepSort::Hash.deep_sort(obj, options)
|
12
|
+
when Enumerable
|
13
|
+
if options[:sort_enum]
|
14
|
+
DeepSort::Array.deep_sort(obj.to_a, options)
|
15
|
+
else
|
16
|
+
obj
|
17
|
+
end
|
18
|
+
else
|
19
|
+
obj
|
20
|
+
end
|
21
|
+
end
|
22
|
+
module_function :deep_sort
|
23
|
+
end
|
@@ -1,11 +1,15 @@
|
|
1
|
-
module DeepSort::
|
1
|
+
module DeepSort::Array
|
2
2
|
def deep_sort!(options = {})
|
3
3
|
new_array = self.deep_sort(options)
|
4
4
|
self.replace(new_array)
|
5
5
|
end
|
6
6
|
|
7
|
-
def deep_sort(options
|
8
|
-
self
|
7
|
+
def deep_sort(options)
|
8
|
+
DeepSort::Array.deep_sort(self, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.deep_sort(array, options = {})
|
12
|
+
array.map {|value|
|
9
13
|
case value
|
10
14
|
when Array, Hash
|
11
15
|
value.deep_sort(options)
|
@@ -21,7 +25,3 @@ module DeepSort::ArrayExt
|
|
21
25
|
}.sort_by(&:to_s)
|
22
26
|
end
|
23
27
|
end
|
24
|
-
|
25
|
-
class Array
|
26
|
-
prepend DeepSort::ArrayExt
|
27
|
-
end
|
@@ -1,14 +1,18 @@
|
|
1
|
-
module DeepSort::
|
1
|
+
module DeepSort::Hash
|
2
2
|
def deep_sort!(options = {})
|
3
3
|
new_hash = self.deep_sort(options)
|
4
4
|
self.replace(new_hash)
|
5
5
|
end
|
6
6
|
|
7
7
|
def deep_sort(options = {})
|
8
|
+
DeepSort::Hash.deep_sort(self, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.deep_sort(hash, options = {})
|
8
12
|
new_hash = {}
|
9
13
|
|
10
|
-
|
11
|
-
value =
|
14
|
+
hash.keys.sort_by(&:to_s).each do |key|
|
15
|
+
value = hash[key]
|
12
16
|
|
13
17
|
case value
|
14
18
|
when Array, Hash
|
@@ -25,7 +29,3 @@ module DeepSort::HashExt
|
|
25
29
|
new_hash
|
26
30
|
end
|
27
31
|
end
|
28
|
-
|
29
|
-
class Hash
|
30
|
-
prepend DeepSort::HashExt
|
31
|
-
end
|
data/lib/deep_sort/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_sort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- winebarrel
|
@@ -84,8 +84,9 @@ files:
|
|
84
84
|
- bin/setup
|
85
85
|
- deep_sort.gemspec
|
86
86
|
- lib/deep_sort.rb
|
87
|
-
- lib/deep_sort/
|
88
|
-
- lib/deep_sort/
|
87
|
+
- lib/deep_sort/array.rb
|
88
|
+
- lib/deep_sort/extend.rb
|
89
|
+
- lib/deep_sort/hash.rb
|
89
90
|
- lib/deep_sort/version.rb
|
90
91
|
homepage: https://github.com/winebarrel/deep_sort
|
91
92
|
licenses:
|