Skip to content
Myr

utils.js

This file

Path

lib/utils.js

215 lines · 3.9 KiB · JavaScript

Last changed by

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

History of this file

Take it

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

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

lines 1–8why, one step back
1
2/*!3 * Express - utils4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**

mergedTj Holowaychukbefore adoption547d18c

lines 9–14why, one step back
9 * Module dependencies.
10 */1112var mime = require('mime');1314/**

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

lines 15–48why, one step back
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 back
49 * Normalize the given `type`, for example "html" becomes "text/html".

mergedTj Holowaychukbefore adoption547d18c

lines 50–51why, one step back
50 *
51 * @param {String} type

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

lines 52–83why, one step back
52 * @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} types

mergedTj Holowaychukbefore adoption547d18c

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

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

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

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

line 86why, one step back
86 * @api private

mergedTj Holowaychukbefore adoption547d18c

lines 87–88why, one step back
87 */
88

Added array support. Closes #1053TJ Holowaychukbefore adoption298899d

lines 89–116why, one step back
89exports.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 */116

mergedTj Holowaychukbefore adoption547d18c

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

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

lines 118–119why, one step back
118 if (!Array.isArray(type)) type = [type];
119 return exports.acceptsArray(type, str);

mergedTj Holowaychukbefore adoption547d18c

lines 120–122why, one step back
120};
121122/**

Added `res.respondTo(obj)`TJ Holowaychukbefore adoptionb565e25

lines 123–136why, one step back
123 * 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/**

mergedTj Holowaychukbefore adoption547d18c

lines 137–144why, one step back
137 * Parse accept `str`, returning
138 * 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 back
145 * @api private

mergedTj Holowaychukbefore adoption547d18c

lines 146–165why, one step back
146 */
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 back
166 * @api private

mergedTj Holowaychukbefore adoption547d18c

lines 167–199why, one step back
167 */
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}

Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7

lines 200–215why, one step back
200
201/**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, '&amp;')212 .replace(/"/g, '&quot;')213 .replace(/</g, '&lt;')214 .replace(/>/g, '&gt;');215};