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

Commit

Permalink
Fall back on bad locale objects
Browse files Browse the repository at this point in the history
fixes #6
  • Loading branch information
aredridel committed Feb 10, 2015
1 parent 4002551 commit b3ae685
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ var proto = {
* @returns {*}
*/
resolve: function (name, locale) {
var match, loc;
name = name + this._ext;
loc = locale ? util.parseLangTag(locale) : this._fallback;
match = this._locate(name, loc);
var loc = util.selectLangTag(locale, this._fallback);
var match = this._locate(name + this._ext, loc);
return match;
}

Expand Down
17 changes: 15 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ var fs = require('graceful-fs'),
* @param str String a language tag in the format `en-US`, `en_US`, `en`, etc.
* @returns {{language: string, country: string}}
*/
exports.parseLangTag = function (str) {
exports.parseLangTag = parseLangTag;

function parseLangTag(str) {
var pair, tuple;

if (typeof str === 'object') {
Expand All @@ -46,7 +48,7 @@ exports.parseLangTag = function (str) {
}

return pair;
};
}


/**
Expand Down Expand Up @@ -79,3 +81,14 @@ exports.locate = function locate(name, root, start) {
}

};

exports.selectLangTag = function selectLangTag(primary, fallback) {
primary = parseLangTag(primary);
fallback = parseLangTag(fallback);

if (primary.country && primary.language) {
return primary;
} else {
return fallback;
}
};
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,26 @@ test('fileResolver', function (t) {
t.equal(typeof resolvr._locate, 'function');
t.end();
});

t.test('resolve with a bogus locale object', function (t) {
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', ext: 'dust'});
var info = resolvr.resolve('test', {});

t.equal(info.root, __dirname + '/fixtures/root/');
t.equal(info.file, __dirname + '/fixtures/root/test.dust');
t.equal(info.ext, 'dust');
t.equal(info.name, 'test');
t.end();
});

t.test('fall back with a bogus locale object', function (t) {
var resolvr = fileResolver.create({root: __dirname + '/fixtures/root', ext: 'dust', fallback: 'en_US'});
var info = resolvr.resolve('test', {});

t.equal(info.root, __dirname + '/fixtures/root/US/en/');
t.equal(info.file, __dirname + '/fixtures/root/US/en/test.dust');
t.equal(info.ext, 'dust');
t.equal(info.name, 'test');
t.end();
});
});

0 comments on commit b3ae685

Please sign in to comment.