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 +4 -4
- data/lib/dry/file/version.rb +1 -1
- data/lib/dry-file.rb +12 -1
- 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: '05033942756985182b36d42efab7fab3a5889ef3c962b43f5f23278e0255715e'
|
4
|
+
data.tar.gz: 3db77fccd38ea90edeca92d15b398415e5e76c90b58672516190caefc7cf5557
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7abcbe9c8115af222732a346b204318292b670c1e293b19184e12308bd893f2ccd51c12c46a025f28072a99f5a0420ccbbd38845564589bfdc51037b47b30f24
|
7
|
+
data.tar.gz: 8cb92f269c12dc0bb74b20be903e8655257ddfd8d5549281e617bf4968d8847eea357ac7bc72aecf772de18308edd56e768da3b6a17ce64cc565692c2826284f
|
data/lib/dry/file/version.rb
CHANGED
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
|
-
|
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
|