Skip to content

Commit 96a1df1

Browse files
committed
extend size() to take ownPropsOnly param
- extend size() to take size(obj, ownPropsOnly) - add specs for size() - update docs to mention string support - use size() in ng:repeat including the hasOwnProp check for all object doesn't create significant perf penalty: http://jsperf.com/dedicated-code-branch-for-hasownprop
1 parent 89c25fe commit 96a1df1

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

src/Angular.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,14 @@ function map(obj, iterator, context) {
461461
* @function
462462
*
463463
* @description
464-
* Determines the number of elements in an array or number of properties of an object.
464+
* Determines the number of elements in an array, number of properties of an object or string
465+
* length.
465466
*
466467
* Note: this function is used to augment the Object type in angular expressions. See
467468
* {@link angular.Object} for more info.
468469
*
469-
* @param {Object|Array} obj Object or array to inspect.
470+
* @param {Object|Array|string} obj Object, array or string to inspect.
471+
* @param {boolean} [ownPropsOnly=false] Count only "own" properties in an object
470472
* @returns {number} The size of `obj` or `0` if `obj` is neither an object or an array.
471473
*
472474
* @example
@@ -483,14 +485,15 @@ function map(obj, iterator, context) {
483485
* </doc:scenario>
484486
* </doc:example>
485487
*/
486-
function size(obj) {
488+
function size(obj, ownPropsOnly) {
487489
var size = 0, key;
488490
if (obj) {
489491
if (isNumber(obj.length)) {
490492
return obj.length;
491493
} else if (isObject(obj)){
492494
for (key in obj)
493-
size++;
495+
if (!ownPropsOnly || obj.hasOwnProperty(key))
496+
size++;
494497
}
495498
}
496499
return size;

src/widgets.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -913,19 +913,10 @@ angularWidget('@ng:repeat', function(expression, element){
913913
childCount = children.length,
914914
lastIterElement = iterStartElement,
915915
collection = this.$tryEval(rhs, iterStartElement),
916-
is_array = isArray(collection),
917-
collectionLength = 0,
916+
collectionLength = size(collection, true),
918917
childScope,
919918
key;
920919

921-
if (is_array) {
922-
collectionLength = collection.length;
923-
} else {
924-
for (key in collection)
925-
if (collection.hasOwnProperty(key))
926-
collectionLength++;
927-
}
928-
929920
for (key in collection) {
930921
if (collection.hasOwnProperty(key)) {
931922
if (index < childCount) {

test/AngularSpec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ describe('angular', function(){
110110
});
111111
});
112112

113+
114+
describe('size', function() {
115+
it('should return the number of items in an array', function() {
116+
expect(size([])).toBe(0);
117+
expect(size(['a', 'b', 'c'])).toBe(3);
118+
});
119+
120+
it('should return the number of properties of an object', function() {
121+
expect(size({})).toBe(0);
122+
expect(size({a:1, b:'a', c:noop})).toBe(3);
123+
});
124+
125+
it('should return the number of own properties of an object', function() {
126+
var obj = inherit({protoProp: 'c', protoFn: noop}, {a:1, b:'a', c:noop});
127+
128+
expect(size(obj)).toBe(5);
129+
expect(size(obj, true)).toBe(3);
130+
});
131+
132+
it('should return the string length', function() {
133+
expect(size('')).toBe(0);
134+
expect(size('abc')).toBe(3);
135+
});
136+
});
137+
138+
113139
describe('parseKeyValue', function() {
114140
it('should parse a string into key-value pairs', function() {
115141
expect(parseKeyValue('')).toEqual({});

0 commit comments

Comments
 (0)