Skip to content
Myr

Merge branch 'ext'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parents 5458e5d, b047325 · Browse files · 23 files+70 added−1198 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

23 files+70 added−1198 removed

  • tests:+2 added−477 removed
  • other:+68 added−721 removed

23 files+70 added−1198 removed

deleted:benchmarks/collection.js-111 removed
12require.paths.unshift('lib')3require.paths.unshift('benchmarks')4process.mixin(GLOBAL, require('sys'))5process.mixin(GLOBAL, require('benchmark'))6require('express')78print = puts910range = function(a, b) {11 var array = []12 while (a++ < b)13 array.push(a-1)14 return array15}1617suite('Collection with [0..10,000]', 100, function(){18 array = range(0, 10000)19 20 benchmark('for', function(){21 for (var i = 0, len = array.length; i < len; ++i) ;22 })23 24 benchmark('for uncached', function(){25 for (var i = 0; i < array.length; ++i) ;26 })27 28 benchmark('forEach()', function(){29 array.forEach(function(){})30 })31 32 benchmark('#each()', function(){33 $(array).each(function(){})34 })35 36 benchmark('#map()', function(){37 $(array).map(function(n){ return n += 1 })38 })39 40 benchmark('#map() with shorthand', function(){41 $(array).map('a += 1')42 })43 44 benchmark('#find()', function(){45 $(array).find(function(n){ return n > 5000 })46 })47 48 benchmark('#select()', function(){49 $(array).select(function(n){ return n % 2 })50 })51 52 benchmark('#first()', function(){53 $(array).first(5000)54 })55 56 benchmark('#slice()', function(){57 $(array).slice(100, 5000)58 })59 60 benchmark('#drop()', function(){61 $(array).drop(5000)62 })63 64 benchmark('#length()', function(){65 $(array).length()66 })67 68 benchmark('#keys()', function(){69 $(array).keys()70 })71 72 benchmark('#toArray()', function(){73 $(array).toArray()74 })75 76 benchmark('#min()', function(){77 $(array).min()78 })79 80 benchmark('#max()', function(){81 $(array).max()82 })83 84 benchmark('#sum()', function(){85 $(array).sum()86 })87 88 benchmark('#avg()', function(){89 $(array).avg()90 })91 92 benchmark('#clone()', function(){93 $(array).clone()94 })95 96 benchmark('#merge()', function(){97 $(array).merge({ foo: 'bar' })98 })99 100 benchmark('#sample()', function(){101 $(array).sample()102 })103 104 benchmark('#chunk()', function(){105 $(array).chunk(5)106 })107 108 benchmark('#at()', function(){109 $(array).at(5000)110 })111})
modified:examples/upload/app.js+3 added-4 removed
modified:lib/express/pages/show-exceptions.js+3 added-3 removed
modified:lib/express/plugins/body-decoder.js+6 added
modified:lib/express/plugins/cache.js+7 added-5 removed
28 unmodified lines
29 * @api public30 */31 32 set: function(key, val) {33 if (typeof key !== 'string') throw new Error(this.name + ' store #set() key must be a string')33 if (typeof key !== 'string')34 throw new Error(this.name + ' store #set() key must be a string')35 },36 37 /**38 * Ensure that the given _key_ is a string.
3 unmodified lines
42 * @api public43 */44 45 get: function(key) {45 if (typeof key !== 'string') throw new Error(this.name + 'store #get() key must be a string')46 if (typeof key !== 'string')47 throw new Error(this.name + 'store #get() key must be a string')48 },49 50 /**51 * Convert to '[NAME Store]'.
63 unmodified lines
115 return this.data[key] instanceof Cache ?116 this.data[key].val :117 null118 var regexp = this.normalize(key) 117 return $(this.data).reduce({}, function(vals, cache){119 return this.data.reduce(function(vals, cache){120 if (regexp.test(cache.key))121 vals[cache.key] = cache.val122 return vals121 })123 }, {})124 },125 126 /**127 * Clear data matching the given _key_.
26 unmodified lines
154 155 reap: function(ms) {156 var self = this,157 threshold = Number(new Date(Number(new Date) - ms))156 $(this.data).each(function(cache){158 this.data.each(function(cache){159 if (cache.created < threshold)160 self.clear(cache.key)161 })162 },
58 unmodified lines
modified:lib/express/plugins/cookie.js+4 added-4 removed
8 unmodified lines
9 * @api public10 */1112exports.parseCookie = function(cookie) {13 return $(cookie.replace(/^ *| *$/g, '').split(/ *; */)).reduce({}, function(hash, pair){13 return cookie.replace(/^ *| *$/g, '').split(/ *; */).reduce(function(hash, pair){14 var parts = pair.split(/ *= */)15 hash[parts[0]] = parts[1]16 return hash17 })17 }, {})18}1920/**21 * Compile cookie _name_, _val_ and _options_ to a string.
6 unmodified lines
28 */2930exports.compileCookie = function(name, val, options) {31 if (!options) return name + '=' + val32 return name + '=' + val + '; ' + $(options).map(function(val, key){32 return name + '=' + val + '; ' + options.map(function(val, key){33 if (val instanceof Date)34 val = val.toString()35 .replace(/^(\w+)/, '$1,')36 .replace(/(\w+) (\d+) (\d+)/, '$2-$1-$3')37 .replace(/GMT.*$/, 'GMT')38 return val === true ? key : key + '=' + val39 }).toArray().join('; ')39 }).join('; ')40}4142// --- Cookie43
67 unmodified lines
modified:lib/express/plugins/hooks.js+2 added-2 removed
47 unmodified lines
48 * Invoke all before handlers.49 */50 51 request: function(event) {52 $(exports.callbacks.before).each(function(fn){52 exports.callbacks.before.each(function(fn){53 fn.call(event.request, event.request)54 })55 },56
1 unmodified line
58 * Invoke all after handlers.59 */60 61 response: function(event) {62 $(exports.callbacks.after).each(function(fn){62 exports.callbacks.after.each(function(fn){63 fn.call(event.request, event.request)64 })65 }66 }
1 unmodified line
modified:lib/express/plugins/session.js+2 added-2 removed
116 unmodified lines
117 * @api public118 */119 120 length: function() {121 return $(this.store).length()121 return this.store.values.length122 },123 124 /**125 * Reap sessions older than _ms_.
4 unmodified lines
130 131 reap: function(ms) {132 var self = this,133 threshold = Number(new Date(Number(new Date) - ms))134 $(this.store).each(function(session, sid){134 this.store.each(function(session, sid){135 if (session.lastAccess < threshold)136 self.destroy(sid)137 })138 }
67 unmodified lines
deleted:lib/express/collection.js-549 removed
12// Express - Collection - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Module dependencies.6 */7 8var utils = require('express/utils')910/**11 * Throw $break in order to stop iteration.12 */1314$break = '__break__'1516var property = /^\w+$/,17 method = /^\w+\(/1819/**20 * Normalize callback _fn_. When a string is21 * passed convert the shorthand expr to a function.22 *23 * - Functions are passed through un-touched24 * - Strings with length of < 4 are considered operators between a and b25 * - Single words are considered properties on a26 * - Single functions are considered method calls on a27 * - Larger strings are considered return expressions28 *29 * @param {string, function} fn30 * @return {function}31 * @api private32 */3334function callback(fn) {35 if (fn === undefined) return36 if (fn instanceof Function) return fn37 if (fn.length < 4) return Function('a, b, c', 'return a ' + fn + ' b')38 if (property.test(fn) || method.test(fn)) fn = 'a.' + fn39 return Function('a, b, c', 'return ' + fn)40}4142// --- Collection4344Collection = Class({45 46 /**47 * Initialize collection with an array-like object.48 *49 * @param {object} arr50 * @api private51 */52 53 init: function(arr) {54 this.arr = arr || []55 },56 57 /**58 * Return the value of _index_ or null.59 *60 * @param {int} index61 * @return {mixed}62 * @api public63 */64 65 at: function(index) {66 if ('length' in this.arr)67 return this.arr[index]68 var result, i = 069 this.each(function(val){70 if (i++ == index) {71 result = val72 throw $break73 }74 })75 return result76 },77 78 /**79 * Iterate collection using callback _fn_,80 * passing both the value and index.81 *82 * @param {function} fn83 * @return {Collection}84 * @api public85 */86 87 each: function(fn) {88 try {89 if (this.arr.forEach)90 this.arr.forEach(fn)91 else92 for (var key in this.arr)93 if (this.arr.hasOwnProperty(key))94 fn(this.arr[key], key)95 }96 catch (e) {97 if (e != $break) throw e98 }99 return this100 },101 102 /**103 * Reverse a collection.104 *105 * @return {Collection}106 * @api public107 */108 109 reverse: function() {110 if (this.arr.reverse)111 return $(this.arr.reverse())112 return this113 },114 115 /**116 * Join a collection with the given _str_ or ''.117 *118 * @param {string} str119 * @return {string}120 * @api public121 */122 123 join: function(str) {124 return this.toArray().join(str || '')125 },126 127 /**128 * Sort collection with optional _fn_.129 *130 * @param {function} fn131 * @return {Collection}132 * @warn converts to array133 * @api public134 */135 136 sort: function(fn) {137 fn = callback(fn)138 return $(this.toArray().sort(fn))139 },140 141 /**142 * Iterate with _memo_ using callback _fn_.143 * The _memo_ object is passed as the first 144 * argument, and the return value of _fn_ becomes145 * the value of _memo_ providing a functional 146 * approach to reducing a collection.147 *148 * @param {mixed} memo149 * @param {function} fn150 * @return {mixed}151 * @api public152 */153 154 reduce: function(memo, fn) {155 fn = callback(fn)156 if (this.arr.reduce)157 return this.arr.reduce(fn, memo)158 this.each(function(val, key){159 memo = fn(memo, val, key)160 })161 return memo162 },163 164 /**165 * Map using callback _fn_, returning a 166 * new collection of return values.167 *168 * @param {function} fn169 * @return {Collection}170 * @api public171 */172 173 map: function(fn) {174 fn = callback(fn)175 if (this.arr.map)176 return $(this.arr.map(fn))177 return $(this.reduce([], function(array, val, key){178 array.push(fn(val, key))179 return array180 }))181 },182 183 /**184 * Return collection of values when _fn_ evaluates185 * to true.186 *187 * @param {function} fn188 * @return {Collection}189 * @api public190 */191 192 select: function(fn) {193 fn = callback(fn)194 return $(this.reduce([], function(array, val, key){195 if (fn(val, key))196 array.push(val)197 return array198 }))199 },200 201 /**202 * Return collection of values when _fn_ evaluates203 * to false.204 *205 * @param {function} fn206 * @return {Collection}207 * @api public208 */209 210 reject: function(fn) {211 fn = callback(fn)212 return $(this.reduce([], function(array, val, key){213 if (!fn(val, key))214 array.push(val)215 return array216 }))217 },218 219 /**220 * Check if all arguments passed are found within 221 * the collection using the === operator.222 *223 * @return {bool}224 * @api public225 */226 227 includes: function() {228 var self = this229 return $(arguments).all(function(arg){230 return self.any(function(val){231 return val === arg232 })233 })234 },235 236 /**237 * Return the first _n_ value(s), defaults to 1.238 *239 * @param {int} n240 * @return {mixed}241 * @api public242 */243 244 first: function(n) {245 return n ? 246 this.slice(0, n) :247 this.at(0)248 },249 250 /**251 * Return the last _n_ value(s), defaults to -1.252 *253 * @param {int} n254 * @return {mixed}255 * @api public256 */257 258 last: function(n) {259 var len = this.length()260 return n ? 261 this.slice(len - n, len) :262 this.at(--len)263 },264 265 /**266 * Drop the first _n_ values, returning the others.267 *268 * @param {int} n269 * @return {Collection}270 * @api public271 */272 273 drop: function(n) {274 return this.slice(n, this.length())275 },276 277 /**278 * Return a slice of values from _start_ to _end_.279 *280 * @param {int} start281 * @param {int} end282 * @return {Collection}283 * @api public284 */285 286 slice: function(start, end) {287 if (this.arr.slice)288 return $(this.arr.slice(start, end))289 var i = 0290 return $(this.reduce([], function(array, val){291 if (i++ >= start)292 if (i <= end)293 array.push(val)294 else295 throw $break296 return array297 }))298 },299 300 /**301 * Iterate until _fn_ evaluates to true, then302 * return the matching value.303 *304 * @param {function} fn305 * @return {mixed}306 * @api public307 */308 309 find: function(fn) {310 var result, fn = callback(fn)311 this.each(function(val, key){312 if (fn(val, key)) {313 result = val314 throw $break315 }316 })317 return result318 },319 320 /**321 * Return true if _fn_ ever evaluates to true, otherwise322 * returns false.323 *324 * @param {function} fn325 * @return {bool}326 * @api public327 */328 329 any: function(fn) {330 fn = callback(fn)331 if (this.arr.some)332 return this.arr.some(fn)333 return !! this.find(fn)334 },335 336 /**337 * Returns true if _fn_ always evaluates to true, otherwise338 * returns false.339 *340 * @param {function} fn341 * @return {bool}342 * @api public343 */344 345 all: function(fn) {346 fn = callback(fn)347 if (this.arr.every)348 return this.arr.every(fn)349 return this.reduce(true, function(state, val, key){350 if (!state) throw $break351 return !! fn(val, key)352 })353 },354 355 /**356 * Select values matching _pattern_357 *358 * @param {regexp} pattern359 * @return {Collection}360 * @api public361 */362 363 grep: function(pattern) {364 return this.select(function(val){365 return pattern.exec(val)366 })367 },368 369 /**370 * Return keys as a collection.371 *372 * @return {Collection}373 * @api public374 */375 376 keys: function() {377 return $(Object.keys(this.arr))378 },379 380 /**381 * Convert a collection to a true array.382 * Works recursively383 *384 * @return {array}385 * @api public386 */387 388 toArray: function() {389 return this.reduce([], function(array, val){390 val instanceof Collection ?391 array.push(val.toArray()) :392 array.push(val)393 return array394 })395 },396 397 /**398 * Return the lowest number in the collection.399 *400 * @return {number}401 * @api public402 */403 404 min: function() {405 return this.reduce(null, function(min, val){406 return min === null ? val :407 val < min ? val :408 min409 })410 },411 412 /**413 * Return the largest number in the collection.414 *415 * @return {number}416 * @api public417 */418 419 max: function() {420 return this.reduce(null, function(max, val){421 return max === null ? val :422 val > max ? val :423 max424 })425 },426 427 /**428 * Group collection into several collections grouped by _size_.429 *430 * @param {int} size431 * @return {Collection}432 * @api public433 */434 435 chunk: function(size) {436 var len = this.arr.length, 437 chunks = [], chunk = [], i = 0 438 this.each(function(val, key){439 chunk.push(val)440 if (i++ > 0 && (i % size == 0 || i == len))441 chunks.push($(chunk)),442 chunk = []443 })444 return $(chunks)445 },446 447 /**448 * Return the length of this collection.449 *450 * @return {int}451 * @api public452 */453 454 length: function() {455 if ('length' in this.arr)456 return this.arr.length457 return this.reduce(0, function(len){458 return ++len459 })460 },461 462 /**463 * Return the sum of numeric values in this collection.464 *465 * @return {number}466 * @api public467 */468 469 sum: function() {470 return this.reduce(0, function(sum, n){471 return sum + n472 })473 },474 475 /**476 * Return the average of numeric values in this collection.477 *478 * @return {number}479 * @api public480 */481 482 avg: function() {483 return this.sum() / this.length()484 },485 486 /**487 * Merge _other_ with this collection.488 *489 * @param {mixed} other490 * @return {Collection}491 * @api public492 */493 494 merge: function(other) {495 if (this.arr instanceof Array)496 return $($(other).reduce(this.arr, function(array, val){497 array.push(val)498 return array499 }))500 else501 return $(utils.mixin(this.arr, $(other).arr))502 },503 504 /**505 * Clone the collection.506 *507 * @return {Collection}508 * @api public509 */510 511 clone: function() {512 return $(utils.clone(this.arr))513 },514 515 /**516 * Return the value of a random index.517 *518 * @return {mixed}519 * @api public520 */521 522 sample: function() {523 return this.at(Math.floor(Math.random() * this.length()))524 },525 526 /**527 * Convert collection to a string.528 *529 * @return {string}530 * @api public531 */532 533 toString: function() {534 return '[Collection ' + this.arr + ']'535 }536})537538/**539 * Create a new collection from an array-like object.540 *541 * @param {object} arr542 * @return {Collection}543 * @api public544 */545546var $ = exports.$ = function(arr) {547 if (arr instanceof Collection) return arr548 return new Collection(arr)549}
modified:lib/express/core.js+10 added-13 removed
10 unmodified lines
11 fs = require('fs')1213utils.mixin(require('sys'))14utils.mixin(require('express/exceptions'))15utils.mixin(require('express/collection'))15utils.mixin(require('express/event'))16utils.mixin(require('express/request'))17utils.mixin(require('express/plugin'))18utils.mixin(require('express/dsl'))
48 unmodified lines
67 */6869 normalize: function(path) {70 var self = this72 this.keys = []71 this.params = []72 if (path instanceof RegExp) return path73 return new RegExp('^' + utils.escapeRegexp(normalizePath(path), '.')74 .replace(/\*/g, '(.+)')75 .replace(/(\/|\\\.):(\w+)\?/g, function(_, c, key){77 self.keys.push(key)76 self.params.push(key)77 return '(?:' + c + '([^\/]+))?'78 })79 .replace(/:(\w+)/g, function(_, key){81 self.keys.push(key)80 self.params.push(key)81 return '([^\/]+)'82 }) + '$', 'i')83 }84})
37 unmodified lines
122 * @api private123 */124 125 matchingRoute: function(){127 var self = this128 return $(Express.routes).find(function(route){129 return self.match(route)130 })126 return Express.routes.find(function(route){127 return this.match(route)128 }, this)129 },130 131 /**132 * Check if _route_ matches the current request.
13 unmodified lines
146 },147 148 /**149 * Map #request.captures to #request.params.path based on the152 * given _route_ keys.150 * given _route_ params.151 *152 * @param {Route} route153 * @api private154 */155 156 mapParams: function(route) {159 var self = this160 $(route.keys).each(function(key, i){161 self.request.params.path[key] = self.request.captures[++i]162 })157 route.params.each(function(key, i){158 this.request.params.path[key] = this.request.captures[++i]159 }, this)160 }161})162163// --- Server
172 unmodified lines
modified:lib/express/dsl.js+1 added-1 removed
78 unmodified lines
79 */8081exports.run = function() {82 configure(Express.environment = process.ENV.EXPRESS_ENV || 'development')83 $(Express.plugins).each(function(plugin){83 Express.plugins.each(function(plugin){84 if ('init' in plugin.klass)85 plugin.klass.init(plugin.options)86 })87 Express.server.run.apply(Express.server, arguments)
46 unmodified lines
modified:lib/express/plugin.js+8 added-2 removed
1var utils = require('express/utils');12// Express - Plugin - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Module dependencies.6 */78var utils = require('express/utils');910/**11 * Push _plugin_ with _options_ to the plugin stack.12 * If _plugin_ has already been pushed, then it's options13 * will override any previously set.14 *
4 unmodified lines
1920exports.use = function(plugin, options) {21 if (Express.environment === 'test' && 'init' in plugin)22 plugin.init(options)17 $(Express.plugins).each(function(other, i){23 Express.plugins.each(function(other, i){24 if (other.klass === plugin)25 delete Express.plugins[i]26 })27 Express.plugins.push({
34 unmodified lines
modified:lib/express/plugins.js+2 added-1 removed
12// Express - Plugins - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)33var utils = require('express/utils');4var utils = require('express/utils')5utils.mixin(require('express/plugins/hooks'))6utils.mixin(require('express/plugins/flash'))7utils.mixin(require('express/plugins/cache'))8utils.mixin(require('express/plugins/cookie'))
5 unmodified lines
modified:lib/express/request.js+8 added-8 removed
71 unmodified lines
72 this.params = this.params || {}73 this.params.path = {}74 this.params.get = this.url.query ? queryString.parseQuery(this.url.query) : {}75 this.params.post = this.params.post || {}76 this.plugins = $(Express.plugins).map(function(plugin){76 this.plugins = Express.plugins.map(function(plugin){77 return new plugin.klass(plugin.options) 78 })79 },80
51 unmodified lines
132 */133134 accepts: function() {135 var accept = this.header('accept')136 return accept ? 137 $(arguments).any(function(path){138 var type = mime.type(path)139 return accept.indexOf(type) !== -1 ||140 accept.indexOf(type.split('/')[0]+'/*') !== -1141 }) :142 true136 return accept 137 ? arguments.any(function(path){138 var type = mime.type(path)139 return accept.indexOf(type) !== -1 ||140 accept.indexOf(type.split('/')[0]+'/*') !== -1141 })142 : true143 },144145 /**146 * Set response status to _code_.
129 unmodified lines
modified:lib/express/utils.js+2 added-2 removed
116 unmodified lines
117exports.mergeParam = function(key, val, params) {118 var orig = params,119 keys = key.trim().match(/\w+/g),120 array = /\[\]$/.test(key)121 $(keys).reduce(queryString.parseQuery(key), function(parts, key, i){121 keys.reduce(function(parts, key, i){122 if (i === keys.length - 1)123 if (key in params)124 params[key] instanceof Array ?125 params[key].push(val) :
2 unmodified lines
128 params[key] = array ? [val] : val129 if (!(key in params)) params[key] = {}130 params = params[key]131 return parts[key]132 })132 }, queryString.parseQuery(key))133 return orig134}135136/**
73 unmodified lines
added:lib/support/extnot shown

Its text is not shown, added.

modified:lib/express.js+2 added
1 unmodified line
2require.paths.unshift(__dirname + '/support/js-oo/lib')3require.paths.unshift(__dirname + '/support/ejs/lib')4require.paths.unshift(__dirname + '/support/haml/lib')5require.paths.unshift(__dirname + '/support/sass/lib')6require.paths.unshift(__dirname + '/support/ext/lib')7require('oo')8require('ext')9require('express/core')
modified:spec/node.js-1 removed
20 unmodified lines
21 'utils',22 'request',23 'mime',24 'static',25 'collection',25 'plugins',26 'plugins.cache',27 'plugins.view',28 'plugins.common-logger',
21 unmodified lines
deleted:spec/spec.collection.js-474 removed
12process.mixin(require('express/collection'))34describe 'Express'5 describe 'Collection'6 describe '$(array)'7 it 'should return a Collection'8 $(['foo', 'bar']).should.be_an_instance_of Collection9 end10 end11 12 describe '$(object)'13 it 'should return a Collection'14 $({ foo: 'bar' }).should.be_an_instance_of Collection15 end16 end17 18 describe '$(Collection)'19 it 'should return the collection passed'20 var collection = $(['foo'])21 $(collection).should.equal collection22 end23 end24 25 describe 'shorthand expressions'26 describe 'with 3 or less chars'27 it 'should be considered binary operator between a / b'28 $(5..1).sort('-').toArray().should.eql 1..529 $(5..1).reduce(0, '+').should.eql 1530 end31 end32 33 describe 'with over 3 chars'34 it 'should be considered a return expression'35 $(5..1).sort('a - b').toArray().should.eql 1..536 $(5..1).reduce(0, 'a + b').should.eql 1537 end38 39 it 'should consider a single word a property on a'40 $(['foo', 'foobar']).map('length').toArray().should.eql [3, 6]41 end42 43 it 'should consider a single function a method on a'44 $(['foo', 'foobar']).map('charAt(0)').toArray().should.eql ['f', 'f']45 end46 end47 end48 49 describe '#at()'50 it 'should return the value at the given index'51 $(['foo', 'bar']).at(0).should.eql 'foo'52 $(['foo', 'bar']).at(1).should.eql 'bar'53 $(['foo', 'bar']).at(2).should.be_null54 end55 56 it 'should work with objects'57 $({ foo: 'bar', baz: 'raz' }).at(0).should.eql 'bar'58 $({ foo: 'bar', baz: 'raz' }).at(1).should.eql 'raz'59 $({ foo: 'bar', baz: 'raz' }).at(2).should.be_null60 end61 end62 63 describe '#each()'64 it 'should iterate passing index and value'65 var result = []66 $(['foo', 'bar']).each(function(val, i){67 result.push(i, val)68 })69 result.should.eql [0, 'foo', 1, 'bar']70 end71 72 it 'should work with objects'73 var result = []74 $({ foo: 'bar', baz: 'raz' }).each(function(val, key){75 result.push(key, val)76 })77 result.should.eql ['foo', 'bar', 'baz', 'raz']78 end79 80 it 'should return the collection'81 $([]).each(function(){}).should.be_an_instance_of Collection82 end83 end84 85 describe '#reduce()'86 it 'should iterate with memo object'87 var sum = $([1,2,3]).reduce(0, function(sum, n){ return sum + n })88 sum.should.eql 689 end90 91 it 'should allow shorthand expressions'92 $([1,2,3]).reduce(0, '+').should.eql 693 $([1,2,3]).reduce(0, 'a + b').should.eql 694 end95 end96 97 describe '#map()'98 it 'should iterate collecting results into a new collection'99 var collection = $(['foo', 'bar']).map(function(val){ return val.toUpperCase() })100 collection.at(0).should.eql 'FOO'101 collection.at(1).should.eql 'BAR'102 end103 104 it 'should work with objects'105 var collection = $({ foo: 'bar', baz: 'raz' }).map(function(val){ return val.toUpperCase() })106 collection.at(0).should.eql 'BAR'107 collection.at(1).should.eql 'RAZ'108 end109 110 it 'should allow shorthand expressions'111 $(['foo', 'bar']).map('a.toUpperCase()').toArray().should.eql ['FOO', 'BAR']112 end113 end114 115 describe '#first()'116 it 'should return the first value'117 $(['foo']).first().should.eql 'foo'118 end119 120 it 'should return the first n values'121 $([5,4,3,2,1]).first(2).at(0).should.eql 5122 $([5,4,3,2,1]).first(2).at(1).should.eql 4123 end124 125 it 'should work with objects'126 $({ foo: 'bar' }).first().should.eql 'bar'127 end 128 end129 130 describe '#last()'131 it 'should return the last value'132 $(['foo', 'bar']).last().should.eql 'bar'133 end134 135 it 'should return the last n values'136 $([5,4,3,2,1]).last(2).at(0).should.eql 2137 $([5,4,3,2,1]).last(2).at(1).should.eql 1138 end139 140 it 'should work with objects'141 $({ a: 'foo', b: 'bar' }).last(2).at(0).should.eql 'foo'142 $({ a: 'foo', b: 'bar' }).last(2).at(1).should.eql 'bar'143 $({ a: 'foo', b: 'bar' }).last().should.eql 'bar'144 end 145 end146 147 describe '#drop()'148 it 'should drop the first n values'149 $(1..5).drop(2).arr.should.eql 3..5150 end151 end152 153 describe '#find()'154 it 'should return the value of the first match'155 var result = $(['foo', 'bar']).find(function(val){ return val.charAt(0) == 'b' })156 result.should.eql 'bar'157 end158 159 it 'should return null when nothing matches'160 var result = $(['foo', 'bar']).find(function(val){ return val.charAt(0) == 'a' })161 result.should.be_null162 end163 164 it 'should work with objects'165 var result = $({ foo: 'bar', baz: 'raz' }).find(function(val, key){166 return val.charAt(0) == 'r'167 })168 result.should.eql 'raz'169 end170 171 it 'should allow shorthand expressions'172 $(['foo', 'bar']).find("a.charAt(0) == 'b'").should.eql 'bar'173 end174 end175 176 describe '#all()'177 it 'should return true when all evaluate to true'178 $(['foo', 'foobar']).all(function(val){ return val.charAt(0) == 'f' }).should.be_true179 end180 181 it 'should return false when any evaluate to false'182 $(['foo', 'bar', 'foobar']).all(function(val){ return val.charAt(0) == 'f' }).should.be_false183 end184 185 it 'should work with objects'186 $({ a: 'foo', b: 'foobar' }).all(function(val){ return val.charAt(0) == 'f' }).should.be_true187 end188 189 it 'should allow shorthand expressions'190 $(['foo', 'bar']).all('a.length > 2').should.be_true191 end192 end193 194 describe '#any()'195 it 'should return true when found'196 $(['foo', 'bar']).any(function(val){ return val.charAt(0) == 'b' }).should.be_true197 end198 199 it 'should return false when not found'200 $(['foo', 'bar']).any(function(val){ return val.charAt(0) == 'r' }).should.be_false201 end202 203 it 'should work with objects'204 $({ foo: 'bar' }).any(function(val){ return val.charAt(0) == 'b' }).should.be_true205 $({ foo: 'bar' }).any(function(val){ return val.charAt(0) == 'c' }).should.be_false206 end207 208 it 'should allow shorthand expressions'209 $(['foo', 'bar']).any('a.length > 2').should.be_true210 end211 end212 213 describe '#select()'214 it 'should return values which evaluate to true'215 var result = $([1,2,3,4,5]).select(function(n){ return n % 2 })216 result.at(0).should.eql 1217 result.at(1).should.eql 3218 result.at(2).should.eql 5219 end220 221 it 'should return a Collection'222 var result = $([1,2,3,4,5]).select(function(n){ return n % 2 })223 result.should.be_an_instance_of Collection224 end225 226 it 'should allow shorthand expressions'227 $([1,2,3,4,5]).select('a % 2').toArray().should.eql [1,3,5]228 end229 end230 231 describe '#reject()'232 it 'should return values which evaluate to false'233 var result = $([1,2,3,4,5,6]).reject(function(n){ return n % 2 })234 result.at(0).should.eql 2235 result.at(1).should.eql 4236 result.at(2).should.eql 6237 end238 239 it 'should return a Collection'240 var result = $([1,2,3,4,5]).reject(function(n){ return n % 2 })241 result.should.be_an_instance_of Collection242 end243 244 it 'should allow shorthand expressions'245 $(['foo', 'bar']).reject('a.charAt(0) == "b"').toArray().should.eql ['foo']246 end247 end248 249 describe '#slice()'250 it 'should return a slice of values'251 var collection = $(['foo', 'bar', 'baz']).slice(1, 3)252 collection.at(0).should.eql 'bar'253 collection.at(1).should.eql 'baz'254 end255 256 it 'should work with objects'257 var collection = $({ foo: 1, bar: 2, baz: 3, raz: 4 }).slice(1, 3)258 collection.at(0).should.eql 2259 collection.at(1).should.eql 3 260 end261 end262 263 describe '#grep()'264 it 'should select values matching the regular expression passed'265 var result = $(['foo', 'bar', 'foobar', 'baz']).grep(/foo(bar)?/)266 result.at(0).should.eql 'foo'267 result.at(1).should.eql 'foobar'268 result.at(2).should.be_null269 end270 end271 272 describe '#keys()'273 it 'should return indices when array-like'274 $(['foo', 'bar']).keys().at(0).should.eql '0'275 $(['foo', 'bar']).keys().at(1).should.eql '1'276 end277 278 it 'should return keys when an object'279 $({ foo: 'bar', baz: 'raz' }).keys().at(0).should.eql 'foo'280 $({ foo: 'bar', baz: 'raz' }).keys().at(1).should.eql 'baz'281 end282 end283 284 describe '#toArray()'285 it 'should return an array'286 $(['foo', 'bar']).keys().toArray().should.eql ['0', '1']287 end288 289 it 'should work on nested collections'290 $([$(['foo']), $(['bar'])]).toArray().should.eql [['foo'], ['bar']]291 end292 293 it 'should work with objects'294 $({ foo: 'bar', baz: { name: 'wahoo' }}).toArray().should.eql ['bar', { name: 'wahoo' }]295 end296 end297 298 describe '#min()'299 it 'should return the min value'300 $([4,5,2,3,62]).min().should.eql 2301 end302 end303 304 describe '#max()'305 it 'should return the max value'306 $([3,5,2,3,43,2]).max().should.eql 43307 end308 end309 310 describe '#length()'311 it 'should work with arrays'312 $([1,2,3]).length().should.eql 3313 end314 315 it 'should work with objects'316 $({ a: 'b', c: 'd', e: 'f' }).length().should.eql 3317 end318 end319 320 describe '#chunk()'321 it 'should group into chunks of the given size'322 $([1,1,2,2,3,3]).chunk(2).toArray().should.eql [[1,1],[2,2],[3,3]]323 $([1,1,2,2,3,3]).chunk(4).toArray().should.eql [[1,1,2,2],[3,3]]324 end325 end326 327 describe '#sum()'328 it 'should return the sum of the numeric values'329 $([1,2,3]).sum().should.eql 6330 end331 end332 333 describe '#avg()'334 it 'should return the average of numeric values'335 $([3,1]).avg().should.eql 2336 end337 end338 339 describe '#merge()'340 it 'should merge two array collections'341 var a = $([1,2,3])342 var b = $([4,5,6])343 a.merge(b).toArray().should.eql [1,2,3,4,5,6]344 345 var a = $([1,2,3])346 var b = [4,5,6]347 a.merge(b).toArray().should.eql [1,2,3,4,5,6]348 end349 350 it 'should merge two object collections'351 var a = $({ a: 'b' })352 var b = $({ c: 'd' })353 a.merge(b).arr.should.eql { a: 'b', c: 'd' }354 355 var a = $({ a: 'b' })356 var b = { c: 'd' }357 a.merge(b).arr.should.eql { a: 'b', c: 'd' }358 end359 360 it 'should merge an array and object collection'361 var a = $([1,2])362 var b = $({ a: 'b', c: 'd' })363 a.merge(b).arr.should.eql [1, 2, 'b', 'd']364 365 var a = $([1,2])366 var b = { a: 'b', c: 'd' }367 a.merge(b).arr.should.eql [1, 2, 'b', 'd']368 end369 370 it 'should merge an object and array collection'371 var a = $({ a: 'b', c: 'd' })372 var b = $([1,2])373 a.merge(b).arr.should.eql { a: 'b', c: 'd', 0: 1, 1: 2 }374 375 var a = $({ a: 'b', c: 'd' })376 var b = [1,2]377 a.merge(b).arr.should.eql { a: 'b', c: 'd', 0: 1, 1: 2 }378 end379 end380 381 describe '#clone()'382 it 'should clone an array collection'383 var a = $([1,2,3])384 var b = a.clone()385 a.should.not.equal b386 b.arr.should.eql [1,2,3]387 b.arr.should.not.equal a.arr388 end389 390 it 'should clone an object collection'391 var a = $({ foo: 'bar' })392 var b = a.clone()393 a.should.not.equal b394 b.arr.should.eql { foo: 'bar' }395 b.arr.should.not.equal a.arr396 end397 end398 399 describe '#sample()'400 it 'should return a random value'401 Math.stub('random').and_return(0.1)402 $([1,2,3,4]).sample().should.eql 1403 end404 end405 406 describe '#reverse()'407 it 'should reverse a collection'408 $([1,2,3]).reverse().toArray().should.eql [3,2,1]409 end410 end411 412 describe '#sort()'413 it 'should sort a collection'414 $([3,1,2]).sort().toArray().should.eql [1,2,3]415 end416 417 it 'should sort with a function'418 $([3,1,2]).sort(function(a, b){ return b - a }).toArray().should.eql [3,2,1]419 end420 421 it 'should allow shorthand expressions'422 $([3,1,2]).sort('-').toArray().should.eql [1,2,3]423 end424 end425 426 describe '#join()'427 it 'should join a collection with "" by default'428 $([1,2,3]).join().should.eql '123'429 end430 431 it 'should join with an arbitrary string'432 $([1,2,3]).join(' ').should.eql '1 2 3'433 end434 435 it 'should work with objects'436 $({ foo: 'bar', baz: 'raz' }).join(' ').should.eql 'bar raz'437 end438 end439 440 describe '#includes()'441 it 'should return true when the value is present'442 $([1,2,3]).includes(2).should.be_true443 end444 445 it 'should return true when all values are present'446 $([1,2,3]).includes(2, 3).should.be_true447 end448 449 it 'should return false when the value is not present'450 $([1,2,3]).includes(4).should.be_false451 end452 453 it 'should return false when the any value is not present'454 $([1,2,3]).includes(1,2,4).should.be_false455 $([1,2,3]).includes(1,4,2).should.be_false456 end457 end458 459 describe '#toString()'460 it 'should output [Collection ...] for array'461 $([1,2,3]).toString().should.eql '[Collection 1,2,3]'462 end463 464 it 'should output [Collection [object Object]] for object'465 $({ foo: "bar" }).toString().should.eql '[Collection [object Object]]'466 end467 468 it 'should output [Collection [[Collection ...]]] for nested collections'469 $([$([1,2,3])]).toString().should.eql '[Collection [Collection 1,2,3]]'470 end471 end472 473 end474end
modified:spec/spec.utils.js+2 added-2 removed
53 unmodified lines
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'58 utils.mergeParam('user[names][firstName]', 'tj', params)59 params.user.names.firstName.should.eql 'tj'60 end61 end62 63 describe 'with populated params'
75 unmodified lines
modified:.gitmodules+3 added
5 unmodified lines
6 url = git://github.com/visionmedia/js-oo.git7[submodule "lib/support/sass"]8 path = lib/support/sass9 url = git://github.com/visionmedia/sass.js.git10[submodule "lib/support/ext"]11 path = lib/support/ext12 url = git://github.com/visionmedia/ext.js.git
modified:Makefile+1 added-2 removed
4 unmodified lines
56init:7 @git submodule init && git submodule update89test: init9test:10 @$(NODE) spec/node.js all11 12app: app-chat13
3 unmodified lines
17app-upload:18 @$(NODE) examples/upload/app.js19 20benchmark:21 @$(NODE) benchmarks/collection.js21 @$(NODE) benchmarks/views.js22 23.PHONY: init test benchmark app
modified:Readme.md+4 added-12 removed
69 unmodified lines
70 run()71 72## Running Tests7374First we need to ensure all submodules are updated:75 76 $ make init7778Express uses the [JSpec](http://jspec.info) BDD JavaScript testing79framework to write and run elegant spec suites. JSpec is frozen 80to spec/lib and does not require separate installation.8178To run all specifications run the following command. This will ensure79git submodules are initialized and updated, as well as building test80related dependencies such as libxmljs.8182 $ make test83 84To run independent specs (which do not require building of external apis etc) use:8586 $ make test-independant87 88To run dependent specs (which require building of external apis etc) use:8990 $ make test-dependant91 84Run individual suites:8586 $ node spec/node.js core87 $ node spec/node.js mime
44 unmodified lines