dry-file 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a883549eade781c80813ca23a4c30e815f370f812c6a1f3d2a88c59d9689764
4
- data.tar.gz: 96ff7db47c422bccab7369439e10e59ac0d8f83938e02d77cbef803c71826cc9
3
+ metadata.gz: '05033942756985182b36d42efab7fab3a5889ef3c962b43f5f23278e0255715e'
4
+ data.tar.gz: 3db77fccd38ea90edeca92d15b398415e5e76c90b58672516190caefc7cf5557
5
5
  SHA512:
6
- metadata.gz: d5a8779022a7c96b0ad9b4d9f06edc1ba2faa442d6bf6df008f36a4b9a895981642b13623baa2130455e6cbf21f7498c66c7504de7e198362089a40ce1ccc93e
7
- data.tar.gz: e43abe9421f3b19c16a0c0aa1887a556952d49ba8d35b7d4db64657754bf809cc006cab53335c3f50b31b14762f9f36296eca2b42b1ba363f6993a22f8a6849d
6
+ metadata.gz: 7abcbe9c8115af222732a346b204318292b670c1e293b19184e12308bd893f2ccd51c12c46a025f28072a99f5a0420ccbbd38845564589bfdc51037b47b30f24
7
+ data.tar.gz: 8cb92f269c12dc0bb74b20be903e8655257ddfd8d5549281e617bf4968d8847eea357ac7bc72aecf772de18308edd56e768da3b6a17ce64cc565692c2826284f
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module File
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
data/lib/dry-file.rb CHANGED
@@ -15,8 +15,14 @@ module Dry
15
15
  max_chars = max_chars.to_i
16
16
  previous_line = ''
17
17
  file_out = Tempfile.new(File.basename(file))
18
+
18
19
  File.open(file).each_line do |l|
19
- if (previous_line.size - l.size).abs > max_chars || previous_line.chars.each_with_index.filter_map{|c,i| previous_line[i] != l[i]}.size > max_chars
20
+ # We compare the line size as well as the line similarity. We could have
21
+ # different treshold values for each of these comparisons (eg use
22
+ # +max_chars+ for the size comparison and +max_diff_chars+ for the
23
+ # similarity comparison, but i think using +max_chars+ for both
24
+ # comparisons is also OK, the results seem pretty good.
25
+ if (previous_line.size - l.size).abs > max_chars || diff_size(previous_line, l) > max_chars
20
26
  file_out.write l
21
27
  previous_line = l
22
28
  end
@@ -24,4 +30,9 @@ module Dry
24
30
  file_out.close
25
31
  FileUtils.mv(file_out.path, File.join(File.dirname(file), "dry-#{File.basename(file)}"))
26
32
  end
33
+
34
+ # Returns the number of differing characters between two lines.
35
+ def diff_size(line_1, line_2)
36
+ line1.chars.each_with_index.filter_map{|c,i| line1[i] != line2[i]}.size
37
+ end
27
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergio Romero