Skip to content

Commit

Permalink
after review improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioCrisostomo committed Feb 8, 2015
1 parent ca4b321 commit 38adb5e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
54 changes: 29 additions & 25 deletions Source/Core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,19 @@ for (var i in {toString: 1}) enumerables = null;
if (enumerables) enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'constructor'];
/*</ltIE8>*/
var hasOwnProperty = Object.prototype.hasOwnProperty;
function eachKey(object, fn, thisArg){
for (var k in object) fn.call(thisArg, k, object[k]);
function objectKeys(object, alsoPrototypeChain){
var keys = [];
for (var k in object){
if (alsoPrototypeChain) keys.push(k);
else hasOwnProperty.call(object, k) && keys.push(k);
}
/*<ltIE8>*/
if (enumerables) for (var i = enumerables.length; i--;){
k = enumerables[i];
if (hasOwnProperty.call(object, k)) fn.call(thisArg, k, object[k]);
if (hasOwnProperty.call(object, k)) keys.push(k);
}
/*</ltIE8>*/
return keys;
}

// Function overloading
Expand All @@ -81,8 +86,12 @@ Function.prototype.overloadSetter = function(usePlural){
var self = this;
return function(a, b){
if (a == null) return this;
if (usePlural || typeof a != 'string') eachKey(a, self, this);
else self.call(this, a, b);
if (usePlural || typeof a != 'string'){
var keys = objectKeys(a, true), k;
for (var i = 0; k = keys[i]; i++) self.call(this, k, a[k]);
} else {
self.call(this, a, b);
}
return this;
};
};
Expand Down Expand Up @@ -308,26 +317,6 @@ Number.extend('random', function(min, max){

// forEach, each, keys

Object.extend({

keys: function(object){
var keys = [];
eachKey(object, function(key, value){
if (hasOwnProperty.call(object, key)) keys.push(key);
}, this);
return keys;
},

forEach: function(object, fn, bind){
Object.keys(object).forEach(function(key){
fn.call(bind, object[key], key, object);
});
}

});

Object.each = Object.forEach;

Array.implement({

/*<!ES5>*/
Expand All @@ -345,6 +334,21 @@ Array.implement({

});

Object.extend({

keys: objectKeys,

forEach: function(object, fn, bind){
Object.keys(object).forEach(function(key){
fn.call(bind, object[key], key, object);
});
}

});

Object.each = Object.forEach;


// Array & Object cloning, Object merging and appending

var cloneOf = function(item){
Expand Down
10 changes: 7 additions & 3 deletions Source/Types/Object.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ Object.extend({
},

values: function(object){
return Object.keys(object).map(function(key){
return object[key];
});
var values = [];
var keys = Object.keys(object);
for (var i = 0; i < keys.length; i++){
var k = keys[i];
values.push(object[k]);
}
return values;
},

getLength: function(object){
Expand Down
2 changes: 0 additions & 2 deletions Specs/Core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,6 @@ describe('Object.each', function(){
expect(daysObj).toEqual({first: 'Sunday', second: 'Monday', third: 'Tuesday'});
});

/*<ltIE8>*/
it('should call non-enumerable properties too', function(){
var obj = {
foo: 'bar',
Expand All @@ -835,7 +834,6 @@ describe('Object.each', function(){
expect(keysInObject).toBeTruthy();
expect(iteration).toEqual(8);
});
/*</ltIE8>*/

});

Expand Down

0 comments on commit 38adb5e

Please sign in to comment.