Comparing version 2.1.1 to 3.0.0-pre
166
index.js
/** | ||
* @fileoverview ES7 async wrapper for the csv package. | ||
*/ | ||
'use strict'; | ||
const csv = require('csv'); | ||
class CsvAsync { | ||
/** | ||
* @param {object} [options] | ||
* @param {number} options.seed | ||
* @param {number} options.columns | ||
* @param {number} options.length | ||
*/ | ||
static generate(options) { | ||
return new Promise((resolve, reject) => { | ||
const callback = (error, output) => | ||
error | ||
? reject(error) | ||
: resolve(output); | ||
options | ||
? csv.generate(options, callback) | ||
: csv.generate(callback); | ||
}); | ||
} | ||
/** | ||
* Parses a CSV file into an array of rows. | ||
* @param {string} input | ||
* @param {object} [options] | ||
*/ | ||
static parse(input, options) { | ||
return new Promise((resolve, reject) => { | ||
const callback = (error, output) => | ||
error | ||
? reject(error) | ||
: resolve(output); | ||
options | ||
? csv.parse(input, options, callback) | ||
: csv.parse(input, callback); | ||
}); | ||
} | ||
/** | ||
* | ||
* @param {string[][]} data | ||
* @param {function} handler | ||
* @param {object} [options] | ||
*/ | ||
static transform(data, handler, options) { | ||
return new Promise((resolve, reject) => { | ||
const callback = (error, output) => | ||
error | ||
? reject(error) | ||
: resolve(output); | ||
options | ||
? csv.transform(data, handler, options, callback) | ||
: csv.transform(data, handler, callback); | ||
}); | ||
} | ||
/** | ||
* | ||
* @param {string[][]} data | ||
* @param {object} options | ||
*/ | ||
static stringify(data, options) { | ||
return new Promise((resolve, reject) => { | ||
const callback = (error, output) => | ||
error | ||
? reject(error) | ||
: resolve(output); | ||
options | ||
? csv.stringify(data, options, callback) | ||
: csv.stringify(data, callback); | ||
}); | ||
} | ||
} | ||
module.exports = CsvAsync; | ||
exports.__esModule = true; | ||
var csv = require("csv"); | ||
/** | ||
* Async wrapper for the `csv` package. | ||
*/ | ||
var CsvAsync = /** @class */ (function () { | ||
function CsvAsync() { | ||
} | ||
/** | ||
* @param {object} [options] | ||
* @param {number} options.seed | ||
* @param {number} options.columns | ||
* @param {number} options.length | ||
*/ | ||
CsvAsync.generate = function (options) { | ||
return new Promise(function (resolve, reject) { | ||
var callback = function (error, output) { | ||
return error | ||
? reject(error) | ||
: resolve(output); | ||
}; | ||
options | ||
? csv.generate(options, callback) | ||
: csv.generate(callback); | ||
}); | ||
}; | ||
/** | ||
* Parses a CSV file into an array of rows. | ||
* @param {string} input | ||
* @param {object} [options] | ||
*/ | ||
CsvAsync.parse = function (input, options) { | ||
return new Promise(function (resolve, reject) { | ||
var callback = function (error, output) { | ||
return error | ||
? reject(error) | ||
: resolve(output); | ||
}; | ||
options | ||
? csv.parse(input, options, callback) | ||
: csv.parse(input, callback); | ||
}); | ||
}; | ||
/** | ||
* | ||
* @param {string[][]} data | ||
* @param {function} handler | ||
* @param {object} [options] | ||
*/ | ||
CsvAsync.transform = function (data, handler, options) { | ||
return new Promise(function (resolve, reject) { | ||
var callback = function (error, output) { | ||
return error | ||
? reject(error) | ||
: resolve(output); | ||
}; | ||
options | ||
? csv.transform(data, handler, options, callback) | ||
: csv.transform(data, handler, callback); | ||
}); | ||
}; | ||
/** | ||
* Converts an array of rows into a CSV string. | ||
* @param {string[][]} data | ||
* @param {object} options | ||
*/ | ||
CsvAsync.stringify = function (data, options) { | ||
return new Promise(function (resolve, reject) { | ||
var callback = function (error, output) { | ||
return error | ||
? reject(error) | ||
: resolve(output); | ||
}; | ||
options | ||
? csv.stringify(data, options, callback) | ||
: csv.stringify(data, callback); | ||
}); | ||
}; | ||
return CsvAsync; | ||
}()); | ||
exports["default"] = CsvAsync; | ||
exports.generate = CsvAsync.generate; | ||
exports.parse = CsvAsync.parse; | ||
exports.transform = CsvAsync.transform; | ||
exports.stringify = CsvAsync.stringify; |
@@ -1,2 +0,2 @@ | ||
const csv = require('./'); | ||
const csv = require('./index.js'); | ||
@@ -23,2 +23,14 @@ describe('csv-generate', () => { | ||
}); | ||
it('must handle null/undefined/number', async () => { | ||
const sampleData = [ | ||
['HK', undefined], | ||
['KLN', null], | ||
['NT', 3], | ||
]; | ||
const result = await csv.stringify(sampleData); | ||
expect(result).toBe(`HK,\nKLN,\nNT,3\n`); | ||
}); | ||
}); | ||
@@ -25,0 +37,0 @@ |
{ | ||
"name": "async-csv", | ||
"version": "2.1.1", | ||
"description": "ES7 async-await wrapper for the csv package.", | ||
"version": "3.0.0-pre", | ||
"description": "TypeScript / JavaScript async-await wrapper for the csv package.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node node_modules/jest/bin/jest" | ||
"build": "tsc index.ts", | ||
"test": "npm run build && node node_modules/jest/bin/jest" | ||
}, | ||
@@ -33,4 +34,8 @@ "repository": { | ||
"devDependencies": { | ||
"jest": "^24.8.0" | ||
"@types/node": "^12.6.8", | ||
"jest": "^24.8.0", | ||
"tslint": "^5.18.0", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"typescript": "^3.5.3" | ||
} | ||
} |
@@ -1,5 +0,8 @@ | ||
# ES7 async wrapper for csv package # | ||
# async wrapper for csv package # | ||
This is a wrapper for the popular `csv` package in NPM that can be used with the ES7 async-await pattern, instead of using callbacks. | ||
This is a TypeScript / JavaScript wrapper for the popular `csv` package that | ||
can be used with the ES7 async-await pattern, instead of using callbacks. | ||
Also, it features type definitions which is useful for TypeScript. | ||
## If you just want to read a CSV file ## | ||
@@ -6,0 +9,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
21281
8
569
46
5
1
1