Skip to content
Browse files

'JSHinted' the files.

  • Loading branch information...
1 parent 225dedc commit f790b833d0cee95a697c8e3c8b26eaff12369660 @akoenig committed Sep 26, 2012
Showing with 99 additions and 82 deletions.
  1. +30 −20 lib/guru.js
  2. +12 −9 lib/languagetags.js
  3. +25 −20 lib/lingua.js
  4. +31 −32 lib/trainee.js
  5. +1 −1 package.json
View
50 lib/guru.js
@@ -9,12 +9,15 @@
*
*/
-var fs = require('fs')
- , traverse = require('traverse');
+var fs = require('fs'),
+ traverse = require('traverse');
module.exports = (function() {
- var _name = 'lingua:Guru';
+ "use strict";
+
+ var _name = 'lingua:Guru',
+ Guru;
//
// summary:
@@ -28,17 +31,18 @@ module.exports = (function() {
// If there is no i18n resource file for the configured
// default language the guru has to throw an error.
//
- var Guru = function(config) {
+ Guru = function (config) {
+ var that = this,
+ path = config.resources.path,
+ files = fs.readdirSync(path),
+ available = false;
+
if (!config) {
throw new Error(_name + ': Please pass the configuration to the constructor.');
} else {
this.config = config;
}
- var that = this,
- path = config.resources.path,
- files = fs.readdirSync(path);
-
//
//
// summary:
@@ -56,11 +60,12 @@ module.exports = (function() {
this.resources = [];
files.forEach(function (file) {
- var absolutePath = path + file;
+ var absolutePath = path + file,
+ content,
+ resource = {};
if (fs.statSync(absolutePath).isFile()) {
- var content = fs.readFileSync(absolutePath),
- resource = {};
+ content = fs.readFileSync(absolutePath);
// The ISO locale code from the filename. We remove
// the file extension.
@@ -74,19 +79,24 @@ module.exports = (function() {
// DOCME
resource.content = traverse(resource.content).map(function (value) {
- var hasPlaceholder = function () {
+ var hasPlaceholder,
+ compile;
+
+ hasPlaceholder = function () {
return value.match(config.resources.placeholder);
};
if (!this.isRoot && this.isLeaf) {
if (hasPlaceholder()) {
- var compile = function (data) {
+ compile = function (data) {
var compiled = value;
if (typeof data === 'object') {
for (var param in data) {
- // TODO: Use the pattern from the configuration object.
- compiled = compiled.replace(new RegExp('{'+param+'}','g'), data[param]);
+ if (data.hasOwnProperty(param)) {
+ // TODO: Use the pattern from the configuration object.
+ compiled = compiled.replace(new RegExp('{'+param+'}','g'), data[param]);
+ }
}
} else {
compiled = compiled.replace(/\{(\w*)\}/g, data);
@@ -106,7 +116,6 @@ module.exports = (function() {
// Check if the resource for the default
// locale is available. If not, well, ERROR!
- var available = false;
this.resources.forEach(function(resource) {
if (resource.locale === that.config.resources.defaultLocale) {
available = true;
@@ -129,11 +138,12 @@ module.exports = (function() {
// the content which was defined in the "de-de.json" file.
//
Guru.prototype.ask = function(locales) {
- var that = this;
+ var that = this,
+ resource,
+ i = 0,
+ _filter;
- var resource, i;
-
- var _filter = function(filter) {
+ _filter = function(filter) {
return that.resources.filter(function(resource) {
return (resource.locale === filter);
})[0];
View
21 lib/languagetags.js
@@ -25,7 +25,9 @@
//
module.exports = (function() {
- var LanguageTags = function() {
+ "use strict";
+
+ var LanguageTags = function () {
this.tagQvalues = {}; // associates 'q' values with language tags
};
@@ -37,7 +39,7 @@ module.exports = (function() {
// description:
// More than one tag can be assigned to the same qvalue.
//
- LanguageTags.prototype.addTag = function(tag, qvalue) {
+ LanguageTags.prototype.addTag = function (tag, qvalue) {
try {
this.tagQvalues[qvalue].push(tag);
} catch (e) {
@@ -57,7 +59,7 @@ module.exports = (function() {
// A convenience method to save calling LanguageTags.addTag() multiple
// times.
//
- LanguageTags.prototype.addTags = function(tags, qvalue) {
+ LanguageTags.prototype.addTags = function (tags, qvalue) {
try {
this.tagQvalues[qvalue].push.apply(this.tagQvalues[qvalue], tags);
} catch (e) {
@@ -77,10 +79,13 @@ module.exports = (function() {
// The list of tags is ordered by the associated tag qvalue in
// descending order.
//
- LanguageTags.prototype.getTags = function() {
+ LanguageTags.prototype.getTags = function () {
// get the reverse ordered list of qvalues e.g. -> [1, 0.8, 0.5, 0.3]
- var qvalues = [];
- var qvalue;
+ var qvalues = [],
+ qvalue,
+ tags = [],
+ that = this;
+
for (qvalue in this.tagQvalues) {
if (this.tagQvalues.hasOwnProperty(qvalue)) {
qvalues.push(qvalue);
@@ -89,10 +94,8 @@ module.exports = (function() {
qvalues = qvalues.sort().reverse();
// add the tags to the tag list, ordered by qvalue
- var tags = [];
- var self = this;
qvalues.forEach(function(qvalue) {
- tags.push.apply(tags, self.tagQvalues[qvalue]);
+ tags.push.apply(tags, that.tagQvalues[qvalue]);
});
return tags;
View
45 lib/lingua.js
@@ -11,23 +11,27 @@
var Guru = require('./guru'),
Trainee = require('./trainee');
-module.exports = function(app, options) {
+module.exports = function (app, options) {
- var _name = 'lingua';
+ "use strict";
+
+ var _name = 'lingua',
+ config = {},
+ guru,
+ trainee;
//
// The lingua configuration object.
//
- var config = {
- storage: {
- key: 'language'
- },
- resources: {
- path: options.path,
- defaultLocale: options.defaultLocale,
- extension: '.json',
- placeholder: /{(.*?)}/
- }
+ config.storage = {
+ key: 'language'
+ };
+
+ config.resources = {
+ path: options.path,
+ defaultLocale: options.defaultLocale,
+ extension: '.json',
+ placeholder: /{(.*?)}/
};
//
@@ -61,12 +65,12 @@ module.exports = function(app, options) {
//
// Creating the mighty guru object which knows everything.
//
- var guru = new Guru(config);
+ guru = new Guru(config);
//
// Creating the trainee object which is like a helper for the guru.
//
- var trainee = new Trainee(config);
+ trainee = new Trainee(config);
//
// summary:
@@ -107,14 +111,14 @@ module.exports = function(app, options) {
//
// # Compatibility check
- if('function' === typeof app.dynamicHelpers) { // Express < 3.0
+ if ('function' === typeof app.dynamicHelpers) { // Express < 3.0
app.dynamicHelpers({
- lingua: function(req, res){
+ lingua: function (req, res){
return res.lingua.content;
}
});
- } else if('function' === typeof app.locals.use) { // Express 3.0 beta
- app.locals.use(function(req, res) {
+ } else if ('function' === typeof app.locals.use) { // Express 3.0
+ app.locals.use(function (req, res) {
res.locals.lingua = res.lingua.content;
});
}
@@ -128,7 +132,8 @@ module.exports = function(app, options) {
// HTTP request.
//
return function lingua(req, res, next) {
- var locales, resource;
+ var locales,
+ resource;
//
// Determine the locale in this order:
@@ -146,7 +151,7 @@ module.exports = function(app, options) {
res.lingua = resource;
// # Compatibility check
- if('function' === typeof app.locals) { // Express 3.0 RC
+ if ('function' === typeof app.locals) { // Express 3.0 RC
res.locals.lingua = res.lingua.content;
}
View
63 lib/trainee.js
@@ -14,7 +14,10 @@ var url = require('url'),
module.exports = (function() {
- var _name = 'lingua:Trainee';
+ "use strict";
+
+ var _name = 'lingua:Trainee',
+ Trainee;
//
// summary:
@@ -24,7 +27,7 @@ module.exports = (function() {
// The trainee is a helper which provides methods for determining
// the locales from a given HTTP request header, a cookie and an url query.
//
- var Trainee = function(configuration) {
+ Trainee = function (configuration) {
var that = this;
if (!configuration) {
@@ -43,26 +46,26 @@ module.exports = (function() {
//
// See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
//
- this._extractLocales = function(headers) {
- var locales = [];
-
- var acceptLanguage = headers['accept-language'];
+ this._extractLocales = function (headers) {
+ var locales = [],
+ acceptLanguage = headers['accept-language'],
+ tags = new LanguageTags(),
+ subtags = new LanguageTags(),
+ subtagQvalue;
if (acceptLanguage) {
- var tags = new LanguageTags();
- var subtags = new LanguageTags();
-
// associate language tags by their 'q' value (between 1 and 0)
- acceptLanguage.split(',').forEach(function(lang) {
- var parts = lang.split(';'); // 'en-GB;q=0.8' -> ['en-GB', 'q=0.8']
- var tag = parts.shift().toLowerCase().trim(); // ['en-GB', 'q=0.8'] -> 'en-gb'
- var primarySubtag = tag.split('-')[0].trimRight(); // 'en-gb' -> 'en'
+ acceptLanguage.split(',').forEach(function (lang) {
+ var parts = lang.split(';'), // 'en-GB;q=0.8' -> ['en-GB', 'q=0.8']
+ tag = parts.shift().toLowerCase().trim(), // ['en-GB', 'q=0.8'] -> 'en-gb'
+ primarySubtag = tag.split('-')[0].trimRight(), // 'en-gb' -> 'en'
+ qvalue = 1, // default qvalue
+ i = 0,
+ part;
// get the language tag qvalue: 'q=0.8' -> 0.8
- var qvalue = 1; // default qvalue
- var i;
for (i = 0; i < parts.length; i++) {
- var part = parts[i].split('=');
+ part = parts[i].split('=');
if (part[0] === 'q' && !isNaN(part[1])) {
qvalue = Number(part[1]);
break;
@@ -77,7 +80,8 @@ module.exports = (function() {
// Add all the primary subtags to the tag set if
// required, using a default low qvalue for the
// primary subtags.
- var subtagQvalue = (isNaN(that.configuration.subtagQvalue)) ? 0.1 : that.configuration.subtagQvalue;
+ subtagQvalue = (isNaN(that.configuration.subtagQvalue)) ? 0.1 : that.configuration.subtagQvalue;
+
if (subtagQvalue) {
tags.addTags(subtags.getTags(), subtagQvalue);
}
@@ -107,19 +111,13 @@ module.exports = (function() {
// Based on connect-i18n: https://github.com/masylum/connect-i18n/blob/master/lib/connect-i18n.js
//
Trainee.prototype.determineLocales = function(req, res) {
- var querystring = url.parse(req.url, true);
- var cookies = new Cookies(req, res);
- var headers = req.headers;
-
- var query = querystring.query[this.configuration.storage.key];
- var cookie = cookies.get(this.configuration.storage.key);
-
- //
- // The determined locales which the user has configured.
- //
- var locales = [];
-
- var locale = (query || cookie);
+ var querystring = url.parse(req.url, true),
+ cookies = new Cookies(req, res),
+ headers = req.headers,
+ query = querystring.query[this.configuration.storage.key],
+ cookie = cookies.get(this.configuration.storage.key),
+ locales = [], // The determined locales which the user has configured.
+ locale = (query || cookie);
if (locale) {
locales.push(locale);
@@ -138,8 +136,9 @@ module.exports = (function() {
// Creates a cookie and persists the given locale in it.
//
Trainee.prototype.persistCookie = function(req, res, locale) {
- var cookies = new Cookies(req, res);
- var expirationDate = new Date();
+ var cookies = new Cookies(req, res),
+ expirationDate = new Date();
+
expirationDate.setFullYear(expirationDate.getFullYear() + 1);
cookies.set(this.configuration.storage.key, locale, { expires: expirationDate });
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "lingua",
"description": "A i18n middleware for the Express.js framework.",
- "version": "0.3.1",
+ "version": "0.3.2",
"repository": {
"type": "git",
"url": "git://github.com/akoenig/express-lingua.git"

0 comments on commit f790b83

Please sign in to comment.
Something went wrong with that request. Please try again.