Skip to content
Myr

utils.js

This file

Path

lib/utils.js

282 lines · 5.6 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 812a470 point at it.

docsTJ Holowaychukbefore adoption812a470

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;

docsTJ Holowaychukbefore adoption812a470

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

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

lines 9–41why, 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 locals.use = function(fn){27 if (3 == fn.length) {28 obj.viewCallbacks.push(fn);29 } else {30 obj.viewCallbacks.push(function(req, res, done){31 fn(req, res);32 done();33 });34 }35 return obj;36 };3738 return locals;39};4041/**

docsTJ Holowaychukbefore adoption812a470

lines 42–75why, one step back
42 * Check if `path` looks absolute.
43 *44 * @param {String} path45 * @return {Boolean}46 * @api private47 */4849exports.isAbsolute = function(path){50 if ('/' == path[0]) return true;51 if (':' == path[1] && '\\' == path[2]) return true;52};5354/**55 * Flatten the given `arr`.56 *57 * @param {Array} arr58 * @return {Array}59 * @api private60 */6162exports.flatten = function(arr, ret){63 var ret = ret || []64 , len = arr.length;65 for (var i = 0; i < len; ++i) {66 if (Array.isArray(arr[i])) {67 exports.flatten(arr[i], ret);68 } else {69 ret.push(arr[i]);70 }71 }72 return ret;73};7475/**

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

line 76why, one step back
76 * Normalize the given `type`, for example "html" becomes "text/html".

docsTJ Holowaychukbefore adoption812a470

lines 77–78why, one step back
77 *
78 * @param {String} type

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

lines 79–110why, one step back
79 * @return {String}
80 * @api private81 */8283exports.normalizeType = function(type){84 return ~type.indexOf('/') ? type : mime.lookup(type);85};8687/**88 * Normalize `types`, for example "html" becomes "text/html".89 *90 * @param {Array} types91 * @return {Array}92 * @api private93 */9495exports.normalizeTypes = function(types){96 var ret = [];9798 for (var i = 0; i < types.length; ++i) {99 ret.push(~types[i].indexOf('/')100 ? types[i]101 : mime.lookup(types[i]));102 }103104 return ret;105};106107/**108 * Return the acceptable type in `types`, if any.109 *110 * @param {Array} types

docsTJ Holowaychukbefore adoption812a470

line 111why, one step back
111 * @param {String} str

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

line 112why, one step back
112 * @return {String}

docsTJ Holowaychukbefore adoption812a470

lines 113–115why, one step back
113 * @api private
114 */115

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

lines 116–143why, one step back
116exports.acceptsArray = function(types, str){
117 // accept anything when Accept is not present118 if (!str) return types[0];119120 // parse121 var accepted = exports.parseAccept(str)122 , normalized = exports.normalizeTypes(types)123 , len = accepted.length;124125 for (var i = 0; i < len; ++i) {126 for (var j = 0, jlen = types.length; j < jlen; ++j) {127 if (exports.accept(normalized[j].split('/'), accepted[i])) {128 return types[j];129 }130 }131 }132};133134/**135 * Check if `type(s)` are acceptable based on136 * the given `str`.137 *138 * @param {String|Array} type(s)139 * @param {String} str140 * @return {Boolean|String}141 * @api private142 */143

docsTJ Holowaychukbefore adoption812a470

line 144why, one step back
144exports.accepts = function(type, str){

Added `req.accepts()` comma-delimited string supportTJ Holowaychukbefore adoptiond6d16b7

line 145why, one step back
145 if ('string' == typeof type) type = type.split(/ *, */);

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 146why, one step back
146 return exports.acceptsArray(type, str);

docsTJ Holowaychukbefore adoption812a470

lines 147–242why, one step back
147};
148149/**150 * Check if `type` array is acceptable for `other`.151 *152 * @param {Array} type153 * @param {Object} other154 * @return {Boolean}155 * @api private156 */157158exports.accept = function(type, other){159 return (type[0] == other.type || '*' == other.type)160 && (type[1] == other.subtype || '*' == other.subtype);161};162163/**164 * Parse accept `str`, returning165 * an array objects containing166 * `.type` and `.subtype` along167 * with the values provided by168 * `parseQuality()`.169 *170 * @param {Type} name171 * @return {Type}172 * @api private173 */174175exports.parseAccept = function(str){176 return exports177 .parseQuality(str)178 .map(function(obj){179 var parts = obj.value.split('/');180 obj.type = parts[0];181 obj.subtype = parts[1];182 return obj;183 });184};185186/**187 * Parse quality `str`, returning an188 * array of objects with `.value` and189 * `.quality`.190 *191 * @param {Type} name192 * @return {Type}193 * @api private194 */195196exports.parseQuality = function(str){197 return str198 .split(/ *, */)199 .map(quality)200 .filter(function(obj){201 return obj.quality;202 })203 .sort(function(a, b){204 return b.quality - a.quality;205 });206};207208/**209 * Parse quality `str` returning an210 * object with `.value` and `.quality`.211 *212 * @param {String} str213 * @return {Object}214 * @api private215 */216217function quality(str) {218 var parts = str.split(/ *; */)219 , val = parts[0];220221 var q = parts[1]222 ? parseFloat(parts[1].split(/ *= */)[1])223 : 1;224225 return { value: val, quality: q };226}227228/**229 * Escape special characters in the given string of html.230 *231 * @param {String} html232 * @return {String}233 * @api private234 */235236exports.escape = function(html) {237 return String(html)238 .replace(/&/g, '&amp;')239 .replace(/"/g, '&quot;')240 .replace(/</g, '&lt;')241 .replace(/>/g, '&gt;');242};

utils: added pathRegexp()TJ Holowaychukbefore adoption0377540

lines 243–262why, one step back
243
244/**245 * Normalize the given path string,246 * returning a regular expression.247 *248 * An empty array should be passed,249 * which will contain the placeholder250 * key names. For example "/user/:id" will251 * then contain ["id"].252 *253 * @param {String|RegExp|Array} path254 * @param {Array} keys255 * @param {Boolean} sensitive256 * @param {Boolean} strict257 * @return {RegExp}258 * @api private259 */260261exports.pathRegexp = function(path, keys, sensitive, strict) {262 if (path instanceof RegExp) return path;

ocdTJ Holowaychukbefore adoption53f1ffb

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

utils: added pathRegexp()TJ Holowaychukbefore adoption0377540

lines 264–266why, one step back
264 path = path
265 .concat(strict ? '' : '/?')266 .replace(/\/\(/g, '(?:/')

Added + support to the routerTJ Holowaychukbefore adoption6aaa7dc

line 267why, one step back
267 .replace(/\+/g, '__plus__')

utils: added pathRegexp()TJ Holowaychukbefore adoption0377540

lines 268–278why, one step back
268 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
269 keys.push({ name: key, optional: !! optional });270 slash = slash || '';271 return ''272 + (optional ? '' : slash)273 + '(?:'274 + (optional ? slash : '')275 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'276 + (optional || '');277 })278 .replace(/([\/.])/g, '\\$1')

Added + support to the routerTJ Holowaychukbefore adoption6aaa7dc

line 279why, one step back
279 .replace(/__plus__/g, '(.+)')

utils: added pathRegexp()TJ Holowaychukbefore adoption0377540

lines 280–282why, one step back
280 .replace(/\*/g, '(.*)');
281 return new RegExp('^' + path + '$', sensitive ? '' : 'i');282}