better_csv 0.0.2 → 0.0.3
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/better_csv/version.rb +1 -1
- data/lib/better_csv.rb +25 -9
- data/spec/better_csv_spec.rb +79 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 934f91f530d16204f142e55b3b14e576cd1acc67
|
4
|
+
data.tar.gz: afca2a95bb8095a3599507fc8a795248d472e84d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e9bc0c0d7f3b98365bbab1333a5b7cfd1486147305e9af4cbe4c2cddd99f2547f653bce0b65a13c5bb7eca2fe7f35b36fd03f6b26bfb776f4d9448c2278ccb9
|
7
|
+
data.tar.gz: 93e9bb0b002242ae5e531498e976ce66aac66893d8b3e0116a167908ba9bc839829e33ac17e8a6338b892f4970d534b56c92555aa611d56d7cf0f57c6cc5a525
|
data/lib/better_csv/version.rb
CHANGED
data/lib/better_csv.rb
CHANGED
@@ -2,9 +2,11 @@ require "better_csv/version"
|
|
2
2
|
|
3
3
|
module BetterCsv
|
4
4
|
def self.parse(str)
|
5
|
-
|
5
|
+
in_literal = false
|
6
6
|
csv = Array.new
|
7
|
-
|
7
|
+
|
8
|
+
line = []
|
9
|
+
token = ""
|
8
10
|
back = ""
|
9
11
|
current = ""
|
10
12
|
first = true
|
@@ -12,26 +14,40 @@ module BetterCsv
|
|
12
14
|
current = c
|
13
15
|
if first
|
14
16
|
back = current
|
15
|
-
|
17
|
+
if c == '"'
|
18
|
+
in_literal = true
|
19
|
+
else
|
20
|
+
token << c
|
21
|
+
end
|
16
22
|
first = false
|
17
23
|
next
|
18
24
|
end
|
19
25
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
line
|
26
|
+
in_literal = !in_literal if current == '"' and not back == '\\'
|
27
|
+
|
28
|
+
if current == ',' and not in_literal
|
29
|
+
line << token
|
30
|
+
token = ""
|
31
|
+
next
|
32
|
+
end
|
33
|
+
|
34
|
+
if current == "\n" and not in_literal
|
35
|
+
csv << line
|
36
|
+
line = Array.new
|
24
37
|
next
|
25
38
|
end
|
26
39
|
|
27
|
-
|
40
|
+
token << c unless current == '"' and not back == '\\'
|
28
41
|
back = current
|
29
42
|
end
|
30
|
-
|
43
|
+
|
44
|
+
line << token if token.size > 0
|
45
|
+
csv << line if line.size > 0
|
31
46
|
|
32
47
|
return csv
|
33
48
|
rescue => e
|
34
49
|
STDERR.puts e
|
35
50
|
STDERR.puts e.backtrace
|
36
51
|
end
|
52
|
+
|
37
53
|
end
|
data/spec/better_csv_spec.rb
CHANGED
@@ -4,8 +4,85 @@ describe BetterCsv do
|
|
4
4
|
it 'has a version number' do
|
5
5
|
expect(BetterCsv::VERSION).not_to be nil
|
6
6
|
end
|
7
|
+
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
context 'on basic csv test' do
|
10
|
+
str = 'a,b,c,d,e,f,g'
|
11
|
+
equal = [['a','b','c','d','e','f','g']]
|
12
|
+
|
13
|
+
describe "sepalated with simply ',' == #{str} " do
|
14
|
+
it "shuld be #{equal.to_s}" do
|
15
|
+
expect(BetterCsv.parse(str)).to eq(equal)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
context 'on including nil csv test' do
|
22
|
+
str = 'a,b,,,,f,g'
|
23
|
+
equal = [['a','b',"","","",'f','g']]
|
24
|
+
|
25
|
+
describe "sepalated with simply ',' == #{str} " do
|
26
|
+
it "shuld be #{equal.to_s}" do
|
27
|
+
expect(BetterCsv.parse(str)).to eq(equal)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'on including "" csv test' do
|
33
|
+
str = 'a,b,,"d",,f,g'
|
34
|
+
equal = [['a','b',"",'d',"",'f','g']]
|
35
|
+
|
36
|
+
describe "sepalated with simply ',' == #{str} " do
|
37
|
+
it "shuld be #{equal.to_s}" do
|
38
|
+
expect(BetterCsv.parse(str)).to eq(equal)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'on including "" in first csv test' do
|
44
|
+
str = '"a",b,,d,,f,g'
|
45
|
+
equal = [['a','b',"",'d',"",'f','g']]
|
46
|
+
|
47
|
+
describe "sepalated with simply ',' == #{str} " do
|
48
|
+
it "shuld be #{equal.to_s}" do
|
49
|
+
expect(BetterCsv.parse(str)).to eq(equal)
|
50
|
+
end
|
51
|
+
end
|
10
52
|
end
|
53
|
+
|
54
|
+
context 'on including "" in last csv test' do
|
55
|
+
str = 'a,b,,d,,f,"g"'
|
56
|
+
equal = [['a','b',"",'d',"",'f','g']]
|
57
|
+
|
58
|
+
describe "sepalated with simply ',' == #{str} " do
|
59
|
+
it "shuld be #{equal.to_s}" do
|
60
|
+
expect(BetterCsv.parse(str)).to eq(equal)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'on including return in "" csv test' do
|
66
|
+
str = %Q(a,b,,"d1\nd2",,f,g)
|
67
|
+
equal = [['a','b',"","d1\nd2","",'f','g']]
|
68
|
+
|
69
|
+
describe "sepalated with simply ',' == #{str} " do
|
70
|
+
it "shuld be #{equal.to_s}" do
|
71
|
+
expect(BetterCsv.parse(str)).to eq(equal)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
context 'on including comma, in "" csv test' do
|
78
|
+
str = %Q(a,b,,"d1,d2",,f,g)
|
79
|
+
equal = [['a','b',"","d1,d2","",'f','g']]
|
80
|
+
|
81
|
+
describe "sepalated with simply ',' == #{str} " do
|
82
|
+
it "shuld be #{equal.to_s}" do
|
83
|
+
expect(BetterCsv.parse(str)).to eq(equal)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
11
88
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatumaki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|