csv-writer
Advanced tools
Comparing version 0.0.3 to 1.0.0
### 1.0.0: 28 February 2018 | ||
* Support for adding CSV records to already existing files. Thanks to @jonmelcher. [PR #4](https://github.com/ryu1kn/csv-writer/pull/4) | ||
### 0.0.3: 9 November 2016 | ||
@@ -3,0 +7,0 @@ |
@@ -18,5 +18,3 @@ | ||
return this._header.reduce((memo, field) => | ||
Object.assign({}, memo, {[field.id]: field.title}), | ||
{} | ||
); | ||
Object.assign({}, memo, {[field.id]: field.title}), {}); | ||
} | ||
@@ -23,0 +21,0 @@ |
@@ -21,3 +21,4 @@ | ||
fs, | ||
path: params.path | ||
path: params.path, | ||
append: params.append | ||
}); | ||
@@ -34,3 +35,4 @@ } | ||
fs, | ||
path: params.path | ||
path: params.path, | ||
append: params.append | ||
}); | ||
@@ -37,0 +39,0 @@ } |
@@ -5,2 +5,3 @@ | ||
const DEFAULT_ENCODING = 'utf8'; | ||
const DEFAULT_INITIAL_APPEND_FLAG = false; | ||
@@ -12,10 +13,9 @@ class CsvWriter { | ||
this._path = params.path; | ||
this._encoding = params.encoding; | ||
this._csvStringifier = params.csvStringifier; | ||
this._firstWrite = true; | ||
this._encoding = params.encoding || DEFAULT_ENCODING; | ||
this._append = params.append || DEFAULT_INITIAL_APPEND_FLAG; | ||
} | ||
writeRecords(records) { | ||
const headerString = this._firstWrite && this._csvStringifier.getHeaderString(); | ||
const headerString = !this._append && this._csvStringifier.getHeaderString(); | ||
const recordsString = this._csvStringifier.stringifyRecords(records); | ||
@@ -25,3 +25,3 @@ const writeString = (headerString || '') + recordsString; | ||
return this._write(writeString, option) | ||
.then(() => { this._firstWrite = false; }); | ||
.then(() => { this._append = true; }); | ||
} | ||
@@ -40,4 +40,4 @@ | ||
return { | ||
encoding: this._encoding || DEFAULT_ENCODING, | ||
flag: this._firstWrite ? 'w' : 'a' | ||
encoding: this._encoding, | ||
flag: this._append ? 'a' : 'w' | ||
}; | ||
@@ -44,0 +44,0 @@ } |
{ | ||
"name": "csv-writer", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"description": "Convert objects/arrays into a CSV string or write them into a CSV file", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"test": "mocha test --recursive", | ||
"coverage": "istanbul cover ./node_modules/.bin/_mocha -- test --recursive", | ||
"coverage": "nyc mocha test --recursive", | ||
"lint": "eslint ." | ||
@@ -28,10 +28,10 @@ }, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"codeclimate-test-reporter": "^0.4.0", | ||
"eslint": "^3.3.1", | ||
"eslint-config-xo": "^0.15.3", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.0.2", | ||
"sinon": "^1.17.5" | ||
"chai": "^4.1.2", | ||
"codeclimate-test-reporter": "^0.5.0", | ||
"eslint": "^4.18.1", | ||
"eslint-config-xo": "^0.20.1", | ||
"mocha": "^5.0.1", | ||
"nyc": "^11.4.1", | ||
"sinon": "^4.4.2" | ||
} | ||
} |
@@ -56,3 +56,3 @@ [![Build Status](https://travis-ci.org/ryu1kn/csv-writer.svg?branch=master)](https://travis-ci.org/ryu1kn/csv-writer) [![Code Climate](https://codeclimate.com/github/ryu1kn/csv-writer/badges/gpa.svg)](https://codeclimate.com/github/ryu1kn/csv-writer) [![Test Coverage](https://codeclimate.com/github/ryu1kn/csv-writer/badges/coverage.svg)](https://codeclimate.com/github/ryu1kn/csv-writer/coverage) | ||
If you don't want to write a header line, don't give `title` to header elements and just give field ids as a string. | ||
If you don't want to write a header line, don't give `title` to header elements and just give field IDs as a string. | ||
@@ -76,3 +76,3 @@ ```js | ||
var records = [ | ||
const records = [ | ||
['Bob', 'French, English'], | ||
@@ -133,6 +133,16 @@ ['Mary', 'English'] | ||
Array of objects (`id` and `title` properties) or strings (field ids) | ||
Array of objects (`id` and `title` properties) or strings (field IDs). | ||
A header line will be written to the file only if given as an array of objects. | ||
* encoding `<string>` (optional) | ||
Default: `utf8`. | ||
* append `<boolean>` (optional) | ||
Default: `false`. When `true`, it will append CSV records to the specified file. | ||
If the file doesn't exist, it will create one. | ||
**NOTE:** A header line will not be written to the file if `true` is given. | ||
##### Returns: | ||
@@ -158,2 +168,11 @@ | ||
Default: `utf8`. | ||
* append `<boolean>` (optional) | ||
Default: `false`. When `true`, it will append CSV records to the specified file. | ||
If the file doesn't exist, it will create one. | ||
**NOTE:** A header line will not be written to the file if `true` is given. | ||
##### Returns: | ||
@@ -185,3 +204,3 @@ | ||
Array of objects (`id` and `title` properties) or strings (field ids) | ||
Array of objects (`id` and `title` properties) or strings (field IDs) | ||
@@ -188,0 +207,0 @@ ##### Returns: |
@@ -82,2 +82,28 @@ | ||
it('opens a file in append mode on the first write if `append` flag is specified', () => { | ||
const fs = {writeFile: sinon.stub().callsArgWith(3, null)}; | ||
const arrayCsvStringifier = { | ||
getHeaderString: () => 'HEADER_STRING', | ||
stringifyRecords: () => 'CSV_STRING' | ||
}; | ||
const writer = new CsvWriter({ | ||
fs, | ||
encoding: 'ENCODING', | ||
csvStringifier: arrayCsvStringifier, | ||
path: 'FILE_PATH', | ||
append: true | ||
}); | ||
return writer.writeRecords('RECORDS').then(() => { | ||
expect(fs.writeFile.args[0].slice(0, 3)).to.eql([ | ||
'FILE_PATH', | ||
'CSV_STRING', | ||
{ | ||
encoding: 'ENCODING', | ||
flag: 'a' | ||
} | ||
]); | ||
}); | ||
}); | ||
it('writes to a file with the specified encoding', () => { | ||
@@ -123,3 +149,3 @@ const fs = {writeFile: sinon.stub().callsArgWith(3, null)}; | ||
e => { | ||
expect(e).to.be.an.error; | ||
expect(e).to.be.an('error'); | ||
expect(e.message).to.eql('WRITE_FILE_ERROR'); | ||
@@ -126,0 +152,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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
516
0
250
26378
16