better_csv 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65bda3064d3062ee05e6530463784376edf6a578
4
- data.tar.gz: c1b70eb14836d7c5dd82a3d9d48d90978abec0fd
3
+ metadata.gz: 934f91f530d16204f142e55b3b14e576cd1acc67
4
+ data.tar.gz: afca2a95bb8095a3599507fc8a795248d472e84d
5
5
  SHA512:
6
- metadata.gz: 765eff12f8abf75df4d70b8dbb1a188b2e4fd1cfde6978e5b330351c3edfcd0a4bcc8105f920fbf240994b6b2b7dfd0eb260132124a13df06a9c6ebcd434b3be
7
- data.tar.gz: 70eaeb5e66b8d95222faa69aee368aeaf45b0f788f5cb1e1be37031d1cb0c5b5d0a812959b4c618abab1c5b875617a975802ca09a1ac072a5a12769e3eabcdd8
6
+ metadata.gz: 3e9bc0c0d7f3b98365bbab1333a5b7cfd1486147305e9af4cbe4c2cddd99f2547f653bce0b65a13c5bb7eca2fe7f35b36fd03f6b26bfb776f4d9448c2278ccb9
7
+ data.tar.gz: 93e9bb0b002242ae5e531498e976ce66aac66893d8b3e0116a167908ba9bc839829e33ac17e8a6338b892f4970d534b56c92555aa611d56d7cf0f57c6cc5a525
@@ -1,3 +1,3 @@
1
1
  module BetterCsv
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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
- flag = false
5
+ in_literal = false
6
6
  csv = Array.new
7
- line = ""
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
- line << c if not c == '"'
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
- flag = !flag if current == '"' and not back == '\\'
21
- if current == "\n" and not flag
22
- csv << line.sub(/\A"/,"").sub(/\\\"\z/,"").split(",")
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
- line << c unless current == '"' and not back == '\\'
40
+ token << c unless current == '"' and not back == '\\'
28
41
  back = current
29
42
  end
30
- csv << line.sub(/\A"/,"").sub(/\\\"\z/,"").split(",") if line.size > 0
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
@@ -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
- it 'does something useful' do
9
- expect(false).to eq(true)
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.2
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-08-25 00:00:00.000000000 Z
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler