Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Clean ups to async interface #9

Open
wants to merge 5 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 26 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,33 @@ var path = require('path'),
assert = require('assert');


var proto = {
function Resolver(root, fallback, ext) {
this.root = root;
this.fallback = util.parseLangTag(fallback);
this.ext = ext;
}

get fallbackLocale() {
return this._fallback;
},
/**
* Finds a file that matches the provided name, falling back to a root directory.
*/
Resolver.prototype.resolve = function (name, locale, callback) {
if (!callback) {
callback = locale;
locale = null;
}

if (!locale) {
locale = this.fallback;
}

_locate: function (name, locale) {
var relative = path.join(this._root, locale.country, locale.language);
var val = util.locate(name, this._root, relative);
return val;
},
var loc = util.parseLangTag(locale);

/**
* Finds a file that matches the provided name, falling back to a root directory.
* @param name
* @param locale
* @returns {*}
*/
resolve: function (name, locale, callback) {
if (!callback) {
callback = locale;
locale = null;
}
name = name + this._ext;
var loc = locale ? util.parseLangTag(locale) : this._fallback;
var match = this._locate(name, loc);
process.nextTick(function () {
callback(null, match);
});
var relative = path.join(this.root, loc.country || '', loc.language || '');
var val = util.locate(name + this.ext, this.root, relative);
if (val) {
callback(null, val);
} else {
callback(null, util.locate(name, this.root, this.root));
}

};
Expand All @@ -65,15 +63,6 @@ exports.create = function (options) {
if (ext[0] !== '.') {
ext = '.' + ext;
}
return Object.create(proto, {
_root: {
value: options.root
},
_fallback: {
value: util.parseLangTag(options.fallback)
},
_ext: {
value: ext
}
});

return new Resolver(options.root, util.parseLangTag(options.fallback), ext);
};
20 changes: 3 additions & 17 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,8 @@ exports.locate = function locate(name, root, start) {
removed = arguments[3] || [];
file = path.join(start, name);
if (fs.existsSync(file)) {
return {
root : file.replace(name, ''),
file: file,
ext: path.extname(name).substr(1),
name: name.replace(path.extname(name), ''),
dirs: removed
};
return file;
}


return {
root : undefined,
file: undefined,
ext: path.extname(name).substr(1),
name: name.replace(path.extname(name), ''),
dirs: removed
};

};
return undefined;
};
70 changes: 26 additions & 44 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var fileResolver = require('../index'),
test = require('tape');

test('fileResolver', function (t) {
var resolvr;

t.test('Creating a file resolver without options should throw assertion error', function (t) {
var resolvr;

try {
resolvr = fileResolver.create();
} catch (e) {
Expand All @@ -16,87 +16,69 @@ test('fileResolver', function (t) {
});

t.test('Creating a file resolver with options', function (t) {
resolvr = fileResolver.create({root: process.cwd() + '/test/fixtures/root', fallback: 'en_US', ext: 'dust'});
t.deepEqual(resolvr.fallbackLocale, { country: 'US', language: 'en' });
t.equal(typeof resolvr._locate, 'function');
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', fallback: 'en_US', ext: 'dust'});
t.deepEqual(resolvr.fallback, { country: 'US', language: 'en' });
t.end();
});

t.test('Creating a file resolver with options', function (t) {
resolvr = fileResolver.create({root: process.cwd() + '/test/fixtures/root', fallback: 'en_US', ext: '.dust'});
t.deepEqual(resolvr.fallbackLocale, { country: 'US', language: 'en' });
t.equal(typeof resolvr._locate, 'function');
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', fallback: 'en_US', ext: '.dust'});
t.deepEqual(resolvr.fallback, { country: 'US', language: 'en' });
t.end();
});


t.test('resolving for an extension with default locale', function (t) {
resolvr = fileResolver.create({root: process.cwd() + '/test/fixtures/root', fallback: 'en_US', ext: 'dust'});
resolvr.resolve('test', function (err, info) {
t.equal(info.root, process.cwd() + '/test/fixtures/root/US/en/');
t.equal(info.file, process.cwd() + '/test/fixtures/root/US/en/test.dust');
t.equal(info.ext, 'dust');
t.equal(info.name, 'test');
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', fallback: 'en_US', ext: 'dust'});
resolvr.resolve('test', function (err, file) {
t.equal(file, __dirname + '/fixtures/root/US/en/test.dust');
t.end();
});
});

t.test('resolving for an extension with a specified locale', function (t) {
resolvr.resolve('test', 'es_US', function (err, info) {
t.equal(info.root, process.cwd() + '/test/fixtures/root/US/es/');
t.equal(info.file, process.cwd() + '/test/fixtures/root/US/es/test.dust');
t.equal(info.ext, 'dust');
t.equal(info.name, 'test');
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', fallback: 'en_US', ext: 'dust'});
resolvr.resolve('test', 'es_US', function (err, file) {
t.equal(file, __dirname + '/fixtures/root/US/es/test.dust');
t.end();
});

});

t.test('Creating a file resolver with options', function (t) {
resolvr = fileResolver.create({root: process.cwd() + '/test/fixtures/root', ext: 'dust'});
t.deepEqual(resolvr.fallbackLocale, { country: '', language: '' });
t.equal(typeof resolvr._locate, 'function');
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', ext: 'dust'});
t.deepEqual(resolvr.fallback, { country: '', language: '' });
t.end();
});

t.test('resolving for an extension without locale', function (t) {
var info = resolvr.resolve('test', function (err, info) {
t.equal(info.root, process.cwd() + '/test/fixtures/root/');
t.equal(info.file, process.cwd() + '/test/fixtures/root/test.dust');
t.equal(info.ext, 'dust');
t.equal(info.name, 'test');
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', ext: 'dust'});
resolvr.resolve('test', function (err, file) {
t.equal(file, __dirname + '/fixtures/root/test.dust');
t.end();
});
});

t.test('trying to resolve with a locale object', function (t) {
resolvr.resolve('test', { country: 'US', language: 'es' }, function (err, info) {
t.equal(info.root, process.cwd() + '/test/fixtures/root/US/es/');
t.equal(info.file, process.cwd() + '/test/fixtures/root/US/es/test.dust');
t.equal(info.ext, 'dust');
t.equal(info.name, 'test');
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', ext: 'dust'});
resolvr.resolve('test', { country: 'US', language: 'es' }, function (err, file) {
t.equal(file, __dirname + '/fixtures/root/US/es/test.dust');
t.end();
});
});

t.test('Creating a file resolver with invalid root', function (t) {
resolvr = fileResolver.create({root: process.cwd() + '/test/fixtures', ext: 'dust'});
t.deepEqual(resolvr.fallbackLocale, { country: '', language: '' });
t.equal(typeof resolvr._locate, 'function');
var resolvr = fileResolver.create({root: __dirname + '/fixtures', ext: 'dust'});
t.deepEqual(resolvr.fallback, { country: '', language: '' });

var info = resolvr.resolve('test', function (err, info) {
t.equal(info.root, undefined);
t.equal(info.file, undefined);
t.equal(info.ext, 'dust');
t.equal(info.name, 'test');
resolvr.resolve('test', function (err, file) {
t.equal(file, undefined);
t.end();
});
});

t.test('Creating a file resolver with invalid locale', function (t) {
resolvr = fileResolver.create({root: process.cwd() + '/test/fixtures/root', ext: 'dust', fallback: 'es'});
t.deepEqual(resolvr.fallbackLocale, { country: '', language: 'es' });
t.equal(typeof resolvr._locate, 'function');
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', ext: 'dust', fallback: 'es'});
t.deepEqual(resolvr.fallback, { country: '', language: 'es' });
t.end();
});
});