Skip to content

Commit

Permalink
json write options
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Oct 31, 2016
1 parent 8a3752b commit 79eba7b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1612,8 +1612,9 @@ writers.writeData = function (outPath, data, opts_, cb) {
throw err
}
var fileFormatter = helpers.discernFileFormatter(outPath)
fs.writeFile(outPath, fileFormatter(data, writeOptions), function (err) {
cb(err, data)
var formattedData = fileFormatter(data, writeOptions)
fs.writeFile(outPath, formattedData, function (err) {
cb(err, formattedData)
})
}
}
Expand Down
74 changes: 54 additions & 20 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global describe, it */

var fs = require('fs')
var path = require('path')
var io = require('../lib/index')
var chai = require('chai')
Expand Down Expand Up @@ -2497,26 +2498,59 @@ describe('writers', function () {
})
})

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

describe('json', function () {
Expand Down

0 comments on commit 79eba7b

Please sign in to comment.