utils.js
This file
Path
lib/utils.js
150 lines · 2.7 KiB · JavaScript
Last changed by
moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
History of this fileTake it
moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 1–150why, one step back12/*!3 * Express - Utils4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var path = require('path')13 , basename = path.basename14 , dirname = path.dirname15 , extname = path.extname;1617/**18 * Memory cache.19 */2021var cache = {22 basename: {}23 , dirname: {}24 , extname: {}25};2627/**28 * Cached basename.29 *30 * @param {String} path31 * @return {String}32 * @api private33 */3435exports.basename = function(path){36 return cache.basename[path]37 || (cache.basename[path] = basename(path));38};3940/**41 * Cached dirname.42 *43 * @param {String} path44 * @return {String}45 * @api private46 */4748exports.dirname = function(path){49 return cache.dirname[path]50 || (cache.dirname[path] = dirname(path));51};5253/**54 * Cached extname.55 *56 * @param {String} path57 * @return {String}58 * @api private59 */6061exports.extname = function(path){62 return cache.extname[path]63 || (cache.extname[path] = extname(path));64};6566/**67 * Parse mini markdown implementation.68 * The following conversions are supported,69 * primarily for the "flash" middleware:70 *71 * _foo_ or *foo* become <em>foo</em>72 * __foo__ or **foo** become <strong>foo</strong>73 * [A](B) becomes <a href="B">A</a>74 *75 * @param {String} str76 * @return {String}77 * @api private78 */7980exports.miniMarkdown = function(str){81 return String(str)82 .replace(/(__|\*\*)(.*?)\1/g, '<strong>$2</strong>')83 .replace(/(_|\*)(.*?)\1/g, '<em>$2</em>')84 .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');85};8687/**88 * Escape special characters in the given string of html.89 *90 * @param {String} html91 * @return {String}92 * @api private93 */9495exports.htmlEscape = function(html) {96 return String(html)97 .replace(/&/g, '&')98 .replace(/"/g, '"')99 .replace(/</g, '<')100 .replace(/>/g, '>');101};102103/**104 * Clone `obj`.105 *106 * @param {Object} obj107 * @return {Object}108 * @api private109 */110111exports.clone = function(obj){112 var ret = {};113 for (var key in obj) {114 ret[key] = obj[key];115 }116 return ret;117};118119/**120 * Parse "Range" header `str` relative to the given file `size`.121 *122 * @param {Number} size123 * @param {String} str124 * @return {Array}125 * @api private126 */127128exports.parseRange = function(size, str){129 var valid = true;130 var arr = str.substr(6).split(',').map(function(range){131 var range = range.split('-')132 , start = parseInt(range[0], 10)133 , end = parseInt(range[1], 10);134135 // -500136 if (isNaN(start)) {137 start = size - end;138 end = size - 1;139 // 500-140 } else if (isNaN(end)) {141 end = size - 1;142 }143144 // Invalid145 if (isNaN(start) || isNaN(end) || start > end) valid = false;146147 return { start: start, end: end };148 });149 return valid ? arr : undefined;150};