Skip to content
Myr

find() and map() allow shorthand

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

authored by visionmedia · committed by visionmedia · parent 87d8b24 · Browse files · 2 files+15 added−2 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+15 added−2 removed

  • tests:+9 added−1 removed
  • other:+6 added−1 removed

2 files+15 added−2 removed

modified:lib/express/collection.js+6 added-1 removed
9 unmodified lines
10/**11 * Normalize callback _fn_. When a string is12 * passed convert the shorthand expr to a function.13 *14 * - Functions are passed through un-touched15 * - Strings with length of < 4 are considered operators between a and b16 * - Larger strings are considered return expressions17 *18 * @param {string, function} fn19 * @return {function}20 * @api private21 */
97 unmodified lines
119 * @api public120 */121 122 map: function(fn) {123 fn = callback(fn)124 if (this.arr.map)125 return $(this.arr.map(fn))126 return $(this.reduce([], function(array, val, key){127 array.push(fn(val, key))
108 unmodified lines
236 * @api public237 */238 239 find: function(fn) {235 var result240 var result, fn = callback(fn)241 this.each(function(val, key){242 if (fn(val, key)) {243 result = val244 throw $break
235 unmodified lines
modified:spec/spec.collection.js+9 added-1 removed
62 unmodified lines
63 var sum = $([1,2,3]).reduce(0, function(sum, n){ return sum + n })64 sum.should.eql 665 end66 67 it 'should allow expression shorthand'67 it 'should allow shorthand expressions'68 $([1,2,3]).reduce(0, '+').should.eql 669 $([1,2,3]).reduce(0, 'a + b').should.eql 670 end71 end
9 unmodified lines
81 var collection = $({ foo: 'bar', baz: 'raz' }).map(function(val){ return val.toUpperCase() })82 collection.at(0).should.eql 'BAR'83 collection.at(1).should.eql 'RAZ'84 end85 86 it 'should allow shorthand expressions'87 $(['foo', 'bar']).map('a.toUpperCase()').toArray().should.eql ['FOO', 'BAR']88 end89 end90 91 describe '#first()'92 it 'should return the first value'
49 unmodified lines
142 return val.charAt(0) == 'r'143 })144 result.should.eql 'raz'145 end146 147 it 'should allow shorthand expressions'148 $(['foo', 'bar']).find("a.charAt(0) == 'b'").should.eql 'bar'149 end150 end151 152 describe '#all()'153 it 'should return true when all evaluate to true'
226 unmodified lines