Skip to content

Commit

Permalink
Merge pull request #18 from aredridel/filename-aware-resolver
Browse files Browse the repository at this point in the history
Make resolver filename aware
  • Loading branch information
pvenkatakrishnan committed Nov 10, 2015
2 parents f9163dc + 9179209 commit 129f38d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exports.create = function create(parent) {
var resolve = this.resolve.bind(this);

if (isModule(file)) {
resolve(require(file), callback);
resolve(require(file), file, callback);
return;
}

Expand All @@ -51,7 +51,7 @@ exports.create = function create(parent) {

try {
data = JSON.parse(data);
resolve(data, callback);
resolve(data, file, callback);
} catch (err) {
callback(err);
}
Expand All @@ -61,4 +61,4 @@ exports.create = function create(parent) {

});

};
};
15 changes: 11 additions & 4 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,28 @@ exports.create = function create(parent) {
* @param data The data structure to scan
* @param callback the callback to invoke when processing is complete with the signature `function (err, data)`
*/
resolve: function resolve(data, callback) {
resolve: function resolve(data, filename, callback) {
var self, tasks, handler;

if (!callback) {
callback = filename;
filename = null;
}

self = this;

if (thing.isArray(data) || (thing.isObject(data) && Object.getPrototypeOf(data) === Object.prototype)) {

if (thing.isArray(data)) {

tasks = data.map(function (val) {
return resolve.bind(self, val);
return resolve.bind(self, val, filename);
});

} else {
tasks = {};
Object.keys(data).forEach(function (key) {
tasks[key] = resolve.bind(self, data[key]);
tasks[key] = resolve.bind(self, data[key], filename);
});
}

Expand Down Expand Up @@ -193,8 +198,10 @@ exports.create = function create(parent) {
return handler;
});

tasks.unshift(function init(done) {
tasks.unshift(tasks[0].length == 2 ? function init(done) {
done(null, data);
} : function init(done) {
done(null, data, filename);
});

// Waterfall will *always* resolve asynchronously
Expand Down
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ test('shortstop', function (t) {
});


t.test('resolve with filename', function (t) {
var resolver, expected;

resolver = shortstop.create();
resolver.use('foo', foo);
resolver.use('bar', function(data, file, cb) {
cb(null, file+data);
});

expected = { foo: 'foo:foo', bar: 'bar:bar', baz: false };
resolver.resolve(expected, __filename, function resolve(err, actual) {
t.error(err);
t.equal(actual.foo, 'foo_foo');
t.equal(actual.bar, __filename + 'bar');
t.equal(actual.baz, false);
t.notEqual(actual, expected);
t.end();
});
});


t.test('nested resolve', function (t) {
var resolver, expected;

Expand Down

0 comments on commit 129f38d

Please sign in to comment.