Skip to content

Commit

Permalink
0.2.0; fix 'format' usage; add option to
Browse files Browse the repository at this point in the history
  • Loading branch information
eshao committed Sep 25, 2020
1 parent 7d106c7 commit 70b7449
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions CHANGES.md
@@ -1,8 +1,8 @@
# csvrow Changelog

## 0.1.1 (not yet released)
## 0.2.0

(nothing yet)
- Support specifying other delimiters than just comma.


## 0.1.0
Expand Down
26 changes: 14 additions & 12 deletions lib/csvrow.js
Expand Up @@ -5,8 +5,9 @@
* csvrow -- parsing a single CSV string
*/

var VERSION = '0.1.1';

var VERSION = '0.2.0';
var DEFAULT_DELIMITER = ',' // \t also tested
var format = format || require('util').format

/**
* Parse a CSV row (i.e. a single row) into an array of strings.
Expand All @@ -22,12 +23,12 @@ var VERSION = '0.1.1';
*
* @throws {TypeError} if the given CSV row is invalid
*/
function parseCSVRow(s) {
function parseCSVRow(s, delimiter) {
delimiter = delimiter || DEFAULT_DELIMITER;
var DEBUG = false;
var row = [];
var i = 0;
var ch;

if (s.indexOf('\n') !== -1 || s.indexOf('\r') !== -1) {
throw new TypeError(
format('illegal char: newlines not supported: "%s"', s));
Expand All @@ -43,15 +44,15 @@ function parseCSVRow(s) {
// Find first non-whitespace cell char.
while (i < s.length) {
var ch = s[i];
if (ch === ' ' || ch === '\t') {
if (ch === ' ') {
cell.push(ch);
} else if (ch === '"') {
quoted = true;
iQuote = i;
cell = [ch]; // wipe out leading whitespace
i++;
break;
} else if (ch === ',') {
} else if (ch === delimiter) {
// Empty cell.
break;
} else {
Expand Down Expand Up @@ -89,10 +90,10 @@ function parseCSVRow(s) {
// Advance to comma (or end of string).
while (i < s.length) {
var ch = s[i];
if (ch === ',') {
if (ch === delimiter) {
i++;
break;
} else if (ch !== ' ' && ch !== '\t') {
} else if (ch !== ' ') {
throw new TypeError(format(
"illegal char outside of quoted cell at position %d: '%s'",
i, s));
Expand All @@ -103,7 +104,7 @@ function parseCSVRow(s) {
// Slurp up cell until end of string or comma.
while (i < s.length) {
var ch = s[i];
if (ch === ',') {
if (ch === delimiter) {
i++;
break;
} else if (ch === '"') {
Expand All @@ -128,7 +129,7 @@ function parseCSVRow(s) {
}

// Special case for trailing ','.
if (s[s.length - 1] === ',') {
if (s[s.length - 1] === delimiter) {
DEBUG && console.warn('special case: add cell for trailing comma');
row.push('');
}
Expand All @@ -140,7 +141,8 @@ function parseCSVRow(s) {
/**
* Serialize the given array to a CSV row.
*/
function serializeCSVRow(a) {
function serializeCSVRow(a, delimiter) {
delimiter = delimiter || DEFAULT_DELIMITER
var row = [];
for (var i = 0; i < a.length; i++) {
var elem = a[i];
Expand All @@ -151,7 +153,7 @@ function serializeCSVRow(a) {
row.push(elem);
}
}
return row.join(',');
return row.join(delimiter);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "csvrow",
"version": "0.1.1",
"version": "0.2.0",
"description": "parse a CSV row string",
"author": "Trent Mick <trentm@gmail.com> (http://trentm.com)",
"main": "./lib/csvrow.js",
Expand Down

0 comments on commit 70b7449

Please sign in to comment.