Skip to content

Commit

Permalink
remove deprecated comments option in archieml; remove parsejson dep a…
Browse files Browse the repository at this point in the history
…nd make test more lenient for older versions of node; clarify docs
  • Loading branch information
mhkeller committed Sep 8, 2017
1 parent 187a000 commit 53bfdbd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 106 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "8"
- "7"
- "6"
- "5"
Expand Down
3 changes: 1 addition & 2 deletions src/parsers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default function (str, parserOptions) {
parserOptions = parserOptions || {}
// Do a naive test whether this is a string or an object
var mapFn = parserOptions.map ? str.trim().charAt(0) === '[' ? _.map : _.mapObject : identity
var jsonParser = parserOptions.nativeParser === true ? JSON.parse : JSON.parse
// var jsonParser = parserOptions.nativeParser === true ? JSON.parse : parseJson
var jsonParser = JSON.parse
return mapFn(jsonParser(str, parserOptions.reviver, parserOptions.filename), parserOptions.map)
}
2 changes: 0 additions & 2 deletions src/readers/readData.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import _ from 'underscore'
* @param {Object|Function} [parserOptions] Optional. Set this as a function as a shorthand for `map`.
* @param {String|Function|Object} [parserOptions.parser] optional This can be a string that is the file's delimiter or a function that returns the json. See `parsers` in library source for examples. For convenience, this can also take a dsv object such as `dsv.dsv('_')` or any object that has a `parse` method.
* @param {Function} [parserOptions.map] Transformation function. Takes `(fileString, parserOptions)` where `parserOptions` is the hash you pass in minus the `parser` key. See {@link shorthandReaders} for specifics.
* @param {Boolean} [parserOptions.nativeParser] Used in {@link shorthandReaders.readJson} for now. Otherwise ignored.
* @param {Function} [parserOptions.comments] Used in {@link shorthandReaders.readAml}. Otherwise ignored.
* @param {Function} [parserOptions.reviver] Used in {@link shorthandReaders.readJson}. Otherwise ignored.
* @param {Function} [parserOptions.filename] Used in {@link shorthandReaders.readJson}. Otherwise ignored.
* @param {Function} callback callback used when read data is read, takes error (if any) and the data read
Expand Down
10 changes: 5 additions & 5 deletions src/readers/readDataSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import _ from 'underscore'
* Syncronous version of {@link readers#readData}
*
* @param {String} fileName the name of the file
* @param {Object|Function} [options] Optional. Set this as a function as a shorthand for `map`.
* @param {String|Function|Object} [options.parser] optional This can be a string that is the file's delimiter or a function that returns the json. See `parsers` in library source for examples. For convenience, this can also take a dsv object such as `dsv.dsv('_')` or any object that has a `parse` method.
* @param {Function} [options.map] Transformation function. Takes `(fileString, options)` where `options` is the hash you pass in minus the `parser` key. See {@link shorthandReaders} for specifics.
* @param {Function} [options.reviver] Used in {@link shorthandReaders.readJson}. Otherwise ignored.
* @param {Function} [options.filename] Used in {@link shorthandReaders.readJson}. Otherwise ignored.
* @param {Function|Object} [parserOptions] Can be a map function, or an object specifying other options.
* @param {String|Function|Object} [parserOptions.parser] optional This can be a string that is the file's delimiter or a function that returns the json. See `parsers` in library source for examples. For convenience, this can also take a dsv object such as `dsv.dsv('_')` or any object that has a `parse` method.
* @param {Function} [parserOptions.map] Transformation function. Takes `(fileString, options)` where `options` is the hash you pass in minus the `parser` key. See {@link shorthandReaders} for specifics.
* @param {Function} [parserOptions.reviver] Used in {@link shorthandReaders.readJson}. Otherwise ignored.
* @param {Function} [parserOptions.filename] Used in {@link shorthandReaders.readJson}. Otherwise ignored.
* @returns {Object} the contents of the file as JSON
*
* @example
Expand Down
3 changes: 1 addition & 2 deletions src/shorthandreaders/readJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import parserJson from '../parsers/json'
* Asynchronously read a JSON file. Returns an empty array if file is empty.
*
* @param {String} fileName the name of the file
* @param {Function|String|Object} [parserOptions] Can be a map function, a filename string that's display as an error message or an object specifying both and other options.
* @param {Function|Object} [parserOptions] Can be a map function, or an object specifying other options.
* @param {Function} [parserOptions.map] Optional map function, called once for each row (header row skipped). If your file is an array (tests if first non-whitespace character is a `[`), has signature `(row, i)`, delegates to `_.map`. If file is an object has signature `(value, key)`, delegates to `_.mapObject`. See example below.
* @param {Boolean} [parserOptions.nativeParser] Use native JSON parser instead of parse-json library for better performance but not as good error messaging. This can be nice to turn on for produciton use.
* @param {String} [parserOptions.filename] Filename displayed in the error message.
* @param {String} [parserOptions.reviver] Prescribes how the value originally produced by parsing is mapped, before being returned. See JSON.parse docs for more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter
* @param {Function} callback Function invoked after data is read, takes error (if any) and the data read
Expand Down
4 changes: 2 additions & 2 deletions src/shorthandreaders/readJsonSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import parserJson from '../parsers/json'
* Synchronously read a JSON file. Returns an empty array if file is empty.
*
* @param {String} fileName the name of the file
* @param {Function|String|Object} [parserOptions] Can be a map function, a filename string that's display as an error message or an object specifying both and other options.
* @param {Function|Object} [parserOptions] Can be a map function, or an object specifying other options.
* @param {Function} [parserOptions.map] Optional map function, called once for each row (header row skipped). If your file is an array (tests if first non-whitespace character is a `[`), has signature `(row, i)`, delegates to `_.map`. If file is an object has signature `(value, key)`, delegates to `_.mapObject`. See example below.
* @param {Boolean} [parserOptions.nativeParser] Use native JSON parser instead of parse-json library for better performance but not as good error messaging. This can be nice to turn on for produciton use.
* @param {Function} [parserOptions.comments] Used in {@link shorthandReaders.readAml}. Otherwise ignored.
* @param {String} [parserOptions.filename] Filename displayed in the error message.
* @param {String} [parserOptions.reviver] Prescribes how the value originally produced by parsing is mapped, before being returned. See JSON.parse docs for more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter
* @returns {Array} the contents of the file as JSON
Expand Down
124 changes: 31 additions & 93 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,112 +1712,50 @@ describe('readers', function () {

describe('shorthandReaders', function () {
describe('readJsonSync()', function () {
describe('parseJson parser', function () {
describe('empty', function () {
it('should be empty', function () {
assert.lengthOf(io.readJsonSync(testDataPath('json/empty.json')), 0)
})
})

describe('basic', function () {
it('should match expected json', function () {
var json = io.readJsonSync(testDataPath('json/basic.json'))
assertBasicValid(json)
})
describe('empty', function () {
it('should be empty', function () {
assert.lengthOf(io.readJsonSync(testDataPath('json/empty.json')), 0)
})
})

describe('basic', function () {
it('should match expected geojson', function () {
var json = io.readJsonSync(testDataPath('geojson/basic.geojson'))
assertBasicValid(json)
})
describe('basic', function () {
it('should match expected json', function () {
var json = io.readJsonSync(testDataPath('json/basic.json'))
assertBasicValid(json)
})
})

describe('basic', function () {
it('should match expected topojson', function () {
var json = io.readJsonSync(testDataPath('topojson/basic.topojson'))
assertBasicValid(json)
})
describe('basic', function () {
it('should match expected geojson', function () {
var json = io.readJsonSync(testDataPath('geojson/basic.geojson'))
assertBasicValid(json)
})
})

describe('basic map', function () {
it('should use map', function () {
var json = io.readJsonSync(testDataPath('json/basic.json'), {
map: function (row, i) {
row.height = row.height * 2
return row
}
})
assert(_.isEqual(JSON.stringify(json), '[{"name":"jim","occupation":"land surveyor","height":140},{"name":"francis","occupation":"conductor","height":126}]'))
})
describe('basic', function () {
it('should match expected topojson', function () {
var json = io.readJsonSync(testDataPath('topojson/basic.topojson'))
assertBasicValid(json)
})
})

describe('basic map shorthand', function () {
it('should use map shorthand', function () {
var json = io.readJsonSync(testDataPath('json/basic.json'), function (row, i) {
describe('basic map', function () {
it('should use map', function () {
var json = io.readJsonSync(testDataPath('json/basic.json'), {
map: function (row, i) {
row.height = row.height * 2
return row
})
assert(_.isEqual(JSON.stringify(json), '[{"name":"jim","occupation":"land surveyor","height":140},{"name":"francis","occupation":"conductor","height":126}]'))
})
})

describe('invalid', function () {
it('should raise an error', function () {
assert.throws(function () {
io.readJsonSync(testDataPath('json/invalid.json'))
}, Error)
}
})
assert(_.isEqual(JSON.stringify(json), '[{"name":"jim","occupation":"land surveyor","height":140},{"name":"francis","occupation":"conductor","height":126}]'))
})
})

describe('native parser', function () {
describe('empty', function () {
it('should be empty', function () {
assert.lengthOf(io.readJsonSync(testDataPath('json/empty.json'), {nativeParser: true}), 0)
})
})

describe('basic', function () {
it('should match expected json', function () {
var json = io.readJsonSync(testDataPath('json/basic.json'), {nativeParser: true})
assertBasicValid(json)
})
})

describe('basic', function () {
it('should match expected geojson', function () {
var json = io.readJsonSync(testDataPath('geojson/basic.geojson'), {nativeParser: true})
assertBasicValid(json)
})
})

describe('basic', function () {
it('should match expected topojson', function () {
var json = io.readJsonSync(testDataPath('topojson/basic.topojson'), {nativeParser: true})
assertBasicValid(json)
})
})

describe('basic map', function () {
it('should use map', function () {
var json = io.readJsonSync(testDataPath('json/basic.json'), {
map: function (row, i) {
row.height = row.height * 2
return row
},
nativeParser: true
})
assert(_.isEqual(JSON.stringify(json), '[{"name":"jim","occupation":"land surveyor","height":140},{"name":"francis","occupation":"conductor","height":126}]'))
})
})

describe('invalid', function () {
it('should raise an error', function () {
assert.throws(function () {
io.readJsonSync(testDataPath('json/invalid.json'), {nativeParser: true})
}, Error)
})
describe('invalid', function () {
it('should raise an error', function () {
assert.throws(function () {
io.readJsonSync(testDataPath('json/invalid.json'))
}, Error)
})
})
})
Expand Down Expand Up @@ -2135,7 +2073,7 @@ describe('shorthandReaders', function () {
describe('invalid', function () {
it('should raise an error', function (done) {
io.readJson(testDataPath('json/invalid.json'), function (err, json) {
assert.equal(err.message, 'Unexpected token w in JSON at position 2')
assert.equal(err.message.indexOf('Unexpected token w') > -1)
done()
})
})
Expand Down

0 comments on commit 53bfdbd

Please sign in to comment.