utils.js
This file
Path
lib/utils.js
157 lines · 2.9 KiB · JavaScript
Last changed by
Added union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
History of this fileTake it
moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 1–66why, 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/**Added union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
lines 67–89why, one step back67 * Merge object `b` with `a` giving precedence to68 * values in object `a`.69 *70 * @param {Object} a71 * @param {Object} b72 * @return {Object} a73 * @api private74 */7576exports.union = function(a, b){77 var keys = Object.keys(b)78 , len = keys.length79 , key;80 for (var i = 0; i < len; ++i) {81 key = keys[i];82 if (!a.hasOwnProperty(key)) {83 a[key] = b[key];84 }85 }86 return a;87};8889/**moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5
lines 90–157why, one step back90 * Parse mini markdown implementation.91 * The following conversions are supported,92 * primarily for the "flash" middleware:93 *94 * _foo_ or *foo* become <em>foo</em>95 * __foo__ or **foo** become <strong>foo</strong>96 * [A](B) becomes <a href="B">A</a>97 *98 * @param {String} str99 * @return {String}100 * @api private101 */102103exports.miniMarkdown = function(str){104 return String(str)105 .replace(/(__|\*\*)(.*?)\1/g, '<strong>$2</strong>')106 .replace(/(_|\*)(.*?)\1/g, '<em>$2</em>')107 .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');108};109110/**111 * Escape special characters in the given string of html.112 *113 * @param {String} html114 * @return {String}115 * @api private116 */117118exports.htmlEscape = function(html) {119 return String(html)120 .replace(/&/g, '&')121 .replace(/"/g, '"')122 .replace(/</g, '<')123 .replace(/>/g, '>');124};125126/**127 * Parse "Range" header `str` relative to the given file `size`.128 *129 * @param {Number} size130 * @param {String} str131 * @return {Array}132 * @api private133 */134135exports.parseRange = function(size, str){136 var valid = true;137 var arr = str.substr(6).split(',').map(function(range){138 var range = range.split('-')139 , start = parseInt(range[0], 10)140 , end = parseInt(range[1], 10);141142 // -500143 if (isNaN(start)) {144 start = size - end;145 end = size - 1;146 // 500-147 } else if (isNaN(end)) {148 end = size - 1;149 }150151 // Invalid152 if (isNaN(start) || isNaN(end) || start > end) valid = false;153154 return { start: start, end: end };155 });156 return valid ? arr : undefined;157};