mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
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
This commit is contained in:
parent
89c25fe713
commit
96a1df192a
3 changed files with 34 additions and 14 deletions
|
|
@ -461,12 +461,14 @@ function map(obj, iterator, context) {
|
|||
* @function
|
||||
*
|
||||
* @description
|
||||
* Determines the number of elements in an array or number of properties of an object.
|
||||
* Determines the number of elements in an array, number of properties of an object or string
|
||||
* length.
|
||||
*
|
||||
* Note: this function is used to augment the Object type in angular expressions. See
|
||||
* {@link angular.Object} for more info.
|
||||
*
|
||||
* @param {Object|Array} obj Object or array to inspect.
|
||||
* @param {Object|Array|string} obj Object, array or string to inspect.
|
||||
* @param {boolean} [ownPropsOnly=false] Count only "own" properties in an object
|
||||
* @returns {number} The size of `obj` or `0` if `obj` is neither an object or an array.
|
||||
*
|
||||
* @example
|
||||
|
|
@ -483,14 +485,15 @@ function map(obj, iterator, context) {
|
|||
* </doc:scenario>
|
||||
* </doc:example>
|
||||
*/
|
||||
function size(obj) {
|
||||
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)
|
||||
size++;
|
||||
if (!ownPropsOnly || obj.hasOwnProperty(key))
|
||||
size++;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
|
|
|
|||
|
|
@ -913,19 +913,10 @@ angularWidget('@ng:repeat', function(expression, element){
|
|||
childCount = children.length,
|
||||
lastIterElement = iterStartElement,
|
||||
collection = this.$tryEval(rhs, iterStartElement),
|
||||
is_array = isArray(collection),
|
||||
collectionLength = 0,
|
||||
collectionLength = size(collection, true),
|
||||
childScope,
|
||||
key;
|
||||
|
||||
if (is_array) {
|
||||
collectionLength = collection.length;
|
||||
} else {
|
||||
for (key in collection)
|
||||
if (collection.hasOwnProperty(key))
|
||||
collectionLength++;
|
||||
}
|
||||
|
||||
for (key in collection) {
|
||||
if (collection.hasOwnProperty(key)) {
|
||||
if (index < childCount) {
|
||||
|
|
|
|||
|
|
@ -110,6 +110,32 @@ describe('angular', function(){
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
describe('size', function() {
|
||||
it('should return the number of items in an array', function() {
|
||||
expect(size([])).toBe(0);
|
||||
expect(size(['a', 'b', 'c'])).toBe(3);
|
||||
});
|
||||
|
||||
it('should return the number of properties of an object', function() {
|
||||
expect(size({})).toBe(0);
|
||||
expect(size({a:1, b:'a', c:noop})).toBe(3);
|
||||
});
|
||||
|
||||
it('should return the number of own properties of an object', function() {
|
||||
var obj = inherit({protoProp: 'c', protoFn: noop}, {a:1, b:'a', c:noop});
|
||||
|
||||
expect(size(obj)).toBe(5);
|
||||
expect(size(obj, true)).toBe(3);
|
||||
});
|
||||
|
||||
it('should return the string length', function() {
|
||||
expect(size('')).toBe(0);
|
||||
expect(size('abc')).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('parseKeyValue', function() {
|
||||
it('should parse a string into key-value pairs', function() {
|
||||
expect(parseKeyValue('')).toEqual({});
|
||||
|
|
|
|||
Loading…
Reference in a new issue