Skip to content

Instantly share code, notes, and snippets.

@mk-pmb
Last active September 4, 2017 00:43
Show Gist options
  • Save mk-pmb/f0b66d45bd9a8cda784d52d07ae29fad to your computer and use it in GitHub Desktop.
Save mk-pmb/f0b66d45bd9a8cda784d52d07ae29fad to your computer and use it in GitHub Desktop.
/*jslint indent: 2, maxlen: 80, node: true */
/* -*- tab-width: 2 -*- */
'use strict';
var input, parseCsv = require('csv-parse/lib/sync'), auto, plus,
sep = ';', eq = require('assert').deepStrictEqual;
input = [ '42.23', '042.23', '042', '0042',
'0x42', '0x42.23', '0o42\n' ].join(sep);
auto = parseCsv(input, { delimiter: sep, auto_parse: true });
plus = input.trim().split(sep
).map(function numberize(x) { return +x; }
// v-- required because deepStrictEqual has no special case for NaN
).map(function nan2str(x) { return (Number.isNaN(x) ? String(x) : x); });
eq(auto, [ [ 42.23, 42.23, 42, 42, 0, '0x42.23', 0 ] ]);
eq(plus, [ 42.23, 42.23, 42, 42, 66, 'NaN', 34 ]);
// For '0x42' and '0o42', numberize() yields a more useful result.
// For '0x42.23', auto_parse's type guess is smarter and ok for me.
// $ node-versions csv-parse
// require('csv-parse/package.json').version = 1.2.1
// Node.js v6.11.2, npm v3.10.10, Ubuntu 14.04.5 LTS trusty,
// kernel: 3.13.0-119-generic, machine/CPU/platform: i686 i686 i686
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment