Skip to content

Commit 88f2f86

Browse files
authored
Merge pull request #30 from jdesgats/custom-parser
Added custom callback to parse input files
2 parents dbd4e06 + 608dab4 commit 88f2f86

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/get-files.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ getFiles.raw = function (filePaths) {
3333

3434
/**
3535
* Same as the `.raw()` method, except this method will attempt to parse
36-
* the file contents as JSON.
36+
* the file contents to JavaScript object with provided parser. The parser
37+
* will be called with file contents and file name (in that order).
3738
*
3839
* @param {array} filePaths
40+
* @param {function} callback to parse contents, defaults to JSON parser
3941
* @returns {Promise}
4042
*/
41-
getFiles.asJSON = function (filePaths) {
43+
getFiles.asObjects = function (filePaths, parser) {
4244
return getFiles.raw(filePaths)
4345
.then(function(map) {
4446
return _.mapValues(map, function(str, filePath) {
4547
try {
46-
return JSON.parse(str);
48+
return (parser || getFiles.defaultParser)(str, filePath);
4749
} catch (e) {
4850
e.filePath = filePath;
4951
throw e;
@@ -55,4 +57,8 @@ getFiles.asJSON = function (filePaths) {
5557
});
5658
};
5759

60+
getFiles.defaultParser = function (content, filePath) {
61+
return JSON.parse(content);
62+
};
63+
5864
module.exports = getFiles;

lib/parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Parser.prototype.filterPaths = function (paths) {
5050
* @return {Object}
5151
*/
5252
Parser.prototype.retreive = function(files) {
53-
return getFiles.asJSON(files);
53+
return getFiles.asObjects(files, this.options.parser);
5454
};
5555

5656
/**

test/lib/get-files.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
'use strict';
22
/* globals: describe, it */
33

4-
var expect = require('chai').expect;
4+
var chai = require('chai');
5+
var expect = chai.expect;
6+
var sinon = require('sinon');
57
var getFiles = require('../../lib/get-files');
68
var fixturesDir = process.cwd() + '/test/fixtures';
79

10+
chai.use(require('sinon-chai'));
11+
812
/** @name describe @function */
913
/** @name it @function */
1014
/** @name before @function */
@@ -23,12 +27,24 @@ describe('Get files', function() {
2327
});
2428
});
2529

26-
describe('#asJSON', function() {
30+
describe('#asObjects', function() {
2731
it('should return a mapped object of JSON contents, keyed by file name', function() {
28-
return getFiles.asJSON([fixturesDir + '/schema1.json', fixturesDir + '/schema2.json']).then(function(map) {
32+
return getFiles.asObjects([fixturesDir + '/schema1.json', fixturesDir + '/schema2.json']).then(function(map) {
33+
expect(map).to.be.an('object');
34+
expect(map).to.have.property(fixturesDir + '/schema1.json').that.is.an('object');
35+
expect(map).to.have.property(fixturesDir + '/schema2.json').that.is.an('object');
36+
});
37+
});
38+
39+
it('should allow to specify a custom parser, keyed by file name', function() {
40+
var cb = sinon.spy(JSON.parse);
41+
return getFiles.asObjects([fixturesDir + '/schema1.json', fixturesDir + '/schema2.json'], cb).then(function(map) {
2942
expect(map).to.be.an('object');
3043
expect(map).to.have.property(fixturesDir + '/schema1.json').that.is.an('object');
3144
expect(map).to.have.property(fixturesDir + '/schema2.json').that.is.an('object');
45+
expect(cb).to.have.been.calledTwice;
46+
expect(cb).to.have.been.calledWithMatch(sinon.match.string, fixturesDir + '/schema1.json');
47+
expect(cb).to.have.been.calledWithMatch(sinon.match.string, fixturesDir + '/schema2.json');
3248
});
3349
});
3450
});

0 commit comments

Comments
 (0)