Skip to content

Commit

Permalink
Use plain 'for' loops instead of 'for in' loops on arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Mitchell authored and Will Mitchell committed Aug 14, 2013
1 parent d141864 commit d7c3c9c
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 59 deletions.
10 changes: 6 additions & 4 deletions src/Grammar/dist/Grammar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions src/Grammar/dist/Grammar.min.js

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/Grammar/src/Grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,9 @@ Call_ID = word ( "@" word )? {
// CONTACT

Contact = ( STAR / (contact_param (COMMA contact_param)*) ) {
var idx;
for (idx in data.multi_header) {
var idx, length;
length = data.multi_header.length;
for (idx = 0; idx < length; idx++) {
if (data.multi_header[idx].parsed === null) {
data = null;
break;
Expand Down Expand Up @@ -621,8 +622,9 @@ option_tag = token
// RECORD-ROUTE

Record_Route = rec_route (COMMA rec_route)* {
var idx;
for (idx in data.multi_header) {
var idx, length;
length = data.multi_header.length;
for (idx = 0; idx < length; idx++) {
if (data.multi_header[idx].parsed === null) {
data = null;
break;
Expand Down
8 changes: 5 additions & 3 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function getHeader(data, headerStart) {
}

function parseHeader(message, data, headerStart, headerEnd) {
var header, idx, parsed,
var header, idx, length, parsed,
hcolonIndex = data.indexOf(':', headerStart),
headerName = data.substring(headerStart, hcolonIndex).trim(),
headerValue = data.substring(hcolonIndex + 1, headerEnd).trim();
Expand Down Expand Up @@ -91,7 +91,8 @@ function parseHeader(message, data, headerStart, headerEnd) {
parsed = undefined;
}

for(idx in parsed) {
length = parsed.length;
for (idx = 0; idx < length; idx++) {
header = parsed[idx];
message.addHeader('record-route', headerValue.substring(header.possition, header.offset));
message.headers['Record-Route'][message.countHeader('record-route')-1].parsed = header.parsed;
Expand All @@ -113,7 +114,8 @@ function parseHeader(message, data, headerStart, headerEnd) {
parsed = undefined;
}

for(idx in parsed) {
length = parsed.length;
for (idx = 0; idx < length; idx++) {
header = parsed[idx];
message.addHeader('contact', headerValue.substring(header.possition, header.offset));
message.headers['Contact'][message.countHeader('contact')-1].parsed = header.parsed;
Expand Down
2 changes: 1 addition & 1 deletion src/RTCSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ RTCSession = function(ua) {
this.ua = ua;
this.status = C.STATUS_NULL;
this.dialog = null;
this.earlyDialogs = [];
this.earlyDialogs = {};
this.rtcMediaHandler = null;

// Session Timers
Expand Down
15 changes: 9 additions & 6 deletions src/RTCSession/RTCMediaHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,20 @@ RTCMediaHandler.prototype = {
* @param {Function} onSuccess Fired when there are no more ICE candidates
*/
init: function(constraints) {
var idx, server, scheme, url,
var idx, length, server, scheme, url,
self = this,
servers = [];
servers = [],
config = this.session.ua.configuration;

for (idx in this.session.ua.configuration.stun_servers) {
server = this.session.ua.configuration.stun_servers[idx];
length = config.stun_servers.length;
for (idx = 0; idx < length; idx++) {
server = config.stun_servers[idx];
servers.push({'url': server});
}

for (idx in this.session.ua.configuration.turn_servers) {
server = this.session.ua.configuration.turn_servers[idx];
length = config.turn_servers.length;
for (idx = 0; idx < length; idx++) {
server = config.turn_servers[idx];
url = server.server;
scheme = url.substr(0, url.indexOf(':'));
servers.push({
Expand Down
14 changes: 8 additions & 6 deletions src/SIPMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,15 @@ OutgoingRequest.prototype = {

msg += this.method + ' ' + this.ruri + ' SIP/2.0\r\n';

for(header in this.headers) {
for(idx in this.headers[header]) {
for (header in this.headers) {
length = this.headers[header].length;
for (idx = 0; idx < length; idx++) {
msg += header + ': ' + this.headers[header][idx] + '\r\n';
}
}

length = this.extraHeaders.length;
for(idx=0; idx < length; idx++) {
for (idx = 0; idx < length; idx++) {
msg += this.extraHeaders[idx] +'\r\n';
}

Expand Down Expand Up @@ -208,15 +209,16 @@ IncomingMessage.prototype = {
* @returns {Array} Array with all the headers of the specified name.
*/
getHeaderAll: function(name) {
var idx,
var idx, length,
header = this.headers[JsSIP.Utils.headerize(name)],
result = [];

if(!header) {
return [];
}

for(idx in header) {
length = header.length;
for (idx = 0; idx < length; idx++) {
result.push(header[idx].raw);
}

Expand Down Expand Up @@ -369,7 +371,7 @@ IncomingRequest.prototype.reply = function(code, reason, extraHeaders, body, onS
response += 'CSeq: ' + this.cseq + ' ' + this.method + '\r\n';

length = extraHeaders.length;
for(idx=0; idx < length; idx++) {
for (idx = 0; idx < length; idx++) {
response += extraHeaders[idx] +'\r\n';
}

Expand Down
33 changes: 20 additions & 13 deletions src/UA.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,14 @@ UA.prototype.getCredentials = function(request) {
*/
UA.prototype.onTransportClosed = function(transport) {
// Run _onTransportError_ callback on every client transaction using _transport_
var type, idx,
var type, idx, length,
client_transactions = ['nict', 'ict', 'nist', 'ist'];

transport.server.status = JsSIP.Transport.C.STATUS_DISCONNECTED;
console.log(LOG_PREFIX +'connection state set to '+ JsSIP.Transport.C.STATUS_DISCONNECTED);

for(type in client_transactions) {
length = client_transactions.length;
for (type = 0; type < length; type++) {
for(idx in this.transactions[client_transactions[type]]) {
this.transactions[client_transactions[type]][idx].onTransportError();
}
Expand Down Expand Up @@ -563,10 +564,11 @@ UA.prototype.findDialog = function(request) {
*/
UA.prototype.getNextWsServer = function() {
// Order servers by weight
var idx, ws_server,
var idx, length, ws_server,
candidates = [];

for (idx in this.configuration.ws_servers) {
length = this.configuration.ws_servers.length;
for (idx = 0; idx < length; idx++) {
ws_server = this.configuration.ws_servers[idx];

if (ws_server.status === JsSIP.Transport.C.STATUS_ERROR) {
Expand Down Expand Up @@ -603,12 +605,13 @@ UA.prototype.closeSessionsOnTransportError = function() {
};

UA.prototype.recoverTransport = function(ua) {
var idx, k, nextRetry, count, server;
var idx, length, k, nextRetry, count, server;

ua = ua || this;
count = ua.transportRecoverAttempts;

for (idx in ua.configuration.ws_servers) {
length = ua.configuration.ws_servers.length;
for (idx = 0; idx < length; idx++) {
ua.configuration.ws_servers[idx].status = 0;
}

Expand Down Expand Up @@ -902,7 +905,7 @@ UA.configuration_check = {
},

ws_servers: function(ws_servers) {
var idx, url;
var idx, length, url;

/* Allow defining ws_servers parameter as:
* String: "host"
Expand All @@ -913,7 +916,8 @@ UA.configuration_check = {
if (typeof ws_servers === 'string') {
ws_servers = [{ws_uri: ws_servers}];
} else if (ws_servers instanceof Array) {
for(idx in ws_servers) {
length = ws_servers.length;
for (idx = 0; idx < length; idx++) {
if (typeof ws_servers[idx] === 'string'){
ws_servers[idx] = {ws_uri: ws_servers[idx]};
}
Expand All @@ -926,7 +930,8 @@ UA.configuration_check = {
return false;
}

for (idx in ws_servers) {
length = ws_servers.length;
for (idx = 0; idx < length; idx++) {
if (!ws_servers[idx].ws_uri) {
console.error(LOG_PREFIX +'missing "ws_uri" attribute in ws_servers parameter');
return;
Expand Down Expand Up @@ -1057,15 +1062,16 @@ UA.configuration_check = {
},

stun_servers: function(stun_servers) {
var idx, stun_server;
var idx, length, stun_server;

if (typeof stun_servers === 'string') {
stun_servers = [stun_servers];
} else if (!(stun_servers instanceof Array)) {
return;
}

for (idx in stun_servers) {
length = stun_servers.length;
for (idx = 0; idx < length; idx++) {
stun_server = stun_servers[idx];
if (!(/^stuns?:/.test(stun_server))) {
stun_server = 'stun:' + stun_server;
Expand All @@ -1087,15 +1093,16 @@ UA.configuration_check = {
},

turn_servers: function(turn_servers) {
var idx, turn_server;
var idx, length, turn_server;

if (turn_servers instanceof Array) {
// Do nothing
} else {
turn_servers = [turn_servers];
}

for (idx in turn_servers) {
length = turn_servers.length;
for (idx = 0; idx < length; idx++) {
turn_server = turn_servers[idx];
if (!turn_server.server || !turn_server.username || !turn_server.password) {
return;
Expand Down
7 changes: 4 additions & 3 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,11 @@ Utils= {
'Www-Authenticate': 'WWW-Authenticate'
},
name = string.toLowerCase().replace(/_/g,'-').split('-'),
hname = '', part;
hname = '',
parts = name.length, part;

for (part in name) {
if (part !== '0') {
for (part = 0; part < parts; part++) {
if (part !== 0) {
hname +='-';
}
hname += name[part].charAt(0).toUpperCase()+name[part].substring(1);
Expand Down

0 comments on commit d7c3c9c

Please sign in to comment.