select(), reject(), all(), and any() allow shorthand expressions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 0d71a80 · Browse files · 2 files·+20 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+20 added−0 removed
- tests:+16 added−0 removed
- other:+4 added−0 removed
Provenance
- Commit
8c20409- Tree
1f53385- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
0d71a80
2 files changed+20 added−0 removed
modified:lib/express/collection.js+4 added
137 unmodified lines138 * @api public139 */140 141 select: function(fn) {142+ fn = callback(fn)143 return $(this.reduce([], function(array, val, key){144 if (fn(val, key))145 array.push(val)146 return array9 unmodified lines156 * @api public157 */158 159 reject: function(fn) {160+ fn = callback(fn)161 return $(this.reduce([], function(array, val, key){162 if (!fn(val, key))163 array.push(val)164 return array93 unmodified lines258 * @api public259 */260 261 any: function(fn) {262+ fn = callback(fn)263 if (this.arr.some)264 return this.arr.some(fn)265 return !! this.find(fn)266 },7 unmodified lines274 * @api public275 */276 277 all: function(fn) {278+ fn = callback(fn)279 if (this.arr.every)280 return this.arr.every(fn)281 return this.reduce(true, function(state, val, key){282 if (!state) throw $break201 unmodified linesmodified:spec/spec.collection.js+16 added
159 unmodified lines160 161 it 'should work with objects'162 $({ a: 'foo', b: 'foobar' }).all(function(val){ return val.charAt(0) == 'f' }).should.be_true163 end164+ 165+ it 'should allow shorthand expressions'166+ $(['foo', 'bar']).all('a.length > 2').should.be_true167+ end168 end169 170 describe '#any()'171 it 'should return true when found'7 unmodified lines179 it 'should work with objects'180 $({ foo: 'bar' }).any(function(val){ return val.charAt(0) == 'b' }).should.be_true181 $({ foo: 'bar' }).any(function(val){ return val.charAt(0) == 'c' }).should.be_false182 end183+ 184+ it 'should allow shorthand expressions'185+ $(['foo', 'bar']).any('a.length > 2').should.be_true186+ end187 end188 189 describe '#select()'190 it 'should return values which evaluate to true'6 unmodified lines197 it 'should return a Collection'198 var result = $([1,2,3,4,5]).select(function(n){ return n % 2 })199 result.should.be_an_instance_of Collection200 end201+ 202+ it 'should allow shorthand expressions'203+ $([1,2,3,4,5]).select('a % 2').toArray().should.eql [1,3,5]204+ end205 end206 207 describe '#reject()'208 it 'should return values which evaluate to false'6 unmodified lines215 it 'should return a Collection'216 var result = $([1,2,3,4,5]).reject(function(n){ return n % 2 })217 result.should.be_an_instance_of Collection218 end219+ 220+ it 'should allow shorthand expressions'221+ $(['foo', 'bar']).reject('a.charAt(0) == "b"').toArray().should.eql ['foo']222+ end223 end224 225 describe '#slice()'226 it 'should return a slice of values'169 unmodified lines