Collection now works with objects
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent df0f71e · Browse files · 2 files·+30 added−13 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−13 removed
- tests:+5 added−5 removed
- other:+25 added−8 removed
Provenance
- Commit
f9ee6d1- Tree
fe9e9a2- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
df0f71e
2 files changed+30 added−13 removed
modified:lib/express/collection.js+25 added-8 removed
1234567892 unmodified lines121314151415161716171819203 unmodified lines24252627282929303132333435363738394041344243444546474849424350515253545556575859606162636412 unmodified lines
// Express - Collection - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
$break = '__break__'
Collection = Class({ /** * Initialize collection with an array-like object.2 unmodified lines * @api private */ init: function(arr) { var clone = toArray(arr) this.length = clone.length clone.unshift(0, clone.length) Array.prototype.splice.apply(this, clone) this.arr = arr }, /** * Return the value of _index_ or null.3 unmodified lines * @api public */ at: function(index) { if ('length' in this.arr) return this[index] return this.arr[index] var result, i = 0 this.each(function(val){ if (i++ == index) { result = val throw $break } }) return result }, /** * Iterate collection using callback _fn_, * passing both the index and value. * passing both the value and index. * * @param {function} fn * @return {Collection} * @api public */ each: function(fn) { for (var i = 0, len = this.length; i < len; ++i) fn(i, this[i]) try { if (this.arr.forEach) this.arr.forEach(fn) else for (var key in this.arr) if (this.arr.hasOwnProperty(key)) fn(this.arr[key], key) } catch (e) { if (e != $break) throw e } return this }})
12 unmodified linesmodified:spec/spec.collection.js+5 added-5 removed
30 unmodified lines31323334353536373839404142424344454647484950515051525353545556574 unmodified lines30 unmodified lines it 'should work with objects' $({ foo: 'bar', baz: 'raz' }).at(0).should.eql 'bar' $({ foo: 'bar', baz: 'raz' }).at(1).should.eql 'raz' $({ foo: 'bar', baz: 'raz' }).at(0).should.be_null $({ foo: 'bar', baz: 'raz' }).at(2).should.be_null end end describe '#each()' it 'should iterate passing index and value' var result = [] $(['foo', 'bar']).each(function(i, val){ $(['foo', 'bar']).each(function(val, i){ result.push(i, val) }) result.should.eql [0, 'foo', 1, 'bar'] end it 'should work with objects' var result = [] $({ foo: 'bar', baz: 'raz' }).each(function(k, val){ result.push(k, val) $({ foo: 'bar', baz: 'raz' }).each(function(val, key){ result.push(key, val) }) result.should.eql ['foo', 'bar', 'baz', 'bar'] result.should.eql ['foo', 'bar', 'baz', 'raz'] end it 'should return the collection' $([]).each(function(){}).should.be_an_instance_of Collection4 unmodified lines