Helpers are no longer global. Renamed to utils
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 6ee83ba · Browse files · 9 files·+225 added−205 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+225 added−205 removed
- tests:+73 added−69 removed
- other:+152 added−136 removed
Provenance
- Commit
7e92012- Tree
6f2c2d2- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
6ee83ba
9 files changed+225 added−205 removed
modified:examples/chat/app.js+3 added-2 removed
10 unmodified lines11121314151516171819208 unmodified lines2930313232333435363734 unmodified lines10 unmodified lines enable('cache static files') //enable('cache view contents')})
var messages = []var messages = [], utils = require('express/utils') get('/', function(){ this.redirect('/chat')})8 unmodified lines})
post('/chat', function(){ messages .push(escape(this.param('message')) .push(utils.escape(this.param('message')) .replace(/(http:\/\/[^\s]+)/g, '<a href="$1" target="express-chat">$1</a>') .replace(/:\)/g, '<img src="http://icons3.iconfinder.netdna-cdn.com/data/icons/ledicons/emoticon_smile.png">')) this.halt(200)})34 unmodified linesmodified:lib/express/plugins/view.js+3 added-2 removed
3 unmodified lines45678891011121351 unmodified lines6566676868697071727326 unmodified lines3 unmodified lines/** * Module dependencies. */
var posix = require('posix')var posix = require('posix'), utils = require('express/utils')
/** * Supported template engines. */51 unmodified lines var self = this, options = options || {}, path = set('views') + '/' + view, type = path.split('.').slice(-2)[0], ext = extname(path), ext = utils.extname(path), layout = options.layout === undefined ? true : options.layout self.contentType(ext) function render(content) { content = engine[type].render(content, options)26 unmodified linesmodified:lib/express/core.js+10 added-6 removed
4 unmodified lines5 process.mixin(require('express/exceptions'))6 process.mixin(require('express/collection'))7 process.mixin(require('express/event'))8 process.mixin(require('express/request'))9-process.mixin(require('express/helpers'))9 process.mixin(require('express/plugin'))10 process.mixin(require('express/mime'))11 process.mixin(require('express/dsl'))12 13+/**14+ * Module dependencies.15+ */16+17 var multipart = require('multipart'),15- File = require('file').File18+ File = require('file').File,19+ utils = require('express/utils')20 21 // --- Route22 23 Route = Class({59 unmodified lines83 normalize: function(path) {84 var self = this85 this.keys = []86 if (path instanceof RegExp) return path83- return new RegExp('^' + escapeRegexp(normalizePath(path), '.')87+ return new RegExp('^' + utils.escapeRegexp(normalizePath(path), '.')88 .replace(/\*/g, '(.+)')89 .replace(/(\/|\\\.):(\w+)\?/g, function(_, c, key){90 self.keys.push(key)91 return '(?:' + c + '([^\/]+))?'137 unmodified lines229 request.params = { post: {}}230 promise.timeout(set('upload timeout') || 5000)231 stream.addListener('part', function(part){232 if (part.filename) {229- var file = new File(part.tempfile = '/tmp/express-' + Number(new Date) + uid(), 'w')233+ var file = new File(part.tempfile = '/tmp/express-' + Number(new Date) + utils.uid(), 'w')234 ++pendingFiles235 part.pos = 0236 part.addListener('body', function(chunk){237 file.write(chunk, part.pos, 'binary').addErrback(function(){7 unmodified lines245 promise.emitSuccess()246 }).addErrback(function(){247 promise.emitError.apply(promise, arguments)248 })245- mergeParam(part.name, part, request.params.post)249+ utils.mergeParam(part.name, part, request.params.post)250 })251 }252 else253 part.buf = '',254 part255 .addListener('body', function(chunk){ part.buf += chunk })256 .addListener('complete', function(){257 if (part.buf.length)254- mergeParam(part.name, part.buf, request.params.post)258+ utils.mergeParam(part.name, part.buf, request.params.post)259 })260 }).addListener('complete', function(){261 promise.addCallback(function(){ self.route(request, response) })262 })71 unmodified linesdeleted:lib/express/helpers.js-123 removed
1-2-// Express - Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3-4-/**5- * JSON aliases.6- */7-8-JSON.encode = JSON.stringify9-JSON.decode = JSON.parse10-11-/**12- * Return a unique identifier.13- *14- * @return {string}15- * @api public16- */17-18-exports.uid = function() {19- var uid = ''20- for (var n = 4; n; --n)21- uid += (Math.abs((Math.random() * 0xFFFFFFF) | 0)).toString(16)22- return uid23-}24-25-/**26- * Return the extension name of the given _path_,27- * or null when not present.28- *29- * @param {string} path30- * @return {string}31- * @api public32- */33-34-exports.extname = function(path) {35- if (path.lastIndexOf('.') < 0) return36- return path.slice(path.lastIndexOf('.') + 1)37-}38-39-/**40- * Return the basename of the given _path_.41- *42- * @param {string} path43- * @return {string}44- * @api public45- */46-47-exports.basename = function(path) {48- return path.split('/').slice(-1)[0]49-}50-51-/**52- * Escape special characters in _html_.53- *54- * @param {string} html55- * @return {string}56- * @api public57- */58-59-exports.escape = function(html) {60- return html.toString()61- .replace(/&/g, '&')62- .replace(/"/g, '"')63- .replace(/</g, '<')64- .replace(/>/g, '>')65-}66-67-/**68- * Convert native array-like objects into an69- * array with optional _offset_.70- *71- * @param {object} arr72- * @param {int} offset73- * @return {array}74- * @api public75- */76-77-exports.toArray = function(arr, offset) {78- return Array.prototype.slice.call(arr, offset)79-}80-81-/**82- * Escape RegExp _chars_ in _string_. Where _chars_ 83- * defaults to regular expression special characters.84- *85- * _chars_ should be a space delimited list of characters,86- * for example '[ ] ( )'.87- *88- * @param {string} string89- * @param {string} chars90- * @return {Type}91- * @api public92- */93-94-exports.escapeRegexp = function(string, chars) {95- var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\')96- return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')97-}98-99-/**100- * Merge param _key_ and _val_ into _params_. Key101- * should be a query string key such as 'user[name]',102- * and _val_ is it's associated value. The root _params_103- * object is returned.104- *105- * This is primarily used by Express for merging multi-part106- * body values together.107- *108- * @param {string} key109- * @param {string} val110- * @return {hash}111- * @api public112- */113-114-exports.mergeParam = function(key, val, params) {115- var keys = key.match(/(\w+)/g),116- orig = params117- for (var i = 0, len = keys.length; i < len; ++i)118- if (i == len - 1)119- params[keys[i]] = val120- else121- params = (params[keys[i]] = params[keys[i]] || {})122- return orig123-}modified:lib/express/mime.js+13 added-3 removed
1 2 // Express - Mime - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4-var types = {4+/**5+ * Module dependencies.6+ */7+ 8+var utils = require('express/utils')9+10+/**11+ * Mime type lookup table.12+ */13+14+exports.types = {15 '323' : 'text/h323',16 '3gp' : 'video/3gpp',17 'a' : 'application/octet-stream',18 'acx' : 'application/internet-property-stream',342 unmodified lines361 * @api public362 */363 364 exports.mime = function(path) {355- return types[path] || 356- types[extname(path)] || 365+ return exports.types[path] || 366+ exports.types[utils.extname(path)] || 367 set('default mime type') || 368 'application/octet-stream' 369 }added:lib/express/utils.js+123 added
1+2+// Express - Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3+4+/**5+ * JSON aliases.6+ */7+8+JSON.encode = JSON.stringify9+JSON.decode = JSON.parse10+11+/**12+ * Return a unique identifier.13+ *14+ * @return {string}15+ * @api public16+ */17+18+exports.uid = function() {19+ var uid = ''20+ for (var n = 4; n; --n)21+ uid += (Math.abs((Math.random() * 0xFFFFFFF) | 0)).toString(16)22+ return uid23+}24+25+/**26+ * Return the extension name of the given _path_,27+ * or null when not present.28+ *29+ * @param {string} path30+ * @return {string}31+ * @api public32+ */33+34+exports.extname = function(path) {35+ if (path.lastIndexOf('.') < 0) return36+ return path.slice(path.lastIndexOf('.') + 1)37+}38+39+/**40+ * Return the basename of the given _path_.41+ *42+ * @param {string} path43+ * @return {string}44+ * @api public45+ */46+47+exports.basename = function(path) {48+ return path.split('/').slice(-1)[0]49+}50+51+/**52+ * Escape special characters in _html_.53+ *54+ * @param {string} html55+ * @return {string}56+ * @api public57+ */58+59+exports.escape = function(html) {60+ return html.toString()61+ .replace(/&/g, '&')62+ .replace(/"/g, '"')63+ .replace(/</g, '<')64+ .replace(/>/g, '>')65+}66+67+/**68+ * Convert native array-like objects into an69+ * array with optional _offset_.70+ *71+ * @param {object} arr72+ * @param {int} offset73+ * @return {array}74+ * @api public75+ */76+77+exports.toArray = function(arr, offset) {78+ return Array.prototype.slice.call(arr, offset)79+}80+81+/**82+ * Escape RegExp _chars_ in _string_. Where _chars_ 83+ * defaults to regular expression special characters.84+ *85+ * _chars_ should be a space delimited list of characters,86+ * for example '[ ] ( )'.87+ *88+ * @param {string} string89+ * @param {string} chars90+ * @return {Type}91+ * @api public92+ */93+94+exports.escapeRegexp = function(string, chars) {95+ var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\')96+ return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')97+}98+99+/**100+ * Merge param _key_ and _val_ into _params_. Key101+ * should be a query string key such as 'user[name]',102+ * and _val_ is it's associated value. The root _params_103+ * object is returned.104+ *105+ * This is primarily used by Express for merging multi-part106+ * body values together.107+ *108+ * @param {string} key109+ * @param {string} val110+ * @return {hash}111+ * @api public112+ */113+114+exports.mergeParam = function(key, val, params) {115+ var keys = key.match(/(\w+)/g),116+ orig = params117+ for (var i = 0, len = keys.length; i < len; ++i)118+ if (i == len - 1)119+ params[keys[i]] = val120+ else121+ params = (params[keys[i]] = params[keys[i]] || {})122+ return orig123+}modified:spec/node.js+1 added-1 removed
25 unmodified lines26 specs = {27 independant: [28 'core',29 'routing',30- 'helpers',30+ 'utils',31 'request',32 'mime',33 'static',34 'collection',32 unmodified linesdeleted:spec/spec.helpers.js-68 removed
1-2-describe 'Express'3- describe 'toArray()'4- describe 'when given an array'5- it 'should return the array'6- toArray([1,2,3]).should.eql [1,2,3]7- end8- end9- 10- describe 'when given an object with indexed values and length'11- it 'should return an array'12- var args = -{ return arguments }('foo', 'bar')13- toArray(args).should.eql ['foo', 'bar']14- end15- end16- end17- 18- describe 'escape()'19- it 'should escape html'20- escape('<p>this & that').should.eql '<p>this & that'21- end22- end23- 24- describe 'uid()'25- it 'should return a string of random characters'26- uid().should.not.eql uid()27- uid().length.should.be_greater_than 2028- end29- end30- 31- describe 'extname()'32- it 'should return the a files extension'33- extname('image.png').should.eql 'png'34- extname('image.large.png').should.eql 'png'35- extname('/path/to/image.large.png').should.eql 'png'36- end37- 38- it 'should return null when not found'39- extname('path').should.be_null40- extname('/just/a/path').should.be_null41- end42- end43- 44- describe 'basename()'45- it 'should return a files basename'46- basename('foo/bar/baz.image.png').should.eql 'baz.image.png'47- end48- end49- 50- describe 'mergeParam()'51- describe 'with empty params'52- it 'should merge the given key and value'53- params = {}54- mergeParam('user[names][first]', 'tj', params)55- params.user.names.first.should.eql 'tj'56- end57- end58- 59- describe 'with populated params'60- it 'should merge not overwrite'61- params = { user: { name: 'tj' }}62- mergeParam('user[email]', 'tj@vision-media.ca', params)63- params.user.name.should.eql 'tj'64- params.user.email.should.eql 'tj@vision-media.ca'65- end66- end67- end68-endadded:spec/spec.utils.js+72 added
1+2+describe 'Express'3+ before4+ utils = require('express/utils')5+ end6+ 7+ describe 'toArray()'8+ describe 'when given an array'9+ it 'should return the array'10+ utils.toArray([1,2,3]).should.eql [1,2,3]11+ end12+ end13+ 14+ describe 'when given an object with indexed values and length'15+ it 'should return an array'16+ var args = -{ return arguments }('foo', 'bar')17+ utils.toArray(args).should.eql ['foo', 'bar']18+ end19+ end20+ end21+ 22+ describe 'escape()'23+ it 'should escape html'24+ utils.escape('<p>this & that').should.eql '<p>this & that'25+ end26+ end27+ 28+ describe 'uid()'29+ it 'should return a string of random characters'30+ utils.uid().should.not.eql utils.uid()31+ utils.uid().length.should.be_greater_than 2032+ end33+ end34+ 35+ describe 'extname()'36+ it 'should return the a files extension'37+ utils.extname('image.png').should.eql 'png'38+ utils.extname('image.large.png').should.eql 'png'39+ utils.extname('/path/to/image.large.png').should.eql 'png'40+ end41+ 42+ it 'should return null when not found'43+ utils.extname('path').should.be_null44+ utils.extname('/just/a/path').should.be_null45+ end46+ end47+ 48+ describe 'basename()'49+ it 'should return a files basename'50+ utils.basename('foo/bar/baz.image.png').should.eql 'baz.image.png'51+ end52+ end53+ 54+ describe 'mergeParam()'55+ describe 'with empty params'56+ it 'should merge the given key and value'57+ params = {}58+ utils.mergeParam('user[names][first]', 'tj', params)59+ params.user.names.first.should.eql 'tj'60+ end61+ end62+ 63+ describe 'with populated params'64+ it 'should merge not overwrite'65+ params = { user: { name: 'tj' }}66+ utils.mergeParam('user[email]', 'tj@vision-media.ca', params)67+ params.user.name.should.eql 'tj'68+ params.user.email.should.eql 'tj@vision-media.ca'69+ end70+ end71+ end72+end