json_truncate 0.0.1 → 0.0.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/json_truncate/truncate.rb +9 -3
- data/lib/json_truncate/version.rb +1 -1
- data/spec/json_truncate/truncate_spec.rb +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb27084b5001ec5a67a011e090fae42b4aa3f5676defc85d1fa169a85869c2ea
|
4
|
+
data.tar.gz: c6a69b4ef383d7034fcc9b0305743f6385cb063ac9aad4c1b44837583b657a0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd71faeaf4a2934e0c72e243d3372ebfd4c49658b6a9845fc5ee65f5499ee9dc093fb2aee4f375ecb0c65afd8f49c13dd2a238f2b8ee20c988e823be543e4715
|
7
|
+
data.tar.gz: 3f4dda67c7c36bcb30af90d5f1771a1b657370c1705163db9416620ae212758dddae258e50c12e7b96406728f79287ad7a33c0265fe7b4093694261d2cbfbe3a
|
@@ -1,31 +1,37 @@
|
|
1
1
|
module JsonTruncate
|
2
2
|
def self.truncate(object, opts = {})
|
3
|
-
process(object
|
3
|
+
process(object, opts)
|
4
4
|
end
|
5
5
|
|
6
|
+
private
|
7
|
+
|
6
8
|
def self.process(object, opts, depth = 0)
|
7
9
|
depth += 1
|
8
10
|
|
9
11
|
if opts[:max_array_length] && object.is_a?(Array) && object.length > opts[:max_array_length]
|
12
|
+
object = object.dup
|
10
13
|
object.slice!(opts[:max_array_length]..-1)
|
11
14
|
object.push('...')
|
12
15
|
end
|
13
16
|
|
14
17
|
if opts[:max_string_length] && object.is_a?(String) && object.length > opts[:max_string_length]
|
18
|
+
object = object.dup
|
15
19
|
object.slice!(opts[:max_string_length]..-1)
|
16
20
|
object << '...'
|
17
21
|
end
|
18
22
|
|
19
23
|
if opts[:max_depth] && depth > opts[:max_depth] && [ Array, Hash ].include?(object.class)
|
24
|
+
object = object.dup
|
20
25
|
object.delete_if { |_| true }
|
21
26
|
end
|
22
27
|
|
23
28
|
if object.is_a?(Hash)
|
29
|
+
object = object.dup
|
24
30
|
object.each do |k,v|
|
25
|
-
process(v, opts, depth)
|
31
|
+
object[k] = process(v, opts, depth)
|
26
32
|
end
|
27
33
|
elsif object.is_a?(Array)
|
28
|
-
object.
|
34
|
+
object = object.map do |v|
|
29
35
|
process(v, opts, depth)
|
30
36
|
end
|
31
37
|
end
|
@@ -8,6 +8,7 @@ describe JsonTruncate do
|
|
8
8
|
o,
|
9
9
|
max_depth: 1
|
10
10
|
)).to eql({ foo: 'bar', bar: {}})
|
11
|
+
expect(o).to eql({ foo: 'bar', bar: { baz: 'foo' }})
|
11
12
|
end
|
12
13
|
|
13
14
|
it "should remove array items" do
|
@@ -17,6 +18,7 @@ describe JsonTruncate do
|
|
17
18
|
o,
|
18
19
|
max_array_length: 1
|
19
20
|
)).to eql({ foo: [ 'foo', '...' ], bar: { baz: [ 'foo', '...' ]}})
|
21
|
+
expect(o).to eql({ foo: [ 'foo', 'bar', 'baz' ], bar: { baz: [ 'foo', 'bar' ]}})
|
20
22
|
end
|
21
23
|
|
22
24
|
it "should truncate strings" do
|
@@ -26,5 +28,6 @@ describe JsonTruncate do
|
|
26
28
|
o,
|
27
29
|
max_string_length: 5
|
28
30
|
)).to eql({ foo: '12345...', bar: { baz: '12345...', 'bat': [ '12345...' ]}})
|
31
|
+
expect(o).to eql({ foo: '1234567890', bar: { baz: '1234567890', bat: [ '1234567890' ]}})
|
29
32
|
end
|
30
33
|
end
|