Skip to content

Commit

Permalink
writedatasync option tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Oct 31, 2016
1 parent e43ac2e commit 499cd2e
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 25 deletions.
30 changes: 18 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1596,12 +1596,8 @@ writers.writeData = function (outPath, data, opts_, cb) {
reporters.warn('You didn\'t pass any data to write for file: ' + chalk.bold(outPath) + ' Writing out an empty file...')
}

var writeOptions
if (!_.isFunction(opts_)) {
writeOptions = opts_
}

if (_.isObject(opts_) && opts_.makeDirectories) {
delete opts_.makeDirectories
helpers.makeDirectories(outPath, proceed)
} else {
proceed()
Expand All @@ -1611,6 +1607,12 @@ writers.writeData = function (outPath, data, opts_, cb) {
if (err) {
throw err
}

var writeOptions
if (!_.isFunction(opts_)) {
writeOptions = opts_
}

var fileFormatter = helpers.discernFileFormatter(outPath)
var formattedData = fileFormatter(data, writeOptions)
fs.writeFile(outPath, formattedData, function (err) {
Expand All @@ -1627,8 +1629,12 @@ writers.writeData = function (outPath, data, opts_, cb) {
* @param {String} fileName the name of the file
* @param {Object} [options] Optional config object, see below
* @param {Boolean} [options.makeDirectories=false] If true, create intermediate directories to your data file.
* @param {Function|Array} [options.replacer] Used for JSON formats. Filter your objects before writing. Examples below. See JSON.stringify docs for more info https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
* @param {Number} [options.indent] Used for JSON and YAML formats. Specifies indent level. Default for YAML is `2`, `0` for JSON.
* @param {String} [options.writeMethod='safeDump'] Used for YAML formats. Can also be `"dump"` to allow writing of RegExes and functions. The `options` object will also pass anything onto `js-yaml`. See its docs for other options. Example shown below with `sortKeys`. https://github.com/nodeca/js-yaml#safedump-object---options-
* @param {Array} [options.columns] Used for tabular formats. Optionally specify a list of column names to use. Otherwise they are detected from the data. See `d3-dsv` for more detail: https://github.com/d3/d3-dsv/blob/master/README.md#dsv_format
* @param {Object} data the data to write
* @returns {Object} the data that was written
* @returns {String} the data string that was written
*
* @example
* io.writeDataSync('path/to/data.json', jsonData)
Expand All @@ -1641,17 +1647,17 @@ writers.writeDataSync = function (outPath, data, opts_) {
reporters.warn('You didn\'t pass any data to write for file: ' + chalk.bold(outPath) + ' Writing out an empty file...')
}
var writeOptions
if (opts_) {
if (_.isObject(opts_)) {
if (opts_.makeDirectories) {
helpers.makeDirectoriesSync(outPath)
}
if (opts_.writeOptions) {
writeOptions = opts_.writeOptions
}
delete opts_.makeDirectories
writeOptions = opts_
}
var fileFormatter = helpers.discernFileFormatter(outPath)
fs.writeFileSync(outPath, fileFormatter(data, writeOptions))
return data
var formattedData = fileFormatter(data, writeOptions)
fs.writeFileSync(outPath, formattedData)
return formattedData
}

/**
Expand Down
142 changes: 129 additions & 13 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2511,10 +2511,10 @@ describe('writers', function () {
}
return value
}
}, function (err, json) {
}, function (err, dataString) {
assert.equal(err, null)
assert.equal(json, '[{"height":70},{"height":63}]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), json)
assert.equal(dataString, '[{"height":70},{"height":63}]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
Expand Down Expand Up @@ -2553,9 +2553,7 @@ describe('writers', function () {
})
})
})
})

describe('json', function () {
it('should write as json without making directory', function (done) {
var filePath = ['test', 'test-out-data.json']
io.writeData(filePath.join(path.sep), testData, function (err) {
Expand Down Expand Up @@ -2733,19 +2731,77 @@ describe('writers', function () {
describe('writeDataSync()', function () {
describe('json', function () {
it('should write as json', function (done) {
var filePath = ['test', 'tmp-write-data-json', 'data.json']
var filePath = ['test', 'tmp-write-data-json-sync', 'data.json']
io.writeDataSync(filePath.join(path.sep), testData, {makeDirectories: true})
readAssertBasicValid(filePath.join(path.sep))
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})

it('should write with json replacer fn', function (done) {
var filePath = ['test', 'tmp-write-data-json-replacer-fn-sync', 'data.json']
var dataString = io.writeDataSync(filePath.join(path.sep), testData, {
makeDirectories: true,
replacer: function (key, value) {
// Filtering out string properties
if (typeof value === 'string') {
return undefined
}
return value
}
})
assert.equal(dataString, '[{"height":70},{"height":63}]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})

it('should write with json replacer array', function (done) {
var filePath = ['test', 'tmp-write-data-json-replacer-array-sync', 'data.json']
var dataString = io.writeDataSync(filePath.join(path.sep), testData, {
makeDirectories: true,
replacer: ['height']
})
assert.equal(dataString, '[{"height":70},{"height":63}]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})

it('should write with json indent', function (done) {
var filePath = ['test', 'tmp-write-data-json-indent-sync', 'data.json']
var dataString = io.writeDataSync(filePath.join(path.sep), testData, {
makeDirectories: true,
indent: 2
})
assert.equal(dataString, '[\n {\n "name": "jim",\n "occupation": "land surveyor",\n "height": 70\n },\n {\n "name": "francis",\n "occupation": "conductor",\n "height": 63\n }\n]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})

it('should write as json without making directory', function (done) {
var filePath = ['test', 'test-out-data-sync.json']
io.writeDataSync(filePath.join(path.sep), testData)
readAssertBasicValid(filePath.join(path.sep))
rimraf(filePath.join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})

describe('geojson', function () {
it('should write as geojson', function (done) {
var filePath = ['test', 'tmp-write-data-geojson', 'data.geojson']
var filePath = ['test', 'tmp-write-data-geojson-sync', 'data.geojson']
io.writeDataSync(filePath.join(path.sep), testData, {makeDirectories: true})
readAssertBasicValid(filePath.join(path.sep))
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
Expand All @@ -2757,7 +2813,7 @@ describe('writers', function () {

describe('topojson', function () {
it('should write as topojson', function (done) {
var filePath = ['test', 'tmp-write-data-topojson', 'data.topojson']
var filePath = ['test', 'tmp-write-data-topojson-sync', 'data.topojson']
io.writeDataSync(filePath.join(path.sep), testData, {makeDirectories: true})
readAssertBasicValid(filePath.join(path.sep))
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
Expand All @@ -2767,9 +2823,41 @@ describe('writers', function () {
})
})

describe('geojson', function () {
it('should write as geojson with indent', function (done) {
var filePath = ['test', 'tmp-write-data-geojson-indent-sync', 'data.geojson']
var dataString = io.writeDataSync(filePath.join(path.sep), testData, {
makeDirectories: true,
indent: 2
})
assert.equal(dataString, '[\n {\n "name": "jim",\n "occupation": "land surveyor",\n "height": 70\n },\n {\n "name": "francis",\n "occupation": "conductor",\n "height": 63\n }\n]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})

describe('topojson', function () {
it('should write as topojson with indent', function (done) {
var filePath = ['test', 'tmp-write-data-topojson-indent-sync', 'data.topojson']
var dataString = io.writeDataSync(filePath.join(path.sep), testData, {
makeDirectories: true,
indent: 2
})
assert.equal(dataString, '[\n {\n "name": "jim",\n "occupation": "land surveyor",\n "height": 70\n },\n {\n "name": "francis",\n "occupation": "conductor",\n "height": 63\n }\n]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})

describe('csv', function () {
it('should write as csv', function (done) {
var filePath = ['test', 'tmp-write-data-csv', 'data.csv']
var filePath = ['test', 'tmp-write-data-csv-sync', 'data.csv']
io.writeDataSync(filePath.join(path.sep), testData, {makeDirectories: true})
readAssertBasicValid(filePath.join(path.sep))
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
Expand All @@ -2781,7 +2869,7 @@ describe('writers', function () {

describe('tsv', function () {
it('should write as tsv', function (done) {
var filePath = ['test', 'tmp-write-data-tsv', 'data.tsv']
var filePath = ['test', 'tmp-write-data-tsv-sync', 'data.tsv']
io.writeDataSync(filePath.join(path.sep), testData, {makeDirectories: true})
readAssertBasicValid(filePath.join(path.sep))
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
Expand All @@ -2793,7 +2881,7 @@ describe('writers', function () {

describe('psv', function () {
it('should write as psv', function (done) {
var filePath = ['test', 'tmp-write-data-psv', 'data.psv']
var filePath = ['test', 'tmp-write-data-psv-sync', 'data.psv']
io.writeDataSync(filePath.join(path.sep), testData, {makeDirectories: true})
readAssertBasicValid(filePath.join(path.sep))
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
Expand All @@ -2805,26 +2893,54 @@ describe('writers', function () {

describe('yaml', function () {
it('should write as yaml', function (done) {
var filePath = ['test', 'tmp-write-data-yaml', 'data.yaml']
var filePath = ['test', 'tmp-write-data-yaml-sync', 'data.yaml']
io.writeDataSync(filePath.join(path.sep), testData[0], {makeDirectories: true})
readAssertBasicValidObject(filePath.join(path.sep))
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})

it('should write as yaml with indent', function (done) {
var filePath = ['test', 'tmp-write-data-yaml-indent-sync', 'data.yaml']
var dataString = io.writeDataSync(filePath.join(path.sep), testData, {
makeDirectories: true,
indent: 4
})
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), '- \n name: jim\n occupation: land surveyor\n height: 70\n- \n name: francis\n occupation: conductor\n height: 63\n')
assert.equal(dataString, '- \n name: jim\n occupation: land surveyor\n height: 70\n- \n name: francis\n occupation: conductor\n height: 63\n')
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})

describe('yml', function () {
it('should write as yml', function (done) {
var filePath = ['test', 'tmp-write-data-yml', 'data.yml']
var filePath = ['test', 'tmp-write-data-yml-sync', 'data.yml']
io.writeDataSync(filePath.join(path.sep), testData[0], {makeDirectories: true})
readAssertBasicValidObject(filePath.join(path.sep))
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})

it('should write as yml with indent', function (done) {
var filePath = ['test', 'tmp-write-data-yml-indent-sync', 'data.yml']
var dataString = io.writeDataSync(filePath.join(path.sep), testData, {
makeDirectories: true,
indent: 4
})
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), '- \n name: jim\n occupation: land surveyor\n height: 70\n- \n name: francis\n occupation: conductor\n height: 63\n')
assert.equal(dataString, '- \n name: jim\n occupation: land surveyor\n height: 70\n- \n name: francis\n occupation: conductor\n height: 63\n')
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})
})

Expand Down

0 comments on commit 499cd2e

Please sign in to comment.