deepsort 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/deepsort.rb +131 -0
  3. metadata +43 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmE0NTViYTFjZWQ4NmRhMDJmZDhhZTMzYzQ0MzhlMzYwNzAyNjM3Mg==
5
+ data.tar.gz: !binary |-
6
+ MjhjZGUwM2EwM2QzNjcwYTdhMzhjZDMxNWM0YTMyYzk2OWQ4NDY3Mg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YWFlMmY1NjcwMjEwYjNiYmQ0YTYxMWJhMDgwYTAzZjY1MGExNzE1ZDU0Mzhj
10
+ ZjYzODgwOGUzNTFkZGFmYzUxMzlkNjU5M2FjOWZiNTA0MGE2ZDFjZmNmZWY1
11
+ YzA5NzUyZTcwZTUxODEwZTMyMzYyOTk4Yjg1MTBjNmIzYzBhOWI=
12
+ data.tar.gz: !binary |-
13
+ YTZjM2QwN2QyOTI5ZmIwMzZmY2M3ZWJkNzZjZTFlOTQyOGFiODgwNmVjZjg4
14
+ MjQzMzM5MmJiMzUwYTJlZTAyN2E5YWI0ODI3YTJmOGNkNjUyMWUxNDA5MThm
15
+ N2Y1ZmJlODRlYjRjMmYzYzk3M2M4NTE4ZTYzYWQ2Mjc0ZGVjNmU=
@@ -0,0 +1,131 @@
1
+ ### Deep Sort Utility
2
+ ## Over View
3
+ # when included into a project, this utility gives Arrays and Hashes the ability to deep sort themselves
4
+ # instead of shallow sorts, deep_sort recursively attempts to sort every element of arrays and hashes
5
+ ## Usage
6
+ # {2=>4, 1=>[3, 2]}.deep_sort
7
+ # returns: {1=>[2, 3], 2=>4}
8
+ # [{2=>3}, 1].deep_sort
9
+ # returns an error because a Fixnum (1) cannot be compared with a Hash ({2=>3})
10
+ # [{2=>3}, 1].deep_sort_by {|obj| obj.to_s}
11
+ # returns: [1, {2=>3}] # this avoids the error by using string comparison
12
+ # foo = [2, 1]; foo.deep_sort!
13
+ # returns: [1, 2] # in addition foo has now been sorted in place
14
+
15
+
16
+ module DeepSort
17
+ # inject this method into the Array class to add deep sort functionality to Arrays
18
+ module DeepSortArray
19
+ def deep_sort
20
+ deep_sort_by { |obj| obj }
21
+ end
22
+
23
+ def deep_sort!
24
+ deep_sort_by! { |obj| obj }
25
+ end
26
+
27
+ def deep_sort_by(&block)
28
+ self.map do |value|
29
+ if value.respond_to? :deep_sort_by
30
+ value.deep_sort_by &block
31
+ elsif value.respond_to? :sort_by
32
+ value.sort_by &block
33
+ else
34
+ value
35
+ end
36
+ end.sort_by &block
37
+ end
38
+
39
+ def deep_sort_by!(&block)
40
+ self.map! do |value|
41
+ if value.respond_to? :deep_sort_by!
42
+ value.deep_sort_by! &block
43
+ elsif value.respond_to? :sort_by!
44
+ value.sort_by! &block
45
+ else
46
+ value
47
+ end
48
+ end.sort_by! &block
49
+ end
50
+ end
51
+
52
+ # inject this method into the Hash class to add deep sort functionality to Hashes
53
+ # Note: this cannot sort a hashes keys in place (deep_sort!), only the values
54
+ module DeepSortHash
55
+ def deep_sort
56
+ deep_sort_by { |obj| obj }
57
+ end
58
+
59
+ def deep_sort!
60
+ deep_sort_by! { |obj| obj }
61
+ end
62
+
63
+ def deep_sort_by(&block)
64
+ Hash[self.map do |key, value|
65
+ [if key.respond_to? :deep_sort_by
66
+ key.deep_sort_by &block
67
+ elsif key.respond_to? :sort_by
68
+ key.sort_by &block
69
+ else
70
+ key
71
+ end,
72
+
73
+ if value.respond_to? :deep_sort_by
74
+ value.deep_sort_by &block
75
+ elsif value.respond_to? :sort_by
76
+ value.sort_by &block
77
+ else
78
+ value
79
+ end]
80
+
81
+ end.sort_by &block]
82
+ end
83
+
84
+ # Ruby Hashes don't have in-place-modification like map!, each!, or sort!
85
+ # that means that this method won't be able to sort the hash keys in place either.
86
+ # since Hashes are technically non-sorted key value pairs, this shouldn't be a problem
87
+ def deep_sort_by!(&block)
88
+ Hash[self.map do |key, value|
89
+ if key.respond_to? :deep_sort_by!
90
+ key.deep_sort_by! &block
91
+ elsif key.respond_to? :sort_by!
92
+ key.sort_by! &block
93
+ end
94
+
95
+ if value.respond_to? :deep_sort!
96
+ value.deep_sort_by! &block
97
+ elsif value.respond_to? :sort_by!
98
+ value.sort_by! &block
99
+ end
100
+
101
+ [key, value]
102
+ end]
103
+ end
104
+ end
105
+ end
106
+ Array.send(:include, DeepSort::DeepSortArray)
107
+ Hash.send(:include, DeepSort::DeepSortHash)
108
+
109
+
110
+ # and if you don't like calling member methods on objects, these two functions do it for you.
111
+ # if the object cannot be deep sorted, it will simply return the sorted object or the object itself if sorting isn't available.
112
+ def deep_sort(obj)
113
+ if obj.respond_to? :deep_sort
114
+ obj.deep_sort
115
+ elsif obj.respond_to? :sort
116
+ obj.sort
117
+ else
118
+ obj
119
+ end
120
+ end
121
+
122
+ # similar to the deep_sort method, but performs the deep sort in place
123
+ def deep_sort!(obj)
124
+ if obj.respond_to? :deep_sort!
125
+ obj.deep_sort!
126
+ elsif obj.respond_to? :sort!
127
+ obj.sort!
128
+ else
129
+ obj
130
+ end
131
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deepsort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Crossen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: deepsort recursively sorts nested ruby Arrays and Hashes.
14
+ email: markcrossen@studentbody.byu.edu
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/deepsort.rb
20
+ homepage: https://github.com/mcrossen/deepsort
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.4.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A utility to deep sort arrays and hashes.
43
+ test_files: []