Skip to content
Myr

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

9 files+225 added−205 removed

modified:examples/chat/app.js+3 added-2 removed
modified:lib/express/plugins/view.js+3 added-2 removed
modified:lib/express/core.js+10 added-6 removed
4 unmodified lines
5process.mixin(require('express/exceptions'))6process.mixin(require('express/collection'))7process.mixin(require('express/event'))8process.mixin(require('express/request'))9process.mixin(require('express/helpers'))9process.mixin(require('express/plugin'))10process.mixin(require('express/mime'))11process.mixin(require('express/dsl'))1213/**14 * Module dependencies.15 */1617var multipart = require('multipart'),15 File = require('file').File18 File = require('file').File,19 utils = require('express/utils')20 21// --- Route2223Route = Class({
59 unmodified lines
83 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 lines
229 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 lines
245 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 lines
deleted:lib/express/helpers.js-123 removed
12// Express - Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * JSON aliases.6 */78JSON.encode = JSON.stringify9JSON.decode = JSON.parse1011/**12 * Return a unique identifier.13 *14 * @return {string}15 * @api public16 */1718exports.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}2425/**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 */3334exports.extname = function(path) {35 if (path.lastIndexOf('.') < 0) return36 return path.slice(path.lastIndexOf('.') + 1)37}3839/**40 * Return the basename of the given _path_.41 *42 * @param {string} path43 * @return {string}44 * @api public45 */4647exports.basename = function(path) {48 return path.split('/').slice(-1)[0]49}5051/**52 * Escape special characters in _html_.53 *54 * @param {string} html55 * @return {string}56 * @api public57 */5859exports.escape = function(html) {60 return html.toString()61 .replace(/&/g, '&amp;')62 .replace(/"/g, '&quot;')63 .replace(/</g, '&lt;')64 .replace(/>/g, '&gt;')65}6667/**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 */7677exports.toArray = function(arr, offset) {78 return Array.prototype.slice.call(arr, offset)79}8081/**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 */9394exports.escapeRegexp = function(string, chars) {95 var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\')96 return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')97}9899/**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 */113114exports.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
12// Express - Mime - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34var types = {4/**5 * Module dependencies.6 */7 8var utils = require('express/utils')910/**11 * Mime type lookup table.12 */1314exports.types = {15 '323' : 'text/h323',16 '3gp' : 'video/3gpp',17 'a' : 'application/octet-stream',18 'acx' : 'application/internet-property-stream',
342 unmodified lines
361 * @api public362 */363364exports.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
12// Express - Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * JSON aliases.6 */78JSON.encode = JSON.stringify9JSON.decode = JSON.parse1011/**12 * Return a unique identifier.13 *14 * @return {string}15 * @api public16 */1718exports.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}2425/**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 */3334exports.extname = function(path) {35 if (path.lastIndexOf('.') < 0) return36 return path.slice(path.lastIndexOf('.') + 1)37}3839/**40 * Return the basename of the given _path_.41 *42 * @param {string} path43 * @return {string}44 * @api public45 */4647exports.basename = function(path) {48 return path.split('/').slice(-1)[0]49}5051/**52 * Escape special characters in _html_.53 *54 * @param {string} html55 * @return {string}56 * @api public57 */5859exports.escape = function(html) {60 return html.toString()61 .replace(/&/g, '&amp;')62 .replace(/"/g, '&quot;')63 .replace(/</g, '&lt;')64 .replace(/>/g, '&gt;')65}6667/**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 */7677exports.toArray = function(arr, offset) {78 return Array.prototype.slice.call(arr, offset)79}8081/**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 */9394exports.escapeRegexp = function(string, chars) {95 var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\')96 return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')97}9899/**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 */113114exports.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 lines
26specs = {27 independant: [28 'core',29 'routing',30 'helpers',30 'utils',31 'request',32 'mime',33 'static',34 'collection',
32 unmodified lines
deleted:spec/spec.helpers.js-68 removed
12describe '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 '&lt;p&gt;this &amp; 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 end68end
added:spec/spec.utils.js+72 added
12describe '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 '&lt;p&gt;this &amp; 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 end72end