liquid-nested-sort 0.1.1 → 0.1.2
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/lib/nested_filters.rb +109 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34369c0d821e1d3dd90d10bc53b220bba76474a75a94e4b6e5e310ae28528f56
|
4
|
+
data.tar.gz: e394c609fd5a08cc078ab749ecbc5024c78fb9c6aa20bf8a99399a6244f03825
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 121226ef738ef9cef1cf0e77798cc836d55068ba8b0e64986fcf518e179f3edf67cd309c1cc2675c8a120e0fd4e45fbbccb993afd8242e517196d605dc0e945f
|
7
|
+
data.tar.gz: a21222c2ceb00988546ac926e1a8241f9552bac7c8093fa59e07e91899c952cf623949a2e427612e16c5ceaa2c52960a5c6816dd4482b4e28804139475afc463
|
@@ -0,0 +1,109 @@
|
|
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 ary.all? { |el| el.respond_to?(:[]) }
|
80
|
+
begin
|
81
|
+
ary.sort { |a, b| LiquidUtils.nil_safe_compare(a[property], b[property]) }
|
82
|
+
rescue TypeError
|
83
|
+
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def nested_sort_natural(input, property = nil)
|
89
|
+
ary = InputIterator.new(input)
|
90
|
+
|
91
|
+
return [] if ary.empty?
|
92
|
+
|
93
|
+
if property.nil?
|
94
|
+
ary.sort do |a, b|
|
95
|
+
LiquidUtils.nil_safe_casecmp(a, b)
|
96
|
+
end
|
97
|
+
elsif property.include?('.') && LiquidUtils.nested_respond_to?(ary.first, property)
|
98
|
+
ary.sort do |a, b|
|
99
|
+
LiquidUtils.nil_safe_casecmp(LiquidUtils.nested_send(a, property), LiquidUtils.nested_send(b, property))
|
100
|
+
end
|
101
|
+
elsif ary.all? { |el| el.respond_to?(:[]) }
|
102
|
+
begin
|
103
|
+
ary.sort { |a, b| LiquidUtils.nil_safe_casecmp(a[property], b[property]) }
|
104
|
+
rescue TypeError
|
105
|
+
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabio Bonelli
|
@@ -38,6 +38,7 @@ extensions: []
|
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
40
|
- lib/liquid_nested_sort.rb
|
41
|
+
- lib/nested_filters.rb
|
41
42
|
homepage: https://github.com/bfabio/liquid-nested-sort
|
42
43
|
licenses:
|
43
44
|
- BSD-3-Clause
|