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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc0246cf913cdef7f3bfd498765aa29d6dc5712fbcb259fd46cc44f93724d273
4
- data.tar.gz: db42da9845727e231270614a8666d1860df573446e17586effff62fbfb2d68be
3
+ metadata.gz: fb27084b5001ec5a67a011e090fae42b4aa3f5676defc85d1fa169a85869c2ea
4
+ data.tar.gz: c6a69b4ef383d7034fcc9b0305743f6385cb063ac9aad4c1b44837583b657a0c
5
5
  SHA512:
6
- metadata.gz: 57db2f96a4abc66ae1283ec571c3320c3df50a8a78077145b3e89b8e777d4ed68b7a4f25222011ce9d5eecacf7eb44be1bbd7e5a6debae468ab4ed9d6306e0bd
7
- data.tar.gz: 9b4bac28065b56d104622bde4b4481b5674afbbbb9a860ea9a839c2ff77902f517d48806ccca8f481f3d8a9ce133ba1022727b8e636f7b5d2615e4f4c2fa4793
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.dup, opts)
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.each do |v|
34
+ object = object.map do |v|
29
35
  process(v, opts, depth)
30
36
  end
31
37
  end
@@ -1,3 +1,3 @@
1
1
  module JsonTruncate
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_truncate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean West