fasterer-csv 0.0.10 → 0.0.11
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.
- data/lib/fasterer_csv.rb +44 -26
- metadata +3 -3
data/lib/fasterer_csv.rb
CHANGED
@@ -210,13 +210,17 @@ module FastererCSV
|
|
210
210
|
super(ch.chr)
|
211
211
|
end
|
212
212
|
|
213
|
-
def
|
214
|
-
if
|
215
|
-
|
213
|
+
def convert(as_string = false)
|
214
|
+
if as_string
|
215
|
+
join
|
216
|
+
elsif empty?
|
217
|
+
nil
|
218
|
+
elsif @int
|
219
|
+
join.to_i
|
216
220
|
elsif @float
|
217
|
-
|
221
|
+
join.to_f
|
218
222
|
else
|
219
|
-
|
223
|
+
join
|
220
224
|
end
|
221
225
|
end
|
222
226
|
|
@@ -228,6 +232,16 @@ module FastererCSV
|
|
228
232
|
super(ch.chr)
|
229
233
|
end
|
230
234
|
|
235
|
+
def convert(as_string = false)
|
236
|
+
if as_string
|
237
|
+
join
|
238
|
+
elsif empty?
|
239
|
+
nil
|
240
|
+
else
|
241
|
+
join
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
231
245
|
end
|
232
246
|
|
233
247
|
class << self
|
@@ -255,16 +269,16 @@ module FastererCSV
|
|
255
269
|
next if c == ?\r
|
256
270
|
|
257
271
|
if maybe && c == s
|
258
|
-
row << column.
|
272
|
+
row << column.convert(true)
|
259
273
|
column.clear
|
260
274
|
clean, inquot, maybe, field, endline = true, false, false, true, false
|
261
275
|
elsif maybe && c == ?\n && table.nil?
|
262
|
-
row << column.
|
276
|
+
row << column.convert(true) unless (column.empty? && endline)
|
263
277
|
column.clear
|
264
278
|
table = Table.new(row, fail_on_malformed) unless row.empty?
|
265
279
|
row, clean, inquot, maybe, field, endline = [], true, false, false, false, true
|
266
280
|
elsif maybe && c == ?\n
|
267
|
-
row << column.
|
281
|
+
row << column.convert(true) unless (column.empty? && endline)
|
268
282
|
column.clear
|
269
283
|
table << row unless row.empty?
|
270
284
|
row, clean, inquot, maybe, field, endline = [], true, false, false, false, true
|
@@ -279,27 +293,19 @@ module FastererCSV
|
|
279
293
|
column << c
|
280
294
|
clean, endline = false, false
|
281
295
|
elsif c == s
|
282
|
-
row <<
|
296
|
+
row << column.convert(false)
|
283
297
|
column.clear
|
284
298
|
clean, field, endline = true, true, false
|
285
299
|
elsif c == ?\n && table.nil?
|
286
300
|
|
287
|
-
|
288
|
-
row << nil
|
289
|
-
elsif !column.empty?
|
290
|
-
row << column.join
|
291
|
-
end
|
301
|
+
row << column.convert(false) unless column.empty? && endline
|
292
302
|
|
293
303
|
column.clear
|
294
304
|
table = Table.new(row, fail_on_malformed) unless row.empty?
|
295
305
|
row, clean, inquot, field, endline = [], true, false, false, true
|
296
306
|
elsif c == ?\n
|
297
307
|
|
298
|
-
|
299
|
-
row << nil
|
300
|
-
elsif !column.empty?
|
301
|
-
row << column.join
|
302
|
-
end
|
308
|
+
row << column.convert(false) unless column.empty? && endline
|
303
309
|
|
304
310
|
column.clear
|
305
311
|
table << row unless row.empty?
|
@@ -311,18 +317,14 @@ module FastererCSV
|
|
311
317
|
end
|
312
318
|
|
313
319
|
if !clean
|
314
|
-
|
315
|
-
row << column.join
|
316
|
-
else
|
317
|
-
row << (column.empty? ? nil :column.join)
|
318
|
-
end
|
320
|
+
row << column.convert(maybe)
|
319
321
|
if table
|
320
322
|
table << row unless row.empty?
|
321
323
|
else
|
322
324
|
table = Table.new(row, fail_on_malformed) unless row.empty?
|
323
325
|
end
|
324
326
|
elsif field
|
325
|
-
row <<
|
327
|
+
row << column.convert(maybe)
|
326
328
|
end
|
327
329
|
|
328
330
|
table.each do |line|
|
@@ -333,15 +335,18 @@ module FastererCSV
|
|
333
335
|
end
|
334
336
|
|
335
337
|
def quot_row(row, q = '~', s = ',')
|
338
|
+
needs_quot = /(?:[#{q}#{s}\n]|^\d+$)/
|
336
339
|
row.map do |val|
|
337
340
|
if val.nil?
|
338
341
|
""
|
342
|
+
elsif val.class == Numeric
|
343
|
+
val.to_s
|
339
344
|
else
|
340
345
|
val = String(val)
|
341
346
|
if val.length == 0
|
342
347
|
q * 2
|
343
348
|
else
|
344
|
-
val[
|
349
|
+
val[needs_quot] ? q + val.gsub(q, q * 2) + q : val
|
345
350
|
end
|
346
351
|
end
|
347
352
|
end.join(s) + "\n"
|
@@ -373,3 +378,16 @@ module FastererCSV
|
|
373
378
|
end
|
374
379
|
end
|
375
380
|
end
|
381
|
+
|
382
|
+
data = <<-CSV
|
383
|
+
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p
|
384
|
+
,0,1,-1,0.0,1.1,-1.1,~1~,~~,~-1.1~,~1
|
385
|
+
.1~,x,~x~,,~1~~1~,
|
386
|
+
CSV
|
387
|
+
FastererCSV.parse(data, '~', ',', true, FastererCSV::NumericConversion.new)do |row|
|
388
|
+
h = row.to_hash
|
389
|
+
h.keys.sort{|a,b| a.to_s <=> b.to_s}.each do |k|
|
390
|
+
v = h[k]
|
391
|
+
puts "#{k} : #{v} : #{v.class}"
|
392
|
+
end
|
393
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fasterer-csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 11
|
10
|
+
version: 0.0.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mason
|