liquid-nested-sort 0.1.0 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +56 -0
- data/.rspec +1 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +34 -0
- data/LICENSE +29 -0
- data/README.md +28 -0
- data/lib/liquid-nested-sort.rb +5 -0
- data/lib/liquid_nested_sort/nested_filters.rb +113 -0
- data/liquid-nested-sort.gemspec +23 -0
- metadata +10 -2
- data/lib/liquid_nested_sort.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f960256f408a36cc9c3f5f0f1be3296aa3a919734716a3d1f25bb043e6a5c443
|
4
|
+
data.tar.gz: a1a35ae637123dd1925a5407d02f151054cdb672d42dae2fa5b1749b9579ae6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a951ded949279e72f38cb86b07594cd7c50a09455e2994a1b688a0e3f720f06c45d3c23bad2767d30b03dfc6e93e96a160fc161770434ffdc683c9f613232ee4
|
7
|
+
data.tar.gz: e57d65ed6c30ebb6103da33d93450a6d5014102849b852fb837e15b862453d946e3c328039e61efba75a5a66e4669d885c9f022a40d970dc3ba6ffecd0c4c232
|
data/.gitignore
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
# Ignore Byebug command history file.
|
17
|
+
.byebug_history
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
.dat*
|
21
|
+
.repl_history
|
22
|
+
build/
|
23
|
+
*.bridgesupport
|
24
|
+
build-iPhoneOS/
|
25
|
+
build-iPhoneSimulator/
|
26
|
+
|
27
|
+
## Specific to RubyMotion (use of CocoaPods):
|
28
|
+
#
|
29
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
30
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
31
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
32
|
+
#
|
33
|
+
# vendor/Pods/
|
34
|
+
|
35
|
+
## Documentation cache and generated files:
|
36
|
+
/.yardoc/
|
37
|
+
/_yardoc/
|
38
|
+
/doc/
|
39
|
+
/rdoc/
|
40
|
+
|
41
|
+
## Environment normalization:
|
42
|
+
/.bundle/
|
43
|
+
/vendor/bundle
|
44
|
+
/lib/bundler/man/
|
45
|
+
|
46
|
+
# for a library or gem, you might want to ignore these files since the code is
|
47
|
+
# intended to run in multiple environments; otherwise, check them in:
|
48
|
+
# Gemfile.lock
|
49
|
+
# .ruby-version
|
50
|
+
# .ruby-gemset
|
51
|
+
|
52
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
53
|
+
.rvmrc
|
54
|
+
|
55
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
56
|
+
# .rubocop-https?--*
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
liquid-nested-sort (0.1.4)
|
5
|
+
liquid (>= 4.0, < 5.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
liquid (4.0.3)
|
12
|
+
rspec (3.9.0)
|
13
|
+
rspec-core (~> 3.9.0)
|
14
|
+
rspec-expectations (~> 3.9.0)
|
15
|
+
rspec-mocks (~> 3.9.0)
|
16
|
+
rspec-core (3.9.2)
|
17
|
+
rspec-support (~> 3.9.3)
|
18
|
+
rspec-expectations (3.9.2)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.9.0)
|
21
|
+
rspec-mocks (3.9.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.9.0)
|
24
|
+
rspec-support (3.9.3)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
liquid-nested-sort!
|
31
|
+
rspec (~> 3.9)
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
2.1.4
|
data/LICENSE
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
BSD 3-Clause License
|
2
|
+
|
3
|
+
Copyright (c) 2020, Fabio Bonelli
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
8
|
+
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
10
|
+
list of conditions and the following disclaimer.
|
11
|
+
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
14
|
+
and/or other materials provided with the distribution.
|
15
|
+
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
17
|
+
contributors may be used to endorse or promote products derived from
|
18
|
+
this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# liquid-nested-sort
|
2
|
+
|
3
|
+
[](https://rubygems.org/gems/liquid-nested-sort)
|
4
|
+
|
5
|
+
`nested_sort` and `nested_sort_natural`, sort and sort_natural-like filters
|
6
|
+
with nested fields support for [Liquid](https://shopify.github.io/liquid/).
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
In the Gemfile
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'liquid-nested-sort'
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
```liquid
|
19
|
+
{{ places | nested_sort "address.street" }}
|
20
|
+
```
|
21
|
+
|
22
|
+
```liquid
|
23
|
+
{{ places | nested_sort_natural "address.street" }}
|
24
|
+
```
|
25
|
+
|
26
|
+
## Thanks
|
27
|
+
|
28
|
+
This is mostly based on @untra's work (https://github.com/untra).
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'liquid/errors'
|
4
|
+
|
5
|
+
module LiquidUtils
|
6
|
+
def self.nil_safe_compare(a, b)
|
7
|
+
if !a.nil? && !b.nil?
|
8
|
+
a <=> b
|
9
|
+
else
|
10
|
+
a.nil? ? 1 : -1
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.nil_safe_casecmp(a, b)
|
15
|
+
if !a.nil? && !b.nil?
|
16
|
+
a.to_s.casecmp(b.to_s)
|
17
|
+
else
|
18
|
+
a.nil? ? 1 : -1
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.nested_respond_to?(input, property)
|
23
|
+
return true if property.empty?
|
24
|
+
|
25
|
+
p = property.split('.').first
|
26
|
+
q = property[/#{p}.?([\w.]*)/, 1]
|
27
|
+
|
28
|
+
return nested_respond_to?(input[p], q) if input.respond_to?(:[]) && accepts?(input, p)
|
29
|
+
return nested_respond_to?(input.send(p), q) if input.respond_to?(p)
|
30
|
+
|
31
|
+
false
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.nested_send(input, property)
|
35
|
+
return input if property.empty?
|
36
|
+
|
37
|
+
p = property.split('.').first
|
38
|
+
q = property[/#{p}.?([\w.]*)/, 1]
|
39
|
+
|
40
|
+
return nested_send(input[p], q) if input.respond_to?(:[]) && accepts?(input, p)
|
41
|
+
return nested_send(input.send(p), q) if input.respond_to?(p)
|
42
|
+
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.accepts?(input, index)
|
47
|
+
!input[index].nil?
|
48
|
+
rescue TypeError
|
49
|
+
false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module InputIterator
|
54
|
+
def self.new(input)
|
55
|
+
if input.is_a?(Array)
|
56
|
+
input.flatten
|
57
|
+
elsif input.is_a?(Hash)
|
58
|
+
[input]
|
59
|
+
elsif input.is_a?(Enumerable)
|
60
|
+
input
|
61
|
+
else
|
62
|
+
Array(input)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
module NestedFilters
|
68
|
+
include Liquid
|
69
|
+
|
70
|
+
def nested_sort(input, property = nil)
|
71
|
+
ary = InputIterator.new(input)
|
72
|
+
|
73
|
+
return [] if ary.empty?
|
74
|
+
|
75
|
+
if property.nil?
|
76
|
+
ary.sort do |a, b|
|
77
|
+
LiquidUtils.nil_safe_compare(a, b)
|
78
|
+
end
|
79
|
+
elsif property.include?('.') && LiquidUtils.nested_respond_to?(ary.first, property)
|
80
|
+
ary.sort do |a, b|
|
81
|
+
LiquidUtils.nil_safe_compare(LiquidUtils.nested_send(a, property), LiquidUtils.nested_send(b, property))
|
82
|
+
end
|
83
|
+
elsif ary.all? { |el| el.respond_to?(:[]) }
|
84
|
+
begin
|
85
|
+
ary.sort { |a, b| LiquidUtils.nil_safe_compare(a[property], b[property]) }
|
86
|
+
rescue TypeError
|
87
|
+
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def nested_sort_natural(input, property = nil)
|
93
|
+
ary = InputIterator.new(input)
|
94
|
+
|
95
|
+
return [] if ary.empty?
|
96
|
+
|
97
|
+
if property.nil?
|
98
|
+
ary.sort do |a, b|
|
99
|
+
LiquidUtils.nil_safe_casecmp(a, b)
|
100
|
+
end
|
101
|
+
elsif property.include?('.') && LiquidUtils.nested_respond_to?(ary.first, property)
|
102
|
+
ary.sort do |a, b|
|
103
|
+
LiquidUtils.nil_safe_casecmp(LiquidUtils.nested_send(a, property), LiquidUtils.nested_send(b, property))
|
104
|
+
end
|
105
|
+
elsif ary.all? { |el| el.respond_to?(:[]) }
|
106
|
+
begin
|
107
|
+
ary.sort { |a, b| LiquidUtils.nil_safe_casecmp(a[property], b[property]) }
|
108
|
+
rescue TypeError
|
109
|
+
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'liquid-nested-sort'
|
5
|
+
s.version = '0.1.5'
|
6
|
+
s.date = '2020-09-03'
|
7
|
+
|
8
|
+
s.author = 'Fabio Bonelli'
|
9
|
+
s.email = 'fb@fabiobonelli.it'
|
10
|
+
s.license = 'BSD-3-Clause'
|
11
|
+
s.homepage = 'https://github.com/bfabio/liquid-nested-sort'
|
12
|
+
|
13
|
+
s.summary = 'sort and sort_natural filters with nested fields support for Liquid'
|
14
|
+
s.description =
|
15
|
+
"Liquid's sort and sort_natural don't support nested fields out of the box." \
|
16
|
+
'This gems provides nested_sort and nester_sort_natural.'
|
17
|
+
|
18
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(spec)/!) }
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
s.add_runtime_dependency('liquid', ['>= 4.0', '< 5.0'])
|
22
|
+
s.required_ruby_version = '~> 2.4'
|
23
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: liquid-nested-sort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabio Bonelli
|
@@ -37,7 +37,15 @@ executables: []
|
|
37
37
|
extensions: []
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
|
-
-
|
40
|
+
- ".gitignore"
|
41
|
+
- ".rspec"
|
42
|
+
- Gemfile
|
43
|
+
- Gemfile.lock
|
44
|
+
- LICENSE
|
45
|
+
- README.md
|
46
|
+
- lib/liquid-nested-sort.rb
|
47
|
+
- lib/liquid_nested_sort/nested_filters.rb
|
48
|
+
- liquid-nested-sort.gemspec
|
41
49
|
homepage: https://github.com/bfabio/liquid-nested-sort
|
42
50
|
licenses:
|
43
51
|
- BSD-3-Clause
|
data/lib/liquid_nested_sort.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module NestedFilter
|
4
|
-
def nested_sort(input, property = nil)
|
5
|
-
ary = InputIterator.new(input, context)
|
6
|
-
|
7
|
-
return [] if ary.empty?
|
8
|
-
|
9
|
-
if property.nil?
|
10
|
-
ary.sort do |a, b|
|
11
|
-
nil_safe_compare(a, b)
|
12
|
-
end
|
13
|
-
elsif ary.all? { |el| el.respond_to?(:[]) }
|
14
|
-
begin
|
15
|
-
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
|
16
|
-
rescue TypeError
|
17
|
-
raise_property_error(property)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def nested_sort_natural(input, property = nil)
|
23
|
-
ary = InputIterator.new(input, context)
|
24
|
-
|
25
|
-
return [] if ary.empty?
|
26
|
-
|
27
|
-
if property.nil?
|
28
|
-
ary.sort do |a, b|
|
29
|
-
nil_safe_compare(a, b)
|
30
|
-
end
|
31
|
-
elsif ary.all? { |el| el.respond_to?(:[]) }
|
32
|
-
begin
|
33
|
-
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
|
34
|
-
rescue TypeError
|
35
|
-
raise_property_error(property)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
Liquid::Template.register_filter(NestedFilter)
|