Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,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
Expand All @@ -485,18 +487,21 @@ 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)

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;
Expand Down
5 changes: 2 additions & 3 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function JQLite(element) {
div.innerHTML = '<div>&nbsp;</div>' + element; // IE insanity to make NoScope elements work!
div.removeChild(div.firstChild); // remove the superfluous div
JQLiteAddNodes(this, div.childNodes);
this.remove(); // detach the elements form the temporary DOM div.
this.remove(); // detach the elements from the temporary DOM div.
} else {
JQLiteAddNodes(this, element);
}
Expand Down Expand Up @@ -136,8 +136,7 @@ function JQLiteAddNodes(root, elements) {
? elements
: [ elements ];
for(var i=0; i < elements.length; i++) {
if (elements[i].nodeType != 11)
root.push(elements[i]);
root.push(elements[i]);
}
}
}
Expand Down
32 changes: 19 additions & 13 deletions src/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,19 +913,12 @@ 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),
fragment = (element[0].nodeName != 'OPTION') ? document.createDocumentFragment() : null,
addFragment,
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) {
Expand All @@ -934,6 +927,7 @@ angularWidget('@ng:repeat', function(expression, element){
childScope[valueIdent] = collection[key];
if (keyIdent) childScope[keyIdent] = key;
lastIterElement = childScope.$element;
childScope.$eval();
} else {
// grow children
childScope = createScope(currentScope);
Expand All @@ -946,14 +940,26 @@ angularWidget('@ng:repeat', function(expression, element){
children.push(childScope);
linker(childScope, function(clone){
clone.attr('ng:repeat-index', index);
lastIterElement.after(clone);
lastIterElement = clone;

if (fragment) {
fragment.appendChild(clone[0]);
addFragment = true;
} else {
//temporarily preserve old way for option element
lastIterElement.after(clone);
lastIterElement = clone;
}
});
}
childScope.$eval();
index ++;
}
}

//attach new nodes buffered in doc fragment
if (addFragment) {
lastIterElement.after(jqLite(fragment));
}

// shrink children
while(children.length > index) {
children.pop().$element.remove();
Expand Down
30 changes: 30 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ 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);
});

it('should not rely on length property of an object to determine its size', function() {
expect(size({length:99})).toBe(1);
});
});


describe('parseKeyValue', function() {
it('should parse a string into key-value pairs', function() {
expect(parseKeyValue('')).toEqual({});
Expand Down