utils.js
This file
Path
lib/utils.js
193 lines · 3.9 KiB · JavaScript
Last changed by
refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
History of this fileTake it
The walk stopped after 200 commits: lines older than 480d006 point at it.
Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 1–9why, one step back12/**3 * Module dependencies.4 */56var mime = require('connect').mime7 , crc32 = require('buffer-crc32');89/**refactorTJ Holowaychukbefore adoptionf8a33d1
lines 10–15why, one step back10 * toString ref.11 */1213var toString = {}.toString;1415/**Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 16–28why, one step back16 * Return ETag for `body`.17 *18 * @param {String|Buffer} body19 * @return {String}20 * @api private21 */2223exports.etag = function(body){24 return '"' + crc32.signed(body) + '"';25};2627/**28 * Make `locals()` bound to the given `obj`.Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionc6d7352
lines 29–30why, one step back29 *30 * This is used for `app.locals` and `res.locals`.Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 31–36why, one step back31 *32 * @param {Object} obj33 * @return {Function}34 * @api private35 */36lint: remove unused stuffJonathan Ongbefore adoption9bed2b8
line 37why, one step back37exports.locals = function(){Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 38–56why, one step back38 function locals(obj){39 for (var key in obj) locals[key] = obj[key];40 return obj;41 };4243 return locals;44};4546/**47 * Check if `path` looks absolute.48 *49 * @param {String} path50 * @return {Boolean}51 * @api private52 */5354exports.isAbsolute = function(path){55 if ('/' == path[0]) return true;56 if (':' == path[1] && '\\' == path[2]) return true;Merge pull request #1740 from superic/masterTJ Holowaychukbefore adoptione43ff07
line 57why, one step back57 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute pathMerge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 58–84why, one step back58};5960/**61 * Flatten the given `arr`.62 *63 * @param {Array} arr64 * @return {Array}65 * @api private66 */6768exports.flatten = function(arr, ret){69 var ret = ret || []70 , len = arr.length;71 for (var i = 0; i < len; ++i) {72 if (Array.isArray(arr[i])) {73 exports.flatten(arr[i], ret);74 } else {75 ret.push(arr[i]);76 }77 }78 return ret;79};8081/**82 * Normalize the given `type`, for example "html" becomes "text/html".83 *84 * @param {String} typeadd support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 85why, one step back85 * @return {Object}Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 86–89why, one step back86 * @api private87 */8889exports.normalizeType = function(type){add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 90–92why, one step back90 return ~type.indexOf('/')91 ? acceptParams(type)92 : { value: mime.lookup(type), params: {} };Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 93–106why, one step back93};9495/**96 * Normalize `types`, for example "html" becomes "text/html".97 *98 * @param {Array} types99 * @return {Array}100 * @api private101 */102103exports.normalizeTypes = function(types){104 var ret = [];105106 for (var i = 0; i < types.length; ++i) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 107why, one step back107 ret.push(exports.normalizeType(types[i]));Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 108–114why, one step back108 }109110 return ret;111};112113114/**add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 115–116why, one step back115 * Parse accept params `str` returning an116 * object with `.value`, `.quality` and `.params`.Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 117why, one step back117 * also includes `.originalIndex` for stable sortingMerge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 118–123why, one step back118 *119 * @param {String} str120 * @return {Object}121 * @api private122 */123Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 124why, one step back124function acceptParams(str, index) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 125why, one step back125 var parts = str.split(/ *; */);Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 126why, one step back126 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
line 127why, one step back127add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 128–135why, one step back128 for (var i = 1; i < parts.length; ++i) {129 var pms = parts[i].split(/ *= */);130 if ('q' == pms[0]) {131 ret.quality = parseFloat(pms[1]);132 } else {133 ret.params[pms[0]] = pms[1];134 }135 }Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
line 136why, one step back136add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 137why, one step back137 return ret;Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 138–173why, one step back138}139140/**141 * Escape special characters in the given string of html.142 *143 * @param {String} html144 * @return {String}145 * @api private146 */147148exports.escape = function(html) {149 return String(html)150 .replace(/&/g, '&')151 .replace(/"/g, '"')152 .replace(/</g, '<')153 .replace(/>/g, '>');154};155156/**157 * Normalize the given path string,158 * returning a regular expression.159 *160 * An empty array should be passed,161 * which will contain the placeholder162 * key names. For example "/user/:id" will163 * then contain ["id"].164 *165 * @param {String|RegExp|Array} path166 * @param {Array} keys167 * @param {Boolean} sensitive168 * @param {Boolean} strict169 * @return {RegExp}170 * @api private171 */172173exports.pathRegexp = function(path, keys, sensitive, strict) {refactorTJ Holowaychukbefore adoptionf8a33d1
line 174why, one step back174 if (toString.call(path) == '[object RegExp]') return path;Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 175–193why, one step back175 if (Array.isArray(path)) path = '(' + path.join('|') + ')';176 path = path177 .concat(strict ? '' : '/?')178 .replace(/\/\(/g, '(?:/')179 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){180 keys.push({ name: key, optional: !! optional });181 slash = slash || '';182 return ''183 + (optional ? '' : slash)184 + '(?:'185 + (optional ? slash : '')186 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'187 + (optional || '')188 + (star ? '(/*)?' : '');189 })190 .replace(/([\/.])/g, '\\$1')191 .replace(/\*/g, '(.*)');192 return new RegExp('^' + path + '$', sensitive ? '' : 'i');193}