Skip to content

Commit

Permalink
Merge b4be569 into 41d4166
Browse files Browse the repository at this point in the history
  • Loading branch information
vpishuk committed Jun 14, 2019
2 parents 41d4166 + b4be569 commit ae7583c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/index.js
Expand Up @@ -16,8 +16,17 @@ function getDefaults() {
function readFile(filename, options, callback) {
const extension = path.extname(filename);
let result;

if (/^\.(js|ts)$/.test(extension)) {
if (options.parse) {
try {
result = options.parse(data);
if (typeof result !== 'object') {
return callback(new Error('A resource file must export an object.'));
}
callback(null, result);
} catch (err) {
callback(err);
}
} else if (/^\.(js|ts)$/.test(extension)) {
try {
const file = require(filename);
result = file.default ? file.default : file;
Expand Down
16 changes: 16 additions & 0 deletions test/backend.js
@@ -1,6 +1,7 @@
var mockery = require('mockery');
var expect = require('chai').expect;
var path = require('path');
var sinon = require('sinon');

var Interpolator = require('i18next/dist/commonjs/Interpolator').default;

Expand Down Expand Up @@ -132,4 +133,19 @@ describe('backend', function() {
});
});

it.only('calls custom parse function if it is passed', function(done) {
const parseStub = sinon.stub().returns({key: 'passing', evaluated: 2})
backend = new Backend({
interpolator: new Interpolator()
}, {
loadPath: path.join(__dirname, '/locales/{{lng}}/{{ns}}.json'),
parse: parseStub
});

backend.read('en', 'test', function(err, data) {
expect(parseStub.callCount).to.equal(1)
done();
});
});

});

0 comments on commit ae7583c

Please sign in to comment.