Skip to content

Instantly share code, notes, and snippets.

@adohe-zz
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save adohe-zz/9340848 to your computer and use it in GitHub Desktop.

Select an option

Save adohe-zz/9340848 to your computer and use it in GitHub Desktop.
add a javascript util module
var http = require('http')
, parse = require('url').parse
, sep = require('path').sep;
exports.error = function(msg, code) {
var err = new Error(msg || http.STATUS_CODES[code]);
err.status = code;
return err;
}
exports.merge = function(a, b) {
if(a && b) {
for(var key in b) {
a[key] = b[key];
}
}
return a;
}
exports.escape = function(html) {
return String(html)
.replace(/&(?!\w+;)/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
exports.parseUrl = function(req) {
var parsed = req._parsedUrl;
if(parsed && parsed.href == req.url) {
return parsed;
} else {
parsed = parse(req.url);
if(parsed.auth && !parsed.protocol && ~parsed.href.indexOf('//')) {
parsed = parse(req.url.replace(/@/g, '%40'));
}
return req._parsedUrl = parsed;
}
}
exports.inherits = function(ctor, superctor) {
ctor.super_ = superctor;
ctor.prototype = Object.create(superctor.prototype, {
constructor: {
value: ctor,
writable: false,
enumerable: false,
configurable: false
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment