spec.collection.js
This file
Path
spec/spec.collection.js
366 lines · 10.8 KiB · JavaScript
Last changed by
Stubbing Math.random() for Collection#sample() specvisionmediabefore adoptionc19b6e2
History of this fileTake it
Started collection specsvisionmediabefore adoptiond9a977f
lines 1–4why, one step back12process.mixin(require('express/collection'))34describe 'Express'Added Collection#at()visionmediabefore adoption3153eef
line 5why, one step back5 describe 'Collection'Started collection specsvisionmediabefore adoptiond9a977f
lines 6–9why, one step back6 describe '$(array)'7 it 'should return a Collection'8 $(['foo', 'bar']).should.be_an_instance_of Collection9 endCollection specs with objectsvisionmediabefore adoptiondf0f71e
lines 10–14why, one step back10 end11 12 describe '$(object)'13 it 'should return a Collection'14 $({ foo: 'bar' }).should.be_an_instance_of CollectionStarted collection specsvisionmediabefore adoptiond9a977f
lines 15–21why, one step back15 end16 end17 18 describe '$(Collection)'19 it 'should return the collection passed'20 var collection = $(['foo'])21 $(collection).should.equal collectionAdded Collection#at()visionmediabefore adoption3153eef
lines 22–25why, one step back22 end23 end24 25 describe '#at()'Docsvisionmediabefore adoption6968077
line 26why, one step back26 it 'should return the value at the given index'Added Collection#at()visionmediabefore adoption3153eef
lines 27–29why, one step back27 $(['foo', 'bar']).at(0).should.eql 'foo'28 $(['foo', 'bar']).at(1).should.eql 'bar'29 $(['foo', 'bar']).at(2).should.be_nullStarted collection specsvisionmediabefore adoptiond9a977f
line 30why, one step back30 endCollection specs with objectsvisionmediabefore adoptiondf0f71e
lines 31–34why, one step back31 32 it 'should work with objects'33 $({ foo: 'bar', baz: 'raz' }).at(0).should.eql 'bar'34 $({ foo: 'bar', baz: 'raz' }).at(1).should.eql 'raz'Collection now works with objectsvisionmediabefore adoptionf9ee6d1
line 35why, one step back35 $({ foo: 'bar', baz: 'raz' }).at(2).should.be_nullCollection specs with objectsvisionmediabefore adoptiondf0f71e
line 36why, one step back36 endStarted collection specsvisionmediabefore adoptiond9a977f
line 37why, one step back37 endAdded Collection#each()visionmediabefore adoption6f0183d
lines 38–41why, one step back38 39 describe '#each()'40 it 'should iterate passing index and value'41 var result = []Collection now works with objectsvisionmediabefore adoptionf9ee6d1
line 42why, one step back42 $(['foo', 'bar']).each(function(val, i){Added Collection#each()visionmediabefore adoption6f0183d
lines 43–45why, one step back43 result.push(i, val)44 })45 result.should.eql [0, 'foo', 1, 'bar']Collection specs with objectsvisionmediabefore adoptiondf0f71e
lines 46–49why, one step back46 end47 48 it 'should work with objects'49 var result = []Collection now works with objectsvisionmediabefore adoptionf9ee6d1
lines 50–51why, one step back50 $({ foo: 'bar', baz: 'raz' }).each(function(val, key){51 result.push(key, val)Collection specs with objectsvisionmediabefore adoptiondf0f71e
line 52why, one step back52 })Collection now works with objectsvisionmediabefore adoptionf9ee6d1
line 53why, one step back53 result.should.eql ['foo', 'bar', 'baz', 'raz']Added Collection#each()visionmediabefore adoption6f0183d
lines 54–59why, one step back54 end55 56 it 'should return the collection'57 $([]).each(function(){}).should.be_an_instance_of Collection58 end59 endAdded Collection#map()visionmediabefore adoptionb5bbab9
line 60why, one step back60 Added Collection#reduce()visionmediabefore adoptioneb676a2
lines 61–67why, one step back61 describe '#reduce()'62 it 'should iterate with memo object'63 var sum = $([1,2,3]).reduce(0, function(sum, n){ return sum + n })64 sum.should.eql 665 end66 end67 Added Collection#map()visionmediabefore adoptionb5bbab9
lines 68–80why, one step back68 describe '#map()'69 it 'should iterate collecting results into a new collection'70 var collection = $(['foo', 'bar']).map(function(val){ return val.toUpperCase() })71 collection.at(0).should.eql 'FOO'72 collection.at(1).should.eql 'BAR'73 end74 75 it 'should work with objects'76 var collection = $({ foo: 'bar', baz: 'raz' }).map(function(val){ return val.toUpperCase() })77 collection.at(0).should.eql 'BAR'78 collection.at(1).should.eql 'RAZ'79 end80 endAdded Collection#first()visionmediabefore adoptionb5e48c9
lines 81–86why, one step back81 82 describe '#first()'83 it 'should return the first value'84 $(['foo']).first().should.eql 'foo'85 end86 Added first(n) supportvisionmediabefore adoption0669b54
lines 87–91why, one step back87 it 'should return the first n values'88 $([5,4,3,2,1]).first(2).at(0).should.eql 589 $([5,4,3,2,1]).first(2).at(1).should.eql 490 end91 Added Collection#first()visionmediabefore adoptionb5e48c9
lines 92–96why, one step back92 it 'should work with objects'93 $({ foo: 'bar' }).first().should.eql 'bar'94 end 95 end96 Added Collection#last()visionmediabefore adoption3622914
lines 97–113why, one step back97 describe '#last()'98 it 'should return the last value'99 $(['foo', 'bar']).last().should.eql 'bar'100 end101 102 it 'should return the last n values'103 $([5,4,3,2,1]).last(2).at(0).should.eql 2104 $([5,4,3,2,1]).last(2).at(1).should.eql 1105 end106 107 it 'should work with objects'108 $({ a: 'foo', b: 'bar' }).last(2).at(0).should.eql 'foo'109 $({ a: 'foo', b: 'bar' }).last(2).at(1).should.eql 'bar'110 $({ a: 'foo', b: 'bar' }).last().should.eql 'bar'111 end 112 end113 Added Collection#drop()visionmediabefore adoption4101e4a
lines 114–119why, one step back114 describe '#drop()'115 it 'should drop the first n values'116 $(1..5).drop(2).arr.should.eql 3..5117 end118 end119 Added Collection#find()visionmediabefore adoptionb9d526c
lines 120–138why, one step back120 describe '#find()'121 it 'should return the value of the first match'122 var result = $(['foo', 'bar']).find(function(val){ return val.charAt(0) == 'b' })123 result.should.eql 'bar'124 end125 126 it 'should return null when nothing matches'127 var result = $(['foo', 'bar']).find(function(val){ return val.charAt(0) == 'a' })128 result.should.be_null129 end130 131 it 'should work with objects'132 var result = $({ foo: 'bar', baz: 'raz' }).find(function(val, key){133 return val.charAt(0) == 'r'134 })135 result.should.eql 'raz'136 end137 end138 Added Collection#all()visionmediabefore adoption7a4a900
lines 139–152why, one step back139 describe '#all()'140 it 'should return true when all evaluate to true'141 $(['foo', 'foobar']).all(function(val){ return val.charAt(0) == 'f' }).should.be_true142 end143 144 it 'should return false when any evaluate to false'145 $(['foo', 'bar', 'foobar']).all(function(val){ return val.charAt(0) == 'f' }).should.be_false146 end147 148 it 'should work with objects'149 $({ a: 'foo', b: 'foobar' }).all(function(val){ return val.charAt(0) == 'f' }).should.be_true150 end151 end152 Added Collection#any()visionmediabefore adoption3276ec8
lines 153–160why, one step back153 describe '#any()'154 it 'should return true when found'155 $(['foo', 'bar']).any(function(val){ return val.charAt(0) == 'b' }).should.be_true156 end157 158 it 'should return false when not found'159 $(['foo', 'bar']).any(function(val){ return val.charAt(0) == 'r' }).should.be_false160 endCollection#any() now uses V8s #some()visionmediabefore adoption5552e7e
lines 161–165why, one step back161 162 it 'should work with objects'163 $({ foo: 'bar' }).any(function(val){ return val.charAt(0) == 'b' }).should.be_true164 $({ foo: 'bar' }).any(function(val){ return val.charAt(0) == 'c' }).should.be_false165 endAdded Collection#any()visionmediabefore adoption3276ec8
lines 166–167why, one step back166 end167 Added Collection#select()visionmediabefore adoptionca229ba
lines 168–170why, one step back168 describe '#select()'169 it 'should return values which evaluate to true'170 var result = $([1,2,3,4,5]).select(function(n){ return n % 2 })Added Collection#reject()visionmediabefore adoptiondd76816
lines 171–173why, one step back171 result.at(0).should.eql 1172 result.at(1).should.eql 3173 result.at(2).should.eql 5Added Collection#select()visionmediabefore adoptionca229ba
lines 174–181why, one step back174 end175 176 it 'should return a Collection'177 var result = $([1,2,3,4,5]).select(function(n){ return n % 2 })178 result.should.be_an_instance_of Collection179 end180 end181 Added Collection#reject()visionmediabefore adoptiondd76816
lines 182–195why, one step back182 describe '#reject()'183 it 'should return values which evaluate to false'184 var result = $([1,2,3,4,5,6]).reject(function(n){ return n % 2 })185 result.at(0).should.eql 2186 result.at(1).should.eql 4187 result.at(2).should.eql 6188 end189 190 it 'should return a Collection'191 var result = $([1,2,3,4,5]).reject(function(n){ return n % 2 })192 result.should.be_an_instance_of Collection193 end194 end195 Added Collection#slice()visionmediabefore adoption3e845a8
lines 196–209why, one step back196 describe '#slice()'197 it 'should return a slice of values'198 var collection = $(['foo', 'bar', 'baz']).slice(1, 3)199 collection.at(0).should.eql 'bar'200 collection.at(1).should.eql 'baz'201 end202 203 it 'should work with objects'204 var collection = $({ foo: 1, bar: 2, baz: 3, raz: 4 }).slice(1, 3)205 collection.at(0).should.eql 2206 collection.at(1).should.eql 3 207 end208 end209 Added Collection#grep()visionmediabefore adoption9d02ed2
lines 210–218why, one step back210 describe '#grep()'211 it 'should select values matching the regular expression passed'212 var result = $(['foo', 'bar', 'foobar', 'baz']).grep(/foo(bar)?/)213 result.at(0).should.eql 'foo'214 result.at(1).should.eql 'foobar'215 result.at(2).should.be_null216 end217 end218 Added Collection#keys()visionmediabefore adoption933baf9
lines 219–227why, one step back219 describe '#keys()'220 it 'should return indices when array-like'221 $(['foo', 'bar']).keys().at(0).should.eql 0222 $(['foo', 'bar']).keys().at(1).should.eql 1223 end224 225 it 'should return keys when an object'226 $({ foo: 'bar', baz: 'raz' }).keys().at(0).should.eql 'foo'227 $({ foo: 'bar', baz: 'raz' }).keys().at(1).should.eql 'baz'Added specs for sort()visionmediabefore adoption123b981
lines 228–230why, one step back228 end229 end230 Added Collection#toArray()visionmediabefore adoptionb3429a4
lines 231–233why, one step back231 describe '#toArray()'232 it 'should return an array'233 $(['foo', 'bar']).keys().toArray().should.eql [0, 1]Added Collection#{min,max}()visionmediabefore adoption996271b
line 234why, one step back234 endMore specs for Collection#toArray()visionmediabefore adoption4eee822
lines 235–242why, one step back235 236 it 'should work on nested collections'237 $([$(['foo']), $(['bar'])]).toArray().should.eql [['foo'], ['bar']]238 end239 240 it 'should work with objects'241 $({ foo: 'bar', baz: { name: 'wahoo' }}).toArray().should.eql ['bar', { name: 'wahoo' }]242 endAdded Collection#{min,max}()visionmediabefore adoption996271b
lines 243–250why, one step back243 end244 245 describe '#min()'246 it 'should return the min value'247 $([4,5,2,3,62]).min().should.eql 2248 end249 end250 Added Collection#chunk()visionmediabefore adoptionc0a15f2
line 251why, one step back251 describe '#max()'Added Collection#{min,max}()visionmediabefore adoption996271b
lines 252–253why, one step back252 it 'should return the max value'253 $([3,5,2,3,43,2]).max().should.eql 43Added Collection#chunk()visionmediabefore adoptionc0a15f2
lines 254–256why, one step back254 end255 end256 Added Collection#length()visionmediabefore adoptiond1f5c08
lines 257–266why, one step back257 describe '#length()'258 it 'should work with arrays'259 $([1,2,3]).length().should.eql 3260 end261 262 it 'should work with objects'263 $({ a: 'b', c: 'd', e: 'f' }).length().should.eql 3264 end265 end266 Added Collection#chunk()visionmediabefore adoptionc0a15f2
lines 267–270why, one step back267 describe '#chunk()'268 it 'should group into chunks of the given size'269 $([1,1,2,2,3,3]).chunk(2).toArray().should.eql [[1,1],[2,2],[3,3]]270 $([1,1,2,2,3,3]).chunk(4).toArray().should.eql [[1,1,2,2],[3,3]]Added Collection#toArray()visionmediabefore adoptionb3429a4
lines 271–273why, one step back271 end272 end273 Added Collection#sum()visionmediabefore adoption16ef731
lines 274–279why, one step back274 describe '#sum()'275 it 'should return the sum of the numeric values'276 $([1,2,3]).sum().should.eql 6277 end278 end279 Added Collection#avg()visionmediabefore adoption68b1089
lines 280–285why, one step back280 describe '#avg()'281 it 'should return the average of numeric values'282 $([3,1]).avg().should.eql 2283 end284 end285 Added Collection#merge()visionmediabefore adoption660ac65
lines 286–327why, one step back286 describe '#merge()'287 it 'should merge two array collections'288 var a = $([1,2,3])289 var b = $([4,5,6])290 a.merge(b).toArray().should.eql [1,2,3,4,5,6]291 292 var a = $([1,2,3])293 var b = [4,5,6]294 a.merge(b).toArray().should.eql [1,2,3,4,5,6]295 end296 297 it 'should merge two object collections'298 var a = $({ a: 'b' })299 var b = $({ c: 'd' })300 a.merge(b).arr.should.eql { a: 'b', c: 'd' }301 302 var a = $({ a: 'b' })303 var b = { c: 'd' }304 a.merge(b).arr.should.eql { a: 'b', c: 'd' }305 end306 307 it 'should merge an array and object collection'308 var a = $([1,2])309 var b = $({ a: 'b', c: 'd' })310 a.merge(b).arr.should.eql [1, 2, 'b', 'd']311 312 var a = $([1,2])313 var b = { a: 'b', c: 'd' }314 a.merge(b).arr.should.eql [1, 2, 'b', 'd']315 end316 317 it 'should merge an object and array collection'318 var a = $({ a: 'b', c: 'd' })319 var b = $([1,2])320 a.merge(b).arr.should.eql { a: 'b', c: 'd', 0: 1, 1: 2 }321 322 var a = $({ a: 'b', c: 'd' })323 var b = [1,2]324 a.merge(b).arr.should.eql { a: 'b', c: 'd', 0: 1, 1: 2 }325 end326 end327 Added Collection#clone()visionmediabefore adoption1ee61b4
lines 328–343why, one step back328 describe '#clone()'329 it 'should clone an array collection'330 var a = $([1,2,3])331 var b = a.clone()332 a.should.not.equal b333 b.arr.should.eql [1,2,3]334 end335 336 it 'should clone an object collection'337 var a = $({ foo: 'bar' })338 var b = a.clone()339 a.should.not.equal b340 b.arr.should.eql { foo: 'bar' }341 end342 end343 Added Collection#sample()visionmediabefore adoption95e9e3d
lines 344–345why, one step back344 describe '#sample()'345 it 'should return a random value'Stubbing Math.random() for Collection#sample() specvisionmediabefore adoptionc19b6e2
lines 346–347why, one step back346 Math.stub('random').and_return(0.1)347 $([1,2,3,4]).sample().should.eql 1Added Collection#sample()visionmediabefore adoption95e9e3d
lines 348–350why, one step back348 end349 end350 Added Collection#toString()visionmediabefore adoptioneebf938
lines 351–364why, one step back351 describe '#toString()'352 it 'should output [Collection ...] for array'353 $([1,2,3]).toString().should.eql '[Collection 1,2,3]'354 end355 356 it 'should output [Collection [object Object]] for object'357 $({ foo: "bar" }).toString().should.eql '[Collection [object Object]]'358 end359 360 it 'should output [Collection [[Collection ...]]] for nested collections'361 $([$([1,2,3])]).toString().should.eql '[Collection [Collection 1,2,3]]'362 end363 end364 Started collection specsvisionmediabefore adoptiond9a977f
lines 365–366why, one step back365 end366end