utils.js
This file
Path
lib/utils.js
253 lines · 5.1 KiB · JavaScript
Last changed by
ocdTJ Holowaychukbefore adoption53f1ffb
History of this fileTake it
The walk stopped after 200 commits: lines older than 85ea5f6 point at it.
Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
lines 1–3why, one step back12/*!3 * Express - utilsupdate headersTJ Holowaychukbefore adoptiona376980
line 4why, one step back4 * Copyright(c) 2010-2012 TJ Holowaychuk <tj@vision-media.ca>Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
lines 5–48why, one step back5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var mime = require('mime');1314/**15 * Check if `path` looks absolute.16 *17 * @param {String} path18 * @return {Boolean}19 * @api private20 */2122exports.isAbsolute = function(path){23 if ('/' == path[0]) return true;24 if (':' == path[1] && '\\' == path[2]) return true;25};2627/**28 * Flatten the given `arr`.29 *30 * @param {Array} arr31 * @return {Array}32 * @api private33 */3435exports.flatten = function(arr, ret){36 var ret = ret || []37 , len = arr.length;38 for (var i = 0; i < len; ++i) {39 if (Array.isArray(arr[i])) {40 exports.flatten(arr[i], ret);41 } else {42 ret.push(arr[i]);43 }44 }45 return ret;46};4748/**Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 49why, one step back49 * Normalize the given `type`, for example "html" becomes "text/html".Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
lines 50–51why, one step back50 *51 * @param {String} typeAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
lines 52–83why, one step back52 * @return {String}53 * @api private54 */5556exports.normalizeType = function(type){57 return ~type.indexOf('/') ? type : mime.lookup(type);58};5960/**61 * Normalize `types`, for example "html" becomes "text/html".62 *63 * @param {Array} types64 * @return {Array}65 * @api private66 */6768exports.normalizeTypes = function(types){69 var ret = [];7071 for (var i = 0; i < types.length; ++i) {72 ret.push(~types[i].indexOf('/')73 ? types[i]74 : mime.lookup(types[i]));75 }7677 return ret;78};7980/**81 * Return the acceptable type in `types`, if any.82 *83 * @param {Array} typesMerge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
line 84why, one step back84 * @param {String} strAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 85why, one step back85 * @return {String}Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 86why, one step back86 * @api privateMerge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
lines 87–88why, one step back87 */88Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
lines 89–116why, one step back89exports.acceptsArray = function(types, str){90 // accept anything when Accept is not present91 if (!str) return types[0];9293 // parse94 var accepted = exports.parseAccept(str)95 , normalized = exports.normalizeTypes(types)96 , len = accepted.length;9798 for (var i = 0; i < len; ++i) {99 for (var j = 0, jlen = types.length; j < jlen; ++j) {100 if (exports.accept(normalized[j].split('/'), accepted[i])) {101 return types[j];102 }103 }104 }105};106107/**108 * Check if `type(s)` are acceptable based on109 * the given `str`.110 *111 * @param {String|Array} type(s)112 * @param {String} str113 * @return {Boolean|String}114 * @api private115 */116Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
line 117why, one step back117exports.accepts = function(type, str){Added `req.accepts()` comma-delimited string supportTJ Holowaychukbefore adoptiond6d16b7
line 118why, one step back118 if ('string' == typeof type) type = type.split(/ *, */);refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 119why, one step back119 return exports.acceptsArray(type, str);Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
lines 120–122why, one step back120};121122/**Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
lines 123–136why, one step back123 * Check if `type` array is acceptable for `other`.124 *125 * @param {Array} type126 * @param {Object} other127 * @return {Boolean}128 * @api private129 */130131exports.accept = function(type, other){132 return (type[0] == other.type || '*' == other.type)133 && (type[1] == other.subtype || '*' == other.subtype);134};135136/**Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
lines 137–144why, one step back137 * Parse accept `str`, returning138 * an array objects containing139 * `.type` and `.subtype` along140 * with the values provided by141 * `parseQuality()`.142 *143 * @param {Type} name144 * @return {Type}Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 145why, one step back145 * @api privateMerge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
lines 146–165why, one step back146 */147148exports.parseAccept = function(str){149 return exports150 .parseQuality(str)151 .map(function(obj){152 var parts = obj.value.split('/');153 obj.type = parts[0];154 obj.subtype = parts[1];155 return obj;156 });157};158159/**160 * Parse quality `str`, returning an161 * array of objects with `.value` and162 * `.quality`.163 *164 * @param {Type} name165 * @return {Type}Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25
line 166why, one step back166 * @api privateMerge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption85ea5f6
lines 167–215why, one step back167 */168169exports.parseQuality = function(str){170 return str171 .split(/ *, */)172 .map(quality)173 .filter(function(obj){174 return obj.quality;175 })176 .sort(function(a, b){177 return b.quality - a.quality;178 });179};180181/**182 * Parse quality `str` returning an183 * object with `.value` and `.quality`.184 *185 * @param {String} str186 * @return {Object}187 * @api private188 */189190function quality(str) {191 var parts = str.split(/ *; */)192 , val = parts[0];193194 var q = parts[1]195 ? parseFloat(parts[1].split(/ *= */)[1])196 : 1;197198 return { value: val, quality: q };199}200201/**202 * Escape special characters in the given string of html.203 *204 * @param {String} html205 * @return {String}206 * @api private207 */208209exports.escape = function(html) {210 return String(html)211 .replace(/&/g, '&')212 .replace(/"/g, '"')213 .replace(/</g, '<')214 .replace(/>/g, '>');215};utils: added pathRegexp()TJ Holowaychukbefore adoption0377540
lines 216–235why, one step back216217/**218 * Normalize the given path string,219 * returning a regular expression.220 *221 * An empty array should be passed,222 * which will contain the placeholder223 * key names. For example "/user/:id" will224 * then contain ["id"].225 *226 * @param {String|RegExp|Array} path227 * @param {Array} keys228 * @param {Boolean} sensitive229 * @param {Boolean} strict230 * @return {RegExp}231 * @api private232 */233234exports.pathRegexp = function(path, keys, sensitive, strict) {235 if (path instanceof RegExp) return path;ocdTJ Holowaychukbefore adoption53f1ffb
line 236why, one step back236 if (Array.isArray(path)) path = '(' + path.join('|') + ')';utils: added pathRegexp()TJ Holowaychukbefore adoption0377540
lines 237–253why, one step back237 path = path238 .concat(strict ? '' : '/?')239 .replace(/\/\(/g, '(?:/')240 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){241 keys.push({ name: key, optional: !! optional });242 slash = slash || '';243 return ''244 + (optional ? '' : slash)245 + '(?:'246 + (optional ? slash : '')247 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'248 + (optional || '');249 })250 .replace(/([\/.])/g, '\\$1')251 .replace(/\*/g, '(.*)');252 return new RegExp('^' + path + '$', sensitive ? '' : 'i');253}