utils.js
This file
Path
lib/utils.js
122 lines · 2.4 KiB · JavaScript
Last changed by
Merge branch 'feature/router'Tj Holowaychukbefore adoption1d2dd2a
History of this fileTake it
The walk stopped after 200 commits: lines older than e85752c point at it.
Add haml back in docsTj Holowaychukbefore adoptione85752c
lines 1–33why, one step back12/*!3 * Express - Utils4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Merge object `b` with `a` giving precedence to10 * values in object `a`.11 *12 * @param {Object} a13 * @param {Object} b14 * @return {Object} a15 * @api private16 */1718exports.union = function(a, b){19 if (a && b) {20 var keys = Object.keys(b)21 , len = keys.length22 , key;23 for (var i = 0; i < len; ++i) {24 key = keys[i];25 if (!a.hasOwnProperty(key)) {26 a[key] = b[key];27 }28 }29 }30 return a;31};3233/**Merge branch 'feature/router'Tj Holowaychukbefore adoption1d2dd2a
lines 34–54why, one step back34 * Flatten the given `arr`.35 *36 * @param {Array} arr37 * @return {Array}38 * @api private39 */4041exports.flatten = function(arr, ret){42 var ret = ret || []43 , len = arr.length;44 for (var i = 0; i < len; ++i) {45 if (Array.isArray(arr[i])) {46 exports.flatten(arr[i], ret);47 } else {48 ret.push(arr[i]);49 }50 }51 return ret;52};5354/**Add haml back in docsTj Holowaychukbefore adoptione85752c
lines 55–82why, one step back55 * Parse mini markdown implementation.56 * The following conversions are supported,57 * primarily for the "flash" middleware:58 *59 * _foo_ or *foo* become <em>foo</em>60 * __foo__ or **foo** become <strong>foo</strong>61 * [A](B) becomes <a href="B">A</a>62 *63 * @param {String} str64 * @return {String}65 * @api private66 */6768exports.miniMarkdown = function(str){69 return String(str)70 .replace(/(__|\*\*)(.*?)\1/g, '<strong>$2</strong>')71 .replace(/(_|\*)(.*?)\1/g, '<em>$2</em>')72 .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');73};7475/**76 * Escape special characters in the given string of html.77 *78 * @param {String} html79 * @return {String}80 * @api private81 */82Merge branch 'feature/router'Tj Holowaychukbefore adoption1d2dd2a
line 83why, one step back83exports.escape = function(html) {Add haml back in docsTj Holowaychukbefore adoptione85752c
lines 84–122why, one step back84 return String(html)85 .replace(/&/g, '&')86 .replace(/"/g, '"')87 .replace(/</g, '<')88 .replace(/>/g, '>');89};9091/**92 * Parse "Range" header `str` relative to the given file `size`.93 *94 * @param {Number} size95 * @param {String} str96 * @return {Array}97 * @api private98 */99100exports.parseRange = function(size, str){101 var valid = true;102 var arr = str.substr(6).split(',').map(function(range){103 var range = range.split('-')104 , start = parseInt(range[0], 10)105 , end = parseInt(range[1], 10);106107 // -500108 if (isNaN(start)) {109 start = size - end;110 end = size - 1;111 // 500-112 } else if (isNaN(end)) {113 end = size - 1;114 }115116 // Invalid117 if (isNaN(start) || isNaN(end) || start > end) valid = false;118119 return { start: start, end: end };120 });121 return valid ? arr : undefined;122};