JSHint i18n.js

- Mark the I18n object as an overridable global
- Normalize comma first style rather than turning on lax comma
- Add missing semicolons
- Remove unnecessary semicolons
- Fix options overloading in “lookup”
- Use strict comparison for typeof checks and remove unnecessary parens
This commit is contained in:
Nick Schonning 2014-03-11 03:01:12 -04:00
parent 62d5a10873
commit 635c0dbf7a
2 changed files with 57 additions and 62 deletions

View file

@ -1,5 +1,4 @@
app/assets/javascripts/defer/html-sanitizer-bundle.js
app/assets/javascripts/locales/
lib/autospec/run-qunit.js
lib/headless-ember.js
lib/javascripts/locale/

View file

@ -1,3 +1,5 @@
/*global I18n:true */
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
@ -52,7 +54,7 @@ I18n.fallbackRules = {};
I18n.pluralizationRules = {
en: function (n) {
return n == 0 ? ["zero", "none", "other"] : n == 1 ? "one" : "other";
return n === 0 ? ["zero", "none", "other"] : n === 1 ? "one" : "other";
}
};
@ -60,8 +62,8 @@ I18n.getFallbacks = function(locale) {
if (locale === I18n.defaultLocale) {
return [];
} else if (!I18n.fallbackRules[locale]) {
var rules = []
, components = locale.split("-");
var rules = [],
components = locale.split("-");
for (var l = 1; l < components.length; l++) {
rules.push(components.slice(0, l).join("-"));
@ -73,23 +75,23 @@ I18n.getFallbacks = function(locale) {
}
return I18n.fallbackRules[locale];
}
};
I18n.isValidNode = function(obj, node, undefined) {
return obj[node] !== null && obj[node] !== undefined;
};
I18n.lookup = function(scope, options) {
var options = options || {}
, lookupInitialScope = scope
, translations = this.prepareOptions(I18n.translations)
, locale = options.locale || I18n.currentLocale()
, messages = translations[locale] || {}
, options = this.prepareOptions(options)
, currentScope
;
options = options || {};
var lookupInitialScope = scope,
translations = this.prepareOptions(I18n.translations),
locale = options.locale || I18n.currentLocale(),
messages = translations[locale] || {},
currentScope;
if (typeof(scope) == "object") {
options = this.prepareOptions(options);
if (typeof scope === "object") {
scope = scope.join(this.defaultSeparator);
}
@ -130,10 +132,9 @@ I18n.lookup = function(scope, options) {
// #=> {name: "John Doe", role: "user"}
//
I18n.prepareOptions = function() {
var options = {}
, opts
, count = arguments.length
;
var options = {},
opts,
count = arguments.length;
for (var i = 0; i < count; i++) {
opts = arguments[i];
@ -154,11 +155,10 @@ I18n.prepareOptions = function() {
I18n.interpolate = function(message, options) {
options = this.prepareOptions(options);
var matches = message.match(this.PLACEHOLDER)
, placeholder
, value
, name
;
var matches = message.match(this.PLACEHOLDER),
placeholder,
value,
name;
if (!matches) {
return message;
@ -185,8 +185,8 @@ I18n.translate = function(scope, options) {
var translation = this.lookup(scope, options);
try {
if (typeof(translation) == "object") {
if (typeof(options.count) == "number") {
if (typeof translation === "object") {
if (typeof options.count === "number") {
return this.pluralize(options.count, scope, options);
} else {
return translation;
@ -221,9 +221,9 @@ I18n.parseDate = function(date) {
var matches, convertedDate;
// we have a date, so just return it.
if (typeof(date) == "object") {
if (typeof date === "object") {
return date;
};
}
// it matches the following formats:
// yyyy-mm-dd
@ -247,14 +247,14 @@ I18n.parseDate = function(date) {
} else {
convertedDate = new Date(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6]);
}
} else if (typeof(date) == "number") {
} else if (typeof date === "number") {
// UNIX timestamp
convertedDate = new Date();
convertedDate.setTime(date);
} else if (date.match(/\d+ \d+:\d+:\d+ [+-]\d+ \d+/)) {
// a valid javascript format with timezone info
convertedDate = new Date();
convertedDate.setTime(Date.parse(date))
convertedDate.setTime(Date.parse(date));
} else {
// an arbitrary javascript string
convertedDate = new Date();
@ -265,9 +265,8 @@ I18n.parseDate = function(date) {
};
I18n.toTime = function(scope, d) {
var date = this.parseDate(d)
, format = this.lookup(scope)
;
var date = this.parseDate(d),
format = this.lookup(scope);
if (date.toString().match(/invalid/i)) {
return date.toString();
@ -289,20 +288,19 @@ I18n.strftime = function(date, format) {
options.meridian = options.meridian || ["AM", "PM"];
var weekDay = date.getDay()
, day = date.getDate()
, year = date.getFullYear()
, month = date.getMonth() + 1
, hour = date.getHours()
, hour12 = hour
, meridian = hour > 11 ? 1 : 0
, secs = date.getSeconds()
, mins = date.getMinutes()
, offset = date.getTimezoneOffset()
, absOffsetHours = Math.floor(Math.abs(offset / 60))
, absOffsetMinutes = Math.abs(offset) - (absOffsetHours * 60)
, timezoneoffset = (offset > 0 ? "-" : "+") + (absOffsetHours.toString().length < 2 ? "0" + absOffsetHours : absOffsetHours) + (absOffsetMinutes.toString().length < 2 ? "0" + absOffsetMinutes : absOffsetMinutes)
;
var weekDay = date.getDay(),
day = date.getDate(),
year = date.getFullYear(),
month = date.getMonth() + 1,
hour = date.getHours(),
hour12 = hour,
meridian = hour > 11 ? 1 : 0,
secs = date.getSeconds(),
mins = date.getMinutes(),
offset = date.getTimezoneOffset(),
absOffsetHours = Math.floor(Math.abs(offset / 60)),
absOffsetMinutes = Math.abs(offset) - (absOffsetHours * 60),
timezoneoffset = (offset > 0 ? "-" : "+") + (absOffsetHours.toString().length < 2 ? "0" + absOffsetHours : absOffsetHours) + (absOffsetMinutes.toString().length < 2 ? "0" + absOffsetMinutes : absOffsetMinutes);
if (hour12 > 12) {
hour12 = hour12 - 12;
@ -350,13 +348,12 @@ I18n.toNumber = function(number, options) {
{precision: 3, separator: ".", delimiter: ",", strip_insignificant_zeros: false}
);
var negative = number < 0
, string = Math.abs(number).toFixed(options.precision).toString()
, parts = string.split(".")
, precision
, buffer = []
, formattedNumber
;
var negative = number < 0,
string = Math.abs(number).toFixed(options.precision).toString(),
parts = string.split("."),
precision,
buffer = [],
formattedNumber;
number = parts[0];
precision = parts[1];
@ -378,8 +375,8 @@ I18n.toNumber = function(number, options) {
if (options.strip_insignificant_zeros) {
var regex = {
separator: new RegExp(options.separator.replace(/\./, "\\.") + "$")
, zeros: /0+$/
separator: new RegExp(options.separator.replace(/\./, "\\.") + "$"),
zeros: /0+$/
};
formattedNumber = formattedNumber
@ -409,12 +406,11 @@ I18n.toCurrency = function(number, options) {
};
I18n.toHumanSize = function(number, options) {
var kb = 1024
, size = number
, iterations = 0
, unit
, precision
;
var kb = 1024,
size = number,
iterations = 0,
unit,
precision;
while (size >= kb && iterations < 4) {
size = size / kb;
@ -480,7 +476,7 @@ I18n.pluralize = function(count, scope, options) {
var pluralizer = this.pluralizer(this.currentLocale());
var key = pluralizer(Math.abs(count));
var keys = ((typeof key == "object") && (key instanceof Array)) ? key : [key];
var keys = ((typeof key === "object") && (key instanceof Array)) ? key : [key];
var message = this.findAndTranslateValidNode(keys, translation);
if (message == null) message = this.missingTranslation(scope, keys[0]);