Skip to content
Myr

Added Collection#last()

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

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

  • tests:+17 added−0 removed
  • other:+13 added−0 removed

2 files+30 added−0 removed

modified:lib/express/collection.js+13 added
157 unmodified lines
158 return n ? this.slice(0, n) : this.at(0)159 },160 161 /**162 * Return the last _n_ value(s), defaults to -1.163 *164 * @param {int} n165 * @return {mixed}166 * @api public167 */168 169 last: function(n) {170 var len = this.length()171 return n ? this.slice(len-n, len) : this.at(--len)172 },173 174 /**175 * Return a slice of values from _start_ to _end_.176 *177 * @param {int} start178 * @param {int} end
258 unmodified lines
modified:spec/spec.collection.js+17 added
92 unmodified lines
93 $({ foo: 'bar' }).first().should.eql 'bar'94 end 95 end96 97 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 114 describe '#find()'115 it 'should return the value of the first match'116 var result = $(['foo', 'bar']).find(function(val){ return val.charAt(0) == 'b' })117 result.should.eql 'bar'
238 unmodified lines