Skip to content
Myr

utils.js

This file

Path

lib/utils.js

159 lines · 2.9 KiB · JavaScript

Last changed by

Fixed partial() issue where the parent locals clobber those given directlyTj Holowaychukbefore adoption1fc520b

History of this file

Take it

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

Updated connect submoduleTj Holowaychukbefore adoptiondb8b288

lines 1–66why, one step back
1
2/*!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–76why, one step back
67 * Merge object `b` with `a` giving precedence to
68 * values in object `a`.69 *70 * @param {Object} a71 * @param {Object} b72 * @return {Object} a73 * @api private74 */7576exports.union = function(a, b){

Fixed partial() issue where the parent locals clobber those given directlyTj Holowaychukbefore adoption1fc520b

lines 77–85why, one step back
77 if (a && b) {
78 var keys = Object.keys(b)79 , len = keys.length80 , key;81 for (var i = 0; i < len; ++i) {82 key = keys[i];83 if (!a.hasOwnProperty(key)) {84 a[key] = b[key];85 }

Added union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758

lines 86–91why, one step back
86 }
87 }88 return a;89};9091/**

Updated connect submoduleTj Holowaychukbefore adoptiondb8b288

lines 92–159why, one step back
92 * Parse mini markdown implementation.
93 * The following conversions are supported,94 * primarily for the "flash" middleware:95 *96 * _foo_ or *foo* become <em>foo</em>97 * __foo__ or **foo** become <strong>foo</strong>98 * [A](B) becomes <a href="B">A</a>99 *100 * @param {String} str101 * @return {String}102 * @api private103 */104105exports.miniMarkdown = function(str){106 return String(str)107 .replace(/(__|\*\*)(.*?)\1/g, '<strong>$2</strong>')108 .replace(/(_|\*)(.*?)\1/g, '<em>$2</em>')109 .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');110};111112/**113 * Escape special characters in the given string of html.114 *115 * @param {String} html116 * @return {String}117 * @api private118 */119120exports.htmlEscape = function(html) {121 return String(html)122 .replace(/&/g, '&amp;')123 .replace(/"/g, '&quot;')124 .replace(/</g, '&lt;')125 .replace(/>/g, '&gt;');126};127128/**129 * Parse "Range" header `str` relative to the given file `size`.130 *131 * @param {Number} size132 * @param {String} str133 * @return {Array}134 * @api private135 */136137exports.parseRange = function(size, str){138 var valid = true;139 var arr = str.substr(6).split(',').map(function(range){140 var range = range.split('-')141 , start = parseInt(range[0], 10)142 , end = parseInt(range[1], 10);143144 // -500145 if (isNaN(start)) {146 start = size - end;147 end = size - 1;148 // 500-149 } else if (isNaN(end)) {150 end = size - 1;151 }152153 // Invalid154 if (isNaN(start) || isNaN(end) || start > end) valid = false;155156 return { start: start, end: end };157 });158 return valid ? arr : undefined;159};