utils.js
This file
Path
lib/utils.js
163 lines · 2.9 KiB · JavaScript
Last changed by
Removed `req.notify()`Tj Holowaychukbefore adoption28f32b9
History of this fileTake it
The walk stopped after 200 commits: lines older than 8111a19 point at it.
Removed sass.js support from express(1)Tj Holowaychukbefore adoption8111a19
lines 1–8why, one step back12/*!3 * Express - utils4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**mergedTj Holowaychukbefore adoption547d18c
lines 9–14why, one step back9 * Module dependencies.10 */1112var mime = require('mime');1314/**Removed sass.js support from express(1)Tj Holowaychukbefore adoption8111a19
lines 15–48why, one step back15 * 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/**mergedTj Holowaychukbefore adoption547d18c
lines 49–147why, one step back49 * Check if `type` is acceptable based on50 * the given `str`.51 *52 * @param {String} type53 * @param {String} str54 * @return {Boolean}55 * @api public56 */5758exports.accepts = function(type, str){59 // accept anything when Accept is not present60 if (!str) return true;6162 // resolve mime63 if (!~type.indexOf('/')) type = mime.lookup(type);6465 // split type/subtype66 type = type.split('/');6768 // parse69 var accepted = exports.parseAccept(str)70 , len = accepted.length71 , obj72 , ok;7374 for (var i = 0; i < len; ++i) {75 obj = accepted[i];76 ok = (type[0] == obj.type || '*' == obj.type)77 && (type[1] == obj.subtype || '*' == obj.subtype);78 if (ok) return true;79 }8081 return false;82};8384/**85 * Parse accept `str`, returning86 * an array objects containing87 * `.type` and `.subtype` along88 * with the values provided by89 * `parseQuality()`.90 *91 * @param {Type} name92 * @return {Type}93 * @api public94 */9596exports.parseAccept = function(str){97 return exports98 .parseQuality(str)99 .map(function(obj){100 var parts = obj.value.split('/');101 obj.type = parts[0];102 obj.subtype = parts[1];103 return obj;104 });105};106107/**108 * Parse quality `str`, returning an109 * array of objects with `.value` and110 * `.quality`.111 *112 * @param {Type} name113 * @return {Type}114 * @api public115 */116117exports.parseQuality = function(str){118 return str119 .split(/ *, */)120 .map(quality)121 .filter(function(obj){122 return obj.quality;123 })124 .sort(function(a, b){125 return b.quality - a.quality;126 });127};128129/**130 * Parse quality `str` returning an131 * object with `.value` and `.quality`.132 *133 * @param {String} str134 * @return {Object}135 * @api private136 */137138function quality(str) {139 var parts = str.split(/ *; */)140 , val = parts[0];141142 var q = parts[1]143 ? parseFloat(parts[1].split(/ *= */)[1])144 : 1;145146 return { value: val, quality: q };147}Removed sass.js support from express(1)Tj Holowaychukbefore adoption8111a19
lines 148–163why, one step back148149/**150 * Escape special characters in the given string of html.151 *152 * @param {String} html153 * @return {String}154 * @api private155 */156157exports.escape = function(html) {158 return String(html)159 .replace(/&/g, '&')160 .replace(/"/g, '"')161 .replace(/</g, '<')162 .replace(/>/g, '>');163};