Skip to content

Commit

Permalink
clean for JSLint
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuichiro MASUI committed Nov 17, 2011
1 parent 0f30370 commit 1bc8bb4
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 308 deletions.
2 changes: 1 addition & 1 deletion bin/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var extract_require = function(filename) {
var content = fs.readFileSync(LIB_DIR + filename + '.js', 'utf8');
return content.replace(/require\s*\(\s*['"]{1}(.*?)['"]{1}\s*\)/g, function(full, fname) {
var exports = extract_require(fname);
return "(function(){var exports={};" + exports + ";return exports;})()"
return "(function(){var exports={};" + exports + "return exports;}())"
});
};

Expand Down
46 changes: 24 additions & 22 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ exports.EventEmitter = EventEmitter;
// that to be increased. Set to zero for unlimited.
var defaultMaxListeners = 10;
EventEmitter.prototype.setMaxListeners = function(n) {
if (!this._events) this._events = {};
if (!this._events) { this._events = {}; }
this._maxListeners = n;
};


EventEmitter.prototype.emit = function() {
var type = arguments[0];

if (!this._events) return false;
if (!this._events) { return false; }
var handler = this._events[type];
if (!handler) return false;
if (!handler) { return false; }

if (typeof handler == 'function') {
var args, l, i;
if (typeof handler === 'function') {
switch (arguments.length) {
// fast cases
case 1:
Expand All @@ -58,20 +59,20 @@ EventEmitter.prototype.emit = function() {
break;
// slower
default:
var l = arguments.length;
var args = new Array(l - 1);
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
l = arguments.length;
args = new Array(l - 1);
for (i = 1; i < l; i++) { args[i - 1] = arguments[i]; }
handler.apply(this, args);
}
return true;

} else if (isArray(handler)) {
var l = arguments.length;
var args = new Array(l - 1);
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
l = arguments.length;
args = new Array(l - 1);
for (i = 1; i < l; i++) { args[i - 1] = arguments[i]; }

var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
for (i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
return true;
Expand All @@ -88,9 +89,9 @@ EventEmitter.prototype.addListener = function(type, listener) {
throw new Error('addListener only takes instances of Function');
}

if (!this._events) this._events = {};
if (!this._events) { this._events = {}; }

// To avoid recursion in the case that type == "newListeners"! Before
// To avoid recursion in the case that type === "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit('newListener', type, listener);

Expand Down Expand Up @@ -139,7 +140,7 @@ EventEmitter.prototype.once = function(type, listener) {
function g() {
self.removeListener(type, g);
listener.apply(this, arguments);
};
}

g.listener = listener;
self.on(type, g);
Expand All @@ -153,13 +154,13 @@ EventEmitter.prototype.removeListener = function(type, listener) {
}

// does not use listeners(), so no side effect of creating _events[type]
if (!this._events || !this._events[type]) return this;
if (!this._events || !this._events[type]) { return this; }

var list = this._events[type];

if (isArray(list)) {
var position = -1;
for (var i = 0, length = list.length; i < length; i++) {
var i, position = -1;
for (i = 0, length = list.length; i < length; i++) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener))
{
Expand All @@ -168,10 +169,11 @@ EventEmitter.prototype.removeListener = function(type, listener) {
}
}

if (position < 0) return this;
if (position < 0) { return this; }
list.splice(position, 1);
if (list.length == 0)
if (list.length === 0) {
delete this._events[type];
}
} else if (list === listener ||
(list.listener && list.listener === listener))
{
Expand All @@ -188,13 +190,13 @@ EventEmitter.prototype.removeAllListeners = function(type) {
}

// does not use listeners(), so no side effect of creating _events[type]
if (type && this._events && this._events[type]) this._events[type] = null;
if (type && this._events && this._events[type]) { this._events[type] = null; }
return this;
};

EventEmitter.prototype.listeners = function(type) {
if (!this._events) this._events = {};
if (!this._events[type]) this._events[type] = [];
if (!this._events) { this._events = {}; }
if (!this._events[type]) { this._events[type] = []; }
if (!isArray(this._events[type])) {
this._events[type] = [this._events[type]];
}
Expand Down
131 changes: 69 additions & 62 deletions lib/sha1.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,24 @@ var SHA1 = (function(){
/**
* Spec is the BDD style test utilities.
*/
var Spec = {
var Spec;
Spec = {
/** Replace the Spec.describe function with empty function if false. */
enabled: true,

/** Indicates whether object 'a' is "equal to" 'b'. */
equals: function(a, b) {
var i;
if (a instanceof Array && b instanceof Array) {
if (a.length != b.length) return false;
for (var i = 0; i < a.length; i++) if (!Spec.equals(a[i], b[i])) return false;
if (a.length !== b.length) { return false; }
for (i = 0; i < a.length; i++) { if (!Spec.equals(a[i], b[i])) { return false; } }
return true;
}
if ((a != null && b != null) && (typeof a == "object" && typeof b == "object")) {
for (var i in a) if (!Spec.equals(a[i], b[i])) return false;
if ((a !== null && b !== null) && (typeof a === "object" && typeof b === "object")) {
for (i in a) { if(a.hasOwnProperty(i)) { if (!Spec.equals(a[i], b[i])) { return false; } } }
return true;
}
return (a == b);
return (a === b);
},

/** equivalent to xUint's assert */
Expand All @@ -101,11 +103,14 @@ var SHA1 = (function(){
/** Write your specification by using describe method. */
describe: function(title, spec) {
Spec.currentTitle = title;
for (var name in spec) {
Spec.currentMessage = name;
Spec.currentIndicator = 0;
spec[name]();
Spec.currentIndicator = null;
var name;
for (name in spec) {
if (spec.hasOwnProperty(name)) {
Spec.currentMessage = name;
Spec.currentIndicator = 0;
spec[name]();
Spec.currentIndicator = null;
}
}
Spec.currentMessage = Spec.currentTitle = null;
},
Expand All @@ -116,7 +121,7 @@ var SHA1 = (function(){
Spec.should.equal = function(a, b, message) { return Spec.should(Spec.equals(a, b), message); };
Spec.should.not = function(a, message) { return Spec.should(!a, message); };
Spec.should.not.equal = function(a, b, message) { return Spec.should(!Spec.equals(a, b), message); };
if (!Spec.enabled) Spec.describe = function(){};
if (!Spec.enabled) { Spec.describe = function(){}; }


// self test
Expand Down Expand Up @@ -152,9 +157,9 @@ var SHA1 = (function(){
// int32 -> hexdigits string (e.g. 0x123 -> '00000123')
function strfhex32(i32) {
i32 &= 0xffffffff;
if (i32 < 0) i32 += 0x100000000;
if (i32 < 0) { i32 += 0x100000000; }
var hex = Number(i32).toString(16);
if (hex.length < 8) hex = "00000000".substr(0, 8 - hex.length) + hex;
if (hex.length < 8) { hex = "00000000".substr(0, 8 - hex.length) + hex; }
return hex;
}
Spec.describe("sha1", {
Expand All @@ -167,7 +172,7 @@ var SHA1 = (function(){
/*
// int32 -> string (e.g. 123 -> '00000000 00000000 00000000 01111011')
function strfbits(i32) {
if (typeof arguments.callee.ZERO32 == 'undefined') {
if (typeof arguments.callee.ZERO32 === 'undefined') {
arguments.callee.ZERO32 = new Array(33).join("0");
}
Expand All @@ -180,9 +185,9 @@ var SHA1 = (function(){
}
Spec.describe("sha1", {
"strfbits": function() {
Ti.API.info(strfbits(0));
Ti.API.info(strfbits(1));
Ti.API.info(strfbits(123));
Ti.API.info(strfbits(0));
Ti.API.info(strfbits(1));
Ti.API.info(strfbits(123));
Spec.should.equal(strfbits(0), "00000000 00000000 00000000 00000000");
Spec.should.equal(strfbits(1), "00000000 00000000 00000000 00000001");
Spec.should.equal(strfbits(123), "00000000 00000000 00000000 01111011");
Expand All @@ -195,7 +200,7 @@ var SHA1 = (function(){
// -----------------------------------------------------------
// Returns Number(32bit unsigned integer) array size to fit for blocks (512-bit strings)
function padding_size(nbits) {
var n = nbits + 1 + 64
var n = nbits + 1 + 64;
return 512 * Math.ceil(n / 512) / 32;
}
Spec.describe("sha1", {
Expand All @@ -212,13 +217,14 @@ var SHA1 = (function(){
var nchar = m.length;
var size = padding_size(nchar * 8);
var words = new Array(size);
for (var i = 0, j = 0; i < nchar; ) {
var i;
for (i = 0, j = 0; i < nchar; ) {
words[j++] = ((m.charCodeAt(i++) & 0xff) << 24) |
((m.charCodeAt(i++) & 0xff) << 16) |
((m.charCodeAt(i++) & 0xff) << 8) |
((m.charCodeAt(i++) & 0xff))
((m.charCodeAt(i++) & 0xff));
}
while (j < size) words[j++] = 0;
while (j < size) { words[j++] = 0; }
return words;
}
Spec.describe("sha1", {
Expand All @@ -231,7 +237,7 @@ var SHA1 = (function(){
function write_nbits(words, length, nbits) {
if (nbits > 0xffffffff) {
var lo = nbits & 0xffffffff;
if (lo < 0) lo += 0x100000000;
if (lo < 0) { lo += 0x100000000; }
words[length - 1] = lo;
words[length - 2] = (nbits - lo) / 0x100000000;
} else {
Expand Down Expand Up @@ -267,7 +273,7 @@ var SHA1 = (function(){
var W = new Array(80);

// (a)
for (t = 0; t < 16; t++) W[t] = words[i++];
for (t = 0; t < 16; t++) { W[t] = words[i++]; }

// (b)
for (t = 16; t < 80; t++) {
Expand All @@ -283,10 +289,10 @@ var SHA1 = (function(){
for (t = 0; t < 80; t++) {
var tmp = ((A << 5) | (A >>> 27)) + E + W[t];

if (t >= 0 && t <= 19) tmp += ((B & C) | ((~B) & D)) + 0x5a827999;
else if (t >= 20 && t <= 39) tmp += (B ^ C ^ D) + 0x6ed9eba1;
else if (t >= 40 && t <= 59) tmp += ((B & C) | (B & D) | (C & D)) + 0x8f1bbcdc;
else if (t >= 60 && t <= 79) tmp += (B ^ C ^ D) + 0xca62c1d6;
if (t >= 0 && t <= 19) { tmp += ((B & C) | ((~B) & D)) + 0x5a827999; }
else if (t >= 20 && t <= 39) { tmp += (B ^ C ^ D) + 0x6ed9eba1; }
else if (t >= 40 && t <= 59) { tmp += ((B & C) | (B & D) | (C & D)) + 0x8f1bbcdc; }
else if (t >= 60 && t <= 79) { tmp += (B ^ C ^ D) + 0xca62c1d6; }

E = D; D = C; C = ((B << 30) | (B >>> 2)); B = A; A = tmp;
}
Expand All @@ -297,11 +303,11 @@ var SHA1 = (function(){
H[2] = (H[2] + C) & 0xffffffff;
H[3] = (H[3] + D) & 0xffffffff;
H[4] = (H[4] + E) & 0xffffffff;
if (H[0] < 0) H[0] += 0x100000000;
if (H[1] < 0) H[1] += 0x100000000;
if (H[2] < 0) H[2] += 0x100000000;
if (H[3] < 0) H[3] += 0x100000000;
if (H[4] < 0) H[4] += 0x100000000;
if (H[0] < 0) { H[0] += 0x100000000; }
if (H[1] < 0) { H[1] += 0x100000000; }
if (H[2] < 0) { H[2] += 0x100000000; }
if (H[3] < 0) { H[3] += 0x100000000; }
if (H[4] < 0) { H[4] += 0x100000000; }
}

return H;
Expand All @@ -310,13 +316,13 @@ var SHA1 = (function(){
// message: 8bit string
var SHA1 = function(message) {
this.message = message;
}
};

function strfhex8(i8) {
i8 &= 0xff;
if (i8 < 0) i8 += 0x100;
if (i8 < 0) { i8 += 0x100; }
var hex = Number(i8).toString(16);
if (hex.length < 2) hex = "00".substr(0, 2 - hex.length) + hex;
if (hex.length < 2) { hex = "00".substr(0, 2 - hex.length) + hex; }
return hex;
}

Expand All @@ -331,37 +337,38 @@ var SHA1 = (function(){

base64digest: function() {
var hex = this.hexdigest();
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
while (i < hex.length) {
chr1 = parseInt(hex.substring(i+0, i+2), 16);
chr2 = parseInt(hex.substring(i+2, i+4), 16);
chr3 = parseInt(hex.substring(i+4, i+6), 16);

enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;

if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}

output = output +
_base64_keyStr.charAt(enc1) + _base64_keyStr.charAt(enc2) +
_base64_keyStr.charAt(enc3) + _base64_keyStr.charAt(enc4);
i += 6;
}
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
while (i < hex.length) {
chr1 = parseInt(hex.substring(i, i+2), 16);
chr2 = parseInt(hex.substring(i+2, i+4), 16);
chr3 = parseInt(hex.substring(i+4, i+6), 16);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
_base64_keyStr.charAt(enc1) + _base64_keyStr.charAt(enc2) +
_base64_keyStr.charAt(enc3) + _base64_keyStr.charAt(enc4);
i += 6;
}

return output;
},

hexdigest: function() {
var digest = this.digest();
for (var i = 0; i < digest.length; i++) digest[i] = strfhex32(digest[i]);
var i;
for (i = 0; i < digest.length; i++) { digest[i] = strfhex32(digest[i]); }
return digest.join("");
}
};
Expand All @@ -377,6 +384,6 @@ var SHA1 = (function(){
});

return SHA1;
})();
}());

exports.SHA1 = SHA1; // add for node.js
Loading

0 comments on commit 1bc8bb4

Please sign in to comment.