Skip to content
Myr

utils.js

This file

Path

lib/utils.js

269 lines · 5.4 KiB · JavaScript

Last changed by

remove app.locals.use and res.locals.useTJ Holowaychukbefore adoption4f7c4d1

History of this file

Take it

The walk stopped after 200 commits: lines older than 0ddc40a point at it.

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption0ddc40a

lines 1–5why, one step back
1
2/**3 * Module dependencies.4 */5

Fixed: expose connects mime module. Cloases #1165TJ Holowaychukbefore adoption8956731

line 6why, one step back
6var mime = require('connect').mime;

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption0ddc40a

lines 7–8why, one step back
7
8/**

Added res.locals.use(). Closes #1120TJ Holowaychukbefore adoptiondd33ef2

lines 9–29why, one step back
9 * Make `locals()` bound to the given `obj`.
10 * 11 * This is used for `app.locals` and `res.locals`. 12 *13 * @param {Object} obj14 * @return {Function}15 * @api private16 */1718exports.locals = function(obj){19 obj.viewCallbacks = obj.viewCallbacks || [];2021 function locals(obj){22 for (var key in obj) locals[key] = obj[key];23 return obj;24 };2526 return locals;27};2829/**

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption0ddc40a

lines 30–250why, one step back
30 * Check if `path` looks absolute.
31 *32 * @param {String} path33 * @return {Boolean}34 * @api private35 */3637exports.isAbsolute = function(path){38 if ('/' == path[0]) return true;39 if (':' == path[1] && '\\' == path[2]) return true;40};4142/**43 * Flatten the given `arr`.44 *45 * @param {Array} arr46 * @return {Array}47 * @api private48 */4950exports.flatten = function(arr, ret){51 var ret = ret || []52 , len = arr.length;53 for (var i = 0; i < len; ++i) {54 if (Array.isArray(arr[i])) {55 exports.flatten(arr[i], ret);56 } else {57 ret.push(arr[i]);58 }59 }60 return ret;61};6263/**64 * Normalize the given `type`, for example "html" becomes "text/html".65 *66 * @param {String} type67 * @return {String}68 * @api private69 */7071exports.normalizeType = function(type){72 return ~type.indexOf('/') ? type : mime.lookup(type);73};7475/**76 * Normalize `types`, for example "html" becomes "text/html".77 *78 * @param {Array} types79 * @return {Array}80 * @api private81 */8283exports.normalizeTypes = function(types){84 var ret = [];8586 for (var i = 0; i < types.length; ++i) {87 ret.push(~types[i].indexOf('/')88 ? types[i]89 : mime.lookup(types[i]));90 }9192 return ret;93};9495/**96 * Return the acceptable type in `types`, if any.97 *98 * @param {Array} types99 * @param {String} str100 * @return {String}101 * @api private102 */103104exports.acceptsArray = function(types, str){105 // accept anything when Accept is not present106 if (!str) return types[0];107108 // parse109 var accepted = exports.parseAccept(str)110 , normalized = exports.normalizeTypes(types)111 , len = accepted.length;112113 for (var i = 0; i < len; ++i) {114 for (var j = 0, jlen = types.length; j < jlen; ++j) {115 if (exports.accept(normalized[j].split('/'), accepted[i])) {116 return types[j];117 }118 }119 }120};121122/**123 * Check if `type(s)` are acceptable based on124 * the given `str`.125 *126 * @param {String|Array} type(s)127 * @param {String} str128 * @return {Boolean|String}129 * @api private130 */131132exports.accepts = function(type, str){133 if ('string' == typeof type) type = type.split(/ *, */);134 return exports.acceptsArray(type, str);135};136137/**138 * Check if `type` array is acceptable for `other`.139 *140 * @param {Array} type141 * @param {Object} other142 * @return {Boolean}143 * @api private144 */145146exports.accept = function(type, other){147 return (type[0] == other.type || '*' == other.type)148 && (type[1] == other.subtype || '*' == other.subtype);149};150151/**152 * Parse accept `str`, returning153 * an array objects containing154 * `.type` and `.subtype` along155 * with the values provided by156 * `parseQuality()`.157 *158 * @param {Type} name159 * @return {Type}160 * @api private161 */162163exports.parseAccept = function(str){164 return exports165 .parseQuality(str)166 .map(function(obj){167 var parts = obj.value.split('/');168 obj.type = parts[0];169 obj.subtype = parts[1];170 return obj;171 });172};173174/**175 * Parse quality `str`, returning an176 * array of objects with `.value` and177 * `.quality`.178 *179 * @param {Type} name180 * @return {Type}181 * @api private182 */183184exports.parseQuality = function(str){185 return str186 .split(/ *, */)187 .map(quality)188 .filter(function(obj){189 return obj.quality;190 })191 .sort(function(a, b){192 return b.quality - a.quality;193 });194};195196/**197 * Parse quality `str` returning an198 * object with `.value` and `.quality`.199 *200 * @param {String} str201 * @return {Object}202 * @api private203 */204205function quality(str) {206 var parts = str.split(/ *; */)207 , val = parts[0];208209 var q = parts[1]210 ? parseFloat(parts[1].split(/ *= */)[1])211 : 1;212213 return { value: val, quality: q };214}215216/**217 * Escape special characters in the given string of html.218 *219 * @param {String} html220 * @return {String}221 * @api private222 */223224exports.escape = function(html) {225 return String(html)226 .replace(/&/g, '&amp;')227 .replace(/"/g, '&quot;')228 .replace(/</g, '&lt;')229 .replace(/>/g, '&gt;');230};231232/**233 * Normalize the given path string,234 * returning a regular expression.235 *236 * An empty array should be passed,237 * which will contain the placeholder238 * key names. For example "/user/:id" will239 * then contain ["id"].240 *241 * @param {String|RegExp|Array} path242 * @param {Array} keys243 * @param {Boolean} sensitive244 * @param {Boolean} strict245 * @return {RegExp}246 * @api private247 */248249exports.pathRegexp = function(path, keys, sensitive, strict) {250 if (path instanceof RegExp) return path;

ocdTJ Holowaychukbefore adoption53f1ffb

line 251why, one step back
251 if (Array.isArray(path)) path = '(' + path.join('|') + ')';

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption0ddc40a

lines 252–254why, one step back
252 path = path
253 .concat(strict ? '' : '/?')254 .replace(/\/\(/g, '(?:/')

* immediately after :param would become not greedyriadhbefore adoption73bed61

line 255why, one step back
255 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption0ddc40a

lines 256–262why, one step back
256 keys.push({ name: key, optional: !! optional });
257 slash = slash || '';258 return ''259 + (optional ? '' : slash)260 + '(?:'261 + (optional ? slash : '')262 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'

* immediately after :param would become not greedyriadhbefore adoption73bed61

line 263why, one step back
263 + (optional || '')

tests for issues 1115 fix for utilsriadhbefore adoption3827f5e

line 264why, one step back
264 + (star ? '(/*)?' : '');

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption0ddc40a

lines 265–269why, one step back
265 })
266 .replace(/([\/.])/g, '\\$1')267 .replace(/\*/g, '(.*)');268 return new RegExp('^' + path + '$', sensitive ? '' : 'i');269}