Comparing version 2.0.1 to 2.1.0
88
index.js
/** | ||
* @fileoverview ES7 wrapper for the csv package. | ||
* @fileoverview ES7 async wrapper for the csv package. | ||
*/ | ||
@@ -11,3 +11,3 @@ | ||
/** | ||
* @param {object} options | ||
* @param {object} [options] | ||
* @param {number} options.seed | ||
@@ -19,9 +19,10 @@ * @param {number} options.columns | ||
return new Promise((resolve, reject) => { | ||
csv.generate(options, (error, output) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(output); | ||
} | ||
}); | ||
const callback = (error, output) => | ||
error | ||
? reject(error) | ||
: resolve(output); | ||
options | ||
? csv.generate(options, callback) | ||
: csv.generate(callback); | ||
}); | ||
@@ -31,15 +32,16 @@ } | ||
/** | ||
* | ||
* Parses a CSV file into an array of rows. | ||
* @param {string} input | ||
* @param {object} options | ||
* @param {object} [options] | ||
*/ | ||
static parse(input, options) { | ||
return new Promise((resolve, reject) => { | ||
csv.parse(input, options, (error, output) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(output); | ||
} | ||
}); | ||
const callback = (error, output) => | ||
error | ||
? reject(error) | ||
: resolve(output); | ||
options | ||
? csv.parse(input, options, callback) | ||
: csv.parse(input, callback); | ||
}); | ||
@@ -56,19 +58,10 @@ } | ||
return new Promise((resolve, reject) => { | ||
if (options) { | ||
csv.transform(data, handler, options, (error, data) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
} else { | ||
csv.transform(data, handler, (error, data) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
} | ||
const callback = (error, output) => | ||
error | ||
? reject(error) | ||
: resolve(output); | ||
options | ||
? csv.transform(data, handler, options, callback) | ||
: csv.transform(data, handler, callback); | ||
}); | ||
@@ -84,19 +77,10 @@ } | ||
return new Promise((resolve, reject) => { | ||
if (options) { | ||
csv.stringify(data, options, (error, output) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(output); | ||
} | ||
}); | ||
} else { | ||
csv.stringify(data, (error, output) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(output); | ||
} | ||
}); | ||
} | ||
const callback = (error, output) => | ||
error | ||
? reject(error) | ||
: resolve(output); | ||
options | ||
? csv.stringify(data, options, callback) | ||
: csv.stringify(data, callback); | ||
}); | ||
@@ -103,0 +87,0 @@ } |
const csv = require('./'); | ||
describe('csv-generate', () => { | ||
it('must generate a specific CSV file', async () => { | ||
const rows = await csv.parse(await csv.generate({ columns: 3, length: 5 })); | ||
expect(rows.length).toBe(5); | ||
rows.every(row => expect(row.length).toBe(3)); | ||
}); | ||
}); | ||
describe('csv-stringify', () => { | ||
@@ -14,3 +22,24 @@ it('must generate correct CSV string', async () => { | ||
expect(result).toBe('HK,Hong Kong Island\nKLN,Kowloon\nNT,New Territories\n'); | ||
}) | ||
}); | ||
}); | ||
describe('csv-parse', () => { | ||
it('must be able to parse a CSV file successfully', async () => { | ||
const sample = `Column 1,Column 2,Column 3 | ||
1,2,3 | ||
4,5,6 | ||
7,8,some other data`; | ||
const rows = await csv.parse(sample); | ||
expect(rows.length).toBe(4); | ||
rows.every(row => expect(row.length).toBe(3)); | ||
}); | ||
it('must be able to take optional params too', async () => { | ||
const sample = `Column 1,Column 3 | ||
1,2,3 | ||
4,5,6 | ||
7,8,some other data`; | ||
const rows = await csv.parse(sample, { relaxColumnCount: true }); | ||
expect(rows.length).toBe(4); | ||
}); | ||
}); |
{ | ||
"name": "async-csv", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "ES7 async-await wrapper for the csv package.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,17 @@ # ES7 async wrapper for csv package # | ||
## If you just want to read a CSV file ## | ||
```js | ||
const csv = require('async-csv'); | ||
const fs = require('fs').promises; | ||
(async() => { | ||
// Read file from disk: | ||
const csvString = await fs.readFile('./test.csv', 'utf-8'); | ||
// Convert CSV string into rows: | ||
const rows = await csv.parse(csvString); | ||
})(); | ||
``` | ||
## Documentation ## | ||
@@ -19,6 +34,7 @@ | ||
// `options` are optional | ||
let result1 = await csv.generate(options); | ||
let result2 = await csv.parse(input, options); | ||
let result3 = await csv.transform(data, handler, options); // `options` are not required | ||
let result4 = await csv.stringify(data, options); // `options` are not required | ||
let result3 = await csv.transform(data, handler, options); | ||
let result4 = await csv.stringify(data, options); | ||
``` | ||
@@ -28,2 +44,2 @@ | ||
Feedback, bug reports and pull requests are welcome. See the linked github repository. | ||
Feedback, bug reports and pull requests are welcome. See the linked Github repository. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6318
112
43