mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
correct size() impl for object's w/ 'length' prop
the original implementation returned incorrect value value for objects with 'length' property.
This commit is contained in:
parent
96a1df192a
commit
a4863d5244
2 changed files with 14 additions and 8 deletions
|
|
@ -487,17 +487,19 @@ function map(obj, iterator, context) {
|
|||
*/
|
||||
function size(obj, ownPropsOnly) {
|
||||
var size = 0, key;
|
||||
if (obj) {
|
||||
if (isNumber(obj.length)) {
|
||||
return obj.length;
|
||||
} else if (isObject(obj)){
|
||||
for (key in obj)
|
||||
if (!ownPropsOnly || obj.hasOwnProperty(key))
|
||||
size++;
|
||||
}
|
||||
|
||||
if (isArray(obj) || isString(obj)) {
|
||||
return obj.length;
|
||||
} else if (isObject(obj)){
|
||||
for (key in obj)
|
||||
if (!ownPropsOnly || obj.hasOwnProperty(key))
|
||||
size++;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
function includes(array, obj) {
|
||||
for ( var i = 0; i < array.length; i++) {
|
||||
if (obj === array[i]) return true;
|
||||
|
|
|
|||
|
|
@ -133,6 +133,10 @@ describe('angular', function(){
|
|||
expect(size('')).toBe(0);
|
||||
expect(size('abc')).toBe(3);
|
||||
});
|
||||
|
||||
it('should not rely on length property of an object to determine its size', function() {
|
||||
expect(size({length:99})).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue