Skip to content

Commit

Permalink
more lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Dec 2, 2010
1 parent ddbaeef commit e232f6e
Show file tree
Hide file tree
Showing 8 changed files with 564 additions and 556 deletions.
265 changes: 132 additions & 133 deletions lib/http.js

Large diffs are not rendered by default.

261 changes: 136 additions & 125 deletions lib/net.js

Large diffs are not rendered by default.

82 changes: 38 additions & 44 deletions lib/path.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,89 @@
function validPathPart (p, keepBlanks) {
return typeof p === "string" && (p || keepBlanks);
function validPathPart(p, keepBlanks) {
return typeof p === 'string' && (p || keepBlanks);
}


exports.join = function () {
exports.join = function() {
var args = Array.prototype.slice.call(arguments);
// edge case flag to switch into url-resolve-mode
var keepBlanks = false;
if (args[ args.length - 1 ] === true) {
if (args[args.length - 1] === true) {
keepBlanks = args.pop();
}
// return exports.split(args.join("/"), keepBlanks).join("/");
var joined = exports.normalizeArray(args, keepBlanks).join("/");
var joined = exports.normalizeArray(args, keepBlanks).join('/');
return joined;
};


exports.split = function (path, keepBlanks) {
exports.split = function(path, keepBlanks) {
// split based on /, but only if that / is not at the start or end.
return exports.normalizeArray(path.split(/^|\/(?!$)/), keepBlanks);
};


function cleanArray (parts, keepBlanks) {
function cleanArray(parts, keepBlanks) {
var i = 0;
var l = parts.length - 1;
var stripped = false;

// strip leading empty args
while (i < l && !validPathPart(parts[i], keepBlanks)) {
stripped = true;
i ++;
i++;
}

// strip tailing empty args
while (l >= i && !validPathPart(parts[l], keepBlanks)) {
stripped = true;
l --;
l--;
}

if (stripped) {
// if l chopped all the way back to i, then this is empty
parts = Array.prototype.slice.call(parts, i, l + 1);
}

return parts.filter(function (p) { return validPathPart(p, keepBlanks) })
return parts.filter(function(p) { return validPathPart(p, keepBlanks) })
.join('/')
.split(/^|\/(?!$)/);
}


exports.normalizeArray = function (original, keepBlanks) {
exports.normalizeArray = function(original, keepBlanks) {
var parts = cleanArray(original, keepBlanks);
if (!parts.length || (parts.length === 1 && !parts[0])) return ["."];
if (!parts.length || (parts.length === 1 && !parts[0])) return ['.'];

// now we're fully ready to rock.
// leading/trailing invalids have been stripped off.
// if it comes in starting with a slash, or ending with a slash,
var leadingSlash = (parts[0].charAt(0) === "/");
var leadingSlash = (parts[0].charAt(0) === '/');

if (leadingSlash) parts[0] = parts[0].substr(1);
var last = parts.slice(-1)[0];
var tailingSlash = (last.substr(-1) === "/");
var tailingSlash = (last.substr(-1) === '/');
if (tailingSlash) parts[parts.length - 1] = last.slice(0, -1);
var directories = [];
var prev;
for (var i = 0, l = parts.length - 1 ; i <= l ; i ++) {
for (var i = 0, l = parts.length - 1; i <= l; i++) {
var directory = parts[i];

// if it's blank, and we're not keeping blanks, then skip it.
if (directory === "" && !keepBlanks) continue;
if (directory === '' && !keepBlanks) continue;

// if it's a dot, then skip it
if (directory === "." && (
directories.length
|| (i === 0 && !(tailingSlash && i === l))
|| (i === 0 && leadingSlash)
)) continue;
if (directory === '.' && (directories.length ||
(i === 0 && !(tailingSlash && i === l)) ||
(i === 0 && leadingSlash))) continue;

// if we're dealing with an absolute path, then discard ..s that go
// above that the base.
if (leadingSlash && directories.length === 0 && directory === "..") {
if (leadingSlash && directories.length === 0 && directory === '..') {
continue;
}
// trying to go up a dir
if (
directory === ".."
&& directories.length
&& prev !== ".."
&& prev !== undefined
) {
if (directory === '..' && directories.length && prev !== '..' &&
prev !== undefined) {
directories.pop();
prev = directories.slice(-1)[0];
} else {
Expand All @@ -98,27 +92,27 @@ exports.normalizeArray = function (original, keepBlanks) {
}
}
if (!directories.length) {
directories = [ leadingSlash || tailingSlash ? "" : "." ];
directories = [leadingSlash || tailingSlash ? '' : '.'];
}
var last = directories.slice(-1)[0];
if (tailingSlash && last.substr(-1) !== "/") {
directories[directories.length-1] += "/";
if (tailingSlash && last.substr(-1) !== '/') {
directories[directories.length - 1] += '/';
}
if (leadingSlash && directories[0].charAt(0) !== "/") {
if (directories[0] === ".") directories[0] = "";
directories[0] = "/" + directories[0];
if (leadingSlash && directories[0].charAt(0) !== '/') {
if (directories[0] === '.') directories[0] = '';
directories[0] = '/' + directories[0];
}
return directories;
};


exports.normalize = function (path, keepBlanks) {
exports.normalize = function(path, keepBlanks) {
return exports.join(path, keepBlanks || false);
};


exports.dirname = function (path) {
if (path.length > 1 && '/' === path[path.length-1]) {
exports.dirname = function(path) {
if (path.length > 1 && '/' === path[path.length - 1]) {
path = path.replace(/\/+$/, '');
}
var lastSlash = path.lastIndexOf('/');
Expand All @@ -133,16 +127,16 @@ exports.dirname = function (path) {
};


exports.basename = function (path, ext) {
var f = path.substr(path.lastIndexOf("/") + 1);
exports.basename = function(path, ext) {
var f = path.substr(path.lastIndexOf('/') + 1);
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};


exports.extname = function (path) {
exports.extname = function(path) {
var dot = path.lastIndexOf('.'),
slash = path.lastIndexOf('/');
// The last dot must be in the last path component, and it (the last dot) must
Expand All @@ -152,18 +146,18 @@ exports.extname = function (path) {
};


exports.exists = function (path, callback) {
process.binding('fs').stat(path, function (err, stats) {
exports.exists = function(path, callback) {
process.binding('fs').stat(path, function(err, stats) {
if (callback) callback(err ? false : true);
});
};


exports.existsSync = function (path) {
exports.existsSync = function(path) {
try {
process.binding('fs').stat(path);
return true;
} catch(e){
} catch (e) {
return false;
}
};
70 changes: 29 additions & 41 deletions lib/querystring.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Query String Utilities

var QueryString = exports;
var urlDecode = process.binding("http_parser").urlDecode;
var urlDecode = process.binding('http_parser').urlDecode;


function charCode (c) {
function charCode(c) {
return c.charCodeAt(0);
}


// a safe fast alternative to decodeURIComponent
QueryString.unescapeBuffer = function (s, decodeSpaces) {
QueryString.unescapeBuffer = function(s, decodeSpaces) {
var out = new Buffer(s.length);
var state = "CHAR"; // states: CHAR, HEX0, HEX1
var state = 'CHAR'; // states: CHAR, HEX0, HEX1
var n, m, hexchar;

for (var inIndex = 0, outIndex = 0; inIndex <= s.length; inIndex++) {
Expand Down Expand Up @@ -65,61 +65,50 @@ QueryString.unescapeBuffer = function (s, decodeSpaces) {
out[outIndex++] = c;
break;
}
out[outIndex++] = 16*n + m;
out[outIndex++] = 16 * n + m;
break;
}
}

// TODO support returning arbitrary buffers.

return out.slice(0, outIndex-1);
return out.slice(0, outIndex - 1);
};


QueryString.unescape = function (s, decodeSpaces) {
QueryString.unescape = function(s, decodeSpaces) {
return QueryString.unescapeBuffer(s, decodeSpaces).toString();
};


QueryString.escape = function (str) {
QueryString.escape = function(str) {
return encodeURIComponent(str);
};

var stringifyPrimitive = function(v) {
switch (typeof v) {
case "string":
case 'string':
return v;

case "boolean":
return v ? "true" : "false";
case 'boolean':
return v ? 'true' : 'false';

case "number":
return isFinite(v) ? v : "";
case 'number':
return isFinite(v) ? v : '';

default:
return "";
return '';
}
};

/**
* <p>Converts an arbitrary value to a Query String representation.</p>
*
* <p>Objects with cyclical references will trigger an exception.</p>
*
* @method stringify
* @param obj {Variant} any arbitrary value to convert to query string
* @param sep {String} (optional) Character that should join param k=v pairs together. Default: "&"
* @param eq {String} (optional) Character that should join keys to their values. Default: "="
* @param name {String} (optional) Name of the current key, for handling children recursively.
* @static
*/
QueryString.stringify = QueryString.encode = function (obj, sep, eq, name) {
sep = sep || "&";
eq = eq || "=";

QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
obj = (obj === null) ? undefined : obj;

switch (typeof obj) {
case "object":
case 'object':
return Object.keys(obj).map(function(k) {
if (Array.isArray(obj[k])) {
return obj[k].map(function(v) {
Expand All @@ -128,24 +117,23 @@ QueryString.stringify = QueryString.encode = function (obj, sep, eq, name) {
QueryString.escape(stringifyPrimitive(v));
}).join(sep);
} else {
return QueryString.escape(stringifyPrimitive(k)) +
return QueryString.escape(stringifyPrimitive(k)) +
eq +
QueryString.escape(stringifyPrimitive(obj[k]));
}
}).join(sep);

default:
return (name) ?
QueryString.escape(stringifyPrimitive(name)) + eq +
QueryString.escape(stringifyPrimitive(obj)) :
"";
if (!name) return '';
return QueryString.escape(stringifyPrimitive(name)) + eq +
QueryString.escape(stringifyPrimitive(obj));
}
};

// Parse a key=val string.
QueryString.parse = QueryString.decode = function (qs, sep, eq) {
sep = sep || "&";
eq = eq || "=";
QueryString.parse = QueryString.decode = function(qs, sep, eq) {
sep = sep || '&';
eq = eq || '=';
var obj = {};

if (typeof qs !== 'string') {
Expand All @@ -158,11 +146,11 @@ QueryString.parse = QueryString.decode = function (qs, sep, eq) {
var v = QueryString.unescape(x.slice(1).join(eq), true);

if (!(k in obj)) {
obj[k] = v;
obj[k] = v;
} else if (!Array.isArray(obj[k])) {
obj[k] = [obj[k], v];
obj[k] = [obj[k], v];
} else {
obj[k].push(v);
obj[k].push(v);
}
});

Expand Down
Loading

0 comments on commit e232f6e

Please sign in to comment.