Skip to content
Myr

Added Collection#sort()

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

authored by visionmedia · committed by visionmedia · parent c539e92 · Browse files · 2 files+29 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+29 added−0 removed

  • tests:+14 added−0 removed
  • other:+15 added−0 removed

2 files+29 added−0 removed

modified:lib/express/collection.js+15 added
19 unmodified lines
20 * @api private21 */2223function callback(fn) {24 if (fn === undefined) return25 return fn instanceof Function ? fn :26 fn.length < 4 ?27 Function('a, b, c', 'return a ' + fn + ' b') :28 Function('a, b, c', 'return ' + fn)
84 unmodified lines
113 return this.toArray().join(str || '')114 },115 116 /**117 * Sort collection with optional _fn_.118 *119 * @param {function} fn120 * @return {Collection}121 * @warn converts to array122 * @api public123 */124 125 sort: function(fn) {126 fn = callback(fn)127 return $(this.toArray().sort(fn))128 },129 130 /**131 * Iterate with _memo_ using callback _fn_.132 * The _memo_ object is passed as the first 133 * argument, and the return value of _fn_ becomes134 * the value of _memo_ providing a functional
389 unmodified lines
modified:spec/spec.collection.js+14 added
381 unmodified lines
382 $([1,2,3]).reverse().toArray().should.eql [3,2,1]383 end384 end385 386 describe '#sort()'387 it 'should sort a collection'388 $([3,1,2]).sort().toArray().should.eql [1,2,3]389 end390 391 it 'should sort with a function'392 $([3,1,2]).sort(function(a, b){ return b - a }).toArray().should.eql [3,2,1]393 end394 395 it 'should allow shorthand expressions'396 $([3,1,2]).sort('-').toArray().should.eql [1,2,3]397 end398 end399 400 describe '#join()'401 it 'should join a collection with "" by default'402 $([1,2,3]).join().should.eql '123'403 end
26 unmodified lines