Last active
August 29, 2015 13:56
-
-
Save adohe-zz/9340848 to your computer and use it in GitHub Desktop.
add a javascript util module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, '<') | |
| .replace(/>/g, '>') | |
| .replace(/"/g, '"'); | |
| } | |
| 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