Skip to content
Myr

utils.js

This file

Path

lib/utils.js

184 lines · 3.4 KiB · JavaScript

Last changed by

mergedTj Holowaychukbefore adoption547d18c

History of this file

Take it

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

misc refactoringTj Holowaychukbefore adoption294bc8c

lines 1–2why, one step back
1
2/*!

new ViewTj Holowaychukbefore adoptione1da383

line 3why, one step back
3 * Express - utils

misc refactoringTj Holowaychukbefore adoption294bc8c

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

misc refactoringTj Holowaychukbefore adoption294bc8c

lines 15–67why, 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/**49 * Parse mini markdown implementation.50 * The following conversions are supported,51 * primarily for the "flash" middleware:52 *53 * _foo_ or *foo* become <em>foo</em>54 * __foo__ or **foo** become <strong>foo</strong>55 * [A](B) becomes <a href="B">A</a>56 *57 * @param {String} str58 * @return {String}59 * @api private60 */6162exports.miniMarkdown = function(str){63 return String(str)64 .replace(/(__|\*\*)(.*?)\1/g, '<strong>$2</strong>')65 .replace(/(_|\*)(.*?)\1/g, '<em>$2</em>')66 .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');67};

mergedTj Holowaychukbefore adoption547d18c

lines 68–168why, one step back
68
69/**70 * Check if `type` is acceptable based on71 * the given `str`.72 *73 * @param {String} type74 * @param {String} str75 * @return {Boolean}76 * @api public77 */7879exports.accepts = function(type, str){80 // accept anything when Accept is not present81 if (!str) return true;8283 // resolve mime84 if (!~type.indexOf('/')) type = mime.lookup(type);8586 // split type/subtype87 type = type.split('/');8889 // parse90 var accepted = exports.parseAccept(str)91 , len = accepted.length92 , obj93 , ok;9495 for (var i = 0; i < len; ++i) {96 obj = accepted[i];97 ok = (type[0] == obj.type || '*' == obj.type)98 && (type[1] == obj.subtype || '*' == obj.subtype);99 if (ok) return true;100 }101102 return false;103};104105/**106 * Parse accept `str`, returning107 * an array objects containing108 * `.type` and `.subtype` along109 * with the values provided by110 * `parseQuality()`.111 *112 * @param {Type} name113 * @return {Type}114 * @api public115 */116117exports.parseAccept = function(str){118 return exports119 .parseQuality(str)120 .map(function(obj){121 var parts = obj.value.split('/');122 obj.type = parts[0];123 obj.subtype = parts[1];124 return obj;125 });126};127128/**129 * Parse quality `str`, returning an130 * array of objects with `.value` and131 * `.quality`.132 *133 * @param {Type} name134 * @return {Type}135 * @api public136 */137138exports.parseQuality = function(str){139 return str140 .split(/ *, */)141 .map(quality)142 .filter(function(obj){143 return obj.quality;144 })145 .sort(function(a, b){146 return b.quality - a.quality;147 });148};149150/**151 * Parse quality `str` returning an152 * object with `.value` and `.quality`.153 *154 * @param {String} str155 * @return {Object}156 * @api private157 */158159function quality(str) {160 var parts = str.split(/ *; */)161 , val = parts[0];162163 var q = parts[1]164 ? parseFloat(parts[1].split(/ *= */)[1])165 : 1;166167 return { value: val, quality: q };168}

misc refactoringTj Holowaychukbefore adoption294bc8c

lines 169–184why, one step back
169
170/**171 * Escape special characters in the given string of html.172 *173 * @param {String} html174 * @return {String}175 * @api private176 */177178exports.escape = function(html) {179 return String(html)180 .replace(/&/g, '&amp;')181 .replace(/"/g, '&quot;')182 .replace(/</g, '&lt;')183 .replace(/>/g, '&gt;');184};