nested_string_parser 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: 7bc979b99001df7bd41db9bcd1755e93d4a520b3
4
- data.tar.gz: 921e53f32bdcaa6d781eed16804246442263ad55
3
+ metadata.gz: c03e67f57d3ac7090991e11dcd71f63f58bc46d4
4
+ data.tar.gz: ca10f72da23bc153ea462000863aa14dad1771a1
5
5
  SHA512:
6
- metadata.gz: cecb511ad6f63c406884220096aa74e568c78b9d47caaee94666215a7fa60392c7f249039a88da9fdacb3aaf69b73408fae50792aa3949d7c96e5a1b11f9eea0
7
- data.tar.gz: 3f37047b78246cc086e71080c8cc5d72ed327d6833e577a24473bd3667113000ee7fb6f201146c44d73f02a618c41f1990a3680a95de0006f3dd62f7ff0768da
6
+ metadata.gz: b65af052e74f4a0b49f6c2fe1e03f27efe6418babbed1844abc6a1e0864a7820f8df10aac08b4e8893f12423df423a9882af46c1a3dffd8d012c8b15d9d039d1
7
+ data.tar.gz: 52c1ae8128dd9b177d29c82303cffe6de3141016c52fe6904a14fab941da2cd1c0a8892f254d9f11369110b119af6527f75a04b64aeb977f484e915d3c84371a
@@ -22,7 +22,18 @@ module NestedStringParser
22
22
  # | A | E | G |
23
23
  # | B | H | J |
24
24
  # | B | H | K |
25
+ #
25
26
  # same as
27
+ #
28
+ # | A | B | C |
29
+ # | | | D |
30
+ # | | E | F |
31
+ # | | | G |
32
+ # | B | H | J |
33
+ # | | | K |
34
+ #
35
+ # same as
36
+ #
26
37
  # A
27
38
  # B
28
39
  # C
@@ -36,7 +47,19 @@ module NestedStringParser
36
47
  # K
37
48
  #
38
49
  def self.from_rows rows, root=new_node
39
- rows.each { |row| row.inject(root) { |node, name| node.find(name) || node.nest_child(new_node name) } } ; root
50
+ previous_row = []
51
+ rows.each { |row|
52
+ # row.inject(root) { |node, name| node.find(name) || (node.nb?(name) && node.nest_child(new_node name)) || node } }
53
+ node = root
54
+ row_has_a_name = false
55
+ row.length.times { |i|
56
+ cell_has_a_name = root.nb?(row[i])
57
+ row_has_a_name ||= cell_has_a_name
58
+ previous_row[i] = name = cell_has_a_name ? row[i] : (!row_has_a_name && previous_row[i])
59
+ node = node.find(name) || (node.nb?(name) && node.nest_child(new_node name)) || node
60
+ }
61
+ }
62
+ root
40
63
  end
41
64
 
42
65
  def self.new_node val=nil ; NestedStringParser::Node.new.tap { |n| n.value = val } ; end
@@ -1,3 +1,3 @@
1
1
  module NestedStringParser
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -24,21 +24,21 @@ RSpec::describe NestedStringParser do
24
24
  end
25
25
 
26
26
  QUARKS_IN_ROWS = [
27
- %w{ quarks G1 up },
28
- %w{ quarks G1 down },
29
- %w{ quarks G2 strange },
30
- %w{ quarks G2 charmed },
31
- %w{ quarks G3 top },
32
- %w{ quarks G3 bottom },
33
- %w{ leptons G1 electron },
34
- %w{ leptons G1 electron-neutrino },
35
- %w{ leptons G2 muon },
36
- %w{ leptons G2 muon-neutrino },
37
- %w{ leptons G3 tau },
38
- %w{ leptons G3 tau-neutrino },
39
- ]
27
+ %w{ quarks G1 up },
28
+ %w{ quarks G1 down },
29
+ %w{ quarks G2 strange },
30
+ %w{ quarks G2 charmed },
31
+ %w{ quarks G3 top \ },
32
+ %w{ quarks G3 bottom },
33
+ %w{ leptons G1 electron },
34
+ %w{ leptons G1 electron-neutrino },
35
+ %w{ leptons G2 muon },
36
+ %w{ leptons G2 muon-neutrino \ },
37
+ %w{ leptons G3 tau },
38
+ %w{ leptons G3 tau-neutrino },
39
+ ]
40
40
 
41
- it "parses a single item" do
41
+ it "parses a whole csv file" do
42
42
  node = NestedStringParser.from_rows QUARKS_IN_ROWS
43
43
 
44
44
  expect(node.value ).to eq nil
@@ -59,4 +59,96 @@ RSpec::describe NestedStringParser do
59
59
  expect(leptons.children.map { |q| q.children.map(&:nesting) }).to eq [[2,2], [2,2], [2,2]]
60
60
  expect(leptons.children.map { |q| q.children.map(&:value) }).to eq [%w{electron electron-neutrino}, %w{muon muon-neutrino}, %w{tau tau-neutrino}]
61
61
  end
62
+
63
+ QUARKS_IN_SPARSE_ROWS = [
64
+ %w{ quarks G1 up },
65
+ %w{ \ \ down },
66
+ %w{ \ G2 strange },
67
+ %w{ \ \ charmed },
68
+ %w{ \ G3 top \ },
69
+ %w{ \ \ bottom },
70
+ %w{ leptons G1 electron },
71
+ %w{ \ \ electron-neutrino },
72
+ %w{ \ G2 muon },
73
+ %w{ \ \ muon-neutrino \ },
74
+ %w{ \ G3 tau },
75
+ %w{ \ \ tau-neutrino },
76
+ ]
77
+
78
+ it "parses a whole csv file with parent-items inherited from previous row" do
79
+ node = NestedStringParser.from_rows QUARKS_IN_SPARSE_ROWS
80
+
81
+ expect(node.value ).to eq nil
82
+ expect(node.nesting ).to eq(-1)
83
+ expect(node.children.map(&:value)).to eq %w{ quarks leptons }
84
+
85
+ quarks = node.children[0]
86
+ expect(quarks.nesting ).to eq 0
87
+ expect(quarks.children.map(&:value) ).to eq %w{ G1 G2 G3 }
88
+ expect(quarks.children.map(&:nesting)).to eq [1,1,1]
89
+ expect(quarks.children.map { |q| q.children.map(&:nesting) }).to eq [[2,2], [2,2], [2,2]]
90
+ expect(quarks.children.map { |q| q.children.map(&:value) }).to eq [%w{up down}, %w{strange charmed}, %w{top bottom}]
91
+
92
+ leptons = node.children[1]
93
+ expect(leptons.nesting ).to eq 0
94
+ expect(leptons.children.map(&:value) ).to eq %w{ G1 G2 G3 }
95
+ expect(leptons.children.map(&:nesting)).to eq [1,1,1]
96
+ expect(leptons.children.map { |q| q.children.map(&:nesting) }).to eq [[2,2], [2,2], [2,2]]
97
+ expect(leptons.children.map { |q| q.children.map(&:value) }).to eq [%w{electron electron-neutrino}, %w{muon muon-neutrino}, %w{tau tau-neutrino}]
98
+ end
99
+
100
+ ENGLISH_CURRICULUM = [
101
+ ["English", "Grammar" , "Verbs" , "Past" ],
102
+ ["" , "" , "" , "Present" ],
103
+ ["" , "" , "" , "Future" ],
104
+ ["" , "" , "" , "" ],
105
+ ["" , "" , "Adjectives", "" ],
106
+ ["" , "" , "" , "" ],
107
+ ["" , "" , "Nouns" , "Proper" ],
108
+ ["" , "" , "" , "Pronouns" ],
109
+ ["" , "" , "" , "Other" ],
110
+ ["" , "" , "" , "" ],
111
+ ["" , "" , "Sentences" , "Introduction" ],
112
+ ["" , "" , "" , "Structure" ],
113
+ ["" , "" , "" , "Clauses" ],
114
+ ["" , "" , "" , "Conjunctions" ],
115
+ ["" , "" , "" , "" ],
116
+ ["" , "Literature", "Stories" , "" ],
117
+ ["" , "" , "Novels" , "" ],
118
+ ["" , "" , "Drama" , "" ],
119
+ ["" , "" , "Poetry" , "" ],
120
+ ["" , "" , "Criticism" , "" ],
121
+ ]
122
+
123
+ it "parses a whole csv file with parent-items inherited from previous row, ignoring empty rows and empty trailing cells" do
124
+ node = NestedStringParser.from_rows ENGLISH_CURRICULUM
125
+
126
+ expected = <<FORMATTED
127
+
128
+ English
129
+ Grammar
130
+ Verbs
131
+ Past
132
+ Present
133
+ Future
134
+ Adjectives
135
+ Nouns
136
+ Proper
137
+ Pronouns
138
+ Other
139
+ Sentences
140
+ Introduction
141
+ Structure
142
+ Clauses
143
+ Conjunctions
144
+ Literature
145
+ Stories
146
+ Novels
147
+ Drama
148
+ Poetry
149
+ Criticism
150
+ FORMATTED
151
+
152
+ expect(node.format("")).to eq expected
153
+ end
62
154
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_string_parser
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
  - Conan Dalton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-29 00:00:00.000000000 Z
11
+ date: 2019-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.2.2
95
+ rubygems_version: 2.5.2.3
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: Parse multi-line string with indentation, return a structure where nesting